Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: model: sphere-model is now working

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