Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/graphics/importer/framework.cc @ 4079

Last change on this file since 4079 was 3910, checked in by bensch, 19 years ago

orxonox/trunk: importer: having color again

File size: 6.4 KB
RevLine 
[3140]1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
[2931]16#include "framework.h"
[3427]17
[3479]18#include "vector.h"
[3427]19
[3657]20#include "primitive_model.h"
21
22
[3398]23int verbose;
[2748]24void DrawGLScene()
25{
[2940]26  currFrame = SDL_GetTicks();
27  dt = currFrame - lastFrame; 
28  if (dt == 0)
29    dist += (zoomTo-dist)/500;
30  else 
31    dist += (zoomTo-dist)/500 *(float)dt;
[2939]32
[2940]33  rotatorP += rotatorV *(float)dt;
[2759]34 
[2952]35
[2748]36  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
37  glLoadIdentity(); // Reset the view
38 
[2794]39  glMatrixMode(GL_PROJECTION);
[2759]40  glLoadIdentity();     
[2933]41  gluPerspective(45.0f,500/375,0.1f,dist * 5.0f);
[2963]42    gluLookAt (0, dist , dist, 0,0,0, up.x,up.y,up.z);
[2794]43
[2963]44  glMatrixMode(GL_MODELVIEW);
45  glPushMatrix();
46  //  glRotatef (180, dir.x, dir.y, dir.z);
47  glMultMatrixf (*matQ);
[3211]48  if (obj)
49    obj->draw();
[2748]50
[2963]51  glPopMatrix();
52
[2748]53  SDL_GL_SwapBuffers(); // Swap the buffers
[2940]54  lastFrame = currFrame;
[2748]55}
56
57
58int main(int argc, char *argv[])
59{
[3910]60  verbose = 3;
[3201]61
[2748]62  Uint8* keys; // This variable will be used in the keyboard routine
63  int done=FALSE; // We aren't done yet, are we?
64
65  // Create a new OpenGL window with the title "Cone3D Basecode" at
66  // 640x480x32, fullscreen and check for errors along the way
[3140]67  if(wHandler.CreateGLWindow("Whandler Basecode", 800, 600, 32, FALSE) == FALSE)
[2748]68  {
69    // If an error is found, display a message, kill the GL and SDL screens (if they were created) and exit
[3206]70    PRINTF(1)("Could not initalize OpenGL :(\n\n");
[2748]71    wHandler.KillGLWindow();
72    return 0;
73  }
[2936]74 
[3206]75  PRINTF(2)("screensize: %i, %i\n", wHandler.screen->w, wHandler.screen->h);
[2846]76  if (argc>=3)
[3396]77    obj = new OBJModel (argv[1], atof(argv[2]));
[2846]78  else if (argc>=2)
[3396]79    obj = new OBJModel(argv[1]);
[2848]80  else 
[3657]81    obj = new PrimitiveModel(SPHERE);
[3398]82
[2952]83  M = Vector(wHandler.screen->w/2, wHandler.screen->h/2, 0); 
84  rotAxis = Vector (0.0,1.0,0.0);
85  rotAngle = 0;
[2963]86
87  matQ[0][0] = matQ[1][1] = matQ[2][2] = matQ[3][3] = 1;
[2952]88  rotQ = Quaternion (rotAngle, rotAxis);
[2963]89  rotQlast = rotQ;
[2952]90  dir = Vector (0.0, 0.0, 1.0);
[2963]91  up = Vector (0.0, 1.0, 0.0);
[2952]92
[2931]93  glEnable(GL_LIGHTING);
94  glEnable(GL_DEPTH_TEST);
95
96  GLfloat whiteLight[] = {1.0, 1.0, 1.0,1.0};
97  GLfloat light0Position[] = {10.0, 10.0, 10.0, 0.0};
98  GLfloat light1Position[] = {-10.0, -7.0, -6.0, 0.0};
[2780]99  GLfloat lmodelAmbient[] = {.1, .1, .1, 1.0};
[2931]100
101  glEnable(GL_LIGHT0);
102  glLightfv(GL_LIGHT0, GL_POSITION, light0Position);
[2780]103  glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight);
104  glLightfv(GL_LIGHT0, GL_SPECULAR, whiteLight);
[2931]105 
106  glEnable(GL_LIGHT1);
107  glLightfv(GL_LIGHT1, GL_POSITION, light1Position);
108  glLightfv(GL_LIGHT1, GL_DIFFUSE, whiteLight);
109  glLightfv(GL_LIGHT1, GL_SPECULAR, whiteLight);
110 
[3070]111
112  glEnable(GL_TEXTURE_2D);
[2931]113  rotatorP = .0;
114  rotatorV = .0;
[2933]115  dist = 5.0;
[2938]116  zoomTo = dist;
[2748]117  // Build the font from a TGA image font.tga in the data directory
118  // Hide the mouse cursor
[2931]119    SDL_ShowCursor(2);
[2952]120    mouse1Down = false;
[2748]121
122  // This is the main loop for the entire program and it will run until done==TRUE
123  while(!done)
124  {
125    // Draw the scene
126    DrawGLScene();
127    // And poll for events
128    SDL_Event event;
129    while ( SDL_PollEvent(&event) ) {
130      switch (event.type) {
[2931]131      case SDL_MOUSEMOTION:
[3656]132        PRINTF(4)("Mouse motion about %d,%d Pixels to (%d,%d).\n", 
[3206]133                  event.motion.xrel, event.motion.yrel,
134                  event.motion.x, event.motion.y);
[2952]135        // TRACKBALL
136        if (mouse1Down)
137          {
[2963]138            int mX = event.button.x;
139            int mY = event.button.y;
140            int wH = wHandler.screen->h;
141            int wW = wHandler.screen->w;
142            Vector tmpV (mX, mY, sqrt ( (float) abs(wH * wH/4 - (wW/2-mX) * (wW/2-mX) - (wH/2-mY) * (wH/2-mY)) ));
[3208]143            //      PRINTF(0)("tmpV: %f, %f, %f\n", tmpV.x, tmpV.y, tmpV.z);
[2952]144            p2 = tmpV-M;
[2966]145            p2.y = -p2.y;
[2952]146            rotAxis = p1.cross(p2);
[3208]147            //  PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z);
[2952]148
[2963]149            // in case that there is no rotation-axis defined
150            if (rotAxis.x != 0 || rotAxis.y != 0 || rotAxis.z != 0)
151              {
152                rotAxis.normalize();
[3208]153                //              PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z, rotAngle);
[2963]154                               
[3237]155                rotAngle = angleRad (p1, p2);
[2963]156                rotQ = Quaternion (rotAngle, rotAxis);
157                rotQ = rotQ * rotQlast;
158                rotQ.matrix (matQ);
159                //      dir = rotQ.apply(dir);
160                //      dir.normalize();
[3208]161                //      PRINTF(0)("rotAxis: %f, %f, %f, %f\n", dir.x, dir.y, dir.z, rotAngle);
[2963]162              }
163            rotQlast = rotQ;
[2952]164            p1 = p2;
[2963]165
[2952]166          }
[2931]167        break;
168      case SDL_MOUSEBUTTONDOWN:
[2932]169        if (event.button.button == 4)
[2933]170          {
[3656]171            PRINTF(4)("MouseWheel up\n");
[2938]172            zoomTo *= .5;
[2933]173          }
[2932]174        else if (event.button.button == 5)
[2933]175          {
[3656]176            PRINTF(4)("MouseWheel down\n");
[2940]177            zoomTo *= 2.0;
[2933]178          }
[2952]179        else if (event.button.button == 1)
180          {
181            mouse1Down = true;
[2963]182            int mX = event.button.x;
183            int mY = event.button.y;
184            int wH = wHandler.screen->h;
185            int wW = wHandler.screen->w;
186            Vector tmpV (mX, mY, sqrt ( (float) abs(wH * wH/4 - (wW/2-mX) * (wW/2-mX) - (wH/2-mY) * (wH/2-mY)) ));
[2952]187            p1 = tmpV-M;
[2966]188            p1.y = -p1.y;
[2952]189
190          }
[2932]191        else
192          {
[3208]193            PRINTF(0)("MouseButton %d pressed at (%d,%d).\n",
[2932]194                   event.button.button, event.button.x, event.button.y);
[2936]195            rotatorV = ( (float)wHandler.screen->w/2 -event.button.x) / (float)wHandler.screen->w / 100.0;
[2932]196          }
197           
[2931]198        break;
[2952]199      case SDL_MOUSEBUTTONUP:
200        if (event.button.button == 4);
201        else if (event.button.button == 5);
202        else if (event.button.button == 1)
203          mouse1Down =false;
204        else 
205            {
[3656]206              PRINTF(4)("MouseButton %d released at (%d,%d).\n",
[3208]207                        event.button.button, event.button.x, event.button.y);
[2952]208            }
[2931]209        break;
[3211]210
211
212      case SDL_KEYDOWN:
213        switch (event.key.keysym.sym)
214          {
215          case SDLK_x:
216            delete obj;
217            obj = NULL;
218            break;
219          case SDLK_c:
220            if (!obj)
[3396]221              obj = new OBJModel(argv[1]);
[3211]222            break;
[3418]223          case SDLK_a:
224            zoomTo /=2;
225            break;
226          case SDLK_z:
227            zoomTo *=2;
228
[3211]229          }
230        break;
[2952]231           
[2748]232        // If a quit event was recieved
[2863]233      case SDL_QUIT:
234        // then we're done and we'll end this program
[2748]235          done=TRUE;
236          break;
[2863]237      default:
[2748]238          break;
239      }
[2952]240
241
[2748]242    }
243
244    // Get the state of the keyboard keys
245    keys = SDL_GetKeyState(NULL);
246
247    // and check if ESCAPE has been pressed. If so then quit
248    if(keys[SDLK_ESCAPE]) done=TRUE;
249  }
250
251  // Kill the GL & SDL screens
[3211]252  if (obj)
253    delete obj;
[2748]254  wHandler.KillGLWindow();
255  // And quit
256  return 0;
257}
Note: See TracBrowser for help on using the repository browser.