Repository navigation
Expand file tree
/
Copy pathproject.cpp
More file actions
476 lines (418 loc) · 10.2 KB
/
Copy pathproject.cpp
File metadata and controls
476 lines (418 loc) · 10.2 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#include <string>
#include <iostream>
#include <fstream>
#include <math.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
// #include "imageloader.h" //used this and imageloader.cpp to handle loading of BMP textures. I did not write it.
using namespace std;
//Menu Options List
enum MenuOptions
{
MENU_LIGHT,
MENU_TEXTURE,
MENU_BOUNCE,
MENU_ROLL,
MENU_EXIT,
NOTHING
};
//This class is used to create an array of pixels for the image
class Image {
public:
Image(char* ps, int w, int h);
~Image();
char* pixels;
int width;
int height;
};
//static declarations and definitions
typedef int BOOL; //define type boolean for easier statements
#define TRUE 1
#define FALSE 0
static float g_lightPos[4] = { 100, 100, -10, 1 };
static BOOL bounce = false;
static GLfloat translation = 4.0;
static BOOL up = false;
static float maxUp = 4;
static float zoom = 10;
static float rotate = 1.5;
static int r = 1;
static float t = 0;
static BOOL roll = false;
static int xmouse = 0;
static int ymouse = 0;
static float rotatePot = 0;
static int windowWidth = 800;
static int windowHeight = 600;
static BOOL lightSwitch = true;
GLuint _textureId;
///////////////////////////
//method declerations
void Display(void);
void RenderObjects(void);
void AnimateScene(void);
void Reshape(GLint width, GLint height);
void Bounce(void);
void Keyboard(unsigned char key, int x, int y);
void Reset(void);
void Roll(void);
void MouseButton(int button, int state, int x, int y);
void Zoom(int z);
void Roll(float r);
void SelectFromMenu(int idCommand);
int BuildPopupMenu();
void processSpecialKeys(int key, int x, int y);
Image* loadBMP(const char* filename);
GLuint loadTextureFromImage(Image* image);
int toInt(const char* bytes);
int readInt(ifstream &input);
///////////////////////////
//main method
int main(int argc, char** argv) {
// GLUT Window Initialization:
glutInit (&argc, argv);
glutInitWindowSize (windowWidth, windowHeight);
glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow ("David & Taylor's Amazing OpenGL Project - A++++++");
// Initialize OpenGL. Put it here instead for my easier reading.
glEnable(GL_DEPTH_TEST);
glutMouseFunc (MouseButton);
BuildPopupMenu ();
glutAttachMenu (GLUT_RIGHT_BUTTON);
glDepthFunc(GL_LESS);
glShadeModel(GL_SMOOTH);
glDisable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
//load texture. Uses imageloader.h which uses imageloader.cpp
Image* img = loadBMP("ball.bmp");
_textureId = loadTextureFromImage(img);
delete img;
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glutDisplayFunc (Display);
glutReshapeFunc (Reshape);
glutKeyboardFunc (Keyboard);
glutSpecialFunc(processSpecialKeys);
glutIdleFunc (AnimateScene);
// Start the main loop
glutMainLoop ();
return 0;
}
///////////////////////////
//display method
void Display(void) {
// Clear frame buffer and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set up the view
glLoadIdentity();
gluLookAt(cos(rotate) * zoom , 3,(zoom * -sin(rotate)), 0, 0, 0, 0, 1, 0); // Set up the camera and allow for zoom and rotation :)
glLightfv(GL_LIGHT0, GL_POSITION, g_lightPos);
// Render the scene
RenderObjects();
glutSwapBuffers();
// Render Bounce if bounce == true
if (bounce) {
if (maxUp <= 0) {
bounce = false;
}
if (!up && translation <= 1.5 ) {
up = true;
maxUp -= 0.50;
}
if (up && translation > maxUp) {
up = false;
}
if(up == false) {
translation -= 0.1; //0.02
} else {
translation += 0.1;
}
}
//Render roll effect if roll = true
if (roll) {
if (maxUp <=0) {
if (r <= 355) {
r+=3;
t+=.05;
} else {
roll = !roll;
}
} else { //Don't roll if it isn't on the ground!!
roll = !roll;
}
}
}
///////////////////////////
//Object renderer
void RenderObjects(void) {
float colorBronzeDiff[4] = { 0.8, 0.6, 0.0, 1.0 }; //borrowed these colors from your sample code :)
float colorBronzeSpec[4] = { 1.0, 1.0, 0.4, 1.0 };
float colorNone[4] = { 0.0, 0.0, 0.0, 0.0 };
float colorWhite[4] = { 1.0, 1.0, 1.0, 1.0 };
glMatrixMode(GL_MODELVIEW);
//make lines for plane
glPushMatrix();
glBegin(GL_LINES);
glMaterialfv(GL_FRONT, GL_DIFFUSE, colorNone);
for (GLfloat i = -10.5; i <= 10.5; i += 0.25) {
glVertex3f(i, 0, 10.5); glVertex3f(i, 0, -10.5);
glVertex3f(10.5, 0, i); glVertex3f(-10.5, 0, i);
}
glEnd();
glPopMatrix();
//Making the Sphere
glPushMatrix();
GLUquadricObj *qObj = gluNewQuadric();
gluQuadricNormals(qObj, GLU_SMOOTH);
gluQuadricTexture(qObj, GL_TRUE);
glTranslatef(xmouse, translation + ymouse, 0 + t);
glMaterialfv(GL_FRONT, GL_DIFFUSE, colorWhite);
//enable texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,_textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glRotated(r,r,0,0);
gluSphere(qObj, 1.0f, 24, 24);
glDisable(GL_TEXTURE_2D); //disable texture so teapot looks bronze still!
glPopMatrix();
//Making the teapot
glPushMatrix();
glTranslatef(4, .75, 0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, colorBronzeDiff);
glMaterialfv(GL_FRONT, GL_SPECULAR, colorBronzeSpec);
glMaterialf(GL_FRONT, GL_SHININESS, 50.0);
glRotatef(rotatePot,0,1,0);
glColor4fv(colorBronzeDiff);
glTranslatef(xmouse, ymouse, 0);
glutSolidTeapot(1);
glPopMatrix();
}
///////////////////////////
//idle animator method
void AnimateScene(void) {
glClearColor(.8, .8, 1, .8);
glutPostRedisplay();
}
///////////////////////////
//Shaping function
void Reshape(GLint width, GLint height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (float)width / height, 1, 1000);
glMatrixMode(GL_MODELVIEW);
}
///////////////////////////
//Bounce function
void Bounce(void) {
if (maxUp > 0)
bounce = !bounce;
glutPostRedisplay();
}
///////////////////////////
//Zoom function
void Zoom(int z) {
zoom += z;
if (zoom > 15)
zoom = 15;
if (zoom < 5)
zoom = 5;
}
///////////////////////////
//Rotate function
void Rotate(float r) {
rotate += r;
}
///////////////////////////
//Roll function
void Roll(void) {
roll = !roll;
glutPostRedisplay();
}
///////////////////////////
//Reset function to reset scene
void Reset(void) {
maxUp = 4;
up = FALSE;
translation = 4;
zoom = 10;
rotate = 1.5;
r = 1;
t = 0;
roll = FALSE;
bounce = FALSE;
xmouse = 0;
ymouse = 0;
rotatePot = 0;
glEnable(GL_LIGHTING);
}
///////////////////////////
//Register Keyboard Commands
void Keyboard(unsigned char key, int x, int y) {
switch (key)
{
case 'd':
SelectFromMenu(MENU_BOUNCE);
break;
case 'w':
Zoom(-1);
break;
case 's':
Zoom(1);
break;
case 'q':
Rotate(-.1);
break;
case 'e':
Rotate(.1);
break;
case 'r':
Reset();
break;
case 'p':
SelectFromMenu(MENU_ROLL);
break;
case 27:
exit(0);
break;
}
}
void processSpecialKeys(int key, int x, int y) {
switch(key) {
case GLUT_KEY_LEFT :
rotatePot-=3;
break;
case GLUT_KEY_RIGHT :
rotatePot+=3;
break;
case GLUT_KEY_UP :
Zoom(-1);
break;
case GLUT_KEY_DOWN :
Zoom(1);
break;
}
}
///////////////////////////
//Register Mouse Click for moving objects
void MouseButton(int button, int prostate, int x, int y) {
if (button == GLUT_LEFT_BUTTON)
{
if (y < (windowWidth/2) && ymouse < 4) {
ymouse++;
} else if (y > (windowWidth/2) && ymouse > -4) {
ymouse--;
}
if (x < (windowHeight/2) && xmouse < 4) {
xmouse++;
} else if (x > (windowHeight/2) && xmouse > -4) {
xmouse--;
}
}
}
///////////////////////////
//Create the right click menu
int BuildPopupMenu (void)
{
int menu;
menu = glutCreateMenu (SelectFromMenu);
glutAddMenuEntry ("RIGHT CLICK ON SELECTION", NOTHING);
glutAddMenuEntry ("Flick the Lights", MENU_LIGHT);
glutAddMenuEntry ("[D]rop the Ball", MENU_BOUNCE);
glutAddMenuEntry ("[P]ush the Ball", MENU_ROLL);
glutAddMenuEntry ("[Esc]ape/Exit", MENU_EXIT);
return menu;
}
void SelectFromMenu(int idCommand)
{
switch (idCommand)
{
case MENU_LIGHT:
lightSwitch = !lightSwitch;
if (lightSwitch)
glEnable(GL_LIGHTING);
else
glDisable(GL_LIGHTING);
break;
case MENU_BOUNCE:
Bounce();
break;
case MENU_ROLL:
Roll();
break;
case MENU_EXIT:
exit(0);
break;
}
glutPostRedisplay();
}
///////////////////////////
//Define the Image types for the texture
Image::Image(char* ps, int w, int h) : pixels(ps), width(w), height(h) {
}
Image::~Image() {
}
///////////////////////////
//Convert a char to an integer
int toInt(const char* bytes) {
return (int)(((unsigned char)bytes[3] << 24) |
((unsigned char)bytes[2] << 16) |
((unsigned char)bytes[1] << 8) |
(unsigned char)bytes[0]);
}
///////////////////////////
//Reads the next four bytes as a char and converts them to an integer using toInt
int readInt(ifstream &input) {
char buffer[4];
input.read(buffer, 4);
return toInt(buffer);
}
///////////////////////////
//Load the Bitmap file (found on the web)
Image* loadBMP(const char* filename) {
ifstream infile(filename, ifstream::binary);
infile.seekg(10, std::ios::cur);
int dataOffset = readInt(infile);
//Read the header
int headerSize = readInt(infile);
int width = readInt(infile);
int height = readInt(infile);
//Read the data
int bytesPerRow = ((width * 3 + 3) / 4) * 4 - (width * 3 % 4);
int size = bytesPerRow * height;
char* pixels = new char[size];
infile.seekg(dataOffset, ios_base::beg);
infile.read(pixels, size);
infile.close();
//Get the data into the right format
char* pixels2 = new char[width * height * 3];
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
for(int c = 0; c < 3; c++) {
pixels2[3*(width*y + x) + c] = pixels[bytesPerRow*y + 3*x + (2 - c)];
}
}
}
delete[] pixels;
return new Image(pixels2, width, height);
}
///////////////////////////
//Convert image (bitmap) to OpenGL unsigned integer which will be our actual texture
GLuint loadTextureFromImage(Image* image) {
GLuint textureId;
glGenTextures(1, &textureId); //Make room for our texture
glBindTexture(GL_TEXTURE_2D, textureId);
//Map the image to the texture
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGB,
image->width, image->height,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
image->pixels);
return textureId;
}
///////////////////////////