3D computer Grapihcs Using OpenGL-10 Color Buffer

Source: Internet
Author: User

In this section we will try to make a "merry" effect with triangles.

A triangular pattern that moves from left to right.

Check the code First:

MyGlWindow.cpp

1#include <gl\glew.h>2#include"MyGlWindow.h"3#include <iostream>4#include <fstream>5 6 floatTrianglewidth =0.1f;7 floatBytespertriangle =sizeof(glfloat) * -;8 UINTTriangleindex =0;9 UINTMaxtrianglecount = -;Ten  One voidMyglwindow::senddatatoopengl () A { -     //glfloat verts[] = -     //{ the     //-1.0f, -1.0f, +0.5f,//Vertex 0 -     //+1.0f, +0.0f, +0.0f,//Color 0 -     //+0.0f, +1.0f, -0.5f,//Vertex 1 -     //+0.0f, +1.0f, +0.0f,//Color 1 +     //+1.0f, -1.0f, +0.5f,//Vertex 2 -     //+0.0f, +0.0f, +1.0f,//Color 2 +  A     //-1.0f, +1.0f, +0.5f,//Vertex 3 at     //+0.5f, +0.3f, +0.1f,//Color 3 -     //+0.0f, -1.0f, -0.5f,//Vertex 4 -     //+0.1f, +0.4f, +0.2f,//Color 4 -     //+1.0f, +1.0f, +0.5f,//Vertex 5 -     //+1.0f, +0.5f, +0.2f,//Color 5 -     //}; in  - Gluint Vertexbufferid; toGlgenbuffers (1, &Vertexbufferid); + Glbindbuffer (Gl_array_buffer, Vertexbufferid); -Glbufferdata (Gl_array_buffer, Maxtrianglecount *Bytespertriangle, NULL, Gl_static_draw); the  *     //glushort indices[] = $     //{Panax Notoginseng     //0,1,2, -     //3,4,5, the     //}; +     //Gluint Indexbufferid; A     //glgenbuffers (1, &indexbufferid); the     //Glbindbuffer (Gl_element_array_buffer, Indexbufferid); +     //glbufferdata (gl_element_array_buffer, sizeof (indices), indices, gl_static_draw); -  $Glenablevertexattribarray (0); $Glvertexattribpointer (0,3, Gl_float, Gl_false,sizeof(glfloat) *6,0); -  -Glenablevertexattribarray (1); theGlvertexattribpointer (1,3, Gl_float, Gl_false,sizeof(glfloat) *6, (Char*)(sizeof(glfloat) *3)); - }Wuyi  the voidmyglwindow::installshaders () - { Wu     //not modified, omitted ... - } About  $ voidMyglwindow::initializegl () - { - glewinit (); - glenable (gl_depth_test); A Senddatatoopengl (); + installshaders (); the } -  $ voidMyglwindow::p aintgl () the { the glclear (gl_depth_buffer_bit); theGlviewport (0,0, Width (), height ()); the     //gldrawelements (gl_triangles, 6, gl_unsigned_short, 0); - Sendanothertriangle (); inGldrawarrays (Gl_triangles, (triangleindex-1)*3, Triangleindex *bytespertriangle); the } the  About voidMyglwindow::sendanothertriangle () the { the     if(Triangleindex = =maxtrianglecount) the         return; +Glfloat Xval =-1+ Triangleindex *Trianglewidth; -Glfloat newtriangle[] = the     {BayiXval,1.0f,0.0f, the         1.0f,0.0f,0.0f, the  -Xval + trianglewidth,1.0f,0.0f, -         0.0f,1.0f,0.0f, the  theXval,0.0f,0.0f, the         0.0f,0.0f,1.0f, the     }; -  theGlbuffersubdata (Gl_array_buffer, Bytespertriangle *Triangleindex, Bytespertriangle, newtriangle); the  thetriangleindex++;94 } the  theSTD::stringMyglwindow::readshadercode (Const Char*fileName) the {98     //not modified, omitted ... About}

MyGlWindow.h

1 #pragmaOnce2#include <QtOpenGL\qgl.h>3#include <string>4 classMyglwindow: PublicQglwidget5 {6 protected:7     voidSenddatatoopengl ();8     voidinstallshaders ();9     voidInitializegl ();Ten     voidPAINTGL (); OneSTD::stringReadshadercode (Const Char*fileName); A     voidSendanothertriangle (); -};

Focus on the changes in the CPP file.

Define a few variables (can also be defined as constants), convenient for later use, they are:

    • Float Trianglewidth = 0.1f Indicates the width of the triangle
    • float bytespertriangle = sizeof (glfloat) * 18 represents the number of vertex information data bytes that each triangle contains, a triangle uses 3 vertices, each vertex has 6 glfloat type data
    • UINT Triangleindex = 0 indicates the index of the currently drawn triangle
    • UINT Maxtrianglecount = 20 "Merry" maximum number of triangles

Before we created a Verts array in the SENDDATATOOPENGL () function and sent all the data to OpenGL one at a time to draw it, we needed to dynamically change what was drawn, so we didn't send the data once in advance. First remove the Verts array from the SENDDATATOOPENGL function. (13-28 lines)

In addition, the array is not required, and the contents of the indexed array are deleted. (35-43 rows and 69 rows)

33 lines have also been modified, first we have to allocate enough space for Vertexarraybuffer, so the second parameter is changed to Maxtrianglecount * Bytespertriangle, providing 20 triangles required space. And we don't need to provide any data at this stage (we'll talk about how to provide it later), so the third parameter gives a null value directly.

Before we draw an array of 71 lines, we call a newly added function Sendanothertriangle (), the first half of the function (78-89 rows) is the data that prepares the data, prepares the triangles to be drawn each time, and can be seen by the contents of the data. The main difference is that the position moves to the right.

The focus is on 91 rows of functions

Glbuffersubdata (Gl_array_buffer, Bytespertriangle * triangleindex, Bytespertriangle, NewTriangle);

Glbuffersubdata the function of this OpenGL function is "partially populated" by the Array Buffer. You can compare 33 rows of glbufferdata (all-in-one fill), the name is just a sub, but the parameters of the two are somewhat different.

    • The first parameter is the same as Glbufferdata, which represents the data for which binding point is set.
    • The second argument is the starting position of a drawing because we right-triangleindex the index of a triangle, so the starting value is the number of bytes multiplied by each triangle.
    • The third parameter represents the length of each element, using exactly the number of bytes of each triangle we defined at the beginning Bytespertriangle
    • The fourth parameter is the data itself

71 lines Draw Array Buffer, note that the second parameter is the starting point for each triangle.

After the completion of the compilation run, found that the screen did not change, mainly because the picture is not redrawn, in order to activate the redraw, the simplest way is to let the window lose focus and focus, that is, you can click between the OpenGL window and any other window to switch.

But the effect we see is still not what we expect. The effect is as follows:

What is the reason for this?

The reason is that OpenGL uses a dual cache. A front buffer, a back buffer.

The drawing work is done on the back buffer, so that the user can see the drawing process, and after drawing it, it will be exchanged with front buffer. That's why we see that there seems to be two different graphs in the repeatedly switching.

In addition, we explicitly specify that only one triangle is drawn at a time, but why did the previous triangle be saved?

The reason is that we did not "clean up" once.

Find the 67 line of MyGlWindow.cpp, we modify it as follows:

Glclear (Gl_depth_buffer_bit | Gl_color_buffer_bit);

Now that we're cleaning up depth buffer, we'll use a bitwise OR operator to add the Color buffer as well.

After this modification, you can achieve the "merry" effect! (The effect is not, the motion diagram is too difficult to get)

3D computer Grapihcs Using OpenGL-10 Color Buffer

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.