Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/subprojects/importer/framework.cc @ 4263

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

orxonox/trunk: building subprojects externaly

File size: 6.4 KB
Line 
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
16#include "framework.h"
17
18#include "vector.h"
19
20#include "primitive_model.h"
21
22
23int verbose;
24void DrawGLScene()
25{
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;
32
33  rotatorP += rotatorV *(float)dt;
34 
35
36  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
37  glLoadIdentity(); // Reset the view
38 
39  glMatrixMode(GL_PROJECTION);
40  glLoadIdentity();     
41  gluPerspective(45.0f,500/375,0.1f,dist * 5.0f);
42    gluLookAt (0, dist , dist, 0,0,0, up.x,up.y,up.z);
43
44  glMatrixMode(GL_MODELVIEW);
45  glPushMatrix();
46  //  glRotatef (180, dir.x, dir.y, dir.z);
47  glMultMatrixf (*matQ);
48  if (obj)
49    obj->draw();
50
51  glPopMatrix();
52
53  SDL_GL_SwapBuffers(); // Swap the buffers
54  lastFrame = currFrame;
55}
56
57
58int main(int argc, char *argv[])
59{
60  verbose = 3;
61
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
67  if(wHandler.CreateGLWindow("Whandler Basecode", 800, 600, 32, FALSE) == FALSE)
68  {
69    // If an error is found, display a message, kill the GL and SDL screens (if they were created) and exit
70    PRINTF(1)("Could not initalize OpenGL :(\n\n");
71    wHandler.KillGLWindow();
72    return 0;
73  }
74 
75  PRINTF(2)("screensize: %i, %i\n", wHandler.screen->w, wHandler.screen->h);
76  if (argc>=3)
77    obj = new OBJModel (argv[1], atof(argv[2]));
78  else if (argc>=2)
79    obj = new OBJModel(argv[1]);
80  else 
81    obj = new PrimitiveModel(CYLINDER);
82
83  M = Vector(wHandler.screen->w/2, wHandler.screen->h/2, 0); 
84  rotAxis = Vector (0.0,1.0,0.0);
85  rotAngle = 0;
86
87  matQ[0][0] = matQ[1][1] = matQ[2][2] = matQ[3][3] = 1;
88  rotQ = Quaternion (rotAngle, rotAxis);
89  rotQlast = rotQ;
90  dir = Vector (0.0, 0.0, 1.0);
91  up = Vector (0.0, 1.0, 0.0);
92
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};
99  GLfloat lmodelAmbient[] = {.1, .1, .1, 1.0};
100
101  glEnable(GL_LIGHT0);
102  glLightfv(GL_LIGHT0, GL_POSITION, light0Position);
103  glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight);
104  glLightfv(GL_LIGHT0, GL_SPECULAR, whiteLight);
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 
111
112  glEnable(GL_TEXTURE_2D);
113  rotatorP = .0;
114  rotatorV = .0;
115  dist = 5.0;
116  zoomTo = dist;
117  // Build the font from a TGA image font.tga in the data directory
118  // Hide the mouse cursor
119    SDL_ShowCursor(2);
120    mouse1Down = false;
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) {
131      case SDL_MOUSEMOTION:
132        PRINTF(4)("Mouse motion about %d,%d Pixels to (%d,%d).\n", 
133                  event.motion.xrel, event.motion.yrel,
134                  event.motion.x, event.motion.y);
135        // TRACKBALL
136        if (mouse1Down)
137          {
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)) ));
143            //      PRINTF(0)("tmpV: %f, %f, %f\n", tmpV.x, tmpV.y, tmpV.z);
144            p2 = tmpV-M;
145            p2.y = -p2.y;
146            rotAxis = p1.cross(p2);
147            //  PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z);
148
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();
153                //              PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z, rotAngle);
154                               
155                rotAngle = angleRad (p1, p2);
156                rotQ = Quaternion (rotAngle, rotAxis);
157                rotQ = rotQ * rotQlast;
158                rotQ.matrix (matQ);
159                //      dir = rotQ.apply(dir);
160                //      dir.normalize();
161                //      PRINTF(0)("rotAxis: %f, %f, %f, %f\n", dir.x, dir.y, dir.z, rotAngle);
162              }
163            rotQlast = rotQ;
164            p1 = p2;
165
166          }
167        break;
168      case SDL_MOUSEBUTTONDOWN:
169        if (event.button.button == 4)
170          {
171            PRINTF(4)("MouseWheel up\n");
172            zoomTo *= .5;
173          }
174        else if (event.button.button == 5)
175          {
176            PRINTF(4)("MouseWheel down\n");
177            zoomTo *= 2.0;
178          }
179        else if (event.button.button == 1)
180          {
181            mouse1Down = true;
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)) ));
187            p1 = tmpV-M;
188            p1.y = -p1.y;
189
190          }
191        else
192          {
193            PRINTF(0)("MouseButton %d pressed at (%d,%d).\n",
194                   event.button.button, event.button.x, event.button.y);
195            rotatorV = ( (float)wHandler.screen->w/2 -event.button.x) / (float)wHandler.screen->w / 100.0;
196          }
197           
198        break;
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            {
206              PRINTF(4)("MouseButton %d released at (%d,%d).\n",
207                        event.button.button, event.button.x, event.button.y);
208            }
209        break;
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)
221              obj = new OBJModel(argv[1]);
222            break;
223          case SDLK_a:
224            zoomTo /=2;
225            break;
226          case SDLK_z:
227            zoomTo *=2;
228
229          }
230        break;
231           
232        // If a quit event was recieved
233      case SDL_QUIT:
234        // then we're done and we'll end this program
235          done=TRUE;
236          break;
237      default:
238          break;
239      }
240
241
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
252  if (obj)
253    delete obj;
254  wHandler.KillGLWindow();
255  // And quit
256  return 0;
257}
Note: See TracBrowser for help on using the repository browser.