## Posted By

patelmitra on 12/11/12

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

/ Published in: Visual Basic

`#include <windows.h> ;#include "../gl/glut.h";#include<assert.h>;#include <math.h>;#include <gl/GL.h>;#include <gl/GLU.h>;  //#include "camera.h"Camera cam;  //global camera object void teaPot(void); class Point3 { public:	float x,y,z;	void set(float dx, float dy, float dz) {x = dx; y = dy; z = dz;}	void set(Point3& p){x = p.x; y = p.y; z = p.z;}	Point3(float xx, float yy, float zz) {x = xx; y = yy; z = zz;}	Point3(){x = y = z = 0;}	void build4tuple(float v[])	{		v[0] = x; v[1] = y; v[2] = z; v[3] = 1.0f;	}}; class Vector3{public:	float x,y,z;	void set(float dx, float dy, float dz) 	{ x = dx; y = dy; z = dz;}	void set(Vector3& v){x = v.x; y = v.y; z = v.z;}	void flip() {x = -x; y = -y; z = -z;}	void setDiff(Point3& a, Point3& b) {x = a.x - b.x; y = a.y - b.y; z = a.z - b.z;}	void normalize();	Vector3(float xx, float yy, float zz) {x = xx; y = yy; z = zz;}	Vector3 (Vector3& v) {x = v.x; y = v.y; z = v.z;}	Vector3() {x = y = z = 0;}	Vector3 cross(Vector3 b);	float dot(Vector3 b);};  class Camera { private:	Point3 eye;	Vector3 u,v,n;	double viewAngle, aspect, nearDist, farDist;	void setModelViewMatrix(); public:	Camera();	void set(Point3 Eye, Point3 look, Vector3 up);	void roll(float angle);	void pitch(float angle);	void yaw(float angle);	void slide(float delU, float delV, float delN);	void setShape(float vAng, float asp, float nearD, float farD); }; void Camera :: setModelViewMatrix(void){	float m[16];	Vector3 eVec(eye.x, eye.y, eye.z);	m[0] = u.x;	m[1] = v.x;	m[2] = n.x;	m[3] = 0;	m[4] = u.y;	m[5] = v.y;	m[6] = n.y;	m[7] = 0;	m[8] = u.z;	m[9] = v.z;	m[10] = n.z;	m[11] = 0;	m[12] = -eVec.dot(u);	m[13] = -eVec.dot(v);	m[14] = -eVec.dot(n);	m[15] = 1.0;	glMatrixMode(GL_MODELVIEW);	glLoadMatrixf(m);} void Camera :: set(Point3 Eye, Point3 look, Vector3 up){	eye.set(Eye);	n.set(eye.x - look.x, eye.y - look.y, eye.z - look.z);	u.set(up.cross(n));	n.normalize();	u.normalize();	v.set(n.cross(u));	setModelViewMatrix();} void Camera :: slide(float delU, float delV, float delN){	eye.x += delU * u.x + delV * v.x + delN * n.x;	eye.y += delU * u.y + delV * v.y + delN * n.y;	eye.z += delU * u.z + delV * v.z + delN * n.z;	setModelViewMatrix();} void Camera :: roll(float angle){	float cs = cos(3.14159265/180 * angle);	float sn = sin(3.14159265/180 * angle);	Vector3 t = u;	u.set(cs*t.x - sn*v.x, cs*t.y - sn*v.y, cs*t.z - sn*v.z);	v.set(sn*t.x + cs*v.x, sn*t.y + cs*v.y, sn*t.z + cs*v.z);	setModelViewMatrix();} void axis(double length){   // draw a z-axis       glPushMatrix();       glBegin(GL_LINES);            glVertex3d(0, 0, 0);             glVertex3d(0,0,length); // along the z-axis       glEnd();   // draw a cone at end       glTranslated(0, 0,length -0.2);       glutWireCone(0.04, 0.2, 12, 9);       glPopMatrix();}     void myKeyboard(unsigned char key, int x, int y){	switch(key)	{	case 'f':		cam.slide(0, 0, 0.2);		break; 	case 'f'-64:		cam.slide(0, 0, -0.2);		break; 	case 'p':		cam.roll(-1.0);		break; 	case 'p'-64:		cam.roll(1.0);		break;	}	glutPostRedisplay(); } //<<<<<<<<<< displayWire >>>>>>>>>>>>>>>>>>>>>>void displaySceen(void){// set the view volume shape and size       glMatrixMode(GL_PROJECTION);        glLoadIdentity();       glOrtho(-2.0*64/48.0, 2.0*64/48.0, -2.0, 2.0, 0.1, 100);// To Do: Change the view volume 	   // position and aim the camera       	   glMatrixMode(GL_PROJECTION);	   glLoadIdentity();	   double winHt = 1.0;	   glOrtho(-winHt*64/48.0, winHt*64/48.0, -winHt, winHt, 0.1, 100.0);	   	   glMatrixMode(GL_MODELVIEW);        glLoadIdentity();       gluLookAt(2.3, 1.3, 2, 0.30, 0.25, 0.30, 0.0, 1.0, 0.25);       //start drawing	   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);	   glPushMatrix();	   glTranslated(0.4, 0.4, 0.6);	   glScaled(0.5, 0.5, 0.5); 	   //set properties of the surface material	   GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f};	   GLfloat mat_diffuse[] = {0.6f, 0.6f, 0.6f, 1.0f};	   GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};	   GLfloat mat_shininess[] = {50.0f}; 	   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);	   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);	   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);	   glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); 	   //set the light source properties	   GLfloat lightIntensity[] = {0.7f, 0.7f, 0.7f, 1.0f};	   GLfloat light_position[] = {2.0f, 6.0f, 3.0f, 0.0f};	   glLightfv(GL_LIGHT0, GL_POSITION, light_position);	   glLightfv(GL_LIGHT0, GL_DIFFUSE, lightIntensity);// To Do: Change the position of the camera // Draw the coordinate system        glPushMatrix();	   glColor3d(1,0,0); // draw red line: z-axis       axis(2);                                glRotated(90, 0, 1.0, 0);       glColor3d(0,1,0); // draw green line: y-axis       axis(2);                                glRotated(-90.0, 1, 0, 0);       glColor3d(0,0,1); // draw blue line: x-axis       axis(2);                                glPopMatrix();  	   teaPot();        glFlush(); } // tableleg	   void tableLeg(double thick, double len)	   {		glPushMatrix();		glTranslated(0, len/2, 0);		glScaled(thick, len, thick);		glutSolidCube(1.0);		glPopMatrix(); 	   } 	   void table(double topWid, double topThick, double legThick, double legLen)	   {		glPushMatrix();		glTranslated(0, legLen, 0);		glScaled(topWid, topThick, topWid);		glutSolidCube(1.0);		glPopMatrix();		double dist = 0.95 * topWid/2.0 - legThick / 2.0;		glPushMatrix();		glTranslated(dist, 0, dist);		tableLeg(legThick, legLen);		glTranslated(0, 0, -2 * dist);		tableLeg(legThick, legLen);		glTranslated(-2 * dist, 0, 2*dist);		tableLeg(legThick, legLen);		glTranslated(0, 0, -2*dist);		tableLeg(legThick, legLen);		glPopMatrix();	   } void teaPot(void){	glClear(GL_COLOR_BUFFER_BIT||GL_DEPTH_BUFFER_BIT);	glPushMatrix();	glPopMatrix();	//glBegin();	glTranslated(0.60, 0.50, 0.20);	glRotated(20, 0, 1, 0);	glColor3d(1,0,1);	glutSolidTeapot(0.5); 	glPopMatrix();	glPushMatrix();	glTranslated(0.4, 0, 0.4);	table(0.6, 0.02, 0.02, 0.3); 	glFlush();	glutSwapBuffers();}  void main(int argc, char **argv){       glutInit(&argc, argv);       glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);       glutInitWindowSize(640,480);       glutInitWindowPosition(100, 100);       glutCreateWindow("The Teapot");	   glutKeyboardFunc(myKeyboard);       glutDisplayFunc(displaySceen);	   glEnable(GL_LIGHTING);	   glEnable(GL_LIGHT0);	   glShadeModel(GL_SMOOTH);	   glEnable(GL_DEPTH_TEST);	   glEnable(GL_NORMALIZE);	   glClearColor(0.1f, 0.1f, 0.1f, 0.0f);	   glViewport(0, 0, 640, 480);	   cam.set(4, 0, 1);	   cam.setShape(30.0f, 64.0f/48.0f, 0.5f, 50.0f);       glutMainLoop();}`