PDA

View Full Version : Using gl_quad_strips with multiple colors



Auguri
03-29-2006, 11:02 AM
I'd like to use gl_quad_strips but have unique colors on each quad. I'm guessing this may be impossible, but I'd like to know for sure.

Thanks.

jide
03-29-2006, 01:51 PM
what ? how do you do your stuff ?

RigidBody
03-29-2006, 02:12 PM
"having unique colors on each quad" should be what you get when using glShadeModel(GL_FLAT);

although this might not really be what you want, i think it is a valid answer to your question :p

Auguri
03-30-2006, 02:27 PM
Let me give a simplistic example of what I want. Then maybe you'll see what's puzzling me.

Suppose I want a red rectangle abutting a green rectangle.
//Set up some vertices
float v[ 6 ][ 2 ] = {
/////////////////////// 1 3 5
{ -0.5, -0.5 }, // x x x
{ -0.5, 0.5 }, //
{ 0.0, -0.5 }, //
{ 0.0, 0.5 }, //
{ 0.5, -0.5 }, // x x x
{ 0.5, 0.5 } // 0 2 4
};

// Set color for first quad
glColor3f( 1, 0, 0 );

glBegin( GL_QUADS_STRIPS );
glVertex2fv( v[ 0 ] );
glVertex2fv( v[ 1 ] );
glVertex2fv( v[ 3 ] );
// I want to add other color call here somewhere, // but I'm guessing you can't since you're in the
// in the middle of a glBegin/End sequence.
// Besides, you're reusing these 2 vertices, so
// where would it go?
glVertex2fv( v[ 2 ] );
glVertex2fv( v[ 5 ] );
glVertex2fv( v[ 4 ] );
glEnd();

Do you see what I'm thinking about now?

I don't know how to used the shading model to specify color.

Thanks for your help.

Ehsan Kamrani
03-30-2006, 03:09 PM
you can call the function glColor*() between the glBegin()/glEnd() pairs. Before each glVertex*(), you can specify a color by calling the function glColor*():

glBegin( GL_QUAD_STRIP );
glColor*();
glVertex*();
glColor*();
glVertex*();
...
glEnd();

Remember to enable the smooth shading by calling the following function:
glShadeModel( GL_SMOOTH ); //(As Rigid Body said before )

-Ehsan-

Auguri
03-30-2006, 03:27 PM
Hmmm, okay, thanks for that comment. I see what you're saying, but I don't want to blend the colors.

For this particular example, I want a purely red rectangle next to a purely green rectangle.

Let me try again to explain what I'm trying to do. Here's how you do it with 2 consecutive quads:

glColor3f( 1,0,0 );

glBegin( GL_QUADS );
glVertex2fv( v[ 0 ] );
glVertex2fv( v[ 1 ] );
glVertex2fv( v[ 3 ] );
glVertex2fv( v[ 2 ] );

glColor3f( 0,1,0 );

glVertex2fv( v[ 2 ] );
glVertex2fv( v[ 3 ] );
glVertex2fv( v[ 5 ] );
glVertex2fv( v[ 4 ] );
glEnd();

RigidBody
03-31-2006, 12:52 AM
ehsan,

actually i said that smooth shading should be disabled ;)

if glShadeModel(GL_FLAT) is used, each tria will have a constant color.

Ehsan Kamrani
03-31-2006, 06:28 AM
Sorry. I didn't notcie you don't want to blend the colors. So as Rigid Body said, disable smooth shading :rolleyes:
-Ehsan-

Auguri
03-31-2006, 09:27 AM
Hi, thanks again for your efforts, but we're still back where we started.

Smooth shading or no, there's still the problem of what color to set the vertices that are being REUSED in the rect. strip.

The more I talk about this, the more convinced I become that you just can't do it. Do you agree?

RigidBody
03-31-2006, 09:57 AM
try this

glBegin(GL_QUAD_STRIP);
// FIRST QUAD
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-1.0, -1.0, 0.);
glVertex3f( 1.0, -1.0, 0.);
glVertex3f(-1.0, 0.0, 0.);
glVertex3f( 1.0, 0.0, 0.);
// SECOND QUAD
glColor3f(1.0, 1.0, 0.0);
glVertex3f(-1.2, 1.0, 0.);
glVertex3f( 1.2, 1.0, 0.);
// THIRD QUAD
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-1.2, 2.0, 0.);
glVertex3f( 1.2, 2.0, 0.);
glEnd();

Tin Whisker
03-31-2006, 11:22 AM
Do you agree?Wholeheartedly.

The idea with strips is to reduce bandwidth consumption while maximizing cache reuse by sending vertices that are unique in position only. The strip itself usually describes a continuous (smooth) surface of shared vertex data. If you need distinct faces in general (like cube faces) with unique colors/normals/texcoords per vertex, then you need to supply them as such, with QUAD/TRIANGLE lists. If you order your quads in roughly the same layout as a strip, you won't see much of a performance hit (aside form the extra bandwidth/memory overhead).