GL_STATIC_DRAW) // usage // copy index data to VBO GlBindBuffer(GL_ARRAY_BUFFER, vboId) // for vertex dataĬylinder.getInterleavedVertexSize(), // data size, # of bytesĬylinder.getInterleavedVertices(), // ptr to 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 cylinder with default params // radii=1, height=1, sectors=36, stacks=1, smooth=true, up-axis=Z(3) Or, download cylinderShader.zip ( with GLFW, ) for more details. The following code draws a cylinder with interleaved vertex data using VBO and GLSL. in order to access the vertex data in GLSL. This C++ class also provides getVertices(), getIndices(), getInterleavedVertices(), etc. create a cylinder with base radius=1, top radius=2, height=3, // height=4, sectors=5, stacks=6, smooth=true, up-axis=Z(3)Ĭylinder cylinder(1, 2, 3, 4, 5, 6, true, 3) Ĭylinder.drawWithLines() // draw surface and lines Press the space key to change the number of sectors and stacks of the cylinders.Ĭylinder.cpp class provides pre-defined drawing functions using OpenGL VertexArray draw(), drawWithLines(), drawLines(), drawSide(), drawBase() and drawTop(). You could also pass the custom parameters to the constructor, similar to gluCylinder(). By default, the top surface is facing to +Z axis, but it can be changed by the last parameter of Cylinder class constructor (X=1, Y=2 or Z=3), or by calling setUpAxis() function after it is constructed. With the default constructor (without arguments), it generates a cylinder with base/top radius = 1, height = 2, sectors = 36, and stacks = 1. This example constructs cylinders with 36 sectors and 8 stacks, but with different shadings flat, smooth or textured. indices for the base surface //NOTE: baseCenterIndex and topCenterIndices are pre-computed during vertex generation // please see the previous code snippetįor(int i = 0, k = baseCenterIndex + 1 i < sectorCount ++i, ++k)įor(int i = 0, k = topCenterIndex + 1 i < sectorCount ++i, ++k)ĭownload: cylinder.zip, cylinderShader.zip (Updated: ) Int k2 = sectorCount + 1 // 1st vertex index at top // indices for the side surface Std::vector unitVertices = getUnitCircleVertices() Std::vector Cylinder::getUnitCircleVertices() These are also used for the normal vectors of the side faces of the cylinder. In order to reduce multiple computations of sine and cosine, we compute the vertices of a unit circle on XY plane only once, and then re-use these points multiple times by scaling with the base radius. It also creates other vertex attributes surface normals and texture coordinates. The following C++ code generates all vertices of the cylinder with the given base radius, height and the number of sectors (slices). The sector angle for each step can be calculated by the following The range of sector angles is from 0 to 360 degrees. An arbitrary point ( x, y, z) on the cylinder can be computed from the equation of circle with the corresponding sector angle θ. Suppose a cylinder is centered at the origin and its radius is r and the height is h. Triangular Prism (3 sides) Rectangluar Prism (4 sides) Octagonal Prism (8 sides) Hexadecagonal Prism (16 sides)
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |