## Posted By

patelmitra on 12/11/12

# roll camera, no error in code, error when code runs

/ Published in: Visual Basic

1. #include <windows.h> ;
2. #include "../gl/glut.h";
3. #include<assert.h>;
4. #include <math.h>;
5. #include <gl/GL.h>;
6. #include <gl/GLU.h>;
7.
8.
9. //#include "camera.h"
10. Camera cam; //global camera object
11.
12. void teaPot(void);
13.
14. class Point3 {
15.
16. public:
17. float x,y,z;
18. void set(float dx, float dy, float dz) {x = dx; y = dy; z = dz;}
19. void set(Point3& p){x = p.x; y = p.y; z = p.z;}
20. Point3(float xx, float yy, float zz) {x = xx; y = yy; z = zz;}
21. Point3(){x = y = z = 0;}
22. void build4tuple(float v[])
23. {
24. v[0] = x; v[1] = y; v[2] = z; v[3] = 1.0f;
25. }
26. };
27.
28. class Vector3{
29. public:
30. float x,y,z;
31. void set(float dx, float dy, float dz)
32. { x = dx; y = dy; z = dz;}
33. void set(Vector3& v){x = v.x; y = v.y; z = v.z;}
34. void flip() {x = -x; y = -y; z = -z;}
35. void setDiff(Point3& a, Point3& b) {x = a.x - b.x; y = a.y - b.y; z = a.z - b.z;}
36. void normalize();
37. Vector3(float xx, float yy, float zz) {x = xx; y = yy; z = zz;}
38. Vector3 (Vector3& v) {x = v.x; y = v.y; z = v.z;}
39. Vector3() {x = y = z = 0;}
40. Vector3 cross(Vector3 b);
41. float dot(Vector3 b);
42. };
43.
44.
45. class Camera {
46.
47. private:
48. Point3 eye;
49. Vector3 u,v,n;
50. double viewAngle, aspect, nearDist, farDist;
51. void setModelViewMatrix();
52.
53. public:
54. Camera();
55. void set(Point3 Eye, Point3 look, Vector3 up);
56. void roll(float angle);
57. void pitch(float angle);
58. void yaw(float angle);
59. void slide(float delU, float delV, float delN);
60. void setShape(float vAng, float asp, float nearD, float farD);
61.
62. };
63.
64. void Camera :: setModelViewMatrix(void)
65. {
66. float m[16];
67. Vector3 eVec(eye.x, eye.y, eye.z);
68. m[0] = u.x;
69. m[1] = v.x;
70. m[2] = n.x;
71. m[3] = 0;
72. m[4] = u.y;
73. m[5] = v.y;
74. m[6] = n.y;
75. m[7] = 0;
76. m[8] = u.z;
77. m[9] = v.z;
78. m[10] = n.z;
79. m[11] = 0;
80. m[12] = -eVec.dot(u);
81. m[13] = -eVec.dot(v);
82. m[14] = -eVec.dot(n);
83. m[15] = 1.0;
84. glMatrixMode(GL_MODELVIEW);
86. }
87.
88. void Camera :: set(Point3 Eye, Point3 look, Vector3 up)
89. {
90. eye.set(Eye);
91. n.set(eye.x - look.x, eye.y - look.y, eye.z - look.z);
92. u.set(up.cross(n));
93. n.normalize();
94. u.normalize();
95. v.set(n.cross(u));
96. setModelViewMatrix();
97. }
98.
99. void Camera :: slide(float delU, float delV, float delN)
100. {
101. eye.x += delU * u.x + delV * v.x + delN * n.x;
102. eye.y += delU * u.y + delV * v.y + delN * n.y;
103. eye.z += delU * u.z + delV * v.z + delN * n.z;
104. setModelViewMatrix();
105. }
106.
107. void Camera :: roll(float angle)
108. {
109. float cs = cos(3.14159265/180 * angle);
110. float sn = sin(3.14159265/180 * angle);
111. Vector3 t = u;
112. u.set(cs*t.x - sn*v.x, cs*t.y - sn*v.y, cs*t.z - sn*v.z);
113. v.set(sn*t.x + cs*v.x, sn*t.y + cs*v.y, sn*t.z + cs*v.z);
114. setModelViewMatrix();
115. }
116.
117. void axis(double length)
118. { // draw a z-axis
119. glPushMatrix();
120. glBegin(GL_LINES);
121. glVertex3d(0, 0, 0);
122. glVertex3d(0,0,length); // along the z-axis
123. glEnd();
124. // draw a cone at end
125. glTranslated(0, 0,length -0.2);
126. glutWireCone(0.04, 0.2, 12, 9);
127. glPopMatrix();
128. }
129.
130.
131.
132. void myKeyboard(unsigned char key, int x, int y)
133. {
134. switch(key)
135. {
136. case 'f':
137. cam.slide(0, 0, 0.2);
138. break;
139.
140. case 'f'-64:
141. cam.slide(0, 0, -0.2);
142. break;
143.
144. case 'p':
145. cam.roll(-1.0);
146. break;
147.
148. case 'p'-64:
149. cam.roll(1.0);
150. break;
151. }
152. glutPostRedisplay();
153.
154. }
155.
156. //<<<<<<<<<< displayWire >>>>>>>>>>>>>>>>>>>>>>
157. void displaySceen(void)
158. {
159. // set the view volume shape and size
160. glMatrixMode(GL_PROJECTION);
162. glOrtho(-2.0*64/48.0, 2.0*64/48.0, -2.0, 2.0, 0.1, 100);
163. // To Do: Change the view volume
164.
165. // position and aim the camera
166. glMatrixMode(GL_PROJECTION);
168. double winHt = 1.0;
169. glOrtho(-winHt*64/48.0, winHt*64/48.0, -winHt, winHt, 0.1, 100.0);
170. glMatrixMode(GL_MODELVIEW);
172. gluLookAt(2.3, 1.3, 2, 0.30, 0.25, 0.30, 0.0, 1.0, 0.25);
173. //start drawing
174. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
175. glPushMatrix();
176. glTranslated(0.4, 0.4, 0.6);
177. glScaled(0.5, 0.5, 0.5);
178.
179. //set properties of the surface material
180. GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f};
181. GLfloat mat_diffuse[] = {0.6f, 0.6f, 0.6f, 1.0f};
182. GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
183. GLfloat mat_shininess[] = {50.0f};
184.
185. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
186. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
187. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
188. glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
189.
190. //set the light source properties
191. GLfloat lightIntensity[] = {0.7f, 0.7f, 0.7f, 1.0f};
192. GLfloat light_position[] = {2.0f, 6.0f, 3.0f, 0.0f};
193. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
194. glLightfv(GL_LIGHT0, GL_DIFFUSE, lightIntensity);
195. // To Do: Change the position of the camera
196.
197. // Draw the coordinate system
198. glPushMatrix();
199. glColor3d(1,0,0); // draw red line: z-axis
200. axis(2);
201. glRotated(90, 0, 1.0, 0);
202. glColor3d(0,1,0); // draw green line: y-axis
203. axis(2);
204. glRotated(-90.0, 1, 0, 0);
205. glColor3d(0,0,1); // draw blue line: x-axis
206. axis(2);
207. glPopMatrix();
208.
209.
210. teaPot();
211.
212. glFlush();
213.
214. }
215.
216. // tableleg
217. void tableLeg(double thick, double len)
218. {
219. glPushMatrix();
220. glTranslated(0, len/2, 0);
221. glScaled(thick, len, thick);
222. glutSolidCube(1.0);
223. glPopMatrix();
224.
225. }
226.
227. void table(double topWid, double topThick, double legThick, double legLen)
228. {
229. glPushMatrix();
230. glTranslated(0, legLen, 0);
231. glScaled(topWid, topThick, topWid);
232. glutSolidCube(1.0);
233. glPopMatrix();
234. double dist = 0.95 * topWid/2.0 - legThick / 2.0;
235. glPushMatrix();
236. glTranslated(dist, 0, dist);
237. tableLeg(legThick, legLen);
238. glTranslated(0, 0, -2 * dist);
239. tableLeg(legThick, legLen);
240. glTranslated(-2 * dist, 0, 2*dist);
241. tableLeg(legThick, legLen);
242. glTranslated(0, 0, -2*dist);
243. tableLeg(legThick, legLen);
244. glPopMatrix();
245. }
246.
247. void teaPot(void)
248. {
249. glClear(GL_COLOR_BUFFER_BIT||GL_DEPTH_BUFFER_BIT);
250. glPushMatrix();
251. glPopMatrix();
252. //glBegin();
253. glTranslated(0.60, 0.50, 0.20);
254. glRotated(20, 0, 1, 0);
255. glColor3d(1,0,1);
256. glutSolidTeapot(0.5);
257.
258. glPopMatrix();
259. glPushMatrix();
260. glTranslated(0.4, 0, 0.4);
261. table(0.6, 0.02, 0.02, 0.3);
262.
263. glFlush();
264. glutSwapBuffers();
265. }
266.
267.
268. void main(int argc, char **argv)
269. {
270. glutInit(&argc, argv);
271. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
272. glutInitWindowSize(640,480);
273. glutInitWindowPosition(100, 100);
274. glutCreateWindow("The Teapot");
275. glutKeyboardFunc(myKeyboard);
276. glutDisplayFunc(displaySceen);
277. glEnable(GL_LIGHTING);
278. glEnable(GL_LIGHT0);