Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Camera Problems

Started by whaledawg Aug 6, 2007 at 12:06 PM 3 replies 1k views
Original Post
whaledawg
whaledawg
OK, so I pretty much stole a camera class from here. It links to some source that I took and altered for my purposes. But for some reason, when I'm looking straight at my object it's very far away. As I rotate to the left and right, the object gets closer. It's weird. Here's the camera code:

camControl::camControl(const Vector3 &cPosition, float h, float v) :
     radius(1), moveDist(CAM_STEP)
{
    Vector3 blank; blank[0] = 0; blank[1] = 0; blank[2] = 0;
    AssignVector(cameraPosition,cPosition);
    AssignVector(cameraTarget,blank);
    AssignVector(cameraUpVector,blank);
    hRadians = h;
    vRadians = v;

    RotateCamera(0,0);
}

void  camControl::RotateCamera(float h, float v)
{
    hRadians += h;
    vRadians += v;

    cameraTarget[1] = cameraPosition[1]+(float)(radius*sin(vRadians));
    cameraTarget[0] = cameraPosition[0]+(float)(radius*cos(vRadians)*cos(hRadians));
    cameraTarget[2] = cameraPosition[2]+(float)(radius*cos(vRadians)*sin(hRadians));
    
    cameraUpVector[0] = cameraPosition[0]-cameraTarget[0];
    cameraUpVector[1] = fabs(cameraPosition[1]+(float)(radius*sin(vRadians+PI/2))) ;
    cameraUpVector[2] = cameraPosition[2]-cameraTarget[2];

}
And then my keyhandler calls RotateCamera with a positive or negative 0.05 for h and 0.0 for v. Then my render function looks like this:

bool DrawGLScene(GLvoid)								// Here's Where We Do All The Drawing
{	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer
		
    glLoadIdentity();								

    gluLookAt(Camera->XPosition(), Camera->YPosition(), Camera->ZPosition(),
              Camera->XTarget(), Camera->YTarget(), Camera->ZTarget(),
              Camera->XUp(), Camera->YUp(), Camera->ZUp());    


    theBall->draw();

    return true;
}
I can't figure out what it's doing wrong, so I'd really appreciate any help. I'm new to this whole camera thing and it's driving me nuts.
D3DXVECTOR3
D3DXVECTOR3
Im not really sure but, i had similar problems in the past when porting my D3D camera to OpenGL.

If i remember it correctly the problem was with the matrix conventions used by both API's. OpenGL uses column-major and Direct3D uses row-major. OpenGL matrices are transformed D3D matrices.

It looks like the artice is writen for d3d while you use opengl so this could be it. btw I also dont see you setting the current matrix like glMatrixMode(GL_MODELVIEW);
whaledawg
whaledawg
Thank you very much. I'm not sure, but I'll look into that. I appreciate your help.

And I only set the matrix mode in the resize function and assume MODEL_VIEW everywhere else.
Zakwayda
Zakwayda
Quote:
Original post by D3DXVECTOR3
If i remember it correctly the problem was with the matrix conventions used by both API's. OpenGL uses column-major and Direct3D uses row-major. OpenGL matrices are transformed D3D matrices.
OpenGL matrices are actually transposed with respect to D3D matrices, not transformed (I'm guessing this is what you meant). Also, this transposition really only applies to how the matrices are written out (i.e. row-basis vs. column-basis notation); OpenGL and D3D matrices are actually laid out in memory the same way, so usually there's no need to transpose them when switching from one API to another.

@The OP: Some of the stuff in your RotateCamera() function looks a bit suspect (are you sure your up vector is perpendicular to the forward vector?). You'll also want to make sure you have the right matrix mode set when you set up the camera transform, and that your projection matrix is correct as well (the behavior you described earlier sounds a little bit like a field-of-view issue, but I'm guessing there are other problems as well).
whaledawg
whaledawg
Thank you all for taking the time to read this and think about my problem. I really appreciate that.

I found it, it had nothing to do with the camera and it's so stupid I'm too embarrassed to write it here :(

Suffice it to say I appreciate your help.

Topic Locked

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

Sign in to reply to this topic.