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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
#include <iostream>
#include <GL/freeglut.h>
#include <GL/glut.h>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <vector>
using namespace std;
int X = 0, Y = 1, Z = 0;
GLfloat x = 0., y = .05, z = 0.;
GLfloat xC = 0.2, yC = 0.05, zC = 0.;
GLfloat xP = 0., yP = .6, zP = 0.;
GLfloat spin = .0;
GLfloat ambient_color[] = { .9, .0, .0, 1.0 };
GLfloat specular_color[] = { 1.0, .0, 1.0, 1.0 };
GLfloat light_position[] = { .5, .75, -1.0, 1.0 };
GLfloat black[] = { .0, .0, .0, 1.0 };
GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
int MAX_HEIGHT = 10;
vector<GLfloat> coordinate;
bool start = true;
void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glLightfv(GL_LIGHT0, GL_DIFFUSE, black);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
int startup(void)
{
int n = 0;
GLfloat height = 0.0;
srand(time(0));
GLfloat min = -.80, max = .80;
GLfloat range = (max - min);
for (int i = 0; i < 50; i++) {
height = static_cast<GLfloat>(1 + rand() % (MAX_HEIGHT + 1));
coordinate.push_back(height);
for (int j = 0; j < height; j++) {
coordinate.push_back(x);
coordinate.push_back(y);
coordinate.push_back(z);
n++;
y += .05;
}
x = RAND_MAX / range;
x = min + (rand() / x);
z = RAND_MAX / range;
z = min + (rand() / z);
y = .05;
}
for (int i = 0; i < coordinate.size(); i++) {
cout << coordinate[i] << endl;
}
cout << endl;
return n;
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLfloat min = -.001, max = .001;
GLfloat range = (max - min);
GLfloat a = RAND_MAX / range;
int count = 0;
int k = 0, h = 0, n = 0;
if (start) {
n = startup();
start = false;
}
else {
// Create ground
glPushMatrix();
glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
glTranslatef(.0, .0, .0);
glScalef(2.5, .1, 2.5);
glRotatef(.0, X, Y, Z);
glutSolidCube(.75);
glPopMatrix();
// Create Loner car
glPushMatrix();
glLightfv(GL_LIGHT0, GL_DIFFUSE, black);
glTranslatef(xC, yC, zC);
glScalef(.5, .1, .5);
glutSolidCube(.05);
glPopMatrix();
// Begin Creation of Police Helicopter
glPushMatrix();
glLightfv(GL_LIGHT0, GL_DIFFUSE, ambient_color);
glTranslatef(xP, yP, zP);
glRotatef(spin, X, Y, Z);
glScalef(1., .25, 1.);
glutSolidCube(.05);
glPopMatrix();
srand(time(0));
xP += (min + (rand() / a));
zP += (min + (rand() / a));
if (xP >= .8) {
xP -= .1;
}
if (xP <= -.8) {
xP += .1;
}
if (zP >= .8) {
zP -= .1;
}
if (zP <= -.8) {
zP += .1;
}
// End of Creation
// Check coordinates of car with coordinates of buildings
do {
h = coordinate[k];
for (int i = 0; i < h; i++) {
if (i == 0) {
if ((xC >= (coordinate[k + 1] - .03) && xC <= (coordinate[k + 1] + .03)) && yC == coordinate[k + 2] && (zC >= (coordinate[k + 3] - .03) && zC <= (coordinate[k + 3] + .03))) {
xC -= .01;
zC -= .01;
}
}
k += 3;
}
if (k >= (coordinate.size() - 1))
break;
else
k++;
} while (true);
k = 0;
h = 0;
// Create buildings using stored heights and coordinates.
do {
h = coordinate[k];
for (int i = 0; i < h; i++) {
glPushMatrix();
glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
glTranslatef(coordinate[k + 1], coordinate[k + 2], coordinate[k + 3]);
glutSolidCube(.05);
k += 3;
count++;
glPopMatrix();
}
if (k == (coordinate.size() - 1))
break;
else
k++;
} while (count != n);
}
glutSwapBuffers();
}
void spinDisplay(void) {
spin += 1.0;
if (spin > 360)
spin = 0.;
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'x': glRotatef(1., 1., 0., 0.); break;
case 'y': glRotatef(1., 0., 1., 0.); break;
case 'z': glRotatef(1., 0., 0., 1.); break;
case 'X': glRotatef(-1., 1., 0., 0.); break;
case 'Y': glRotatef(-1., 0., 1., 0.); break;
case 'Z': glRotatef(-1., 0., 0., 1.); break;
case 'w': zC += .01; break;
case 's': zC -= .01; break;
case 'a': xC -= .01; break;
case 'd': xC += .01; break;
}
glutPostRedisplay();
}
void main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1150, 650); glutInitWindowPosition(0, 0);
glutCreateWindow("The City Loner - Mustapha Olokun");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutIdleFunc(spinDisplay);
glutMainLoop();
}
|