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