In fact, most of the code was written in the previous example, and the main purpose of this example was to understand what the system-defined models are in OpenGL.
The final result is as follows:
The overall flow chart is shown below
According to the above flow chart, roughly describe the function
main
Function:- Initialize GL related libraries
- Register custom functions
- Initialize data
ChangeSize
Function:- Set the viewport
- Set the projection mode to get the projection matrix, and load the projection matrix stack
- Set up transform pipes and manage matrix stacks
setupRC
function- Initialization: Background color, fixed shader
- Enable depth testing
- Setting the Observation Mode
- Use the triangle batch class to create balls, rings, cylinders, cones, and disks
RenderScene
function- Clear buffer
- Matrix stack push
- Draw graphics –
DrawWireFrameBatch
function - Matrix stack POP
- Swap buffer
DrawWireFrameBatch
function- Draw graphics using fixed shaders and triangular batch classes
- Drawing graph border: you can refer to this case 03 supplement: drawing graph border such as pyramid, need to enable polygon offset, mixing function, etc
- After drawing, restore Settings: turn off polygon offset, blending, etc
SpecialKeys
Function: mainly according to the special key switch, rotate the objectKeyPressFunc
Function: Toggle rendering of different graphics by using the space bar
OpenGL’s built-in model
- The ball –
gltMakeSphere
- In general, iStacks are twice as many as iSlices
iSlices * 2 = iStacks
- The spheres are drawn around the z-axis, with +z being the vertex and -z being the bottom of the sphere
- In general, iStacks are twice as many as iSlices
// Parameter 1: sphereBatch, triangle batch class object // Parameter 2: fRadius, sphere radius // Parameter 3: iSlices, number of triangle strips stacked from the bottom of the sphere to the top; In fact, the sphere is composed of a circle of triangular strips // Parameter 4: IStacks, a triangle logarithm gltMakeSphere(GLTriangleBatch& sphereBatch, GLfloat fRadius, GLint iSlices, GLint iStacks) arranged around a sphere;Copy the code
- Ring –
gltMakeTorus
// Parameter 1: torusBatch, triangle batch class object // Parameter 2: majorRadius, the radius from the center of the doughnut to the outer edge // Parameter 3: minorRadius, the radius from the center of the doughnut to the inner edge // Parameter 4: NumMajor, number of triangles along the main radius // parameter 5: GltMakeTorus (GLTriangleBatch& torusBatch, GLfloat majorRadius, GLfloat minorRadius, GLfloat minorRadius, GLfloat minorRadius, GLint numMajor, GLint numMinor);Copy the code
-
Cylinder/cone – gltMakeCylinder
-
Set baseRadius and topRadius equal, that is, the effect of the cylinder in the effect picture, that is, to get regular cylinder,
-
Setting baseRadius and topRadius not equal and not zero gives you something like a horn
-
Only set one of them, the other one is 0, you can get a cone, that is, the cone effect in the effect drawing
-
/ / parameter 1: cylinderBatch, triangle batch class 2: / / parameters baseRadius, radius at the bottom of the 3: / / parameter topRadius, 4: / / head radius parameter fLength, circular length / / 5: NumSlices, number of triangle pairs around the z-axis // parameter 6: Void gltMakeCylinder(GLTriangleBatch& cylinderBatch, GLfloat baseRadius, GLfloat topRadius) GLfloat fLength, GLint numSlices, GLint numStacks);Copy the code
- Disk –
gltMakeDisk
Parameter 1:diskBatch, Triangle batch objects // Parameter 2:innerRadius, inner circle radius // Parameter 3:outerRadius, outer circle radius // Parameter 4:nSlices, number of triangle pairs around the Z-axis // Parameter 5: nchips, number of triangles from the outer circle to the inner circle void gltMakeDisk(GLTriangleBatch& diskBatch, GLfloat innerRadius, GLfloat outerRadius, GLint nSlices, GLint nStacks);Copy the code
See GithuB-05_OpengL_02_ Geometry Drawing for the full Demo implementation