Repository navigation
Expand file tree
/
Copy pathGraphicsDemo2.cpp
More file actions
378 lines (298 loc) · 7.98 KB
/
Copy pathGraphicsDemo2.cpp
File metadata and controls
378 lines (298 loc) · 7.98 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
// GraphicsDemo2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Particles.h"
#define DIST 3.5
double tStart, tFinish, tFTime; // the start time for the timer, finish time, and frame rate
__int64 startclock; // we want a start time
double rate_inv;
bool done = false;
unsigned int testpic;
unsigned int pics[5];
corona::Image *pmyimage;
StaticParticle *ptl1 = NULL;
StaticParticle *ptl2 = NULL;
StaticParticle *ptl3 = NULL;
float lightpos[4] = {0.0, 6.0, 0.0, 0.0};
float lightspos[3] = {0.0,0.0,0.0};
float ambient[4] = {1.0,1.0,1.0,1.0};
bool forward = true;
int count = 0;
ifstream fd;
float colors[3][4] = { 1.0, 0.00, 0.00, 0.65,
0.0, 1.00, 0.00, 0.65,
0.00, 0.00, 1.0, 0.65 };
float fogcolor[4] = { 0.2, 0.2, 0.2, 0.2};
float fogpos[4] = { 0.0, 0.0, 19.0 };
float angle[3] = {0.0, 0.0, 0.0};
BOOL initftime(void) {
__int64 rate;
// we need the accuracy
if(!QueryPerformanceFrequency((LARGE_INTEGER*)&rate)) {
return FALSE; // win errors
}
// usually the rate will be 1193180
if(!rate) {
return FALSE;
}
rate_inv=1.0/(double)rate;
if(!QueryPerformanceCounter((LARGE_INTEGER*)&startclock)) {
return FALSE; // win errors
}
return TRUE; // there is a clock
}
double getFTime(void) {
// by dividing by its rate you get accurate seconds
__int64 endclock;
QueryPerformanceCounter((LARGE_INTEGER*)&endclock);
return (double)(endclock-startclock)*rate_inv;
// note: I recommend that you multiply but the inverse of a constant.
// (speed reasons)
}
bool Initialize()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glEnable(GL_POLYGON_SMOOTH);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_DEPTH_TEST);
glCullFace(GL_BACK);
glEnable(GL_TEXTURE);
glEnable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lightspos);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 115.0);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 75.0);
glEnable(GL_LIGHT0);
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
if(!initftime())
return false;
tStart = getFTime();
pmyimage = corona::OpenImage("data/galaxybackground.png");
// load the test picture
glGenTextures(1, &testpic);
glBindTexture(GL_TEXTURE_2D, testpic);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexEnvf(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pmyimage->getWidth(), pmyimage->getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, pmyimage->getPixels());
ptl1 = new StaticParticle("data/t1.png");
if(!ptl1)
return false;
ptl1->setSize(125);
ptl1->setDepth(20.0);
ptl1->setArea(10);
ptl1->InitializeParticles();
ptl2 = new StaticParticle("data/starfield1-256.png");
if(!ptl2)
return false;
ptl2->setSize(125);
ptl2->setDepth(15.0);
ptl2->setArea(7);
ptl2->InitializeParticles();
ptl3 = new StaticParticle("data/star6-128.png");
if(!ptl3)
return false;
ptl3->setSize(100);
ptl3->setDepth(12.0);
ptl3->setArea(4);
ptl3->InitializeParticles();
// fog settings
glFogi(GL_FOG_MODE, GL_LINEAR);
glFogfv(GL_FOG_COLOR, fogcolor);
glFogf(GL_FOG_DENSITY, 0.12);
glHint(GL_FOG_HINT, GL_NICEST);
glFogf(GL_FOG_START, 19.5);
glFogf(GL_FOG_END, 60.0);
return true;
}
void ReshapeWindow(int w, int h)
{
if(h==0)
h = 1;
float ratio = 1.0* w / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45.0, ratio, 1.0, 1050.0);
}
// This scene is used for the final credits for all the people involved
// One side of the door will have the 3d model of the names and the other side
// will have the shadow of the name
void DoorwayScene(float tFTime)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(-lightpos[0]+(2*(0.30*tFTime)), lightpos[1]+4.0, -9.0, 0.0, 2.0, 0.0, 0.0, 1.0, 0.0);
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, testpic);
glPushMatrix();
glTranslatef(0.0, 0.0, 0.0);
glBegin(GL_QUADS);
glColor4f(1.0,1.0,1.0,0.65);
glTexCoord2f(0.0, 1.0); glVertex3f(0.001, 0.0, -2.5);
glTexCoord2f(1.0, 1.0); glVertex3f(0.001, 0.0, 2.5);
glTexCoord2f(1.0, 0.0); glVertex3f(0.001, 4.0, 2.5);
glTexCoord2f(0.0, 0.0); glVertex3f(0.001, 4.0, -2.5);
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glPushMatrix();
glTranslatef(0.0, 0.0, 0.0);
glBegin(GL_QUADS);
if((lightpos[0] >= -0.001) && (lightpos[0] <= 0.001))
{
count++;
if(count > 2)
count = 0;
}
glColor4fv(colors[count]);
glVertex3f(0.0,0.0,-2.5);
glVertex3f(0.0,0.0, 2.5);
glVertex3f(-2.40*(lightpos[0]), 0.0, 2.5);
glVertex3f(-2.40*(lightpos[0]), 0.0, -2.5);
glEnd();
glPopMatrix();
glDisable(GL_BLEND);
glPushMatrix();
glTranslatef(lightpos[0], lightpos[1], lightpos[2]);
glColor3f(1.0, 1.0, 1.0);
glutWireSphere(0.05, 10, 10);
glLineWidth(0.2);
glBegin(GL_LINES);
glColor3f(1.0,1.0,1.0);
glVertex3f(0.0,0.0,0.0);
glVertex3f(-lightpos[0], lightpos[1], lightpos[2]);
glEnd();
glPopMatrix();
if(forward)
{
lightpos[0] = lightpos[0]+(0.30*tFTime);
lightpos[1] += sin(lightpos[0]/180.0);
if(lightpos[0] > DIST)
{
forward = false;
}
}
else
{
lightpos[0] = lightpos[0]-(0.30*tFTime);
lightpos[1] -= sin(lightpos[0]/180.0);
if(lightpos[0] < -DIST)
{
forward = true;
}
}
}
void DrawStarField(float t)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, -10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glDisable(GL_LIGHTING);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0,0.001);
glEnable(GL_TEXTURE_2D);
glEnable(GL_FOG);
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPushMatrix();
glRotatef(-angle[1], 1.0, 1.0, 0.0);
ptl1->Draw(t);
glPopMatrix();
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.1);
glPushMatrix();
glRotatef(-angle[0], 1.0, 1.0, 0.0);
ptl2->Draw(t);
glPopMatrix();
glPushMatrix();
glRotatef(-angle[0], 1.0, 1.0, 0.0);
ptl3->Draw(t);
glPopMatrix();
glDisable(GL_ALPHA_TEST);
glDisable(GL_POLYGON_OFFSET_FILL);
glDisable(GL_FOG);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
angle[0] += 0.4 * tFTime;
angle[1] += 0.3 * tFTime;
angle[1] += 0.2 * tFTime;
if(angle[0] > 360.0)
angle[0] = 1.0;
if(angle[1] > 360.0)
angle[1] = 1.0;
if(angle[3] > 360.0)
angle[3] = 1.0;
}
// simple draw routine
void Draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(done)
{
exit(1); // shutdown code should go here
}
DrawStarField(tFTime);
//DoorwayScene(tFTime);
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
done = true;
break;
default:
break;
}
}
void specialKeys(int key, int x, int y)
{
switch(key)
{
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowPosition(0,0);
glutInitWindowSize(800, 600);
glutCreateWindow("Funky Demo");
int isFull;
if((isFull = MessageBoxA(NULL, "Fullscreen?", "GraphicsDemo2", MB_YESNO)) == 6)
{
glutFullScreen();
}
glutDisplayFunc(Draw);
glutIdleFunc(Draw);
glutReshapeFunc(ReshapeWindow);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialKeys);
if(!Initialize())
{
return -1;
}
do{
tFinish = getFTime();
}while(tFinish == tStart);
tFTime = tFinish-tStart;
tStart = tFinish;
// FSOUND_Update();
glutMainLoop();
return 0;
}