1 Replies - 789 Views - Last Post: 30 April 2009 - 12:52 AM

#1 deli10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-April 09

Camera Problem

Posted 29 April 2009 - 05:11 PM

hi, i attached my project. visual studio 2005 or 2008 should run it with no problem.
i am trying to follow my flying-bike when its flying (3rd person camera ) but i am not sure where to add that and how to do it
any help would be appreciated

well i could not attach neither a .rar or a .cpp so i am copy pasting the code here


#include <gl\glut.h>
#include <math.h>

GLfloat red[] = { 0.8f, 0.0, 0.0, 1.0 };
 GLfloat blue[] = { 0.0, 0.2f, 1.0, 1.0 };
 GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
 GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
 GLfloat polished[] = { 100.0 };
 GLfloat dull[] = { 0.0 };

GLfloat m_diff[] = { 0.8, 0.0, 1.0, 1.0 };		// Diffuse
GLfloat m_amb[] = { 0.5f, 0.5f, 0.5f, 1.0 };	// Ambient
GLfloat m_spec[] = { 0.0, 0.0, 0.0, 1.0 };		// Specular
GLfloat m_emit[] = { 0.0, 0.0, 0.0, 1.0 };		// Emission
GLfloat m_shine[] = { 0.4 };					// Shininess

void myDisplay();
void resize(int w, int h);
void myKeyboard(unsigned char key, int x , int y);
void Skimmer(bool fly);
void axis();
void DrawWheels();
void DrawRims();
void sky();
void land();
void Init();

void DrawDeck();
//Update scene for animaiton
void updateScene();
GLvoid window_idle();

//ADD
void inputKey(int key, int x, int y);
float angleX;
float angleY;
float radius;




//Add
/*
set projecttion as "perspective" or "ortho"
*/
bool perspective;
int firstperson=0;

float eyeZ;
float eyeX;
float eyeY;


GLint angle1;
GLint angle2;
GLint angle3;

//Add
GLfloat zoom;			// Camera zoom setting

//Add to control resize();
GLint win_width = 400;			// Window dimensions
GLint win_height = 300;

GLdouble left,right,bottom,top,near,far;
//This variable is used to control the wing of skimmer
//True means the skimmer is flying
bool fly;

//This variable is used to control wheel rotation
bool rotate;
//Wheel rotate angle
double wheelRotateAngle;

//This variable is used to control the animation of the Skimmer
bool animation;

//This variable is used to control the acceleration/deceleration of Skmmer
int accelerate;

int speed;


// Position the light at the origin.
const GLfloat light_pos[] = { 0, 10, 0, 1.0 };

// Attenuation factors for light.
GLfloat const_att = 0.1;

// Translation values for light.
GLfloat move_y =-5.0;
GLfloat move_z =-5.0;
GLfloat move_x =0.0;

bool light;


int main(int argc, char ** argv)
{
	// initialize the display window
	glutInit(&argc, argv);
	
 
	//Add
	/*
	Initial all values
	*/
	Init();

	

	// Request hidden surface elimination and register callbacks.
	glEnable(GL_DEPTH_TEST);

	//Glfloat global_ambient[] = {0.5f, 0.5f, 0.5f, 1.0f};
	

	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
	glutInitWindowSize(1200, 1200);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("Skimmer ");
	
	// Callback functions

	//Add
	glutSpecialFunc(inputKey);
	
	glutDisplayFunc(myDisplay);

	glutIdleFunc(&window_idle);

	glutReshapeFunc(resize);
	glutKeyboardFunc(myKeyboard);


	// opengl loop
	glutMainLoop();

	return 0;
}

void myDisplay()
{
	glClearColor(0.12, 0.45, 0.11, 1);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	
	
	eyeZ = radius * cos(angleX)*cos(angleY);
	eyeX = radius * sin(angleX)*cos(angleY);
	eyeY = radius * sin(angleY);
	gluLookAt(eyeX, eyeY, -1*eyeZ, 0, 0, 0, 0, 1, 0); 

	if (light)
	{
		glPushMatrix();
		glTranslatef(10,0,25);
		  // Initialize the light.
	
		glEnable(GL_LIGHTING);
		glEnable(GL_LIGHT0);
		glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, const_att);
		glLightModelfv(GL_LIGHT_MODEL_AMBIENT, m_amb);
		glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
		glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
		glLightfv(GL_LIGHT0, GL_SPECULAR, white);
	
		glPopMatrix();
	}

	else
	{
		glDisable(GL_LIGHT0);
		glDisable(GL_LIGHTING);
	
	}
	 // enable color tracking
	glEnable(GL_COLOR_MATERIAL);
	// set material properties which will be assigned by glColor
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
	
	glPushMatrix();

	sky();

	land();
	
	//glPushMatrix();

	//These 3 statements place the skimmer on the path
	glTranslatef(5,-5,10);
	glScalef(0.1,0.1,0.1);
	glRotatef(-90,0,1,0);
	
	//This statement control moving forward/backward along the path

	if (speed <= 210)
	{	
		glTranslatef(-speed,0,0);
		wheelRotateAngle+=10.0f;
	}

	else { 
		wheelRotateAngle=0.0;
		//glTranslatef(-15,0,0);

		glTranslatef(-100+speed*-0.5, speed/20, 0);


	
	}
	
	{Skimmer(fly);}
	//glTranslatef(-speed,0,0);
	Skimmer(fly);	
	glPopMatrix();

	glutSwapBuffers();
}

GLvoid window_idle()
{
  updateScene();

  glutPostRedisplay(); // redraw the window at the next possible moment
}

void updateScene()
{

  //wheelRotateAngle+=10.0f;

 
 if(accelerate>0)
 {
	// wheelRotateAngle+=1.0f;
	  speed+=accelerate/2;
 }
 else
	 accelerate=0;

}
void DrawShaft()					  // bike cylinder shafts that connects the wheel to the steering.

{
	glColor3f(0.75, 0.75, 0.75);
	GLUquadricObj * qobj;
	qobj = gluNewQuadric();
	gluQuadricDrawStyle(qobj,GLU_FILL);
	gluCylinder(qobj, 0.15, 0.20, 3.0, 80,80);

}

void Track()					  // bike track start and end

{
	//glColor3f(0.2, 0.2, 1.0);
	glColor3f(0.0f,0.0f,1.0f);
	GLUquadricObj * qobj;
	qobj = gluNewQuadric();
	gluQuadricDrawStyle(qobj,GLU_FILL);
	gluPartialDisk(qobj, 0.0, 10, 50, 4,0, 180);

}
void DrawRims() 
{	
	glColor3f(0.75, 0, 0.2);
	//glutSolidTorus(0.2, 0.5, 100 ,100);
	glScalef(0.3,2.5,0.1);
	glutSolidCube(0.8);
	glRotatef(45,0,0,1);
	glutSolidCube(1);
}

void DrawWheels() 
{	
	glColor3f(0, 0, 0);
	
	
	if (rotate)
	{
	   glRotatef(wheelRotateAngle,0,0,1);
	}
	glutSolidTorus(0.9, 1.0, 100 ,100);
	DrawRims();
	
}



void DrawGasTank()
{
	glColor3f(0.0, 0.0, 0.41);
	glutSolidSphere(0.4, 20.0, 100.0);
}

void TankLock()
{
	glColor3f(0.55, 0.55, 0.55);
	
	glutSolidTorus(0.1, 0.15, 100 ,100);
}

void DrawAxe()

{
	glColor3f(0.00, 0.00, 0.00);   // cylinder which is connected to the wheel and shaft to tighten the shaft to the wheel.
	GLUquadricObj * qobj;
	qobj = gluNewQuadric();
	gluQuadricDrawStyle(qobj,GLU_FILL);
	gluCylinder(qobj, 0.4, 0.4, 1.5, 80,80);

}



	
void DrawSteering()					  // bike cylinder shafts that connects the wheel to the steering.

{
	glColor3f(0.65, 0.65, 0.65);
	GLUquadricObj * qobj;
	qobj = gluNewQuadric();
	gluQuadricDrawStyle(qobj,GLU_FILL);
	gluCylinder(qobj, 0.15, 0.15, 4.0, 80,80);

}

void DrawWing() 
{
	glColor3f(0.6,0.0, 0.0);
	glScalef(1.5, 1.5, 0.2);
	glutSolidCube(1.0);

}


void DrawEngine()

{
	glColor3f(0.25, 0.50, 0.50);   // cylinder for the jet engine
	GLUquadricObj * qobj;
	qobj = gluNewQuadric();
	gluQuadricDrawStyle(qobj,GLU_FILL);
	gluCylinder(qobj, 0.3, 0.2, 1.5, 80,80);

}

void Skimmer(bool fly)
{

	//Add
	//draw a temp main body
	glPushMatrix();
	glColor3f(0, 0, 1);
	glScalef(1,0.2,0.3);
	glutSolidCube(6);
	glPopMatrix();
	// draw chair

	glPushMatrix();
	glColor3f(0.5, 0.25, 0.25);
	glTranslatef (-1.4, 0.8, 0.0);
	glRotatef (0.0, 0.0, 0.0, 1.0);
	glTranslatef (1.0, 0.0, 0.0);
	
	glPushMatrix();
	glScalef (2.0, 0.4, 1.0);
	glutSolidCube (1.0);
	glPopMatrix();

	glTranslatef (1.0, 0.0, 0.0);
	glRotatef (65.0, 0.0, 0.0, 1.0);
	glTranslatef (1.0, 0.0, 0.0);
	
	glPushMatrix();
	glScalef (2.0, 0.4, 1.0);
	glutSolidCube (1.0);
	glPopMatrix();
	
	glPopMatrix();


	// draw front wheel & front Rims with the axe.
	glPushMatrix();
	glTranslatef(-4, 0, 0);
	DrawWheels();
	glPopMatrix();
	
	glPushMatrix();
	// back wheel & rims with the axe.
	glTranslatef(4, 0, 0);
	DrawWheels();
	glPopMatrix();

	
	glPushMatrix();
	glTranslatef(-4, 0.0, -0.5);
	glRotatef (90.0, 0.0, 1.0, 0.0);
	glRotatef (-60.0, 1.0, 0.0, 0.0);
	DrawShaft();	// outer front shaft to connect the steering to the wheel
	glPopMatrix();
	
	
	glPushMatrix();	
	glTranslatef(-4, 0.0, 0.5);
	glRotatef (90.0, 0.0, 1.0, 0.0);
	glRotatef (-60.0, 1.0, 0.0, 0.0);
	DrawShaft();	// inner front shaft to connect the steering to the wheel
	glPopMatrix();
	

	glPushMatrix();
	glTranslatef(-2.0, 0.9, 0.0);
	glScalef(1.4, 1.0,1.0);
	DrawGasTank();
	glPopMatrix();
	
	glPushMatrix();
	glTranslatef(-2.0, 1.1, 0.0);			
	glScalef(0.4, 0.4,0.4);
	TankLock();
	glPopMatrix();
	
	glPushMatrix();
	glTranslatef(-2.9,1.8,-0.15);
	glScalef(0.2, 0.8,0.9);						// modeling windscreen
	glutWireCube(1.3);
	glPushMatrix();
	glTranslatef(0.0, 0.6, -1.9);
	DrawSteering();					/// drawing steering
	glPopMatrix();
	glPopMatrix();
	
	glPushMatrix();
	//Add
	
	glTranslatef(-2, 1, -1.0);
	if (fly) 
	{
	
	glScalef(0.5, 6, 0.5);
	glRotatef(75, -1.0, 0.0, 0.0);
	}	
	
	
	DrawWing();	// draw right wing
	glPopMatrix();

	glPushMatrix();
	//Add
	 glTranslatef(-2.0, 1, 1.0);
	if (fly) 
	{ 
   
	glScalef(0.5, 6, 0.5);
	glRotatef(-75, -1.0, 0.0, 0.0);
	}
	else 
		glTranslatef(-2.0, 0, 1.0);
	
	DrawWing();	// draw left wing
	glPopMatrix();


	glPushMatrix();
	glTranslatef(2.0, 0.0, -1.0);
	glRotatef(90.0, 0.0, 1.0, 0.0);
	DrawEngine();	// draw right Jet Engine
	glPopMatrix();

	glPushMatrix();
	glTranslatef(2.0, 0.0, 1.0);
	glRotatef(90.0, 0.0, 1.0, 0.0);
	DrawEngine();	// draw left  Jet Engine
	glPopMatrix();
	
	glPopMatrix();
}
	//axis();
	
	


void sky()
{
	glBegin(GL_POLYGON);
		// x axis
		glColor3f(0.3, 0.4, 0.9);
		glVertex3f(60, 0, 60);
		glVertex3f(60, 0, -60);
		glVertex3f(-60, 0, -60);
		glVertex3f(-60, 0, 60);
	glEnd();
}

void land()
{	glPushMatrix();
	
	glTranslatef(30,-10,20);
	glRotatef(-90.0,1,0,0);
	glRotatef(90,0,0,-1);
	Track();
	glPopMatrix();

	glPushMatrix();
	glTranslatef(30,-10,0);
  


	//Track();
	for (int i=-10;i<10;i++)
		for(int j=-20;j<20;j++)
		{
			glBegin(GL_TRIANGLES);	
			glColor3f(0.6f,0.8f,0.7f);
			glVertex3f(0.0f+i,0.0f,1.0f+j);
			glVertex3f(0.0f+i,0.0f,0.0f+j);
			glVertex3f(1.0f+i,0.0f,0.0f+j);
			
			glColor3f(0.0f,0.0f,1.0f);
			glVertex3f(0.0f+i,0.0f,1.0f+j);
			glVertex3f( 1.0f+i,0.0f,0.0f+j);
			glVertex3f( 1.0f+i,0.0f,1.0f+j);
			glEnd();
		
		}
	
		glPopMatrix();

	glPushMatrix();
	glTranslatef(30,-10,-20);
	glRotatef(-90.0,1,0,0);
	glRotatef(90,0,0,1);
	Track();
	glPopMatrix();
	glPushMatrix();
	glTranslatef(30,-10,0);
	//glRotatef(-90.0,1,0,0);
	glRotatef(90,0,1,0);
	glColor3fv(black);
	glRotatef(90,0,0,1);
	glRectf(-0,0,2,24);
	glTranslatef(0,-4,0);
	glRectf(-0,0,2,-20);
	glPopMatrix();

	glPopMatrix();
}

void axis()
{
	glBegin(GL_LINES);
		// x axis
		glColor3f(1, 0, 0);
		glVertex3f(0, 0, 0);
		glVertex3f(5, 0, 0);

		// y axis
		glColor3f(0, 1, 0);
		glVertex3f(0, 0, 0);
		glVertex3f(0, 5, 0);

		// z axis
		glColor3f(0, 0, 1);
		glVertex3f(0, 0, 0);
		glVertex3f(0, 0, 5);

	glEnd();
}


//Add 
/*
Put every initial value to this function
*/
void Init(){

angleX =-1.6;
angleY = -0.15;
radius = 10;

zoom=90.0;


perspective=true;

eyeZ = 0;
eyeX = 0;
eyeY = 0;

angle1 = 0;
angle2 = 0;
angle3 = 0;


float firstpersonX = -3.0;
float firstpersonY = -3.0;
float firstpersonZ =  0.0;

float firstpersonViewX = -3.0;
float firstpersonViewY = -3.0;
float firstpersonViewZ =  0.0;


//parameter for ortho
left=-10;
right=10;
bottom=-10;
top=10;
near=1;
far=500;


//Control Skimmer wings
fly=true;
rotate=false;
accelerate=0;
animation=false;

//acceleration
speed=0;

//Lighting
glEnable (GL_DEPTH_TEST);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);


}

//Add
void set_ortho(){

	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();							// Reset The Projection Matrix
	glOrtho(left,right,bottom,top,near,far);				
}

//Add 
void set_projection () {
	// Reset the projection when zoom setting or window shape changes.
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	gluPerspective(zoom, GLfloat(win_width) / GLfloat(win_height), 
		1.0, 500.0);
}
//Add resize windows
void resize (GLint new_width, GLint new_height) {
	// Window reshaping function.
	win_width = new_width;
	win_height = new_height;
	glViewport(0, 0, win_width, win_height);
	
	//Switch between perspective or othor
	if(perspective)
		set_projection();
	else
		set_ortho();
}



void myKeyboard(unsigned char key, int x, int y)
{																			//keyboard input to exit program
	if (key == 27) 
		exit(0);

	switch (key)
	{

	case 'q' : 
		axis(); 
		break;
	//Add
	case 'o':
	case'O':																		////keyboard input to activate ortho view 				
		perspective=false;
		set_ortho();
		break;
	//Add
	case 'p':																	//keyboard input to activate prespective view
	case 'P':
		perspective=true;
		set_projection();
		break;	

	//Add for Zoom in																// //keyboard input to zoom in and out
	case 'z':
		if(perspective==true)
		{																					
		zoom += 1.0f;
		set_projection();
		}
		else
		{
		 left-=1.00;
		 right+=1.00;
		 bottom-=1.00;
		 top+=1.00;
		//near+=0.2;
		//far+=0.2f;
		set_ortho();
		}
		break;
	//Add for zoom out   
	case 'Z':
		if(perspective==true)
		{
		zoom -= 1.00f;
		set_projection();
		}
		else
		{
		 left+=1.00;
		 right-=1.00;
		 bottom+=1.00;
		 top-=1.00;
		 //near-=0.2f;
		 //far-=0.2f;
		 set_ortho();
		}
		break;

	
	case 'c':
	case 'C':																////keyboard input to reset view
		Init();
		set_projection();
		break;

	
	//reduce the distance between camera and object by changing the "radius" of camera
	case 'f':
	case 'F':
		
		radius-=1.00;
		break;
	
	///increase the distance between camera and object by changing the "radius" of camera
	case 'b':
	case 'B':
	
		radius+=1.00;
		break;
	
	
	//start skimmer animation
	case 's':{
		animation=true;
		break;
			 }
	//Stop skimmer animation
	case 'S':{
		animation=false;
		break;
			 }
	//Accelerate 
	case 'a':{
		if (accelerate <6)
		accelerate+=2;
		rotate=true;
		break;
			 }
	//Decelerate
	case 'A':{
		accelerate-=2;
		rotate=true;
		break;
			 }
	 //Stop
	case't':
	case'T':{
		accelerate=0;
		rotate=false;
		break;
			}
	case '1':{
	firstperson=1;
		break;
			 }
	case '3': {
		firstperson=0;
		break;
			  }
   



			
   }
	//glutPostRedisplay();
	myDisplay();

}


//ADD special Keys
void inputKey(int key, int x, int y)
{
	switch (key) {
		case GLUT_KEY_LEFT : 
			angleX -= 0.05f;
			break;
		case GLUT_KEY_RIGHT : 
			angleX +=0.05f;
			break;
		case GLUT_KEY_UP : 
			angleY +=0.05f;
			
			break;
		case GLUT_KEY_DOWN : 
			angleY -=0.05f;
			
			break;

		case GLUT_KEY_F1 :
			light=!light; 
				 break;
		//case GLUT_KEY_F2 :
	}
	glutPostRedisplay();


}



Is This A Good Question/Topic? 0
  • +

Replies To: Camera Problem

#2 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Re: Camera Problem

Posted 30 April 2009 - 12:52 AM

This could better suit in VB.NET and regardless of you giving your code, it doesnt show where you have started that part of the code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1