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