### OpenGL place many quads problem

Hey everyone, I am trying to write a script that will randomly generate squares all over the map however I can even get it to generate squares all over the map without implementing an randomness. I have tried using a for and while loop but it doesn't seem to be working. It only draws one square. Any help would be much appreciated

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111`` ``````#include #include #include #include #include #ifdef __APPLE__ #include #include #else #include #endif using namespace std; class Shapes{ public: float x = -0.9f; float y = -0.9f; void background(){ glBegin(GL_LINE_LOOP); glColor3f(0.22f, 0.22f, 0.22f); glVertex3f(-0.9f, -0.9f, 0.0f); glVertex3f(0.9f, -0.9f, 0.0f); glVertex3f(0.9f, 0.9f, 0.0f); glVertex3f(-0.9f, 0.9f, 0.0f); glEnd(); float i = -0.9f; while (i < 0.9f) { glBegin(GL_LINES); glColor3f(0.22f, 0.22f, 0.22f); glVertex3f(0.9f, i, 0.0f); glVertex3f(-0.9f, i, 0.0f); i += 0.02; glEnd(); } float j = -0.9f; while (j < 0.9f) { glBegin(GL_LINES); glColor3f(0.22f, 0.22f, 0.22f); glVertex3f(j, 0.9f, 0.0f); glVertex3f(j, -0.9f, 0.0f); j += 0.02; glEnd(); } } void greySquare(){ glBegin(GL_POLYGON); //Draw the square with respect to x and y glVertex3f(x, y, 0.0f); glVertex3f(x, y + 0.02f, 0.0f); glVertex3f(x + 0.02f, y + 0.02f, 0.0f); glVertex3f(x + 0.02f, y, 0.0f); glEnd(); } void randomlyPlaceSquares(){ Shapes shapesKey; while (x < 0.9f && y < 0.9f) { //if random number % 2 = 0 shapesKey.greySquare(); //else x += 0.02; if (x > 0.9) { x = -0.9f; y += 0.02f; } } } }; void initRendering(){ //Makes 3D drawing work when something is in front of something else glEnable(GL_DEPTH_TEST); glEnable(GL_COLOR_MATERIAL); } void drawScene() { //Clear information from last draw glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective glLoadIdentity(); //Reset the drawing perspective Shapes shapesKey; shapesKey.background(); shapesKey.randomlyPlaceSquares(); glutSwapBuffers(); //Send the 3D scene to the screen } int main (int argc, char **argv) { glutInit(&argc, argv); // Initialize GLUT glutInitDisplayMode (GLUT_SINGLE); // Set up a basic display buffer (only single buffered for now) glutInitWindowSize (800, 800); // Set the width and height of the window glutInitWindowPosition (0, 0); // Set the position of the window glutCreateWindow ("Randomly Generated Map"); // Set the title for the window glutDisplayFunc(drawScene); // Tell GLUT to use the method "display" for rendering glutMainLoop(); // Enter GLUT's main loop }``````
To be honest i do not see why this draws any Quads at all. The problem is that the x and y variables are members of the Shapes class, and you never reset them to their original values.

So when you run the main rendering loop for the first time, it should correctly display all the quads (not very randomly distributed tho). But just a fraction of a second later the main rendering loop will be executed again, clearing the last drawing and then calling `shapesKey.randomlyPlaceSquares();` again.
But this time, x is approximately zero and y is a bit greater than 0.9, so no quad should be printed at all.

What you should do is reset x and y to their original values -0.9 each time before you call `shapesKey.randomlyPlaceSquares();`.

Also in line 70 you are decreasing x by 0.9f when it's greater than 0.9. It would be better to directly set it to 0.02, because then the results you get will be more predictable. In general it is no good practice to use floats as loop variables the way you did, because you can get rather surprising results when doing this. The reason is that you're doing all the calculations with finite precision, so you get rounding errors each time you do an arithmetic operation like addition, multiplication, division. Your while loop in line 64 runs about 90+44*90=4140 times, so the rounding errors add up and may lead to unprecise results. I suggest change the loop variable to one of integer type (in all the loops).

Edit: I just noticed that you create a new instance of the Shapes class each time the main rendering loop executes, so everything i've written above and crossed out is actually wrong, although it would be a more efficient solution just to reset the x and y values each time.

Maybe the problem is that it actually draws all the quads, but so close to each other that you are unable to distinguish them from each other, which makes it look like a single large quad? After all there is no distance at all between the different quads (because you're adding 0.02f in the greySquare routine)
Last edited on
Hey I managed to fix the problem by reseting the x and y values before calling shapesKey.randomlyPlaceSquares();. Thanks a lot for the help! :D
Topic archived. No new replies allowed.