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