views:

187

answers:

1

Hi,
Is there a way to achieve this (OpenGL 2.1)? If I draw lines like this

glShadeModel(GL_FLAT);
glBegin(GL_LINES);
  glColor3f(1.0, 1.0, 0.0);
  glVertex3fv(bottomLeft);
  glVertex3fv(topRight);

  glColor3f(1.0, 0.0, 0.0);
  glVertex3fv(topRight);
  glVertex3fv(topLeft);
  .
  .
  (draw a square)
  .
  .
glEnd();

I get the desired result (a different colour for each edge) but I want to be able to calculate the fragment values in a shader. If I do the same after setting up my shader program I always get interpolated colors between vertices. Is there a way around this? (would be even better if I could get the same results using quads)

Thanks

+1  A: 

If you don't want the interpolation between vertice attributes inside a primitive (e.g. color in a line segment in your case) you'll need to pass the same color twice, so end up duplicating your geometry:

 v0             v1          v2
 x--------------x-----------x    

(v0 is made of structs p0 and c0, v1 of p1 and c1 etc..)

For drawing the line with color interpolation:

glBegin(GL_LINES);
//draw first segment
glColor3fv(&c0); glVertex3fv(&p0);
glColor3fv(&c1); glVertex3fv(&p1);
//draw second segment
glColor3fv(&c1); glVertex3fv(&p1);
glColor3fv(&c2); glVertex3fv(&p2);
glEnd();

For drawing without interpolation:

glBegin(GL_LINES);
//draw first segment
glColor3fv(&c0); glVertex3fv(&p0);
glColor3fv(&c0); glVertex3fv(&p1);
//draw second segment
glColor3fv(&c1); glVertex3fv(&v1);
glColor3fv(&c1); glVertex3fv(&v2);
glEnd();

Note this mean you can no longer use GL_x_STRIP topology, since you don't want to share attributes inside a primitive.

Stringer Bell
Ok thanks. I understand, I need to do the same computation in each vertex on the line.
Brett