Sign in to follow this  

My Camera class returning wierd values...

This topic is 4753 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, here is my camera class (it's based on the class from gametutorials.com:
////////////////////////////////////////////////////////////
// This file is a part of Narcotik Engine Colin Hill 2004 //
////////////////////////////////////////////////////////////
// CCamera.h                                              //
////////////////////////////////////////////////////////////
// The camera has functions and such that you will need   //
// for camera-like situations... totaly                   //
////////////////////////////////////////////////////////////

#ifndef __CCamera_h__
#define __CCamera_h__

//! DLL-Interface
#ifndef NARCOTIK_API
#define NARCOTIK_API __declspec(dllexport)
#endif

//! Windows Includes
#include <windows.h>	
#include <math.h>	

//! OpenGL Includes
#include <gl/gl.h>
#include <gl/glu.h>
#pragma comment(lib, "OpenGL32.lib")
#pragma comment(lib, "GLu32.lib")

//! Internal Includes
#include "CRenderer.h"
#include "vector3d.h"
#include "dimension2d.h"

namespace Narcotik {

	class NARCOTIK_API CCamera {
	public:
		//! Constructor and Deconstructor
		CCamera();
		~CCamera();

		//! Public Functions
		void render(dimension2di nScreenSize);
		void moveForward(float fAmount);
		void setViewByMouse(dimension2di nScreenSize);
		void rotateView(float fAngle, float fXAxis, float fYAxis, float fZAxis);
		void setTarget(vector3df vNewTarget);
		void setUp(vector3df vNewUp);
		void setPosition(vector3df vNewPos);
		void setFov(float fDegrees);
		void setFarClip(float fDistance);
		void setNearClip(float fDistance);

		//! Accessors
		vector3df getPosition() { return m_vPos; }
		vector3df getTarget() { return m_vTarget; }
		vector3df getUp() { return m_vUp; }
		float getFov() { return m_fFov; }
		float getFarClip() { return m_fFarClip; }
		float getNearClip() { return m_fNearClip; }

	private:
		//! Private Members
		float m_fFov;
		float m_fFarClip;
		float m_fNearClip;

		vector3df m_vPos;
		vector3df m_vTarget;
		vector3df m_vUp;
	};

} // End namespace Narcotik

#endif
////////////////////////////////////////////////////////////
// This file is a part of Narcotik Engine Colin Hill 2004 //
////////////////////////////////////////////////////////////
// CCamera.cpp                                            //
////////////////////////////////////////////////////////////

#include "CCamera.h"

namespace Narcotik {
	////////////////////////////////
	// Constructor and Deconstructor
	////////////////////////////////
	CCamera::CCamera() {
		//! Default values
		m_fFov = 90.0f; 
		m_fFarClip = 4000.0f;
		m_fNearClip = 0.01f;
		m_vUp = vector3df(0.0f, 1.0f, 0.0f);
		m_vTarget = vector3df(0.0f, 0.0f, 1.0f);
		m_vPos = vector3df(0.0f, 0.0f, 0.0f);
	}

	CCamera::~CCamera() {
	}

	///////////////////
	// Public Functions
	///////////////////
	void CCamera::render(dimension2di nScreenSize) {
		glMatrixMode(GL_PROJECTION);						
		glLoadIdentity();									

		gluPerspective(m_fFov, nScreenSize.width/nScreenSize.height, m_fNearClip, m_fFarClip);

		glMatrixMode(GL_MODELVIEW);							
		glLoadIdentity();

		gluLookAt(m_vPos.x, m_vPos.y, m_vPos.z, m_vTarget.x, m_vTarget.y, m_vTarget.z, m_vUp.x, m_vUp.y, m_vUp.z);
	}

	void CCamera::moveForward(float fAmount) {
		vector3df vVector = m_vTarget - m_vPos;
		vVector.normalize();

		m_vPos.x += vVector.x * fAmount;		
		m_vPos.y += vVector.y * fAmount;		
		m_vPos.z += vVector.z * fAmount;		
		m_vTarget.x += vVector.x * fAmount;		
		m_vTarget.y += vVector.y * fAmount;			
		m_vTarget.z += vVector.z * fAmount;		
	}

	void CCamera::setViewByMouse(dimension2di nScreenSize) {
		POINT mousePos;									
		int middleX = nScreenSize.width >> 1;			
		int middleY = nScreenSize.height >> 1;				
		float angleY = 0.0f;							
		float angleZ = 0.0f;							
		static float currentRotX = 0.0f;
		
		GetCursorPos(&mousePos);						
		
		if((mousePos.x == middleX) && (mousePos.y == middleY)) return;

		SetCursorPos(middleX, middleY);							

		angleY = (float)( (middleX - mousePos.x) ) / 1000.0f;		
		angleZ = (float)( (middleY - mousePos.y) ) / 1000.0f;		

		currentRotX -= angleZ;  

		if(currentRotX > 1.0f) {
			currentRotX = 1.0f;
		}
		else if(currentRotX < -1.0f) {
			currentRotX = -1.0f;
		}
		else {
			vector3df vAxis = (m_vTarget - m_vPos).crossProduct(m_vUp);
			vAxis.normalize();

			rotateView(angleZ, vAxis.x, vAxis.y, vAxis.z);
			rotateView(angleY, 0, 1, 0);
		}
	}

	void CCamera::rotateView(float fAngle, float fXAxis, float fYAxis, float fZAxis) {
		vector3df vNewTarget;
		vector3df vTarget = m_vTarget - m_vPos;

		float cosTheta = (float)cos(fAngle);
		float sinTheta = (float)sin(fAngle);

		//! Find the new x position for the new rotated point
		vNewTarget.x  = (cosTheta + (1 - cosTheta) * fXAxis * fXAxis)		    * vTarget.x;
		vNewTarget.x += ((1 - cosTheta) * fXAxis * fYAxis - fZAxis * sinTheta)	* vTarget.y;
		vNewTarget.x += ((1 - cosTheta) * fXAxis * fZAxis + fYAxis * sinTheta)	* vTarget.z;

		//! Find the new y position for the new rotated point
		vNewTarget.y  = ((1 - cosTheta) * fXAxis * fYAxis + fZAxis * sinTheta)	* vTarget.x;
		vNewTarget.y += (cosTheta + (1 - cosTheta) * fYAxis * fYAxis)		    * vTarget.y;
		vNewTarget.y += ((1 - cosTheta) * fYAxis * fZAxis - fXAxis * sinTheta)	* vTarget.z;

		//! Find the new z position for the new rotated point
		vNewTarget.z  = ((1 - cosTheta) * fXAxis * fZAxis - fYAxis * sinTheta)	* vTarget.x;
		vNewTarget.z += ((1 - cosTheta) * fYAxis * fZAxis + fXAxis * sinTheta)	* vTarget.y;
		vNewTarget.z += (cosTheta + (1 - cosTheta) * fZAxis * fZAxis)		    * vTarget.z;

		m_vTarget = m_vPos + vNewTarget;
		m_vPos = m_vPos;
	}

	////////////
	// Accessors
	////////////
	void CCamera::setTarget(vector3df vNewTarget) {
		m_vTarget = vNewTarget;
	}

	void CCamera::setUp(vector3df vNewUp) {
		m_vUp = vNewUp;
	}

	void CCamera::setPosition(vector3df vNewPos) {
		m_vPos = vNewPos;
	}

	void CCamera::setFov(float fDegrees) {
		m_fFov = fDegrees;
	}

	void CCamera::setFarClip(float fDistance) {
		m_fFarClip = fDistance;
	}

	void CCamera::setNearClip(float fDistance) {
		m_fNearClip = fDistance;
	}

} // End namespace Narcotik


My function that returns the target vector returns a correct value, but the function that returns the position, all of the values are the x value of the camera's position...... any idea's on what is wrong?

Share this post


Link to post
Share on other sites
I don't have time to really look through that right now, but this looks suspicious:
void CCamera::rotateView(float fAngle, float fXAxis, float fYAxis, float fZAxis) {
// snip
m_vPos = m_vPos;
}

Also your design could probably be improved. Add functionality to your vector class so that e.g. this:

m_vPos.x += vVector.x * fAmount;
m_vPos.y += vVector.y * fAmount;
m_vPos.z += vVector.z * fAmount;


becomes

m_vPos += vVector * fAmount;

Also consider moving functionality into the Camera class instead of providing getters, i.e.:

camera.setView();

instead of e.g.:

gluLookAt(camera.getPosition(), camera.getTarget(), camera.getUp());

(Yes, I realise that wouldn't actually compile, it's just an example).

Finally, can I point you at this article on Hungarian notation.

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by colinisinhere
meh, my bad......

I just cleaned out the project and recompiled and now it works fine....

Theres a good lesson


Kinda like the 1st solution for (over the phone) hardware troubleshooting: Can you unplug it, and turn that back on for me please? :)

Share this post


Link to post
Share on other sites

This topic is 4753 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this