-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
882 lines (752 loc) · 20.8 KB
/
main.cpp
File metadata and controls
882 lines (752 loc) · 20.8 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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include <stdio.h>
#include <iostream>
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "imageloader.h"
#include "vec3f.h"
using namespace std;
//////////////////////////////////////////////////////////////////////////////////////
/////////////////////////PARTICLE ENGINE//////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
const float PI = 3.1415926535f;
//Returns a random float from 0 to < 1
float randomFloat() {
return (float)rand() / ((float)RAND_MAX + 1);
}
//Represents a single particle.
struct Particle {
Vec3f pos;
Vec3f velocity;
Vec3f color;
float timeAlive; //The amount of time that this particle has been alive.
float lifespan; //The total amount of time that this particle is to live.
};
//Rotates the vector by the indicated number of degrees about the specified axis
Vec3f rotate(Vec3f v, Vec3f axis, float degrees) {
axis = axis.normalize();
float radians = degrees * PI;
float s = sin(radians);
float c = cos(radians);
return v * c + axis * axis.dot(v) * (1 - c) + v.cross(axis) * s;
}
//Returns the position of the particle, after rotating the camera
Vec3f adjParticlePos(Vec3f pos) {
return rotate(pos, Vec3f(1, 0, 0), 5);
}
//Returns whether particle1 is in back of particle2
bool compareParticles(Particle* particle1, Particle* particle2) {
return adjParticlePos(particle1->pos)[2] <
adjParticlePos(particle2->pos)[2];
}
//Change this to alter particles Path...
const float GRAVITY = 4.0f;
const int NUM_PARTICLES = 1000;
//The interval of time, in seconds, by which the particle engine periodically
//steps.
const float STEP_TIME = 0.01f;
//The length of the sides of the quadrilateral drawn for each particle.
const float PARTICLE_SIZE = 0.05f;
class ParticleEngine {
private:
GLuint textureId;
Particle particles[NUM_PARTICLES];
//The amount of time until the next call to step().
float timeUntilNextStep;
//The color of particles that the fountain is currently shooting. 0
//indicates red, and when it reaches 1, it starts over at red again. It
//always lies between 0 and 1.
float colorTime;
//The angle at which the fountain is shooting particles, in radians.
float angle;
//Returns the current color of particles produced by the fountain.
Vec3f curColor() {
Vec3f color;
if (colorTime < 0.166667f) {
color = Vec3f(1.0f, colorTime * 6, 0.0f);
}
else if (colorTime < 0.333333f) {
color = Vec3f((0.333333f - colorTime) * 6, 1.0f, 0.0f);
}
else if (colorTime < 0.5f) {
color = Vec3f(0.0f, 1.0f, (colorTime - 0.333333f) * 6);
}
else if (colorTime < 0.666667f) {
color = Vec3f(0.0f, (0.666667f - colorTime) * 6, 1.0f);
}
else if (colorTime < 0.833333f) {
color = Vec3f((colorTime - 0.666667f) * 6, 0.0f, 1.0f);
}
else {
color = Vec3f(1.0f, 0.0f, (1.0f - colorTime) * 6);
}
//Make sure each of the color's components range from 0 to 1
for(int i = 0; i < 3; i++) {
if (color[i] < 0) {
color[i] = 0;
}
else if (color[i] > 1) {
color[i] = 1;
}
}
return color;
}
//Returns the average velocity of particles produced by the fountain.
Vec3f curVelocity() {
return Vec3f(2 * cos(angle), 2.0f, 2 * sin(angle));
}
//Alters p to be a particle newly produced by the fountain.
void createParticle(Particle* p) {
p->pos = Vec3f(0, 0, 0);
p->velocity = curVelocity() + Vec3f(0.5f * randomFloat() - 0.25f,
0.5f * randomFloat() - 0.25f,
0.5f * randomFloat() - 0.25f);
p->color = curColor();
p->timeAlive = 0;
p->lifespan = randomFloat() + 1;
}
//Advances the particle fountain by STEP_TIME seconds.
void step() {
colorTime += STEP_TIME / 10;
while (colorTime >= 1) {
colorTime -= 1;
}
angle += 0.5f * STEP_TIME;
while (angle > 2 * PI/30) {
angle -= 2 * PI;
}
for(int i = 0; i < NUM_PARTICLES; i++) {
Particle* p = particles + i;
p->pos += p->velocity * STEP_TIME;
p->velocity += Vec3f(0.0f, -GRAVITY * STEP_TIME, 0.0f);
p->timeAlive += STEP_TIME;
if (p->timeAlive > p->lifespan) {
createParticle(p);
}
}
}
public:
ParticleEngine(GLuint textureId1) {
textureId = textureId1;
timeUntilNextStep = 0;
colorTime = 0;
angle = 0;
for(int i = 0; i < NUM_PARTICLES; i++) {
createParticle(particles + i);
}
for(int i = 0; i < 5 / STEP_TIME; i++) {
step();
}
}
//Advances the particle fountain by the specified amount of time.
void advance(float dt) {
while (dt > 0) {
if (timeUntilNextStep < dt) {
dt -= timeUntilNextStep;
step();
timeUntilNextStep = STEP_TIME;
}
else {
timeUntilNextStep -= dt;
dt = 0;
}
}
}
//Draws the particle fountain.
void draw() {
vector<Particle*> ps;
for(int i = 0; i < NUM_PARTICLES; i++) {
ps.push_back(particles + i);
}
sort(ps.begin(), ps.end(), compareParticles);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBegin(GL_QUADS);
for(unsigned int i = 0; i < ps.size(); i++) {
Particle* p = ps[i];
glColor4f(p->color[0], p->color[1], p->color[2],
(1 - p->timeAlive / p->lifespan));
float size = PARTICLE_SIZE / 2;
Vec3f pos = adjParticlePos(p->pos);
glTexCoord2f(0, 0);
glVertex3f(pos[0] - size, pos[1] - size-2.1, pos[2]);
glTexCoord2f(0, 1);
glVertex3f(pos[0] - size, pos[1] + size-2.1, pos[2]);
glTexCoord2f(1, 1);
glVertex3f(pos[0] + size, pos[1] + size-2.1, pos[2]);
glTexCoord2f(1, 0);
glVertex3f(pos[0] + size, pos[1] - size-2.1, pos[2]);
}
glEnd();
glDisable(GL_TEXTURE_2D);
}
};
const int TIMER_MS = 25; //The number of milliseconds to which the timer is set
ParticleEngine* _particleEngine;
GLuint _textureId;
void cleanup() {
delete _particleEngine;
}
//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
cleanup();
exit(0);
}
}
char* addAlphaChannel(Image* image, Image* alphaChannel) {
char* pixels = new char[image->width * image->height * 4];
for(int y = 0; y < image->height; y++) {
for(int x = 0; x < image->width; x++) {
for(int j = 0; j < 3; j++) {
pixels[4 * (y * image->width + x) + j] =
image->pixels[3 * (y * image->width + x) + j];
}
pixels[4 * (y * image->width + x) + 3] =
alphaChannel->pixels[3 * (y * image->width + x)];
}
}
return pixels;
}
GLuint loadAlphaTexture(Image* image, Image* alphaChannel) {
char* pixels = addAlphaChannel(image, alphaChannel);
GLuint textureId;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
image->width, image->height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
pixels);
delete pixels;
return textureId;
}
void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//Please Change Destination
Image* image = loadBMP("circle.bmp");
Image* alphaChannel = loadBMP("circlealpha.bmp");
_textureId = loadAlphaTexture(image, alphaChannel);
delete image;
delete alphaChannel;
}
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
glMatrixMode(GL_MODELVIEW);
}
void update(int value) {
_particleEngine->advance(TIMER_MS / 1000.0f);
glutPostRedisplay();
glutTimerFunc(TIMER_MS, update, 0);
}
//////////////////////////////////////////////////////////////////////////////////////
/////////////////////////BUTTON ENGINE//////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
struct Mouse
{
int x; /* the x coordinate of the mouse cursor */
int y; /* the y coordinate of the mouse cursor */
int lmb; /* is the left button pressed? */
int mmb; /* is the middle button pressed? */
int rmb; /* is the right button pressed? */
int xpress; /* stores the x-coord of when the first button press occurred */
int ypress; /* stores the y-coord of when the first button press occurred */
};
typedef struct Mouse Mouse;
/*
* Create a global mouse structure to hold the mouse information.
*/
Mouse TheMouse = {0,0,0,0,0};
/*
* Variables to hold the current size of the window.
*/
//reolution
int winw = 1024;
int winh = 786;
/*----------------------------------------------------------------------------------------
* Button Stuff
*/
typedef void (*ButtonCallback)();
/*
* Button Engine
*/
struct Button
{
int x; /* top left x coord of the button */
int y; /* top left y coord of the button */
int w; /* the width of the button */
int h; /* the height of the button */
int state; /* the state, 1 if pressed, 0 otherwise */
int highlighted; /* is the mouse cursor over the control? */
char* label; /* the text label of the button */
ButtonCallback callbackFunction; /* A pointer to a function to call if the button is pressed */
};
typedef struct Button Button;
void TheButtonCallback()
{
printf("I have been called\n");
}
//Define Button Objects
Button MyButton1 = {5,5, 100,25, 0,0, "Button", TheButtonCallback };
Button MyButton2 = {5,40, 100,25, 0,0, "Enter", TheButtonCallback };
/*----------------------------------------------------------------------------------------
* \brief This function draws a text string to the screen using glut bitmap fonts.
* \param font - the font to use. it can be one of the following :
*
* GLUT_BITMAP_9_BY_15
* GLUT_BITMAP_8_BY_13
* GLUT_BITMAP_TIMES_ROMAN_10
* GLUT_BITMAP_TIMES_ROMAN_24
* GLUT_BITMAP_HELVETICA_10
* GLUT_BITMAP_HELVETICA_12
* GLUT_BITMAP_HELVETICA_18
*
* \param text - the text string to output
* \param x - the x co-ordinate
* \param y - the y co-ordinate
*/
void Font(void *font,char *text,int x,int y)
{
glRasterPos2i(x, y);
while( *text != '\0' )
{
glutBitmapCharacter( font, *text );
++text;
}
}
/*----------------------------------------------------------------------------------------
* \brief This function is used to see if a mouse click or event is within a button
* client area.
* \param b - a pointer to the button to test
* \param x - the x coord to test
* \param y - the y-coord to test
*/
int ButtonClickTest(Button* b,int x,int y)
{
if( b)
{
/*
* If clicked within button area, then return true
*/
if( x > b->x &&
x < b->x+b->w &&
y > b->y &&
y < b->y+b->h ) {
return 1;
}
}
/*
* otherwise false.
*/
return 0;
}
/*----------------------------------------------------------------------------------------
* \brief This function draws the specified button.
* \param b - a pointer to the button to check.
* \param x - the x location of the mouse cursor.
* \param y - the y location of the mouse cursor.
*/
void ButtonRelease(Button *b,int x,int y)
{
if(b)
{
/*
* If the mouse button was pressed within the button area
* as well as being released on the button.....
*/
if( ButtonClickTest(b,TheMouse.xpress,TheMouse.ypress) &&
ButtonClickTest(b,x,y) )
{
/*
* Then if a callback function has been set, call it.
*/
if (b->callbackFunction) {
b->callbackFunction();
}
}
/*
* Set state back to zero.
*/
b->state = 0;
}
}
/*----------------------------------------------------------------------------------------
* \brief This function draws the specified button.
* \param b - a pointer to the button to check.
* \param x - the x location of the mouse cursor.
* \param y - the y location of the mouse cursor.
*/
void ButtonPress(Button *b,int x,int y)
{
if(b)
{
/*
* if the mouse click was within the buttons client area,
* set the state to true.
*/
if( ButtonClickTest(b,x,y) )
{
b->state = 1;
}
}
}
/*----------------------------------------------------------------------------------------
* \brief This function draws the specified button.
* \param b - a pointer to the button to check.
* \param x - the x location of the mouse cursor.
* \param y - the y location of the mouse cursor.
*/
void ButtonPassive(Button *b,int x,int y)
{
if(b)
{
/*
* if the mouse moved over the control
*/
if( ButtonClickTest(b,x,y) )
{
/*
* If the cursor has just arrived over the control, set the highlighted flag
* and force a redraw. The screen will not be redrawn again until the mouse
* is no longer over this control
*/
if( b->highlighted == 0 ) {
b->highlighted = 1;
glutPostRedisplay();
}
}
else
/*
* If the cursor is no longer over the control, then if the control
* is highlighted (ie, the mouse has JUST moved off the control) then
* we set the highlighting back to false, and force a redraw.
*/
if( b->highlighted == 1 )
{
b->highlighted = 0;
glutPostRedisplay();
}
}
}
/*----------------------------------------------------------------------------------------
* \brief This function draws the specified button.
* \param b - a pointer to the button to draw.
*/
void ButtonDraw(Button *b)
{
int fontx;
int fonty;
if(b)
{
/*
* We will indicate that the mouse cursor is over the button by changing its
* colour.
*/
if (b->highlighted)
glColor3f(0.7f,0.7f,0.8f);
else
glColor3f(0.6f,0.6f,0.6f);
/*
* draw background for the button.
*/
glBegin(GL_QUADS);
glVertex2i( b->x , b->y );
glVertex2i( b->x , b->y+b->h );
glVertex2i( b->x+b->w, b->y+b->h );
glVertex2i( b->x+b->w, b->y );
glEnd();
/*
* Draw an outline around the button with width 3
*/
glLineWidth(3);
/*
* The colours for the outline are reversed when the button.
*/
if (b->state)
glColor3f(0.4f,0.4f,0.4f);
else
glColor3f(0.8f,0.8f,0.8f);
glBegin(GL_LINE_STRIP);
glVertex2i( b->x+b->w, b->y );
glVertex2i( b->x , b->y );
glVertex2i( b->x , b->y+b->h );
glEnd();
if (b->state)
glColor3f(0.8f,0.8f,0.8f);
else
glColor3f(0.4f,0.4f,0.4f);
glBegin(GL_LINE_STRIP);
glVertex2i( b->x , b->y+b->h );
glVertex2i( b->x+b->w, b->y+b->h );
glVertex2i( b->x+b->w, b->y );
glEnd();
glLineWidth(1);
/*
* Calculate the x and y coords for the text string in order to center it.
*/
string str = b->label;
fontx = b->x + (b->w - glutBitmapLength(GLUT_BITMAP_HELVETICA_10,(const unsigned char *)str.c_str())) / 2 ;
fonty = b->y + (b->h+10)/2;
/*
* if the button is pressed, make it look as though the string has been pushed
* down. It's just a visual thing to help with the overall look....
*/
if (b->state) {
fontx+=2;
fonty+=2;
}
/*
* If the cursor is currently over the button we offset the text string and draw a shadow
*/
if(b->highlighted)
{
glColor3f(0,0,0);
Font(GLUT_BITMAP_HELVETICA_10,b->label,fontx,fonty);
fontx--;
fonty--;
}
glColor3f(1,1,1);
Font(GLUT_BITMAP_HELVETICA_10,b->label,fontx,fonty);
}
}
/*----------------------------------------------------------------------------------------
* \brief This function is called to initialise opengl.
*/
void Init()
{
glEnable(GL_LIGHT0);
}
/*----------------------------------------------------------------------------------------
* This function will be used to draw the 3D scene
*/
void Draw3D()
{
gluLookAt(0,1,5,0,0,0,0,1,0);
}
/*----------------------------------------------------------------------------------------
* This function will be used to draw an overlay over the 3D scene.
* This will be used to draw our fonts, buttons etc......
*/
void Draw2D()
{
ButtonDraw(&MyButton1);
ButtonDraw(&MyButton2);
}
/*----------------------------------------------------------------------------------------
* \brief This function is called whenever a mouse button is pressed or released
* \param button - GLUT_LEFT_BUTTON, GLUT_RIGHT_BUTTON, or GLUT_MIDDLE_BUTTON
* \param state - GLUT_UP or GLUT_DOWN depending on whether the mouse was released
* or pressed respectivly.
* \param x - the x-coord of the mouse cursor.
* \param y - the y-coord of the mouse cursor.
*/
void MouseButton(int button,int state,int x, int y)
{
/*
* update the mouse position
*/
TheMouse.x = x;
TheMouse.y = y;
/*
* has the button been pressed or released?
*/
if (state == GLUT_DOWN)
{
/*
* This holds the location of the first mouse click
*/
if ( !(TheMouse.lmb || TheMouse.mmb || TheMouse.rmb) ) {
TheMouse.xpress = x;
TheMouse.ypress = y;
}
/*
* Which button was pressed?
*/
switch(button)
{
case GLUT_LEFT_BUTTON:
TheMouse.lmb = 1;
ButtonPress(&MyButton1,x,y);
ButtonPress(&MyButton2,x,y);
case GLUT_MIDDLE_BUTTON:
TheMouse.mmb = 1;
break;
case GLUT_RIGHT_BUTTON:
TheMouse.rmb = 1;
break;
}
}
else
{
/*
* Which button was released?
*/
switch(button)
{
case GLUT_LEFT_BUTTON:
TheMouse.lmb = 0;
ButtonRelease(&MyButton1,x,y);
ButtonRelease(&MyButton2,x,y);
break;
case GLUT_MIDDLE_BUTTON:
TheMouse.mmb = 0;
break;
case GLUT_RIGHT_BUTTON:
TheMouse.rmb = 0;
break;
}
}
/*
* Force a redraw of the screen. If we later want interactions with the mouse
* and the 3D scene, we will need to redraw the changes.
*/
glutPostRedisplay();
}
/*----------------------------------------------------------------------------------------
* \brief This function is called whenever the mouse cursor is moved AND A BUTTON IS HELD.
* \param x - the new x-coord of the mouse cursor.
* \param y - the new y-coord of the mouse cursor.
*/
void MouseMotion(int x, int y)
{
/*
* Calculate how much the mouse actually moved
*/
int dx = x - TheMouse.x;
int dy = y - TheMouse.y;
/*
* update the mouse position
*/
TheMouse.x = x;
TheMouse.y = y;
/*
* Check MyButton to see if we should highlight it cos the mouse is over it
*/
ButtonPassive(&MyButton1,x,y);
ButtonPassive(&MyButton2,x,y);
/*
* Force a redraw of the screen
*/
glutPostRedisplay();
}
/*----------------------------------------------------------------------------------------
* \brief This function is called whenever the mouse cursor is moved AND NO BUTTONS ARE HELD.
* \param x - the new x-coord of the mouse cursor.
* \param y - the new y-coord of the mouse cursor.
*/
void MousePassiveMotion(int x, int y)
{
/*
* Calculate how much the mouse actually moved
*/
int dx = x - TheMouse.x;
int dy = y - TheMouse.y;
/*
* update the mouse position
*/
TheMouse.x = x;
TheMouse.y = y;
/*
* Check MyButton to see if we should highlight it cos the mouse is over it
*/
ButtonPassive(&MyButton1,x,y);
ButtonPassive(&MyButton2,x,y);
}
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////RENDER DISPLAY//////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
void Draw()
{
/*
* Enable lighting and the z-buffer
*/
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
/*
* Set perspective viewing transformation
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(23,(winh==0)?(1):((float)winw/winh),1,200);
glTranslatef(-0.30,0,0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/*
* Draw the 3D elements in the scene
*/
Draw3D();
/*
* Disable depth test and lighting for 2D elements
*/
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
//the following are where comments are where there are a few bugs
//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
glOrtho(0,winw,winh,0,-1,1);
/*
* Draw the 2D overlay
*/
Draw2D();
}
void draw3DScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
Draw();
glPopMatrix();
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,(winh==0)?(1):((float)winw/winh),1,200);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);
glScalef(2.0f, 2.0f, 2.0f);
_particleEngine->draw();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv) {
srand((unsigned int)time(0));
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1024, 786);
glutInitWindowPosition(100,100);
glutCreateWindow("Main Menu");
initRendering();
glutDisplayFunc(draw3DScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutMouseFunc(MouseButton);
glutMotionFunc(MouseMotion);
glutPassiveMotionFunc(MousePassiveMotion);
_particleEngine = new ParticleEngine(_textureId);
glutTimerFunc(TIMER_MS, update, 0);
Init();
glutMainLoop();
return 0;
}