3D computer Grapihcs Using OpenGL-14 OpenGL instancing

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.