Skip to main content
GameDev.net gamedev.net
🔒 Locked

building view matrix

Started by JohnnyCode Feb 4, 2009 at 8:59 AM 2 replies 2.2k views
Original Post
JohnnyCode
JohnnyCode
hello, I am trying to build a view matrix from parameters eye,at,up. The problem is this, in documentation it says the matrix is build like this:

void grViewMatrixLookAt(float* eye,float* at,float* up,float* res)
{
	grIdentity(res);
	float f[3],s[3],u[3],upn[3];
	grSubVector3(at,eye,f);
	grNormalize3(f,f);
	grNormalize3(up,upn);
	grCross3(f,upn,s);
	grCross3(s,f,u);

	*(res) = s[0];
	*(res+1) = u[0];
	*(res+2) = -f[0];
	*(res+4) = s[1];
	*(res+5) = u[1];
	*(res+6) = -f[1];
	*(res+8) = s[2];
	*(res+9) = u[2];
	*(res+10) = -f[2];

}

now the probelm is that my at and eye vectors are allways the same distance from each other, they just get rotated or translated as a pair, so they allways keep the same distance. Thus in f I allways get the same vector for the f is their subtraction. Then , becouse the up vector is allways the same too , my grCross3(f,upn,s) and grCross3(s,f,u) are allways the same value. So my view matrix is never translated but only rotated. Also I get a strange viewing result. How do I build a correct view matrix by eye,at,up parameters?
sebbit
sebbit
In that formula, at seems to be the point to look at. But you want at to be the direction the camera is facing, right?
There meight be a better way to do this but a fast fix would be to set at to position + your view direction (i.e. eye + at (that is the vector you currently use as at)).

Edit: Sorry, didn't look at your code close enough. But, grSubVector3(at,eye,f) should actually do the reverse of what I said above. It takes the camera position and point to look at and builds a vector that just points in the direction you want the camera to face in. So you just have to remove this line (i.e. set f to at) and you should be good to go.

Edit2: Sorry again. Seem to be quit slow at comprehension today. The matrix you build doesn't do any translation at all. It only sets the rotation/scaling part of the matrix. To get propper translation, you would also have to translate by the inverse eye vector (i.e. -eye). So you also have to set the translation part of your matrix to that values (shoule be res + 3, 7, 11 or 12, 13, 14 if I got that confused again).
JohnnyCode
JohnnyCode
I am also getting an ugly fish-eye effect. When I use opengl lookat function all is good, so my projection matrix and world matrix are correct.
songho
songho
Normalize the side and up vectors, after performing cross product.

Topic Locked

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

Sign in to reply to this topic.