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