Original Post
Ok, I hope this is the correct place to post this question. I've done a decent amount of searching and havn't been able to identify what my problem is and the many different solutions/issues I've read about online don't seem to apply, or maybe I just don't understand the problem well enough to see the solutions.
So I have objects and each object has it's own 4x4 matrix I use to keep track of it's current translation and orientation.
I compute all my transformations using matrices. So say I have a cube on the screen and I want to rotate it 90 degrees I do the following:
[source lang="cpp"]
void Transform::RotateLocalY(float radians)
{
matrix4x4 curRot;
curRot.m11 = _transform.m11;
curRot.m21 = _transform.m21;
curRot.m31 = _transform.m31;
curRot.m12 = _transform.m12;
curRot.m22 = _transform.m22;
curRot.m32 = _transform.m32;
curRot.m13 = _transform.m13;
curRot.m23 = _transform.m23;
curRot.m33 = _transform.m33;
matrix4x4 rotation = RotationMatrixY(radians);
rotation = rotation * curRot;
vector3 trans = GetTranslation();
_transform = rotation;
_transform.m14 = trans.x;
_transform.m24 = trans.y;
_transform.m34 = trans.z;
}[/source]
I'm confident in my math in so far that everything seems to work.
I can create a cube rotate it freely along all axis and move it 'forward' along either of it's axis. Everything looks fine and great.
The problem comes in when I try to do the same thing to my camera. My camera inherits from a transform, so it has it's own _transform matrix it uses and I believe I should be able to do all the same types of transformations.
Each time I render an object, I set a uniform on my shader which is supposed to be combination of the camera's view matrix and the objects current transformation matrix.
[source lang="cpp"]modelViewMatrix = modelViewMatrix * renderable->GetTransformMatrix();[/source]
This value changes in the shader for each object being rendered.
All of this appears to work great like I said, until I start rotating my camera. Camera translation appears to be fine too.
Here is how I compute my camera's view matrix from it's Transformation matrix (being manipulated exactly like any object in the scene)
[source lang="cpp"] matrix4x4 view;
view.m11 = _transform.m11;
view.m12 = _transform.m21;
view.m13 = _transform.m31;
view.m14 = 0.0f;
view.m21 = _transform.m12;
view.m22 = _transform.m22;
view.m23 = _transform.m32;
view.m24 = 0.0f;
view.m31 = _transform.m13;
view.m32 = _transform.m23;
view.m33 = _transform.m33;
view.m43 = 0.0f;
view.m14 = -_transform.m14;
view.m24 = -_transform.m24;
view.m34 = -_transform.m34;
view.m44 = 1.0f;
return view;[/source]
Basically, I transpose the 3x3 orientation portion of the Transform and set a negative translation (not transposed). Also, all my matrices are represented in column order, including the variable names of each place. So m32 means column 3 row 2.
What's not working, exactly is when I rotate it appears that all the objects in the scene rotate around 0,0,0. So If i translate my camera way far out and start rotating my camera, I can see the cube rotating around the center of the scene. Up close it's hard to tell and even appears to be correct.
After going through numerous resources, I can't tell what I'm doing wrong. I don't understand why my translation appears to work but rotations don't.
I have a feeling it might be me confusing something between what I've read and how I implement my matrices, but everything except this one thing seems to work and I'm not sure I understand enough to determine why or what is wrong.
Any help or hints in the right direction would be much appreciated. If you'd like and have visual studio 10 you can download the source and run to see what I mean from:
https://github.com/mitchellharvey/CSpace
(If you do, be sure to set your working directory in project settings to $(OutDir) and set startup project to the test project)
So I have objects and each object has it's own 4x4 matrix I use to keep track of it's current translation and orientation.
I compute all my transformations using matrices. So say I have a cube on the screen and I want to rotate it 90 degrees I do the following:
[source lang="cpp"]
void Transform::RotateLocalY(float radians)
{
matrix4x4 curRot;
curRot.m11 = _transform.m11;
curRot.m21 = _transform.m21;
curRot.m31 = _transform.m31;
curRot.m12 = _transform.m12;
curRot.m22 = _transform.m22;
curRot.m32 = _transform.m32;
curRot.m13 = _transform.m13;
curRot.m23 = _transform.m23;
curRot.m33 = _transform.m33;
matrix4x4 rotation = RotationMatrixY(radians);
rotation = rotation * curRot;
vector3 trans = GetTranslation();
_transform = rotation;
_transform.m14 = trans.x;
_transform.m24 = trans.y;
_transform.m34 = trans.z;
}[/source]
I'm confident in my math in so far that everything seems to work.
I can create a cube rotate it freely along all axis and move it 'forward' along either of it's axis. Everything looks fine and great.
The problem comes in when I try to do the same thing to my camera. My camera inherits from a transform, so it has it's own _transform matrix it uses and I believe I should be able to do all the same types of transformations.
Each time I render an object, I set a uniform on my shader which is supposed to be combination of the camera's view matrix and the objects current transformation matrix.
[source lang="cpp"]modelViewMatrix = modelViewMatrix * renderable->GetTransformMatrix();[/source]
This value changes in the shader for each object being rendered.
All of this appears to work great like I said, until I start rotating my camera. Camera translation appears to be fine too.
Here is how I compute my camera's view matrix from it's Transformation matrix (being manipulated exactly like any object in the scene)
[source lang="cpp"] matrix4x4 view;
view.m11 = _transform.m11;
view.m12 = _transform.m21;
view.m13 = _transform.m31;
view.m14 = 0.0f;
view.m21 = _transform.m12;
view.m22 = _transform.m22;
view.m23 = _transform.m32;
view.m24 = 0.0f;
view.m31 = _transform.m13;
view.m32 = _transform.m23;
view.m33 = _transform.m33;
view.m43 = 0.0f;
view.m14 = -_transform.m14;
view.m24 = -_transform.m24;
view.m34 = -_transform.m34;
view.m44 = 1.0f;
return view;[/source]
Basically, I transpose the 3x3 orientation portion of the Transform and set a negative translation (not transposed). Also, all my matrices are represented in column order, including the variable names of each place. So m32 means column 3 row 2.
What's not working, exactly is when I rotate it appears that all the objects in the scene rotate around 0,0,0. So If i translate my camera way far out and start rotating my camera, I can see the cube rotating around the center of the scene. Up close it's hard to tell and even appears to be correct.
After going through numerous resources, I can't tell what I'm doing wrong. I don't understand why my translation appears to work but rotations don't.
I have a feeling it might be me confusing something between what I've read and how I implement my matrices, but everything except this one thing seems to work and I'm not sure I understand enough to determine why or what is wrong.
Any help or hints in the right direction would be much appreciated. If you'd like and have visual studio 10 you can download the source and run to see what I mean from:
https://github.com/mitchellharvey/CSpace
(If you do, be sure to set your working directory in project settings to $(OutDir) and set startup project to the test project)