Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/nico/src/importer/heightMapViewer.cc @ 3393

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

orxonox/branches/nico: lighting enabled, and working again

File size: 7.7 KB
Line 
1/*
2 *  legotown.cpp
3 *  legotown3d
4 *
5 *  Created by Nico Bernold on 08.01.05.
6 *  Copyright 2005 __MyCompanyName__. All rights reserved.
7 *
8 */
9
10#include "heightMapViewer.h"
11
12// constructor
13HeightMapViewer::HeightMapViewer()
14{
15        cout << "HeightMapViewer::HeightMapViewer()" << endl;
16       
17        cameraPos = Vector(0,0,40);
18        sightDirection = Vector(0,0,-1);
19
20        lookAt = cameraPos + sightDirection;
21
22        upVector = Vector(0,1,0);
23       
24        wireframe = false;
25        mousedown = false;
26        smoothShading = false;
27       
28}
29
30// destructor
31HeightMapViewer::~HeightMapViewer()
32{
33        cout << "HeightMapViewer::~HeightMapViewer()" << endl;
34}
35
36// main loop
37bool HeightMapViewer::init(char* fileName)
38{
39        cout << "HeightMapViewer init()" << endl;
40       
41#ifdef FULLSCREEN
42        if (!windowhandler.CreateGLWindow("height map viewer", WIDTH, HEIGHT, 32, true))
43#else
44        if (!windowhandler.CreateGLWindow("height map viewer", WIDTH, HEIGHT, 32, false))
45#endif
46        {
47                cout << "could not create OpenGL-Window." << endl;
48                return false;
49        }
50
51        if (terrain.loadBitmap(fileName) == false)
52        {
53                cout << "could not load bitmap." << endl;
54                return false;
55        }
56       
57        terrain.createDisplayLists(128, 128, 1);
58       
59        this->initLighting();
60        SDL_EnableKeyRepeat(50,10);
61        return true;
62}
63
64bool HeightMapViewer::initLighting(void)
65{
66  /*  glEnable(GL_LIGHTING);
67  glEnable(GL_DEPTH_TEST);
68
69  GLfloat whiteLight[] = {.2, .2, .2, 1.0};
70  GLfloat light0Position[] = {10.0, 10.0, 10.0, 0.0};
71  GLfloat lmodelAmbient[] = {.1, .1, .1, 1.0};
72
73  glEnable(GL_LIGHT0);
74  glLightfv(GL_LIGHT0, GL_POSITION, light0Position);
75  glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight);
76  glLightfv(GL_LIGHT0, GL_SPECULAR, whiteLight);
77 
78        GLfloat klotz_Ka[]  = {0.2,0.2,0.2,1.0};
79        GLfloat klotz_Kd[]  = {0.3,1.0,0.3,1.0};
80        GLfloat klotz_Ks[]  = {0.8,0.8,0.8,1.0};
81        GLfloat klotz_Ke[]  = {0.0,0.0,0.0,1.0};
82        GLfloat klotz_Se    = 20;
83        glMaterialfv(GL_FRONT,GL_AMBIENT,klotz_Ka);
84        glMaterialfv(GL_FRONT,GL_DIFFUSE,klotz_Kd);
85  */
86
87        glShadeModel(GL_FLAT); // Enable Smooth Shading
88       
89        glClearColor(0.445f, 0.726f, 1.00f, 0.0f); // Blue Background
90       
91        glClearDepth(1.0f);  // Depth Buffer Setup
92        glEnable(GL_DEPTH_TEST); // Enables Depth Testing
93        glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
94
95        glEnable(GL_CULL_FACE); // don't draw back faces
96       
97        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Really Nice Perspective Calculations
98
99
100        // fog behaves strange
101        //glEnable(GL_FOG);
102        GLfloat fog_color[4] = {0.7,0.7,0.7,1.0};
103        glFogfv(GL_FOG_COLOR,fog_color);
104        glFogf(GL_FOG_START, 0.0);
105        glFogf(GL_FOG_END, 100.0);
106        glFogi(GL_FOG_MODE,GL_LINEAR);
107
108
109        glEnable(GL_NORMALIZE);
110       
111        // LIGHT SETTINGS
112        glEnable(GL_LIGHTING);
113        glEnable(GL_LIGHT0);
114       
115        // global parameters
116        GLfloat global_Ka[]  = {0.25,0.25,0.25,1.0};
117       
118        // light0 parameters
119        GLfloat light0_pos[] = {0.0,100.0,0.0,0.0};
120        GLfloat light0_Ka[]  = {0.0,0.0,0.0,1.0};
121        GLfloat light0_Kd[]  = {0.6,0.6,0.6,1.0};
122        GLfloat light0_Ks[]  = {0.8,0.8,0.8,1.0};
123       
124        // klotz default parameters
125        GLfloat klotz_Ka[]  = {0.2,0.2,0.2,1.0};
126        GLfloat klotz_Kd[]  = {0.3,1.0,0.3,1.0};
127        GLfloat klotz_Ks[]  = {0.8,0.8,0.8,1.0};
128        GLfloat klotz_Ke[]  = {0.0,0.0,0.0,1.0};
129        GLfloat klotz_Se    = 20;
130       
131        // set light0 options
132        glLightfv(GL_LIGHT0,GL_POSITION,light0_pos);
133        glLightfv(GL_LIGHT0,GL_AMBIENT,light0_Ka);
134        glLightfv(GL_LIGHT0,GL_DIFFUSE,light0_Kd);
135        glLightfv(GL_LIGHT0,GL_SPECULAR,light0_Ks);
136
137        // MATERIAL SETTINGS
138        glMaterialfv(GL_FRONT,GL_AMBIENT,klotz_Ka);
139        glMaterialfv(GL_FRONT,GL_DIFFUSE,klotz_Kd);
140        //glMaterialfv(GL_FRONT,GL_SPECULAR,klotz_Ks);
141        //glMaterialfv(GL_FRONT,GL_EMISSION,klotz_Ke);
142        //glMaterialf(GL_FRONT,GL_SHININESS,klotz_Se);
143
144
145}
146
147
148bool HeightMapViewer::run()
149{
150        cout << "HeightMapViewer run()" << endl;
151
152        bool done = false;
153        SDL_Event event;
154
155        float angleX;
156        float angleY;
157       
158        while(!done)
159        {
160                /* Check for events */
161                while (SDL_PollEvent(&event))
162                {
163                        switch (event.type)
164                        {
165                                case SDL_MOUSEMOTION:
166                                        if (mousedown==true)
167                                        {
168                                                angleX = PI / 180 * event.motion.xrel * 0.2;
169                                                angleY = PI / 180 * event.motion.yrel * 0.2;
170
171                                                // Quaternion(angle,axis)
172                                                rotator = Quaternion(-angleX,Vector(0,1,0));
173                                                sightDirection = rotator.apply(sightDirection);
174
175                                                rotator = Quaternion(angleY,perpendicular(sightDirection));
176                                               
177                                                sightDirection = rotator.apply(sightDirection);
178                                               
179                                                updateView();                                           
180                                        }
181                                        break;
182                                case SDL_MOUSEBUTTONDOWN:
183                                        mousedown = true;
184                                        break;
185                                case SDL_MOUSEBUTTONUP:
186                                        mousedown = false;
187                                        break;
188                                       
189                                case SDL_KEYDOWN:
190
191                                        switch(event.key.keysym.sym)
192                                        {
193                                                case SDLK_UP:
194                                                        // move in direction of sight
195                                                        cameraPos = cameraPos + sightDirection * 0.7;                                                   
196                                                        updateView();
197                                                        break;
198
199                                                case SDLK_DOWN:
200                                                        // move in direction of sight
201                                                        cameraPos = cameraPos - sightDirection * 0.7;
202                                                        updateView();
203                                                        break;
204                                                       
205                                                case SDLK_LEFT:
206                                                        cameraPos = cameraPos + perpendicular(sightDirection) * 0.7;
207                                                        updateView();
208                                                        break;
209                                                       
210                                                case SDLK_RIGHT:
211                                                        cameraPos = cameraPos - perpendicular(sightDirection) * 0.7;
212                                                        updateView();
213                                                        break;
214                                                       
215                                                case SDLK_s:
216                                                        smoothShading = !smoothShading;
217                                                        if (smoothShading)      glShadeModel(GL_SMOOTH);
218                                                        else glShadeModel(GL_FLAT);
219                                                        break;
220                                                       
221                                                case SDLK_w:
222                                                        wireframe = !wireframe;
223                                                        if (wireframe) glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
224                                                        else glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
225                                                        break;
226                                                       
227                                                case SDLK_r:
228                                                        // reset view vectors
229                                                        //cameraPos = Vector(0,0,40);
230                                                        //sightDirection = Vector(0,0,-1);                                                     
231                                                        cameraPos = Vector(73.9871,172.496,286.137);
232                                                        sightDirection = Vector(0.23429,-0.736527,-0.625574);
233
234                                                        updateView();
235                                                        break;
236
237                                                case SDLK_h:
238                                                        cout << "HELP\n"
239                                                        << "display list count:    " << terrain.displayListCount << endl
240                                                        << "first display list at: " << terrain.displayListStart << endl
241                                                        << "camera position:       " << "(" << cameraPos.x << "," << cameraPos.y << "," << cameraPos.z << ")" << endl
242                                                        << "sightDirection:        " << "(" << sightDirection.x << "," << sightDirection.y << "," << sightDirection.z << ")" << endl
243                                                        << endl;
244                                                       
245                                                        break;
246                                                       
247                                                case SDLK_ESCAPE:
248                                                case SDLK_q:
249                                                        done = 1;
250                                                        break;
251                                                       
252                                                default:
253                                                        break;
254                                        }       
255                                        break;
256                                       
257                                case SDL_QUIT:
258                                        done = 1;
259                                        break;
260                                       
261                                default:
262                                        break;
263                        }
264                }
265               
266                drawScene();
267
268                SDL_GL_SwapBuffers();   
269
270                SDL_Delay(1);
271        }
272       
273        return true;
274}
275
276void HeightMapViewer::updateView()
277{
278        // be sure to use up to date lookAt-vector
279        lookAt = cameraPos + sightDirection;
280        upVector = sightDirection.cross(perpendicular(sightDirection));
281       
282        glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
283        glLoadIdentity();
284       
285        // Calculate The Aspect Ratio Of The Window
286        gluPerspective(45.0f,(GLfloat)WIDTH/(GLfloat)HEIGHT,0.5f,300.0f);
287        gluLookAt (cameraPos.x,cameraPos.y,cameraPos.z,
288                           lookAt.x,lookAt.y,lookAt.z,
289                           upVector.x,upVector.y,upVector.z);
290       
291        glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
292        glLoadIdentity(); // Reset The Modelview Matrix
293}
294
295void HeightMapViewer::drawScene()
296{       
297        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
298
299        glPushMatrix();
300        {
301                //glTranslatef(0.0,1.0,0.0);
302                glScalef(1,0.2,1);
303       
304                for (int i=0; i<terrain.displayListCount; i++)
305                {
306                        glCallList(terrain.displayListStart + i);
307                }
308        }
309        glPopMatrix();
310       
311        glColor3f(0.0,1.0,0.0);
312        //      glutWireCube(1.0);
313       
314        glColor3f(0.0,0.0,1.0);
315        glBegin(GL_POINTS);
316        for (float z=0;z<10;z++)
317                for (float x=0;x<10;x++)
318                        glVertex3f(x,0,z);
319        glEnd();
320       
321}
322
323
324
325/**
326   \brief returns a vector that is perpendicular and lies in the xz-plane
327 */
328Vector perpendicular (Vector perpendic)
329{
330        Vector r;
331       
332        r.x =  perpendic.z;
333        r.z = -perpendic.x;
334       
335        r.normalize();
336       
337        return r;       
338}
Note: See TracBrowser for help on using the repository browser.