Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fewer Colors Than Vertices

Tags:

c++

opengl

In the old deprecated OpenGL, we could do something like this:

glBegin(...);
   glColor3f(r_1,g_1,b_1);
   glVertex3f(x_1, y_1, z_1);
   glVertex3f(x_2, y_2, z_2);
   ...
   glVertex3f(x_n, y_n, z_n);

   glColor3f(r_2, g_2, b_2);
   glVertex3f(x_(n+1), y_(n+1), z_(n+1));
   glVertex3f(x_(n+2), y_(n+2), z_(n+2));
   ...
   glVertex3f(x_2n, y_2n, z_2n);

   ...
glEnd();

That is, I am saying that each n consecutive vertices share the same color. Is the same possible to be done with the new and non-deprecated OpenGL?

For example, if I have a cube, it means that I have 36 vertices. If I want each face to have 1 color, then each consecutive 6 vertices must share that color. Currently I have artificially copied the color data 6 times for each color so that the sizes of vertex array and color array are the same. Is there any other way around this? Hope my question was clear.

like image 757
Armen Tsirunyan Avatar asked Aug 29 '11 16:08

Armen Tsirunyan


1 Answers

Maybe this kind of pseudocode clears things up for you:

GLfloat color_state{r,g,b};
GLfloat normal_state{x,y,z};
...

glColor4f(r,g,b,a):
    color_state.r = r
    color_state.g = g
    color_state.b = b
    color_state.a = a

glNormalf(x,y,z):
    normal_state.x = x
    normal_state.y = y
    normal_state.z = z

glVertex3f(x,y,z):
    __glinternal_submit_vertex(
        position = {x,y,z},
        normal = normal_state,
        color = color_state,
        ... );

This is how OpenGL immediate works internally. Colors were not shared among vertices. A copy was created from the current state for each vertex submitted. Now, with Vertex Arrays and Vertex Buffer Object the burden lies upon you, to do the right data duplication.

like image 144
datenwolf Avatar answered Oct 04 '22 18:10

datenwolf