Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 );
void SetColor( unsigned char clr )
switch( clr )
    case RED:
    glColor4f( 1.0f, 0.0f, 0.0f );

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 this post

Link to post
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.


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).

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!