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