Skip to main content
gamedev.net
🔒 Locked

help with camera

Started by LuminusEnlightened Jul 18, 2003 at 10:58 AM 2 replies 400+ views
Original Post
LuminusEnlightened
LuminusEnlightened
i am having some trouble with learning how to control the camera in opengl tryed couple of different methods but none of them seemed to work, can anyone tell me why this isnt working?(and hopefully help in how to fix it)
class CCamera{
public:
	Matrix	m_Position,
		m_ViewDirection,
		m_View;

	float	Velocity;
			//Velocity

	CCamera();
	~CCamera(){}
	Matrix MatrixVectorLength(Matrix,GLfloat);
	void LookAt();
	void MoveForward(GLfloat);
	void rotatex	(float degree);		// rotate around the x axis

	void rotatey	(float degree);		// rotate around the y axis

	void rotatez	(float degree);		// rotate around the z axis

	void rotatexyz	(float degree,float x,float y,float z);// rotate around the x, y, and z axis

};

void CCamera::rotatex(float degree)
{
	glPushMatrix();
	glLoadMatrixf(m_ViewDirection.m_Matrix);
	glRotatef( degree, 1.0f, 0.0f, 0.0f);
	glGetFloatv(GL_MODELVIEW_MATRIX,m_Position.m_Matrix);
	glPopMatrix();


}

void CCamera::rotatey(float degree)
{
	glPushMatrix();
	glLoadMatrixf(m_ViewDirection.m_Matrix);
	glRotatef( degree, 0.0f, 1.0f, 0.0f);
	glGetFloatv(GL_MODELVIEW_MATRIX,m_Position.m_Matrix);
	glPopMatrix();

}

void CCamera::rotatez(float degree)
{
	glPushMatrix();
	glLoadIdentity();
	glLoadMatrixf(m_ViewDirection.m_Matrix);
	glRotatef( degree, 0.0f, 0.0f, 1.0f);
	glGetFloatv(GL_MODELVIEW_MATRIX,m_Position.m_Matrix);
	glPopMatrix();

}

void CCamera::rotatexyz(float degree,float x,float y,float z)
{
	glPushMatrix();
	glLoadIdentity();
	glLoadMatrixf(m_ViewDirection.m_Matrix);
	glRotatef( degree, x, y, z);
	glGetFloatv(GL_MODELVIEW_MATRIX,m_Position.m_Matrix);
	glPopMatrix();

}
--------------------chown -R us /bases
Trienco
Trienco
erm... when did things like a position or direction become a matrix?

"how to control the camera in opengl"

there is no camera unless you write your own (which you seem to try).

"can anyone tell me why this isnt working?"

no, because i have no clue what isnt working if i only look at half the code (which is extremely redundant and i still wonder why bother loading the identity if its going to be overwritten with a matrix the next line).

also i cant see what youre trying to achieve by loading and changing one matrix and writing the result to another matrix, especially since their names dont make much sense. you need exactly one matrix, no more. except you want to keep the inverted version around, but then its still not more than two.

it would probably help to comment that code and mention what youre trying to achieve.
f@dzhttp://festini.device-zero.de
Geocyte
Geocyte

As Trienco has pointed out it is difficult to tell you exactly what is wrong when you haven''t said exactly what the problem is (just that there is one) or provided all of the relevant code - for example, where is the camera actually applied to gl? Trienco was a little rude, however, and has earned an entry in my Book of Naughty Monkeys.

Personally, I reckon I can fix your problem anyway without even knowing what it is.

Trienco is right to question your use of multiple matrices as it is both unneccessary and confusing. You only need one matrix. Rotate it similarly to the way you have and perhaps make similar translate functions to effect local translation ( following examples use a matrix class containing an array of 16 floats called m[] )...


void CCamera::rotatex(float degree)
{
glPushMatrix();
glLoadMatrixf(camera_matrix.m);
glRotatef( degree, 1.0f, 0.0f, 0.0f);
glGetFloatv(GL_MODELVIEW_MATRIX,camera_matrix.m); glPopMatrix();
}

void CCamera::translatex(float distance)
{
glPushMatrix();
glLoadMatrixf(camera_matrix.m);
glTranslatef( distance, 0.0f, 0.0f);
glGetFloatv(GL_MODELVIEW_MATRIX,camera_matrix.m); glPopMatrix();
}


when it comes to applying the camera to gl I personally recommend that you forget all that inverting the matrix stuff because it only works if you stay completely consistent with gl''s coordinate space. I don''t because I want positive z to be forwards, not backwards like gl has it. Other people I know also like to invert the x-axis. In any case if you use gluLookAt to apply your camera these potential problems disappear. Just get the cameras matrix and work out the vectors to pass to gluLookAt() like so...


vector3 eye_pos, target_pos, up_vec;

eye_pos.v[0] = camera_matrix.m[12];
eye_pos.v[1] = camera_matrix.m[13];
eye_pos.v[2] = camera_matrix.m[14];

target_pos.v[0] = camera_matrix.m[8] + camera_matrix.m[12];
target_pos.v[1] = camera_matrix.m[9] + camera_matrix.m[13];
target_pos.v[2] = camera_matrix.m[10] + camera_matrix.m[14];

up_vec.v[0] = camera_matrix.m[4];
up_vec.v[1] = camera_matrix.m[5];
up_vec.v[2] = camera_matrix.m[6];

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( eye_pos.v[0],eye_pos.v[1],eye_pos.v[2],
target_pos.v[0],target_pos.v[1],target_pos.v[2],
up_vec.v[0],up_vec.v[1],up_vec.v[2] );


In future when you get unexpected results try running tests to discover exactly how your programs behaviour deviates from your expectations. This will help you find approximately where the problem is and even if you can''t work it out from there at least when you ask for help you can tell us more about the problem and post only the relevant section of the code instead of posting loads of code which may not even be relevant. Anyway, I hope this helps. Happy hacking




Geocyte Has Committed Suicide.
Geocyte Has Committed Suicide.
Trienco
Trienco
what i dont like about glulookat in this case is that you have to build the three vectors from your matrix to pass them to lookat, which will then reconstruct the matrix you already had and invert it. though it will still be less code.

so to just copy from my cam (pretend Transform is the camera matrix):

void CameraGL::setView() {
glMatrixMode(GL_MODELVIEW);
float viewmatrix[16]={
//Remove the three - for non-inverted z-axis

Transform[0], Transform[4], -Transform[8], 0,
Transform[1], Transform[5], -Transform[9], 0,
Transform[2], Transform[6], -Transform[10], 0,

-(Transform[0]*Transform[12] + Transform[1]*Transform[13] + Transform[2]*Transform[14]),

-(Transform[4]*Transform[12] + Transform[5]*Transform[13] + Transform[6]*Transform[14]),

//add a - like above for non-inverted z-axis

(Transform[8]*Transform[12] + Transform[9]*Transform[13] + Transform[10]*Transform[14]), 1};

glLoadMatrixf(viewmatrix);
f@dzhttp://festini.device-zero.de

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.