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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cmath>

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#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.