~ubuntu-branches/ubuntu/karmic/brutalchess/karmic

« back to all changes in this revision

Viewing changes to objview.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Gürkan Sengün
  • Date: 2006-04-07 10:41:25 UTC
  • Revision ID: james.westby@ubuntu.com-20060407104125-18mnxbl1yzju7e84
Tags: upstream-0.0.20060314cvs
Import upstream version 0.0.20060314cvs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This code was created by Jeff Molofee '99 
 
3
 * (ported to Linux/SDL by Ti Leggett '01)
 
4
 *
 
5
 * Visit Jeff at http://nehe.gamedev.net/
 
6
 * 
 
7
 * or for port-specific comments, questions, bugreports etc. 
 
8
 * email to leggett@eecs.tulane.edu
 
9
 *
 
10
 * modifications by Joe Flint for use by teara
 
11
 * further mods by Joe Flint for use by tatac
 
12
 */
 
13
 
 
14
#ifdef HAVE_CONFIG_H
 
15
#include "config.h"
 
16
#endif
 
17
#ifdef WIN32
 
18
#include <windows.h>
 
19
#endif
 
20
#include <iostream>
 
21
#include <stdio.h>
 
22
#include <stdlib.h>
 
23
#include <stdarg.h>
 
24
#include <vector>
 
25
#include <string>
 
26
#include <math.h>
 
27
 
 
28
#include "SDL.h"
 
29
#include "glhead.h"
 
30
#include "objfile.h"
 
31
 
 
32
using namespace std;
 
33
 
 
34
/* screen width, height, and bit depth */
 
35
#define SCREEN_WIDTH  640
 
36
#define SCREEN_HEIGHT 480
 
37
#define SCREEN_BPP     16
 
38
 
 
39
#define WINDOW_TITLE "objview $Revision: 1.3 $"
 
40
 
 
41
/* This is our SDL surface */
 
42
SDL_Surface *surface;
 
43
 
 
44
 
 
45
double xrot=0, yrot=0, zrot=0;
 
46
double scale = 20;
 
47
double zoom = -60;
 
48
int width=SCREEN_WIDTH, height=SCREEN_HEIGHT;
 
49
 
 
50
//Key array
 
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;
 
55
int mousex, mousey;
 
56
int mousedeltax = 0, mousedeltay = 0;
 
57
 
 
58
//Float to store fps count
 
59
GLfloat fps = 0;
 
60
 
 
61
ObjFile obj;
 
62
 
 
63
void Quit(int);
 
64
 
 
65
/* function to release/destroy our resources and restoring the old desktop */
 
66
void quit( int returnCode )
 
67
{
 
68
        /* clean up the window */
 
69
        SDL_Quit( );
 
70
 
 
71
        /* and exit appropriately */
 
72
        exit( returnCode );
 
73
}
 
74
 
 
75
int initGL( GLvoid );
 
76
// function to reset our viewport after a window resize
 
77
int resizeWindow( int width, int height )
 
78
{
 
79
        cout << "Resizing window" << endl;
 
80
        // Height / width ration
 
81
        GLfloat ratio;
 
82
 
 
83
        // Protect against a divide by zero
 
84
        if ( height == 0 )
 
85
                height = 1;
 
86
 
 
87
        ratio = ( GLfloat )width / ( GLfloat )height;
 
88
 
 
89
        // Setup our viewport.
 
90
        glViewport( 0, 0, ( GLsizei )width, ( GLsizei )height );
 
91
 
 
92
        // change to the projection matrix and set our viewing volume.
 
93
        glMatrixMode( GL_PROJECTION );
 
94
        glLoadIdentity( );
 
95
 
 
96
        // Set our perspective
 
97
        gluPerspective( 45.0f, ratio, 20.0f, 100.0f );
 
98
 
 
99
        // Make sure we're chaning the model view and not the projection
 
100
        glMatrixMode( GL_MODELVIEW );
 
101
 
 
102
        // Reset The View
 
103
        glLoadIdentity( );
 
104
 
 
105
        initGL();
 
106
        return( true );
 
107
}
 
108
 
 
109
 
 
110
// general OpenGL initialization function
 
111
int initGL( GLvoid )
 
112
{
 
113
        cout << "Initializing OpenGL" << endl;
 
114
        // Enable smooth shading
 
115
        glShadeModel( GL_SMOOTH );
 
116
 
 
117
        // Set the background black
 
118
        glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
 
119
 
 
120
        // Depth buffer setup
 
121
        glClearDepth( 1.0f );
 
122
 
 
123
        // Enables Depth Testing
 
124
        glEnable( GL_DEPTH_TEST );
 
125
 
 
126
        // The Type Of Depth Test To Do
 
127
        glDepthFunc( GL_LEQUAL );
 
128
 
 
129
        // Really Nice Perspective Calculations
 
130
        glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
 
131
 
 
132
        //Make sure that culling is disabled
 
133
        glDisable(GL_CULL_FACE);
 
134
 
 
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);
 
154
 
 
155
        obj.build();
 
156
        
 
157
        return( true );
 
158
}
 
159
 
 
160
// Here goes our drawing code
 
161
int drawGLScene( GLvoid )
 
162
{
 
163
        // These are to calculate our fps
 
164
        static GLint T0     = 0;
 
165
        static GLint Frames = 0;
 
166
        static int startTime = SDL_GetTicks();
 
167
        static int start = SDL_GetTicks();
 
168
 
 
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 );
 
173
 
 
174
        //This resets the matrix and applies global rotation
 
175
        //again, this may belong elsewhere
 
176
        glLoadIdentity( );
 
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 );
 
181
 
 
182
        static double matrix[] = {      1, 0, 0, 0, 
 
183
                                                                0, 1, 0, 0,
 
184
                                                                0, 0, 1, 0,
 
185
                                                                0, 0, 0, 1 };
 
186
 
 
187
 
 
188
        static bool leftdown = false;
 
189
        static Vector quant;
 
190
        static double quantw;
 
191
        if( mouseb[SDL_BUTTON_LEFT] ) {
 
192
                static Vector i;
 
193
                if( !leftdown ) {
 
194
                        i.x = ( mousex * 1.0 / (( width - 1) * 0.5 ) ) - 1;
 
195
                        i.y = 1 - ( mousey * 1.0 / (( height - 1) * 0.5 ) );
 
196
                        i.z = 0;
 
197
                        if( i.magnitude() > 1 )
 
198
                                i.normalize();
 
199
                        else
 
200
                                i.z = sqrt(1 - (i.x*i.x + i.y*i.y) );
 
201
                        leftdown = true;
 
202
 
 
203
                }
 
204
                Vector e;
 
205
                e.x = ( mousex * 1.0 / (( width - 1) * 0.5 ) ) - 1;
 
206
                e.y = 1 - ( mousey * 1.0 / (( height - 1) * 0.5 ) );
 
207
                e.z = 0;
 
208
                if( e.magnitude() > 1 )
 
209
                        e.normalize();
 
210
                else
 
211
                        e.z = sqrt(1 - (e.x*e.x + e.y*e.y));
 
212
 
 
213
                Vector perp;
 
214
                perp = i.cross( e );
 
215
                if( perp.magnitude() > 1e-5 ) {
 
216
                        quant = perp;
 
217
                        quantw = i.dot( e );
 
218
                } else {
 
219
                        quant.x = quant.y = quant.z = 0;
 
220
                        quantw = 0;
 
221
                }
 
222
 
 
223
                quant.normalize();
 
224
                if( quantw != 0 )
 
225
                        glRotated( 57.2958*2 * acos( quantw ), quant.x, quant.y, quant.z );
 
226
//              yrot += mousedeltax / 2.0;
 
227
//              zrot += mousedeltay / 2.0;
 
228
                mousedeltax = 0;
 
229
                mousedeltay = 0;
 
230
        } else {
 
231
                if( leftdown ) {
 
232
                        glPushMatrix();
 
233
                        glLoadIdentity();
 
234
                        if( quantw != 0 )
 
235
                                glRotated( 57.2958*2 * acos( quantw ), quant.x, quant.y, quant.z );
 
236
                        glMultMatrixd( matrix );
 
237
                        glGetDoublev( GL_MODELVIEW_MATRIX, matrix );
 
238
                        glPopMatrix();
 
239
 
 
240
                }
 
241
                leftdown = false;
 
242
        }
 
243
 
 
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] )
 
250
                r += 0.001;
 
251
        if( r > 0 && keys[SDLK_e] )
 
252
                r -= 0.001;
 
253
        
 
254
        if( g < 1 && keys[SDLK_g] )
 
255
                g += 0.001;
 
256
        if( g > 0 && keys[SDLK_f] )
 
257
                g -= 0.001;
 
258
        
 
259
        if( b < 1 && keys[SDLK_b] )
 
260
                b += 0.001;
 
261
        if( b > 0 && keys[SDLK_v] )
 
262
                b -= 0.001;
 
263
 
 
264
        obj.draw();
 
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 );
 
270
                }
 
271
                glTranslatef( -8*0.6, 0, 0.6 );
 
272
                if( j == 1 )
 
273
                        glColor3d( 0.2, 0.2, 0.2 );
 
274
        }*/
 
275
        //obj.draw();
 
276
 
 
277
 
 
278
        if( mouseb[SDL_BUTTON_RIGHT] ) {
 
279
                scale += mousedeltay / 2.0;
 
280
                mousedeltay = 0;
 
281
                mousedeltax = 0;
 
282
        }
 
283
 
 
284
        if( keys[SDLK_UP] )
 
285
                zoom += 0.05;
 
286
        
 
287
        if( keys[SDLK_DOWN] )
 
288
                zoom -= 0.05;
 
289
 
 
290
 
 
291
        // Draw it to the screen
 
292
        SDL_GL_SwapBuffers( );
 
293
 
 
294
        // Gather our frames per second
 
295
        Frames++;
 
296
        {
 
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;
 
302
                    T0 = t;
 
303
                    Frames = 0;
 
304
                }
 
305
        }
 
306
        return( true );
 
307
}
 
308
 
 
309
int main( int argc, char **argv )
 
310
{
 
311
        int x;
 
312
        vector<string> args(argc);
 
313
        for(x=0; x < argc; x++) 
 
314
                args[x]=argv[x];
 
315
 
 
316
        if( argc < 2 ) {
 
317
                cout << "Please give an obj file to load" << endl;
 
318
                return 1;
 
319
        }
 
320
 
 
321
        if( !obj.load( args[1].c_str() ) ) {
 
322
                cout << "Failed to load " << args[1] << endl;
 
323
                return 2;
 
324
        }
 
325
 
 
326
        obj.findNorms();
 
327
        
 
328
        // Flags to pass to SDL_SetVideoMode
 
329
        int videoFlags;
 
330
        // main loop variable
 
331
        int done = false;
 
332
        // used to collect events
 
333
        SDL_Event event;
 
334
        // this holds some info about our display
 
335
        const SDL_VideoInfo *videoInfo;
 
336
        // whether or not the window is active
 
337
        int isActive = true;
 
338
        
 
339
        // initialize SDL
 
340
        if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
 
341
        {
 
342
                cerr << "Video initialization failed:" << SDL_GetError() << endl;
 
343
                quit( 1 );
 
344
        }
 
345
 
 
346
 
 
347
        // Fetch the video info
 
348
        videoInfo = SDL_GetVideoInfo( );
 
349
 
 
350
        if ( !videoInfo )
 
351
        {
 
352
                cerr << "Video query failed: " << SDL_GetError() << endl;
 
353
                quit( 1 );
 
354
        }
 
355
 
 
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
 
361
 
 
362
        // This checks to see if surfaces can be stored in memory
 
363
        if ( videoInfo->hw_available )
 
364
                videoFlags |= SDL_HWSURFACE;
 
365
        else
 
366
                videoFlags |= SDL_SWSURFACE;
 
367
 
 
368
        // This checks if hardware blits can be done
 
369
        if ( videoInfo->blit_hw )
 
370
                videoFlags |= SDL_HWACCEL;
 
371
 
 
372
        // Sets up OpenGL double buffering
 
373
        SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
 
374
 
 
375
        // get a SDL surface
 
376
        SDL_WM_SetCaption(WINDOW_TITLE,NULL);
 
377
        surface = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,
 
378
                        videoFlags );
 
379
 
 
380
        // Verify there is a surface
 
381
        if ( !surface )
 
382
        {
 
383
                cerr << "Video mode set failed: " << SDL_GetError( ) << endl;
 
384
                quit( 1 );
 
385
        }
 
386
 
 
387
        // initialize OpenGL
 
388
        initGL( );
 
389
 
 
390
        // resize the initial window
 
391
        resizeWindow( SCREEN_WIDTH, SCREEN_HEIGHT );
 
392
 
 
393
        SDL_GetMouseState(&mousex, &mousey);
 
394
        // wait for events
 
395
        int frameCount = 0;
 
396
 
 
397
        while ( !done )
 
398
        {
 
399
        
 
400
                // handle the events in the queue
 
401
 
 
402
                while ( SDL_PollEvent( &event ) )
 
403
                {
 
404
                        switch( event.type )
 
405
                        {
 
406
                                case SDL_MOUSEBUTTONDOWN:
 
407
                                        mousex = event.button.x;
 
408
                                        mousey = event.button.y;        
 
409
                                        mouseb[event.button.button] = true;
 
410
                                        break;
 
411
                                case SDL_MOUSEBUTTONUP:
 
412
                                        mousex = event.button.x;
 
413
                                        mousey = event.button.y;
 
414
                                        mouseb[event.button.button] = false;
 
415
                                        break;
 
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;
 
422
                                        }
 
423
                                        mousey = event.motion.y;
 
424
                                        break;
 
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;
 
431
                                                isActive = false;
 
432
                                        } else
 
433
                                                isActive = true;
 
434
 
 
435
                                        if ( event.active.gain == 0 && 
 
436
                                                event.active.state ==
 
437
                                                SDL_APPMOUSEFOCUS ) {
 
438
                                                mouseout = true;
 
439
                                                cout << "mouseout" << endl;
 
440
                                        } else if ( event.active.gain == 1 && 
 
441
                                                event.active.state ==
 
442
                                                SDL_APPMOUSEFOCUS ) {
 
443
                                                mouseout = false;
 
444
                                                cout << "mousein" << endl;
 
445
                                        }
 
446
                                        break;                      
 
447
                                case SDL_VIDEORESIZE:
 
448
                                        // handle resize event
 
449
                                        surface = SDL_SetVideoMode( 
 
450
                                                        event.resize.w,
 
451
                                                        event.resize.h,
 
452
                                                        16, videoFlags );
 
453
                                        if ( !surface )
 
454
                                        {
 
455
                                                cerr << "Could not get a surface after resize: " << SDL_GetError( ) << endl;
 
456
                                                quit( 1 );
 
457
                                        }
 
458
                                        resizeWindow( event.resize.w, 
 
459
                                                        event.resize.h );
 
460
                                        width = event.resize.w;
 
461
                                        height = event.resize.h;
 
462
                                        break;
 
463
                                case SDL_KEYDOWN:
 
464
                                        keys[ event.key.keysym.sym ] = true;
 
465
                                        // handle key presses
 
466
                                        //handleKeyPress( &event.key.keysym );
 
467
                                        break;
 
468
                                case SDL_KEYUP:
 
469
                                        keys[ event.key.keysym.sym ] = false;
 
470
                                        break;
 
471
                                case SDL_QUIT:
 
472
                                        // handle quit requests
 
473
                                        done = true;
 
474
                                        break;
 
475
                                default:
 
476
                                    break;
 
477
                        }
 
478
                }
 
479
                if(keys[SDLK_ESCAPE] || keys[SDLK_q])
 
480
                        quit( 0 );
 
481
                if(keys[SDLK_F1]) {
 
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 );
 
485
                        keys[SDLK_F1]=false;
 
486
                }
 
487
                if ( isActive )
 
488
                        drawGLScene( );
 
489
        }
 
490
 
 
491
        /* clean ourselves up and exit */
 
492
        quit( 0 );
 
493
 
 
494
        /* Should never get here */
 
495
        return( 0 );
 
496
}