2
* This code was created by Jeff Molofee '99
3
* (ported to Linux/SDL by Ti Leggett '01)
5
* Visit Jeff at http://nehe.gamedev.net/
7
* or for port-specific comments, questions, bugreports etc.
8
* email to leggett@eecs.tulane.edu
10
* modifications by Joe Flint for use by teara
11
* further mods by Joe Flint for use by tatac
34
/* screen width, height, and bit depth */
35
#define SCREEN_WIDTH 640
36
#define SCREEN_HEIGHT 480
39
#define WINDOW_TITLE "objview $Revision: 1.3 $"
41
/* This is our SDL surface */
45
double xrot=0, yrot=0, zrot=0;
48
int width=SCREEN_WIDTH, height=SCREEN_HEIGHT;
51
bool keys[SDLK_LAST] = {false};
52
//Currently SDL only has 5 mouse constants, numbered 1 to 5
53
bool mouseb[6] = {false};
54
bool mouseout = false;
56
int mousedeltax = 0, mousedeltay = 0;
58
//Float to store fps count
65
/* function to release/destroy our resources and restoring the old desktop */
66
void quit( int returnCode )
68
/* clean up the window */
71
/* and exit appropriately */
76
// function to reset our viewport after a window resize
77
int resizeWindow( int width, int height )
79
cout << "Resizing window" << endl;
80
// Height / width ration
83
// Protect against a divide by zero
87
ratio = ( GLfloat )width / ( GLfloat )height;
89
// Setup our viewport.
90
glViewport( 0, 0, ( GLsizei )width, ( GLsizei )height );
92
// change to the projection matrix and set our viewing volume.
93
glMatrixMode( GL_PROJECTION );
96
// Set our perspective
97
gluPerspective( 45.0f, ratio, 20.0f, 100.0f );
99
// Make sure we're chaning the model view and not the projection
100
glMatrixMode( GL_MODELVIEW );
110
// general OpenGL initialization function
113
cout << "Initializing OpenGL" << endl;
114
// Enable smooth shading
115
glShadeModel( GL_SMOOTH );
117
// Set the background black
118
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
120
// Depth buffer setup
121
glClearDepth( 1.0f );
123
// Enables Depth Testing
124
glEnable( GL_DEPTH_TEST );
126
// The Type Of Depth Test To Do
127
glDepthFunc( GL_LEQUAL );
129
// Really Nice Perspective Calculations
130
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
132
//Make sure that culling is disabled
133
glDisable(GL_CULL_FACE);
135
//Material settings and light positions stolen from teara
136
//these will probably need to be changed at some point
137
//may not even belong in this portion of the code
138
GLfloat light_position[] = { 3.0, 3.0, 5.0, 0.0 };
139
GLfloat light_ambient[]= { 0.1f, 0.1f, 0.1f, 1.0f };
140
GLfloat light_diffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
141
GLfloat light_specular[]= {1.0f, 1.0f, 1.0f, 1.0f };
142
GLfloat specularmat[]= {1.0f, 1.0f, 1.0f, 1.0f };
143
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
144
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
145
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
146
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
147
glEnable( GL_LIGHTING );
148
glEnable( GL_LIGHT0 );
149
glEnable( GL_NORMALIZE );
150
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
151
glEnable( GL_COLOR_MATERIAL );
152
glMaterialfv(GL_FRONT, GL_SPECULAR, specularmat);
153
glMaterialf(GL_FRONT, GL_SHININESS, 128);
160
// Here goes our drawing code
161
int drawGLScene( GLvoid )
163
// These are to calculate our fps
165
static GLint Frames = 0;
166
static int startTime = SDL_GetTicks();
167
static int start = SDL_GetTicks();
169
// Make sure depth buffer writing before trying to clear it
170
glDepthMask(GL_TRUE);
171
// Clear The Screen And The Depth Buffer
172
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
174
//This resets the matrix and applies global rotation
175
//again, this may belong elsewhere
177
glTranslatef( 0.0f, 0.0f, zoom );
178
glRotatef( xrot, 1.0f, 0.0f, 0.0f);
179
glRotatef( yrot, 0.0f, 1.0f, 0.0f );
180
glRotatef( zrot, 0.0f, 0.0f, 1.0f );
182
static double matrix[] = { 1, 0, 0, 0,
188
static bool leftdown = false;
190
static double quantw;
191
if( mouseb[SDL_BUTTON_LEFT] ) {
194
i.x = ( mousex * 1.0 / (( width - 1) * 0.5 ) ) - 1;
195
i.y = 1 - ( mousey * 1.0 / (( height - 1) * 0.5 ) );
197
if( i.magnitude() > 1 )
200
i.z = sqrt(1 - (i.x*i.x + i.y*i.y) );
205
e.x = ( mousex * 1.0 / (( width - 1) * 0.5 ) ) - 1;
206
e.y = 1 - ( mousey * 1.0 / (( height - 1) * 0.5 ) );
208
if( e.magnitude() > 1 )
211
e.z = sqrt(1 - (e.x*e.x + e.y*e.y));
215
if( perp.magnitude() > 1e-5 ) {
219
quant.x = quant.y = quant.z = 0;
225
glRotated( 57.2958*2 * acos( quantw ), quant.x, quant.y, quant.z );
226
// yrot += mousedeltax / 2.0;
227
// zrot += mousedeltay / 2.0;
235
glRotated( 57.2958*2 * acos( quantw ), quant.x, quant.y, quant.z );
236
glMultMatrixd( matrix );
237
glGetDoublev( GL_MODELVIEW_MATRIX, matrix );
244
glMultMatrixd( matrix );
245
static double scale = 10;
246
glScaled( scale, scale, scale );
247
static double r = 0.8, g = 0.8, b = 0.8;
248
glColor3d( r, g, b );
249
if( r < 1 && keys[SDLK_r] )
251
if( r > 0 && keys[SDLK_e] )
254
if( g < 1 && keys[SDLK_g] )
256
if( g > 0 && keys[SDLK_f] )
259
if( b < 1 && keys[SDLK_b] )
261
if( b > 0 && keys[SDLK_v] )
265
/*glTranslatef( -3.5*0.6, 0, -1.5*0.6 );
266
for( int j = 0; j < 4; j++ ) {
267
for( int i = 0; i < 8; i++ ) {
268
glCallList( objdisplist );
269
glTranslatef( 0.6, 0, 0 );
271
glTranslatef( -8*0.6, 0, 0.6 );
273
glColor3d( 0.2, 0.2, 0.2 );
278
if( mouseb[SDL_BUTTON_RIGHT] ) {
279
scale += mousedeltay / 2.0;
287
if( keys[SDLK_DOWN] )
291
// Draw it to the screen
292
SDL_GL_SwapBuffers( );
294
// Gather our frames per second
297
GLint t = SDL_GetTicks();
298
if (t - T0 >= 5000) {
299
GLfloat seconds = (t - T0) / 1000.0;
300
fps = Frames / seconds;
301
cout << Frames << " frames in " << seconds << " seconds = " << fps << " FPS" << endl;
309
int main( int argc, char **argv )
312
vector<string> args(argc);
313
for(x=0; x < argc; x++)
317
cout << "Please give an obj file to load" << endl;
321
if( !obj.load( args[1].c_str() ) ) {
322
cout << "Failed to load " << args[1] << endl;
328
// Flags to pass to SDL_SetVideoMode
330
// main loop variable
332
// used to collect events
334
// this holds some info about our display
335
const SDL_VideoInfo *videoInfo;
336
// whether or not the window is active
340
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
342
cerr << "Video initialization failed:" << SDL_GetError() << endl;
347
// Fetch the video info
348
videoInfo = SDL_GetVideoInfo( );
352
cerr << "Video query failed: " << SDL_GetError() << endl;
356
// the flags to pass to SDL_SetVideoMode
357
videoFlags = SDL_OPENGL; // Enable OpenGL in SDL
358
videoFlags |= SDL_GL_DOUBLEBUFFER; // Enable double buffering
359
videoFlags |= SDL_HWPALETTE; // Store the palette in hardware
360
videoFlags |= SDL_RESIZABLE; // Enable window resizing
362
// This checks to see if surfaces can be stored in memory
363
if ( videoInfo->hw_available )
364
videoFlags |= SDL_HWSURFACE;
366
videoFlags |= SDL_SWSURFACE;
368
// This checks if hardware blits can be done
369
if ( videoInfo->blit_hw )
370
videoFlags |= SDL_HWACCEL;
372
// Sets up OpenGL double buffering
373
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
376
SDL_WM_SetCaption(WINDOW_TITLE,NULL);
377
surface = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,
380
// Verify there is a surface
383
cerr << "Video mode set failed: " << SDL_GetError( ) << endl;
390
// resize the initial window
391
resizeWindow( SCREEN_WIDTH, SCREEN_HEIGHT );
393
SDL_GetMouseState(&mousex, &mousey);
400
// handle the events in the queue
402
while ( SDL_PollEvent( &event ) )
406
case SDL_MOUSEBUTTONDOWN:
407
mousex = event.button.x;
408
mousey = event.button.y;
409
mouseb[event.button.button] = true;
411
case SDL_MOUSEBUTTONUP:
412
mousex = event.button.x;
413
mousey = event.button.y;
414
mouseb[event.button.button] = false;
416
case SDL_MOUSEMOTION:
417
//printf("Mouse moved to (%d,%d)\n", event.motion.x, event.motion.y);
418
mousex = event.motion.x;
419
if( event.motion.state ) {
420
mousedeltax += event.motion.xrel;
421
mousedeltay += event.motion.yrel;
423
mousey = event.motion.y;
425
case SDL_ACTIVEEVENT:
426
// Something's happend with our focus
427
// If we lost focus or we are iconified,
428
// we shouldn't draw the screen
429
if ( event.active.gain == 0 && event.active.state!=SDL_APPMOUSEFOCUS) {
430
cout << "inact" <<endl;
435
if ( event.active.gain == 0 &&
436
event.active.state ==
437
SDL_APPMOUSEFOCUS ) {
439
cout << "mouseout" << endl;
440
} else if ( event.active.gain == 1 &&
441
event.active.state ==
442
SDL_APPMOUSEFOCUS ) {
444
cout << "mousein" << endl;
447
case SDL_VIDEORESIZE:
448
// handle resize event
449
surface = SDL_SetVideoMode(
455
cerr << "Could not get a surface after resize: " << SDL_GetError( ) << endl;
458
resizeWindow( event.resize.w,
460
width = event.resize.w;
461
height = event.resize.h;
464
keys[ event.key.keysym.sym ] = true;
465
// handle key presses
466
//handleKeyPress( &event.key.keysym );
469
keys[ event.key.keysym.sym ] = false;
472
// handle quit requests
479
if(keys[SDLK_ESCAPE] || keys[SDLK_q])
482
//This only works with X
483
//In order to get it to work with Win32 the surface must be recreated
484
SDL_WM_ToggleFullScreen( surface );
491
/* clean ourselves up and exit */
494
/* Should never get here */