//the cube to save eight points to an array in
static const GLfloat vertex_list [] [3] = {
0.5 f, 0.5 f, 0.5 f,
0.5 f, 0.5 f, 0.5 f,
//…
};
glBegin(GL_LINE_STRIP);
glBegin(GL_LINE_STRIP);
glVertex3fv (vertex_list [0]).
glVertex3fv (vertex_list [2]).
glVertex3fv (vertex_list [3]).
glVertex3fv (vertex_list [1]).
//…
glEnd();
changes, although the code to get longer, but it is easy to read. It’s easy to see that the four vertices 0, 2, 3, and 1 form a square.
a little observation can be found, we use a lot of glVertex3fv function, each one is only one of the vertex sequence number is different, so we can define an array of serial number, put all the serial number is in. This makes the code much simpler.
//cube will save eight points to an array of
static const GLfloat vertex_list [] [3] = {
0.5 f, 0.5 f, 0.5 f,
0.5 f to 0.5 f, 0.5 f, f
– 0.5, 0.5 f to 0.5 f,
0.5 f, 0.5 f, 0.5 f,
– 0.5 f to 0.5 f, 0.5 f,
0.5 f to 0.5 f, 0.5 f,
0.5 f, f 0.5, 0.5 f,
0.5 f, f 0.5, 0.5 f,
};
static const GLint index_list[][4] = {
static const GLint index_list[][4] = {
static const GLint index_list[]
0, 2, 3, 1,
0, 4, 6, 2,
0, 1, 5, 4,
4, 6, 7, 5,
1, 3, 7, 3,
2, 6, 7, 7,};
int i, j;
glBegin(GL_LINE_STRIP)
glBegin(GL_LINE_STRIP)
glBegin(GL_LINE_STRIP);
for(i=0; i< 6;
for(j=0; j=0; j=0; j< 4. + + j)// each side has four vertices, cycle four times
glVertex3fv (vertex_list [index_list [I] [j]]).
glEnd();
This gives us a more mature version of how to draw a cube. The data and code are basically separate. All the vertices are put in one array, and the number of the vertices is put in another array, and the code to draw the cube from these two arrays is very simple.
faces counter clockwise, faces away from us clockwise, we have the index_list array above.
Read More:
- An example of 3D data modeling based on VB6 + OpenGL
- Drawing a cube with OpenGL
- Implementation of OpenGL rotating cube
- 2016.11.1 OpenGL learning, black screen does not display graphics
- OpenGL learning summary (1)
- OpenGL learning notes and other learning thinking
- An example of drawing rotating cube with OpenGL
- Vs2015 configuring OpenGL (glfw, glew)
- Drawing function of GLUT
- OpenGL Usage Summary (including problems encountered and solutions)
- 【Bug-python】IndexError: list index out of range
- Vs2015 + OpenGL environment configuration
- Configuring OpenGL in Visual Studio 2015
- Graphics rendering pipeline diagram of OpenGL
- On the configuration of OpenGL Red Book eighth edition environment in vs2013
- Some problems in using arrays
- The difference between “?” and “?:” introduced by php7
- Numpy adds a new dimension: newaxis
- Python error: typeerror: ‘Int’ object is not subscribable
- Python reported error: typeerror:’int’object is not subscriptable