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
}
|