One ofVertex shape with different colors?


I want to draw an ofVertex shape with different colors for different sections of the line (actually, I need different alphas but the same RGB).

Here’s my code:

ofSetLineWidth( 8.0f );  
int nCountVertexPts = 0;  
MPI_Point2D drawPt[] = { MPI_Point2D( -0.5f, -0.5f ), MPI_Point2D( 0.5f, -0.5f ), MPI_Point2D( 0.5f, 0.5f ), MPI_Point2D( -0.5f, 0.5f ) };  
for ( int i=0; i<4; i++ ) {  
	float tmp = (1.0f - i/4.0f) * 255.0f;  
	int drawcolour = (int)tmp;  
	ofSetColor( 128, 64, 192, drawcolour );  
	float worldX = drawPt[i].getX();  
	float worldY = drawPt[i].getY();  
	MPI_Point2D drawPt = ofDifferentStrokes::worldToScreenCoords( MPI_Point2D( worldX, worldY ) );  
	ofVertex( drawPt.getX(), drawPt.getY() );  

This is meant to draw a square with 4 sides of different alpha values, but the result is that only the final drawcolour value is kept.

I think that what happens is that the draw function picks up the last registered ofSetColor() and applies it to the whole shape.

How would one go about doing this?

Thanks for any help!

you can’t do that with ofVertex since it’s using a vertex array internally and as you say it only uses the last color applied.

what you need is a vertex array + color coordinates or if you don’t care about opengl es (iphone…) compatibility you can do it the old way:

        glVertex2f(x, y);  

Thank you for the quick reply, that did fix my problem :slight_smile:
I’m sure you meant glColor3ui instead of glColor3u.