PDA

View Full Version : Question about Colours



Bart
01-15-2003, 01:08 AM
Hello,
i have the following Problem. Through the function below i can create squares. Now i would like to give each square a different color. Is this possible? If not would it be possible to color each row in a different way?
Would be very glad if anyone could help me.
Here is my source code:

void QuadratColor(float length,int quant)
{
GLuint DlQuadrat=1;
int i,j;
glNewList (DlQuadrat,GL_COMPILE);
glBegin(GL_LINE_LOOP);
glVertex2f(length, length);
glVertex2f(0, length);
glVertex2f(0, 0);
glVertex2f(length, 0);
glEnd();
glEndList();
for (i=0;i<quant;i++)
{
glPushMatrix();
for (j=0;j<quant;j++){
glPushMatrix();
glCallList(DlQuadrat);
glPopMatrix();
glTranslatef(length,0,0);
}
glPopMatrix();
glTranslatef(0,length,0);
}}

T
01-15-2003, 01:16 AM
Assuming you are not using lighting, OpenGL uses the current colour when drawing. This is set using the glColor* set of commands (e.g. glColor3f(1.0, 0.0, 0.0); sets red). If you set the colour before executing the display list, it will be used by the drawing commands inside the display list.

Bart
01-15-2003, 02:39 AM
Thx for the answer. But unfortunately that is not my problem. If i do it the way you describe it, thann all my square would be red.
But I would like to know if i can do it in a way that the first square is e.g red the second blue the third cyan and so on.
Or is it possible to allocate a random color?

Mazy
01-15-2003, 03:05 AM
in the loop

glPushMatrix
glColor
glDrawList
glpopMatrix

jebus
01-15-2003, 03:15 AM
Mazy: pushing the current matrix will not save the color information. you need this:

glPushAttrib(GL_COLOR_BUFFER_BIT);
glColor3f(<your color> );
...
glPopAttrib();

there are many other attributes you can pass, including all of them (GL_CURRENT_BIT i think)

Bart: you can set the color to whatever you want. for random colors, make a MACRO that selects a number at random from 0-255, then pass 3 of those MACROS to glColor3ub(); good luck, and don't have a cow, man!

jebus

Bart
01-15-2003, 03:16 AM
@Mazy
Sorry, but could you please explain your code for me a little bit more?
In which loop shall i put the Lines? In the first or the second.
And do i understand it right that through the glColor i can choose the different colors i want to use and through glDrawList they are somwhow randomized?
Would be very nice of you if you can explain it to me.