# Coloring a Rectangle from an array.

This topic is 3030 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So I'm trying to color a rectangle based on an array passed in containing colors and I'm pretty sure I'm doing it wrong. For simplicity, the rectangle is at the points: (0,0,0), (10,0,0), (10,5,0), (0,5,0) The array passed in looks like this:
unsigned char outp[ 1000 ];

And contains values corresponding to these 4 colors: RED (3), YELLOW (2), GREEN(1), BLACK (0). I'm currently breaking the rectangle (long ways) into 1000 smaller rectangles and coloring them individually. Something like this:
glBegin( GL_QUADS );
for( i = 0; i < 1000 - 1; i++ )
{
SetColor( outp[ i ] );
glVertex3f( 0.01f * (float)i, 0.0f, 0.0f );
glVertex3f( 0.01f * ( (float)i + 1.0f ), 0.0f, 0.0f );
glVertex3f( 0.01f * ( (float)i + 1.0f ), 5.0f, 0.0f );
glVertex3f( 0.01f * (float)i, 0.0f, 0.0f );
}
glEnd();
//--------------
void SetColor( unsigned char clr )
{
switch( clr )
{
case RED:
glColor4f( 1.0f, 0.0f, 0.0f );
break;
...


So my questions are: Is there an easier way to make a palette to do this? Surely drawing 1000 rectangles is wrong way to do this, what should I be doing? What happens when I add more colors... say like 256?

##### Share on other sites
There is a Color-Index mode to openGL that allows you to build your own color palette, although I'm not sure why you would want to use it, I think it was generally designed for ancient cards that only had like 8-bit color or something.

If you're still interested in building a palette versus just sending the colors, than you can check out this link which should explain how to use it RGBA versus Color-Index Mode Although be warned that it says RGBA mode works better with lighting, shading, fog, antialiasing, and blending, and that was back in version 1.1, so I have no idea what support for that mode is like now.

Quote:
 Surely drawing 1000 rectangles is wrong way to do this, what should I be doing?

Not necessarily. If you want to make a quad that has 1000 little different color squares on it, then you need to either build a texture, or render 1000 tiny quads. If you render these quads properly (NOT using glBegin), your gpu can probably handle this easily at well over 60 fps.

If you want to render all those quads, then you should eventually try to learn the proper way to render (VBOs), as this would probably be 50+ times faster than drawing it with glBegin (which is very very slow). glBegin immediate mode is probably ok when you're just starting out as a beginner, but it will get you nowhere once you try to start doing anything complicated (and its depreciated).

1. 1
2. 2
Rutin
22
3. 3
JoeJ
20
4. 4
5. 5

• 27
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631735
• Total Posts
3001940
×