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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cmath>
#include <time.h>
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
using namespace std;
float x = -0.9f;
float y = -0.9f;
class UserInput{
public:
bool* keyStates = new bool[256];
void clearKeyStates(){
for (char i=0; i<256; i++)
{
keyStates[i] = false;
}
}
void quitProgram(void){
if (keyStates[27])
{
exit(0);
}
glutPostRedisplay();
}
void keyPressed (unsigned char key, int x, int y){
keyStates[key] = true; // Set the state of the current key to pressed
quitProgram();
}
void keyUp (unsigned char key, int x, int y){
keyStates[key] = false; // Set the state of the current key to not pressed
}
};
class Shapes{
public:
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 randomlyPlaceSquares(){
Shapes shapesKey;
int c = time(NULL);
while (x < 0.88f && y < 0.88f) {
//if random number % 2 = 0
srand(c);
int r = rand() % 5;
if (r == 0)
{
shapesKey.greySquare();
}
//else
x += 0.02;
if (x > 0.88) {
x = -0.9f;
y += 0.02f;
}
c *= c + 1;
}
}
private:
void greySquare(){
glBegin(GL_POLYGON);
glColor3f(0.22f, 0.22f, 0.22f);
//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 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();
x = -0.9f;
y = -0.9f;
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
UserInput userInputKey;
glutKeyboardFunc(keyPressed); // Tell GLUT to use the method "keyPressed" for key presses
glutKeyboardUpFunc(keyUp); // Tell GLUT to use the method "keyUp" for key up events
glutMainLoop(); // Enter GLUT's main loop
}
|