PDA

View Full Version : How do I draw multipule triangles



Wonderfulcow
10-17-2000, 08:49 AM
Hello there, I'm having problems with the script which, i want it to output (print out) multiple triangles at random places.
Do i first, have to define a specific size a triangle and then add a function that will display it randomly on the screen? or is there a way to have the program drawing triangles randomly on the window?

Glossifah
10-17-2000, 01:50 PM
I'll assume you know how to seed a random number and generate a random number with the rand() function and say this:
Create a function that draws a triangle...

glBegin(GL_TRIANGLES)
glVertex3f(x1, y1, z1);
glVertex3f(x2, y2, z2);
glVertex3f(x3, y3, z3);
glEnd();

Have the function take two inputs like xLoc, yLoc, and use those inputs as translation coordinates in a transformation matrix.

Initialize two integers to be random numbers with range validation (use the modulus operator, %). Make sure when you set up the ortho or perspective viewport that the range validation falls within those params, otherwise the triangles will be off screen.

For every triangle you want to randomly place on the screen, call the triangle creation function and pass it the newly updated random numbers.

Okay, after reading back through this reply, I'm being a bit obtuse. Let me clarify:

// initialize the variables
int randomXLoc = 0;
int randomYLoc = 0;

// function to randomize the variables
void randomize(void);
{
// randomize those variables
int randomXLoc = (rand()%100);
int randomYLoc = (rand()%100);
}

// function to draw random triangles
void drawTriangles(void);
{
// container for the number of triangles drawn
int i = 0;

while(i < 100) // or however many tri's you need
{
glPushMatrix();
glTranslatef(randomXLoc, 1.0f, 0.0f, 0.0f);
glTranslatef(randomYLoc, 0.0f, 1.0f, 0.0f);

glBegin(GL_TRIANGLES);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glEnd();

glPopMatrix();
randomize();
}
} // end of triangles function

Does that help?

Glossifah