Coloring a Rectangle from an array.

Started by
0 comments, last by karwosts 14 years ago
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 );
    glVertex3f( <span class="cpp-number">0</span>.01f * (<span class="cpp-keyword">float</span>)i, <span class="cpp-number">0</span>.0f, <span class="cpp-number">0</span>.0f );
    glVertex3f( <span class="cpp-number">0</span>.01f * ( (<span class="cpp-keyword">float</span>)i + <span class="cpp-number">1</span>.0f ), <span class="cpp-number">0</span>.0f, <span class="cpp-number">0</span>.0f );
    glVertex3f( <span class="cpp-number">0</span>.01f * ( (<span class="cpp-keyword">float</span>)i + <span class="cpp-number">1</span>.0f ), <span class="cpp-number">5</span>.0f, <span class="cpp-number">0</span>.0f );
    glVertex3f( <span class="cpp-number">0</span>.01f * (<span class="cpp-keyword">float</span>)i, <span class="cpp-number">0</span>.0f, <span class="cpp-number">0</span>.0f );
    }
glEnd();
<span class="cpp-comment">//————–</span>
<span class="cpp-keyword">void</span> SetColor( <span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span> clr )
{
<span class="cpp-keyword">switch</span>( clr )
    {
    <span class="cpp-keyword">case</span> RED:
    glColor4f( <span class="cpp-number">1</span>.0f, <span class="cpp-number">0</span>.0f, <span class="cpp-number">0</span>.0f );
    <span class="cpp-keyword">break</span>;
…   

</pre></div><!–ENDSCRIPT–>

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?
Advertisement
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).
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game

This topic is closed to new replies.

Advertisement