firstly thank you for looking at my post, I will try to be as detailed as I can to explain my problem.
I am trying to build a simple ray tracer and I am getting some strange results when I try to project rays from my screen to the world.
I have the following camera matrices:
#define deg2rad( d ) ((float)((d)*PI/180.0f))
#define FOV 60
void Camera::buildPerspective( void )
{
float _near = 1.0f;
float _far = 100.0f;
float scale = 1.0 / tan(deg2rad(FOV * 0.5f));
perspective = Matrix4(
scale, 0, 0, 0,
0, scale, 0, 0,
0, 0, -_far / (_far - _near), -1,
0, 0, -_far * _near / (_far - _near), 0 );
}
void Camera::buildScreen( void )
{
float DIM_over_2 = DIM / 2.0f;
screen = Matrix4( DIM_over_2, 0.0f, 0.0f, DIM_over_2,
0.0f, -DIM_over_2, 0.0f, DIM_over_2,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
void Camera::buildView( void )
{
Matrix4 inv_translate( Matrix4::createTranslation( -pos.x, -pos.y, -pos.z ) );
Matrix4 rotation = Matrix4::createTranspose( Matrix4::createFromYawPitchRoll( rot.y, rot.x, rot.z ) );
view = inv_translate * rotation;
}
I then have the following code that de-projects:
Vertex rayOrigin(*eye);
Vector3 rayDir;
float normalised_x = 2.0f * x / DIM - 1.0f;
float normalised_y = 2.0f * y / DIM - 1.0f;
Matrix4 unviewMat = *screen * *perspective;
Vertex near_point = unviewMat * Vertex (normalised_x, normalised_y, 0, 1);
near_point = *view * near_point;
rayDir = near_point - rayOrigin;
Which gives me the following result which is supposed to be of a cube, looking at it when the camera has orbited around the y axis by 45 degrees:
Can anyone see where I have gone wrong? I would appreciate the help massively!