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