View Full Version : Randomly Coloured Polygons

mesmeric

10-18-2005, 09:58 AM

I am trying to create 20 randomly coloured polygons each with a random number of sides. I am using the following code but unfortunatley all the polygons are black. It works for lines and points but not polygons. Any ideas would be very helpful:)

Thanks , Mes...

void polygons(void )

{

glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_POLYGON);

srand (time(0));

for (int i=0;i<=20;i++)

{

int col = 1.0 * rand () / RAND_MAX ;

glColor3f(col,col,col);

glVertex2f(rand()%640, rand()%480);

}

glEnd();

glFlush();

}

Aeluned

10-18-2005, 10:30 AM

You can only send vertices that define a convex polygon in OpenGL.

By randomly generating vertices there is no guarantee that you will honor that convention.

You need some logic in there to ensure that the polygons you are generating are actually convex.

If you don't know the difference betwixt convex and concave polygons then clicky (http://mathworld.wolfram.com/ConvexPolygon.html)

M4C6YV3R

10-21-2005, 10:11 PM

I could easily be wrong, but as you defined col as an int, the only possible values for col are 0 and 1. 1 will only occur when rand() produces RAND_MAX, which is not very likely. Thus, you are calling glColor3f( 0, 0, 0), which is black.

All you really need to do is declare col as a float, not an int.

You might also be more satisfied with your results if you had col_R, col_G, and col_B, all random, so that your polygons aren't all shades of gray.

Hope this helps. Good luck!

Aeluned

10-25-2005, 06:25 AM

You're not wrong M4C6YV3R. That is absolutely correct.

UrbanLegend

10-25-2005, 07:03 AM

Uhm sorry Aeluned you are quite wrong and M4C6YV3R is correct the line

int col = 1.0 * rand () / RAND_MAX ;

will for the most part producs 0 as its output.

Try it out

heres a quick test to prove this with output

for (int i= 0;i <= 20; i++){

int col = 1.0 * rand () / RAND_MAX ;

float colf = 1.0 * rand () / RAND_MAX ;

printf( " Col = %d , colf = %f \n" , col, colf );

}As you can see col is always Zero

output is:

Col = 0 , colf = 0.563585

Col = 0 , colf = 0.808740

Col = 0 , colf = 0.479873

Col = 0 , colf = 0.895962

Col = 0 , colf = 0.746605

Col = 0 , colf = 0.858943

Col = 0 , colf = 0.513535

Col = 0 , colf = 0.014985

Col = 0 , colf = 0.364452

Col = 0 , colf = 0.165899

Col = 0 , colf = 0.445692

Col = 0 , colf = 0.004669

Col = 0 , colf = 0.377880

Col = 0 , colf = 0.571184

Col = 0 , colf = 0.607166

Col = 0 , colf = 0.663045

Col = 0 , colf = 0.352123

Col = 0 , colf = 0.607685

Col = 0 , colf = 0.802606

Col = 0 , colf = 0.301950

Col = 0 , colf = 0.726676

Solution here is to change col from an int to float

Aeluned

10-25-2005, 08:39 AM

Read my post more carefully; I said he was right .

You're not wrong M4C6YV3R

not wrong = right.

UrbanLegend

10-26-2005, 08:13 AM

DOH

Sorry Aeluned , totally mis-read your reply :( , the not got totally lost :eek:

Need more Beer I think

Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.