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 |
---|
13 | HeightMapViewer::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 |
---|
31 | HeightMapViewer::~HeightMapViewer() |
---|
32 | { |
---|
33 | cout << "HeightMapViewer::~HeightMapViewer()" << endl; |
---|
34 | } |
---|
35 | |
---|
36 | // main loop |
---|
37 | bool 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 | return true; |
---|
60 | } |
---|
61 | |
---|
62 | bool HeightMapViewer::run() |
---|
63 | { |
---|
64 | cout << "HeightMapViewer run()" << endl; |
---|
65 | |
---|
66 | bool done = false; |
---|
67 | SDL_Event event; |
---|
68 | |
---|
69 | float angleX; |
---|
70 | float angleY; |
---|
71 | |
---|
72 | while(!done) |
---|
73 | { |
---|
74 | /* Check for events */ |
---|
75 | while (SDL_PollEvent(&event)) |
---|
76 | { |
---|
77 | switch (event.type) |
---|
78 | { |
---|
79 | case SDL_MOUSEMOTION: |
---|
80 | if (mousedown==true) |
---|
81 | { |
---|
82 | angleX = PI / 180 * event.motion.xrel * 0.2; |
---|
83 | angleY = PI / 180 * event.motion.yrel * 0.2; |
---|
84 | |
---|
85 | // Quaternion(angle,axis) |
---|
86 | rotator = Quaternion(-angleX,Vector(0,1,0)); |
---|
87 | sightDirection = rotator.apply(sightDirection); |
---|
88 | |
---|
89 | #ifdef FULLSCREEN |
---|
90 | rotator = Quaternion(angleY,perpendicular(sightDirection)); |
---|
91 | #else |
---|
92 | rotator = Quaternion(-angleY,perpendicular(sightDirection)); |
---|
93 | #endif |
---|
94 | |
---|
95 | sightDirection = rotator.apply(sightDirection); |
---|
96 | |
---|
97 | updateView(); |
---|
98 | } |
---|
99 | break; |
---|
100 | case SDL_MOUSEBUTTONDOWN: |
---|
101 | mousedown = true; |
---|
102 | break; |
---|
103 | case SDL_MOUSEBUTTONUP: |
---|
104 | mousedown = false; |
---|
105 | break; |
---|
106 | |
---|
107 | case SDL_KEYDOWN: |
---|
108 | |
---|
109 | switch(event.key.keysym.sym) |
---|
110 | { |
---|
111 | case SDLK_UP: |
---|
112 | // move in direction of sight |
---|
113 | cameraPos = cameraPos + sightDirection * 0.7; |
---|
114 | updateView(); |
---|
115 | break; |
---|
116 | |
---|
117 | case SDLK_DOWN: |
---|
118 | // move in direction of sight |
---|
119 | cameraPos = cameraPos - sightDirection * 0.7; |
---|
120 | updateView(); |
---|
121 | break; |
---|
122 | |
---|
123 | case SDLK_LEFT: |
---|
124 | cameraPos = cameraPos + perpendicular(sightDirection) * 0.7; |
---|
125 | updateView(); |
---|
126 | break; |
---|
127 | |
---|
128 | case SDLK_RIGHT: |
---|
129 | cameraPos = cameraPos - perpendicular(sightDirection) * 0.7; |
---|
130 | updateView(); |
---|
131 | break; |
---|
132 | |
---|
133 | case SDLK_s: |
---|
134 | smoothShading = !smoothShading; |
---|
135 | if (smoothShading) glShadeModel(GL_SMOOTH); |
---|
136 | else glShadeModel(GL_FLAT); |
---|
137 | break; |
---|
138 | |
---|
139 | case SDLK_w: |
---|
140 | wireframe = !wireframe; |
---|
141 | if (wireframe) glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); |
---|
142 | else glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); |
---|
143 | break; |
---|
144 | |
---|
145 | case SDLK_r: |
---|
146 | // reset view vectors |
---|
147 | //cameraPos = Vector(0,0,40); |
---|
148 | //sightDirection = Vector(0,0,-1); |
---|
149 | cameraPos = Vector(73.9871,172.496,286.137); |
---|
150 | sightDirection = Vector(0.23429,-0.736527,-0.625574); |
---|
151 | |
---|
152 | updateView(); |
---|
153 | break; |
---|
154 | |
---|
155 | case SDLK_h: |
---|
156 | cout << "HELP\n" |
---|
157 | << "display list count: " << terrain.displayListCount << endl |
---|
158 | << "first display list at: " << terrain.displayListStart << endl |
---|
159 | << "camera position: " << "(" << cameraPos.x << "," << cameraPos.y << "," << cameraPos.z << ")" << endl |
---|
160 | << "sightDirection: " << "(" << sightDirection.x << "," << sightDirection.y << "," << sightDirection.z << ")" << endl |
---|
161 | << endl; |
---|
162 | |
---|
163 | break; |
---|
164 | |
---|
165 | case SDLK_ESCAPE: |
---|
166 | case SDLK_q: |
---|
167 | done = 1; |
---|
168 | break; |
---|
169 | |
---|
170 | default: |
---|
171 | break; |
---|
172 | } |
---|
173 | break; |
---|
174 | |
---|
175 | case SDL_QUIT: |
---|
176 | done = 1; |
---|
177 | break; |
---|
178 | |
---|
179 | default: |
---|
180 | break; |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | drawScene(); |
---|
185 | |
---|
186 | SDL_GL_SwapBuffers(); |
---|
187 | |
---|
188 | SDL_Delay(1); |
---|
189 | } |
---|
190 | |
---|
191 | return true; |
---|
192 | } |
---|
193 | |
---|
194 | void HeightMapViewer::updateView() |
---|
195 | { |
---|
196 | // be sure to use up to date lookAt-vector |
---|
197 | lookAt = cameraPos + sightDirection; |
---|
198 | upVector = sightDirection.cross(perpendicular(sightDirection)); |
---|
199 | |
---|
200 | glMatrixMode(GL_PROJECTION); // Select The Projection Matrix |
---|
201 | glLoadIdentity(); |
---|
202 | |
---|
203 | // Calculate The Aspect Ratio Of The Window |
---|
204 | gluPerspective(45.0f,(GLfloat)WIDTH/(GLfloat)HEIGHT,0.5f,300.0f); |
---|
205 | gluLookAt (cameraPos.x,cameraPos.y,cameraPos.z, |
---|
206 | lookAt.x,lookAt.y,lookAt.z, |
---|
207 | upVector.x,upVector.y,upVector.z); |
---|
208 | |
---|
209 | glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix |
---|
210 | glLoadIdentity(); // Reset The Modelview Matrix |
---|
211 | } |
---|
212 | |
---|
213 | void HeightMapViewer::drawScene() |
---|
214 | { |
---|
215 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
216 | |
---|
217 | glPushMatrix(); |
---|
218 | { |
---|
219 | //glTranslatef(0.0,1.0,0.0); |
---|
220 | glScalef(1,0.2,1); |
---|
221 | |
---|
222 | for (int i=0; i<terrain.displayListCount; i++) |
---|
223 | { |
---|
224 | glCallList(terrain.displayListStart + i); |
---|
225 | } |
---|
226 | } |
---|
227 | glPopMatrix(); |
---|
228 | |
---|
229 | glColor3f(0.0,1.0,0.0); |
---|
230 | // glutWireCube(1.0); |
---|
231 | |
---|
232 | glColor3f(0.0,0.0,1.0); |
---|
233 | glBegin(GL_POINTS); |
---|
234 | for (float z=0;z<10;z++) |
---|
235 | for (float x=0;x<10;x++) |
---|
236 | glVertex3f(x,0,z); |
---|
237 | glEnd(); |
---|
238 | |
---|
239 | } |
---|
240 | |
---|
241 | |
---|
242 | |
---|
243 | /** |
---|
244 | \brief returns a vector that is perpendicular and lies in the xz-plane |
---|
245 | */ |
---|
246 | Vector perpendicular (Vector perpendic) |
---|
247 | { |
---|
248 | Vector r; |
---|
249 | |
---|
250 | r.x = perpendic.z; |
---|
251 | r.z = -perpendic.x; |
---|
252 | |
---|
253 | r.normalize(); |
---|
254 | |
---|
255 | return r; |
---|
256 | } |
---|