PDA

View Full Version : Assign color to multiple cubes



Zath
06-16-2006, 06:55 AM
I've been messing around with the Tokamak physics engine. I place many solid cubes in a stack 10 across and as high as needed for the num of cubes I select.

Now, I want the cubes to be different colors.
Easily done. But they were, well, not random enough.

So, I commented out the lines and made the colors random.

k = ( (rand()*1.0)/RAND_MAX ) * 5;

//k++;
if(k == 1)
SetColorGreen();
if(k == 2)
SetColorBlue();
if(k == 3)
SetColorRed();
if(k == 4)
SetColorYellow();
//if(k == 4)
//k=0;

glCallList(cube_id);

Due to the framerate, the colors blink - change colors that is while playing.

Is there a way to assign a color to each cube randomly so that when the cubes are re-rendered, it remembers it's color assignment?

Thanks all,

Zath

jide
06-16-2006, 08:01 AM
LOL

06-16-2006, 08:09 AM
more like => :rolleyes:

Zath
06-16-2006, 08:41 AM
:p That's not much help.

Overmind
06-16-2006, 10:11 AM
How about setting a random color when creating the display list?

Other than that, just store the color somewhere and use the same in the next frame :p

Zath
06-16-2006, 11:36 AM
Yep, I created an array and saved the color assignment there.....

if(bRandom)
{
k = ( (rand()*1.0)/RAND_MAX ) * 5;
color_state[i] = k;
}
else
k = color_state[i];

Then set bRandom to false after the loop.
Worked like a charm!

Thanks,

Zath

RigidBody
06-17-2006, 03:48 PM
well, doesn't look so charming to me. the if-condition is tested in each frame- although you know it is true only once, in the very first frame you draw.

a better way would be filling the array in your main function, before the main loop is started.