LookAt matrix strange results

Started by TheBoneJarmer, September 03, 2015, 14:12:29

Previous topic - Next topic

TheBoneJarmer

Hey guys

I have been working the past 2 days on a LookAt matrix, code is below. Only, I have some troubles with the results. I converted this code from OpenTK's Matrix4 class to Java but unfortunately I do not get the results I expected. Google results did not help me because almost all answers are different. For some odd reason everyone seems to use a different code which works "perfect" for them but all I get is strange or no results. Below is a screenshot with what I got so far and also the code I use.

public static Matrix4 lookAt(Vector3 eye, Vector3 target, Vector3 up) {
	Matrix4 result = IDENTITY;
	    
	Vector3 zaxis = Vector3.normalize(Vector3.subtract(target, eye));
	Vector3 xaxis = Vector3.normalize(Vector3.cross(zaxis, up));
	Vector3 yaxis = Vector3.cross(xaxis, zaxis);
	    
	result.row0.x = xaxis.x;
	result.row0.y = yaxis.x;
	result.row0.z = zaxis.x;
	result.row0.w = 0;
	    
	result.row1.x = xaxis.y;
	result.row1.y = yaxis.y;
	result.row1.z = zaxis.y;
	result.row1.w = 0;
	    
	result.row2.x = xaxis.z;
	result.row2.y = yaxis.z;
	result.row2.z = zaxis.z;
	result.row2.w = 0;
	    
	result.row3.x = -Vector3.dot(xaxis, eye);
	result.row3.y = -Vector3.dot(yaxis, eye);
	result.row3.z = -Vector3.dot(zaxis, eye);
	result.row3.w = 1;
	    
	return result;
}


@Screen 1
This is when the camera is at position (0,0,-5) while the sphere is at position (0,0,0)

@Screen 2
This is what the sphere looks up close. I do not know if it is very visible but the sphere is hollow which should not be the case.

@Screen 3
This is when the camera is at position (0,5,0).

I have been playing around for a while now but I really do not get the right settings for this.

Thanks in advance!
-TBJ

Kai

You are building a lookAt matrix that conforms to Direct3D, probably by using this stackoverflow thread.
Direct3D uses the transpose of what OpenGL uses and a reverse matrix multiplication order.
In the stackoverflow question you see the last row representing the translation of the matrix and being computed using the dot product. With OpenGL, this is the last "column."
Also the row and column vectors of the upper left 3x3 submatrix are transposed between OpenGL and Direct3D.
So, all you have to do is just transpose your matrix to conform to OpenGL.

EDIT: Also note that you are modifying the Matrix4 object stored in your `IDENTITY` field, which is not the identity matrix anymore when the method returns. And you will get some wrong results when that `IDENTITY` field is used in other methods where it is assumed to be the identity and not completely overwritten.
To combat this, you could have an additional parameter to your static method, `Matrix4 out/dest/result`, which will be overwritten with the lookat matrix computation.

quew8

Also you should really be normalizing your "yaxis" variable as well. A cross product will very very very rarely give you a unit result.

TheBoneJarmer

Hey guys!

Sorry for not replying earlier! I've seen your replies a while ago but due to personal circumstances I was unable to do anything related to programming. With this reply I wanted to let you know I appreciate your help and I really did not forget about you. I know it can be frustrating if you try to help someone in a topic and the person does nothing with it. That is why I wanted to apologize. That being said, I will take a look at this at a moment I see fit. I have other things that have a bigger priority right now which I'd like to take care of first.

Sincerely,
TBJ

TheBoneJarmer

I managed to solve this problem! I used JOLM's Matrix4f's lookAt method to create a lookAt matrix and uploaded them to my vertex shader as a mat4 uniform. Thanks guys!