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 = 3; |
---|
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 | M = Vector(wHandler.screen->w/2, wHandler.screen->h/2, 0); |
---|
115 | rotAxis = Vector (0.0,1.0,0.0); |
---|
116 | rotAngle = 0; |
---|
117 | |
---|
118 | matQ[0][0] = matQ[1][1] = matQ[2][2] = matQ[3][3] = 1; |
---|
119 | rotQ = Quaternion (rotAngle, rotAxis); |
---|
120 | rotQlast = rotQ; |
---|
121 | dir = Vector (0.0, 0.0, 1.0); |
---|
122 | up = Vector (0.0, 1.0, 0.0); |
---|
123 | |
---|
124 | glEnable(GL_LIGHTING); |
---|
125 | glEnable(GL_DEPTH_TEST); |
---|
126 | |
---|
127 | GLfloat whiteLight[] = {1.0, 1.0, 1.0,1.0}; |
---|
128 | GLfloat light0Position[] = {10.0, 10.0, 10.0, 0.0}; |
---|
129 | GLfloat light1Position[] = {-10.0, -7.0, -6.0, 0.0}; |
---|
130 | GLfloat lmodelAmbient[] = {.1, .1, .1, 1.0}; |
---|
131 | |
---|
132 | glEnable(GL_LIGHT0); |
---|
133 | glLightfv(GL_LIGHT0, GL_POSITION, light0Position); |
---|
134 | glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight); |
---|
135 | glLightfv(GL_LIGHT0, GL_SPECULAR, whiteLight); |
---|
136 | |
---|
137 | glEnable(GL_LIGHT1); |
---|
138 | glLightfv(GL_LIGHT1, GL_POSITION, light1Position); |
---|
139 | glLightfv(GL_LIGHT1, GL_DIFFUSE, whiteLight); |
---|
140 | glLightfv(GL_LIGHT1, GL_SPECULAR, whiteLight); |
---|
141 | |
---|
142 | |
---|
143 | glEnable(GL_TEXTURE_2D); |
---|
144 | rotatorP = .0; |
---|
145 | rotatorV = .0; |
---|
146 | dist = 5.0; |
---|
147 | zoomTo = dist; |
---|
148 | // Build the font from a TGA image font.tga in the data directory |
---|
149 | // Hide the mouse cursor |
---|
150 | SDL_ShowCursor(2); |
---|
151 | mouse1Down = false; |
---|
152 | |
---|
153 | // This is the main loop for the entire program and it will run until done==TRUE |
---|
154 | while(!done) |
---|
155 | { |
---|
156 | // Draw the scene |
---|
157 | DrawGLScene(); |
---|
158 | // And poll for events |
---|
159 | SDL_Event event; |
---|
160 | while ( SDL_PollEvent(&event) ) { |
---|
161 | switch (event.type) { |
---|
162 | case SDL_MOUSEMOTION: |
---|
163 | PRINTF(3)("Mouse motion about %d,%d Pixels to (%d,%d).\n", |
---|
164 | event.motion.xrel, event.motion.yrel, |
---|
165 | event.motion.x, event.motion.y); |
---|
166 | // TRACKBALL |
---|
167 | if (mouse1Down) |
---|
168 | { |
---|
169 | int mX = event.button.x; |
---|
170 | int mY = event.button.y; |
---|
171 | int wH = wHandler.screen->h; |
---|
172 | int wW = wHandler.screen->w; |
---|
173 | Vector tmpV (mX, mY, sqrt ( (float) abs(wH * wH/4 - (wW/2-mX) * (wW/2-mX) - (wH/2-mY) * (wH/2-mY)) )); |
---|
174 | // PRINTF(0)("tmpV: %f, %f, %f\n", tmpV.x, tmpV.y, tmpV.z); |
---|
175 | p2 = tmpV-M; |
---|
176 | p2.y = -p2.y; |
---|
177 | rotAxis = p1.cross(p2); |
---|
178 | // PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z); |
---|
179 | |
---|
180 | // in case that there is no rotation-axis defined |
---|
181 | if (rotAxis.x != 0 || rotAxis.y != 0 || rotAxis.z != 0) |
---|
182 | { |
---|
183 | rotAxis.normalize(); |
---|
184 | // PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z, rotAngle); |
---|
185 | |
---|
186 | rotAngle = angleRad (p1, p2); |
---|
187 | rotQ = Quaternion (rotAngle, rotAxis); |
---|
188 | rotQ = rotQ * rotQlast; |
---|
189 | rotQ.matrix (matQ); |
---|
190 | // dir = rotQ.apply(dir); |
---|
191 | // dir.normalize(); |
---|
192 | // PRINTF(0)("rotAxis: %f, %f, %f, %f\n", dir.x, dir.y, dir.z, rotAngle); |
---|
193 | } |
---|
194 | rotQlast = rotQ; |
---|
195 | p1 = p2; |
---|
196 | |
---|
197 | } |
---|
198 | break; |
---|
199 | case SDL_MOUSEBUTTONDOWN: |
---|
200 | if (event.button.button == 4) |
---|
201 | { |
---|
202 | PRINTF(0)("MouseWheel up\n"); |
---|
203 | zoomTo *= .5; |
---|
204 | } |
---|
205 | else if (event.button.button == 5) |
---|
206 | { |
---|
207 | PRINTF(2)("MouseWheel down\n"); |
---|
208 | zoomTo *= 2.0; |
---|
209 | } |
---|
210 | else if (event.button.button == 1) |
---|
211 | { |
---|
212 | mouse1Down = true; |
---|
213 | int mX = event.button.x; |
---|
214 | int mY = event.button.y; |
---|
215 | int wH = wHandler.screen->h; |
---|
216 | int wW = wHandler.screen->w; |
---|
217 | Vector tmpV (mX, mY, sqrt ( (float) abs(wH * wH/4 - (wW/2-mX) * (wW/2-mX) - (wH/2-mY) * (wH/2-mY)) )); |
---|
218 | p1 = tmpV-M; |
---|
219 | p1.y = -p1.y; |
---|
220 | |
---|
221 | } |
---|
222 | else |
---|
223 | { |
---|
224 | PRINTF(0)("MouseButton %d pressed at (%d,%d).\n", |
---|
225 | event.button.button, event.button.x, event.button.y); |
---|
226 | rotatorV = ( (float)wHandler.screen->w/2 -event.button.x) / (float)wHandler.screen->w / 100.0; |
---|
227 | } |
---|
228 | |
---|
229 | break; |
---|
230 | case SDL_MOUSEBUTTONUP: |
---|
231 | if (event.button.button == 4); |
---|
232 | else if (event.button.button == 5); |
---|
233 | else if (event.button.button == 1) |
---|
234 | mouse1Down =false; |
---|
235 | else |
---|
236 | { |
---|
237 | PRINTF(2)("MouseButton %d released at (%d,%d).\n", |
---|
238 | event.button.button, event.button.x, event.button.y); |
---|
239 | } |
---|
240 | break; |
---|
241 | |
---|
242 | |
---|
243 | case SDL_KEYDOWN: |
---|
244 | switch (event.key.keysym.sym) |
---|
245 | { |
---|
246 | case SDLK_x: |
---|
247 | delete obj; |
---|
248 | obj = NULL; |
---|
249 | break; |
---|
250 | case SDLK_c: |
---|
251 | if (!obj) |
---|
252 | obj = new OBJModel(argv[1]); |
---|
253 | break; |
---|
254 | } |
---|
255 | break; |
---|
256 | |
---|
257 | // If a quit event was recieved |
---|
258 | case SDL_QUIT: |
---|
259 | // then we're done and we'll end this program |
---|
260 | done=TRUE; |
---|
261 | break; |
---|
262 | default: |
---|
263 | break; |
---|
264 | } |
---|
265 | |
---|
266 | |
---|
267 | } |
---|
268 | |
---|
269 | // Get the state of the keyboard keys |
---|
270 | keys = SDL_GetKeyState(NULL); |
---|
271 | |
---|
272 | // and check if ESCAPE has been pressed. If so then quit |
---|
273 | if(keys[SDLK_ESCAPE]) done=TRUE; |
---|
274 | } |
---|
275 | |
---|
276 | // Kill the GL & SDL screens |
---|
277 | if (obj) |
---|
278 | delete obj; |
---|
279 | wHandler.KillGLWindow(); |
---|
280 | // And quit |
---|
281 | return 0; |
---|
282 | } |
---|