![]() ![]() Since this C++ class uses cylindrical texture mapping, there is a squeeze/distortion at the north and south pole area. (void*)0) // offset to indices // unbind VAO GlDrawElements(GL_TRIANGLES, // primitive type GlBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) GlVertexAttribPointer(attribTex, 2, GL_FLOAT, false, stride, (void*)(sizeof(float)*6)) GlVertexAttribPointer(attribNorm, 3, GL_FLOAT, false, stride, (void*)(sizeof(float)*3)) GlVertexAttribPointer(attribVert, 3, GL_FLOAT, false, stride, (void*)0) Int stride = sphere.getInterleavedStride() // should be 32 bytes set attrib arrays with stride and offset GL_STATIC_DRAW) // usage // activate attrib arrays Sphere.getIndices(), // ptr to index data Sphere.getIndexSize(), // data size, # of bytes GlBufferData(GL_ELEMENT_ARRAY_BUFFER, // target GlBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboId) // for index data GL_STATIC_DRAW) // usage // create VBO to copy index data to VBO Sphere.getInterleavedVertices(), // ptr to vertex data Sphere.getInterleavedVertexSize(), // data size, # of bytes GlBindBuffer(GL_ARRAY_BUFFER, vboId) // for vertex data create VBO to copy interleaved vertex data (V/N/T) to VBO create VAO to store all vertex array state to VAO create a sphere with default params // radius=1, sectors=36, stacks=18, smooth=true Or, download sphereShader.zip (with GLFW) for more details. The following code draws a sphere with interleaved vertex data using VBO, VAO and GLSL. in order to access the vertex data in GLSL. This C++ class also provides getVertices(), getIndices(), getInterleavedVertices(), etc. ![]() Sphere sphere(1.0f, 36, 18, true, 3) // same as above // can change parameters later create a sphere with radius=1, sectors=36, stacks=18, // smooth=true(default), up-axis=Z(default) But, it can be changed by the last parameter of Sphere class contructor (X=1, Y=2, or Z=3), or by calling setUpAxis() function after it is constructed. Sphere.cpp class provides pre-defined functions draw(), drawWithLines() and drawLines(), to draw a sphere using OpenGL VertexArray.īy default, the north pole of the sphere is facing to +Z axis. This example constructs spheres with 36 sectors and 18 stacks, but with different shadings flat, smooth or textured. If(i != 0) // horizontal lines except 1st stack, k1 => k+1ĭownload: sphere.zip, sphereShader.zip (Updated: ) store indices for lines // vertical lines for all stacks, k1 => k2 For more details, please refer to buildVerticesSmooth() or buildVerticesFlat() functions in Sphere.cpp class.įloat nx, ny, nz, lengthInv = 1.0f / radius // vertex normal It also creates other vertex attributes surface normals and texture coordinates. The following C++ code generates all vertices of the sphere for the given radius, sectors and stacks. The sector and stack angle for each step can be calculated by the following The range of sector angles is from 0 to 360 degrees, and the stack angles are from 90 (top) to -90 degrees (bottom). Sectors and stacks of a sphere A point on a sphere using sector and stack anglesĪn arbitrary point ( x, y, z) on a sphere can be computed by parametric equations with the corresponding sector angle θ and stack angle ϕ. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |