If we need to draw two (or more) of the same cube (or objects), just position, scale, rotation is not the same, then we can not need to the object's vertex information, color information, and so on to the video card, but sent once, drawn multiple times, just before each drawing to apply a different transformation matrix. This method is called OpenGL instancing. It is much more efficient than sending the data back to the video card every time.
Look at the specific code:
1 voidMyglwindow::p aintgl ()2 {3Glclear (Gl_depth_buffer_bit |gl_color_buffer_bit);4Glviewport (0,0, Width (), height ());5 6 //Cube17Glm::mat4 ProjectionMatrix = glm::p erspective (30.0f, ((float) width ())/height (),0.1f,10.0f);8Glm::mat4 Translationmatrix = glm::translate (ProjectionMatrix, GLM::VEC3 (0.0f,0.0f,-3.0f));9Glm::mat4 Fulltransformmatrix = glm::rotate (Translationmatrix,54.0f, GLM::VEC3 (1.0f,0.0f,0.0f));Ten OneGlint fulltransformmatrixuniformlocation = glgetuniformlocation (ProgramID,"Fulltransformmatrix"); AGLUNIFORMMATRIX4FV (Fulltransformmatrixuniformlocation,1, Gl_false, &fulltransformmatrix[0][0]); -Gldrawelements (Gl_triangles, numindices, Gl_unsigned_short,0); - the //Cube2 -Translationmatrix = Glm::translate (ProjectionMatrix, GLM::VEC3 (2.0f,0.0f, -4.0f)); -Fulltransformmatrix = Glm::rotate (Translationmatrix,126.0f, GLM::VEC3 (0.0f,1.0f,0.0f)); -GLUNIFORMMATRIX4FV (Fulltransformmatrixuniformlocation,1, Gl_false, &fulltransformmatrix[0][0]); +Gldrawelements (Gl_triangles, numindices, Gl_unsigned_short,0); - +}
We can draw another cube only by adding a few lines of code (16-19 lines) to the PAINTGL () function.
We re-changed the Translationmatrix and Fulltransformmatrix transformation matrices in line 16-17 (we didn't even define a new matrix, just changed its value), and changed position and rotation. Projection matrix We do not want to change, or the previous settings.
Line 18th and Line 12 are exactly the same, but this is necessary because the uniform variable is to be re-applied to work.
Line 19th uses a new transformation matrix to draw the cube a second time. Compile and run we get the following effect:
3D computer Grapihcs Using OpenGL-14 OpenGL instancing