-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMAIN.CPP
More file actions
369 lines (306 loc) · 8.54 KB
/
MAIN.CPP
File metadata and controls
369 lines (306 loc) · 8.54 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
#include <iostream>
/* NOT SURE IF WE NEED THIS ... BUT IT COULD NOT FIND MUTLTI TEXTURE SHIT WITHOUT IT */
#define GL_GLEXT_LEGACY 1
#include <fstream>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
#include <cstdlib>
#include "SDL.h"
//#include "Object.h"
#include "Vector.h"
#include "Point.h"
#include "Ray.h"
#include "Plane.h"
#include "AxisAlignedBoundingBox.h"
#include "Axis.h"
#include "Triangle.h"
#include "Sphere.h"
using namespace Affine;
/* This is our SDL surface */
SDL_Surface *surface;
Vector test;
Point center;
Vector up;
Point eye;
//RadiositySolver *rad;
//Octree::Node *oct;
//RayTracer tracer;
bool rotate = false;
GLuint texture[1]; /* Storage For One Texture ( NEW ) */
bool marglar = false;
/* function to release/destroy our resources and restoring the old desktop */
void Quit( int returnCode ){
SDL_Quit( );
exit( returnCode );
}
/* function to reset our viewport after a window resize */
int resizeWindow( int width, int height ){
float ratio;
if ( height == 0 )height = 1;
ratio = ( GLfloat )width / ( GLfloat )height;
glViewport( 0, 0, ( int )width, ( int )height );
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
gluPerspective( 90.0f, ratio, 0.1f, 100.0f );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
return( 1 );
}
// function to handle mouse motion events
//
// Uses the relative coords.
// Top left of screen is 0,0
// bottom right is width,height
void handleMouseMotion( SDL_MouseMotionEvent *event ) {
//Ignore huge jumps.
if( event->xrel > 50 ) return;
if( event->yrel > 50 ) return;
//Deal with the rest
}
/* function to handle key press events */
void handleKeyPress( SDL_keysym *keysym )
{
switch ( keysym->sym )
{
case SDLK_ESCAPE:
Quit( 0 );
break;
case SDLK_F1:
// camera.lookAt( eye, center, up );
break;
case SDLK_F2:
break;
case SDLK_F3:
// camera.lookRight();
break;
case SDLK_F4:
// camera.lookUp();
break;
case SDLK_F5:
break;
case SDLK_F6:
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDisable( GL_TEXTURE_2D );
break;
case SDLK_F7:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable( GL_TEXTURE_2D );
break;
case SDLK_F8:
break;
case SDLK_F10:
break;
case SDLK_F11:
break;
case SDLK_F12:
break;
case SDLK_UP:
// camera.calculateFirstPersonMatrix(0, 0, 2);
break;
case SDLK_DOWN:
// camera.calculateFirstPersonMatrix(0, 0, 1);
break;
case SDLK_LEFT:
// camera.calculateFirstPersonMatrix(0, 0, 4);
break;
case SDLK_RIGHT:
// camera.calculateFirstPersonMatrix(0, 0, 3);
break;
default:
break;
}
return;
}
unsigned int txt;
/* general OpenGL initialization function */
int initGL( GLvoid )
{
glShadeModel( GL_SMOOTH );
glClearColor( 0.0f, 0.0f, 0.0f, 0.5f );
glEnable( GL_DEPTH_TEST );
glEnable( GL_LIGHTING );
glDepthFunc( GL_LEQUAL );
glCullFace(GL_BACK);
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
GLfloat light0_position[] = { 100.0, 100.0, 100.0, 0.0 };
GLfloat light1_position[] = { -100.0, 100.0, 100.0, 0.0 };
GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };
//glEnable(GL_TEXTURE_2D); // enable texturing
glActiveTextureARB(GL_TEXTURE0_ARB); // texture bank
glEnable(GL_TEXTURE_2D); // enable texturing
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT );
glTexEnvf( GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_REPLACE );
glActiveTextureARB(GL_TEXTURE1_ARB); // lightmap bank
glEnable(GL_TEXTURE_2D); // enable texturing
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT );
glTexEnvf( GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_BLEND );
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
glLightfv(GL_LIGHT1, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT1, GL_SPECULAR, white_light);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
center.set(0.0f, 0.0f, 0.0f);
eye.set(0.0f, 0.0f, 1.0f);
up.set( 0.0, 1.0, 0.0 );
glPointSize( 5.0f );
return( 1 );
}
/* Here goes our drawing code */
int drawGLScene( GLvoid )
{
/* These are to calculate our fps */
static int T0 = 0;
static int Frames = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// glLoadMatrixf( camera.getModelViewMatrix() );
//glColor3f(1.0f, 1.0f, 1.0f);
//g->render();
/*
glBegin(GL_TRIANGLES);
glVertex3f( tri[0][0], tri[0][1], tri[0][2] );
glVertex3f( tri[1][0], tri[1][1], tri[1][2] );
glVertex3f( tri[2][0], tri[2][1], tri[2][2] );
glEnd();
Point *ref = sloot[10];
Ray r( *ref, tri.myNormal );
Point end = r.getPointFromScaler( 5.0f );
glBegin( GL_LINES );
glVertex3f( (*ref)[0], (*ref)[1], (*ref)[2] );
glVertex3f( end[0], end[1], end[2] );
glEnd();
*/
/* Draw it to the screen */
glFlush();
SDL_GL_SwapBuffers( );
/* Gather our frames per second */
Frames++;
{
GLint t = SDL_GetTicks();
if (t - T0 >= 5000) {
GLfloat seconds = (t - T0) / 1000.0;
GLfloat fps = Frames / seconds;
printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
T0 = t;
Frames = 0;
}
}
return( 1 );
}
int main( int argc, char **argv )
{
/* Flags to pass to SDL_SetVideoMode */
int videoFlags;
/* main loop variable */
int done = 0;
/* used to collect events */
SDL_Event event;
/* this holds some info about our display */
const SDL_VideoInfo *videoInfo;
/* whether or not the window is active */
int isActive = 1;
if( argc != 2) {
std::cerr << "Please specify model\n";
return 0;
}
/* initialize SDL */
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
fprintf( stderr, "Video initialization failed: %s\n",SDL_GetError( ) );
Quit( 1 );
}
/* Fetch the video info */
videoInfo = SDL_GetVideoInfo( );
if ( !videoInfo )
{
fprintf( stderr, "Video query failed: %s\n", SDL_GetError( ) );
Quit( 1 );
}
/* the flags to pass to SDL_SetVideoMode */
videoFlags = SDL_OPENGL; /* Enable OpenGL in SDL */
videoFlags |= SDL_GL_DOUBLEBUFFER; /* Enable double buffering */
videoFlags |= SDL_HWPALETTE; /* Store the palette in hardware */
videoFlags |= SDL_RESIZABLE; /* Enable window resizing */
/* This checks to see if surfaces can be stored in memory */
if ( videoInfo->hw_available )
videoFlags |= SDL_HWSURFACE;
else
videoFlags |= SDL_SWSURFACE;
/* This checks if hardware blits can be done */
if ( videoInfo->blit_hw )
videoFlags |= SDL_HWACCEL;
/* Sets up OpenGL double buffering */
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
/* get a SDL surface */
surface = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 32, videoFlags );
/* Verify there is a surface */
if ( !surface ){
fprintf( stderr, "Video mode set failed: %s\n", SDL_GetError( ) );
Quit( 1 );
}
/* initialize OpenGL */
initGL( );
/* resize the initial window */
resizeWindow( SCREEN_WIDTH, SCREEN_HEIGHT );
/* wait for events */
while ( !done )
{
/* handle the events in the queue */
while ( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_ACTIVEEVENT:
/* Something's happend with our focus
* If we lost focus or we are iconified, we
* shouldn't draw the screen
*/
if ( event.active.gain == 0 )
isActive = 0;
else
isActive = 1;
break;
case SDL_VIDEORESIZE:
/* handle resize event */
surface = SDL_SetVideoMode( event.resize.w, event.resize.h, 32, videoFlags );
if ( !surface ){
fprintf( stderr, "Could not get a surface after resize: %s\n", SDL_GetError( ) );
Quit( 1 );
}
resizeWindow( event.resize.w, event.resize.h );
break;
case SDL_KEYDOWN:
/* handle key presses */
handleKeyPress( &event.key.keysym );
break;
case SDL_MOUSEMOTION:
handleMouseMotion( &event.motion );
break;
case SDL_MOUSEBUTTONDOWN:
rotate = true;
break;
case SDL_MOUSEBUTTONUP:
rotate = false;
break;
case SDL_QUIT:
/* handle quit requests */
done = 1;
break;
default:
break;
}
}
/* draw the scene */
drawGLScene( );
}
/* clean ourselves up and exit */
Quit( 0 );
/* Should never get here */
return 0;
}