1

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: enter image description here

Can anyone see where I have gone wrong? I would appreciate the help massively!

RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
Kevin Orriss
  • 1,012
  • 3
  • 11
  • 24
  • Well, there's a lot of things that could affect the result... Could you provide the image you get when you try to draw a cube of size 2, centered at the world space origin, with a camera located at coordinates (6.0, 2.0, 6.0), assuming y is your up vector ? – JBL Feb 14 '13 at 15:03
  • it is a cube, just looks funny because all it's sides are the same colour? 3 sides of the cube are visible, with a corner slap bang in the middle of the rendering you do have..? – Phill Feb 14 '13 at 15:03
  • The cube is 10*10*10 at the world origin where the camera is orbiting at a distance of 20. I haven't added in shading yet which is why it is all the same colour. Also, yes, my Y vector is up and Z faces into the screen. – Kevin Orriss Feb 14 '13 at 15:09
  • What Phill says (and that I should have noticed) is that your rendering is actually correct, it is a cube, which orientation is correct given the camera you described. You just don't see an edge between two faces because you actually didn't apply any shading. – JBL Feb 14 '13 at 15:14
  • Just a hunch - multiply the 'second last' element: [3][2], by 2.0, in the perspective matrix. Or by 0.5? – Brett Hale Feb 14 '13 at 15:15
  • Phil and JBL: The camera is showing 2 of the six faces, but it just looks like the FOV is way too high for 60 degrees... Brett: Changing the perspective matrix done nothing. – Kevin Orriss Feb 14 '13 at 15:54
  • Ok so I just noticed that my cube size was 20*20*20 and my camera was orbiting at a distance of 20 also... explains why it looked so "zoomed in"! Only problem now is that if I change the FOV to 200 the cube gets bigger, and if I change it to 10 it gets really small? Does not seem right to me. – Kevin Orriss Feb 14 '13 at 16:16
  • well i think that is because you are inside the cube ... – Spektre Oct 07 '13 at 22:29

0 Answers0