Anonymous Anonymous - 16 days ago
112 0

the code snippets are in three different places, the camera class, and the update and render functions of Scene

C++

Camera Code

////////camera.cpp

#include "camera.h"
#include "Input.h"


void userCamera::update()
{
		float cosR, cosP, cosY;	//temp values for sin/cos from 
		float sinR, sinP, sinY;


		// Roll, Pitch and Yall are variables stored by the camera
		// handle rotation
		// Only want to calculate these values once, when rotation changes, not every frame. 
		cosY = cosf(yaw*3.1415 / 180);
		cosP = cosf(pitch*3.1415 / 180);
		cosR = cosf(roll*3.1415 / 180);
		sinY = sinf(yaw*3.1415 / 180);
		sinP = sinf(pitch*3.1415 / 180);
		sinR = sinf(roll*3.1415 / 180);

		//This using the parametric equation of a sphere

		// Calculate the three vectors to put into  glu Lookat
		// Look direction,  position and the up vector 
		// This function could also calculate the right vector

		forwardx = sinY * cosP;
		forwardy = sinP;
		forwardz = cosP * -cosY;

	
		// Look At Point
		// To calculate add Forward Vector to Camera position.	

		// Up Vector
		upx = -cosY * sinR - sinY * sinP * cosR;
		upy = cosP * cosR;
		upz = -sinY * sinR - sinP * cosR * -cosY;

		// Side Vector (right)
		// this is a cross product between the forward and up vector. 
		// If you don’t need to calculate this,  don’t do it. 
		
		rightx = (upy*forwardz) - (upz*forwardy);
		righty = (upz*forwardx) - (upx*forwardz);
		rightz = (upx*forwardy) - (upy*forwardx);

		lx = (posx + forwardx);
		ly = (posy + forwardy);
		lz = (posz + forwardz);


		//You now have forward and up

		//To move the way the camera is facing is to move the way the
		//Forward vector is facing, need to slow down

		//To control speed of movement multiply the forward 
		//vector by speed and delta time

		//To move up and down, move along the UP vector, not the y axis
		//To strafe left and right, use RIGHT vector (cross product of forward and up)

		//Rotation
		//X = Pitch
		//Y = Yaw
		//Z = Roll

		//Get user input, do in update and use dt to slow speed
		//Do seperate if statements so multiple key presses are ok
	
}

//Store position vector
	//Store orientation vector (pitch yaw roll)
	//Calculate forward from orientation (point in front)
	//Forward vector + position vector to get look at
	//Store UP (imagine string going through head)
	//Calculate RIght, should be comment saying how to do it
	//Set button presses to change the orientation and position vectors
	//Recalculate the cameras vectors every time theres a change

///////////////camera.h./////////////////////
#ifndef _userCamera_H_
#define _userCamera_H_


#include <math.h>
#include "Vector3.h"

class userCamera{
public:
	
	//update function calculates the vectors

	Vector3 positionVector(float posx, float posy, float posz);
	Vector3 rotationVector(float pitch, float yaw, float roll);
	Vector3 forwardVector(float forwardx, float forwardy, float forwardz);
	Vector3 UPVector(float upx, float upy, float upz);
	Vector3 RIGHTVector(float rightx, float righty, float rightz);

	float getposx(){ return posx; };
	float getposy(){ return posy; };
	float getposz(){ return posz; };

	float getlx(){ return lx; };
	float getly(){ return ly; };
	float getlz(){ return lz; };

	float getpitch(){ return pitch; };
	float getyaw(){ return yaw; };
	float getroll(){ return roll; };

	float getforwardx(){ return forwardx; };
	float getforwardy(){ return forwardy; };
	float getforwardz(){ return forwardz; };

	float getupx(){ return upx; };
	float getupy(){ return upy; };
	float getupz(){ return upz; };

	float getrightx(){ return rightx; };
	float getrighty(){ return righty; };
	float getrightz(){ return rightz; };


	void moveForward(){ 
		posx += forwardx * 0.3;
		posy += forwardy * 0.3;
		posz += forwardz * 0.3;
		//return posx;
	};

	void moveBack(){
		posx -= forwardx * 0.3;
		posy -= forwardy * 0.3;
		posz -= forwardz * 0.3;
		//return posx;
	};

	void moveLeft(){
		posx += rightx * 0.3;
		posy += righty * 0.3;
		posz += rightz * 0.3;
	};

	void moveRight(){
		posx -= rightx * 0.3;
		posy -= righty * 0.3;
		posz -= rightz * 0.3;
	};


	float updateYaw(float distance){
		yaw = yaw + (distance/40);
		return  yaw;
	};

	float updatePitch(float distance){
		pitch = pitch - (distance / 40);
		return  pitch;
	};

	float moveUp()
	{
		 posy = posy+0.1;
		 return posy;
	}
	float moveDown()
	{
		posy = posy-0.1;
		return posy;
	}
	
	void update();

private:

	float posx = 0;
	float posy = 1;
	float posz = 6;

	float pitch, yaw, roll = 0;
	float forwardx, forwardy, forwardz = 0;
	float lx, ly, lz = 0;
	float upx, upy, upz = 0;
	float rightx, righty, rightz = 0;
};

#endif


////////////////////////////////////In Scene:Update//////////////////////////////////////////////////////

	glutWarpPointer(width / 2, height / 2);

	//Movement on X-Z plane
	if (input->isKeyDown('w')){camera.moveForward();}
	if (input->isKeyDown('s')){camera.moveBack();}
	if (input->isKeyDown('a')){camera.moveLeft();}
	if (input->isKeyDown('d')){camera.moveRight();}

	//Vertical Movement
	if (input->isKeyDown('e')){camera.moveUp();}
	if (input->isKeyDown('q')){camera.moveDown();}

	//Yaw & Pitch
	camera.updateYaw((input->getMouseX() - width / 2));
	camera.updatePitch((input->getMouseY() - height / 2));

	camera.update();

In Scene::render


	gluLookAt(	camera.getposx(), camera.getposy(), camera.getposz(),
				camera.getlx(), camera.getly(), camera.getlz(),
				camera.getupx(), camera.getupy(), camera.getupz());