For purposes beyond my control, I need to calculate a ModelView Matrix in my vertex shader. I understand this is a bad idea but I don't have a choice right now. Here is the code in my vertex shader. Based on https://stackoverflow.com/a/6802424
mat4 lookAt(vec3 eye, vec3 center, vec3 up)
{
vec3 zaxis = normalize(center - eye);
vec3 xaxis = normalize(cross(up, zaxis));
vec3 yaxis = cross(zaxis, xaxis);
mat4 matrix;
//Column Major
matrix[0][0] = xaxis.x;
matrix[1][0] = yaxis.x;
matrix[2][0] = zaxis.x;
matrix[3][0] = 0;
matrix[0][1] = xaxis.y;
matrix[1][1] = yaxis.y;
matrix[2][1] = zaxis.y;
matrix[3][1] = 0;
matrix[0][2] = xaxis.z;
matrix[1][2] = yaxis.z;
matrix[2][2] = zaxis.z;
matrix[3][2] = 0;
matrix[0][3] = -dot(xaxis, eye);
matrix[1][3] = -dot(yaxis, eye);
matrix[2][3] = -dot(zaxis, eye);
matrix[3][3] = 1;
return matrix;
}
void main( void )
{
vec3 cam = vec3(0, 0, 3);
mat4 test = lookAt(cam, vec3(0,0,0), vec3(0, 1, 0));
gl_Position = gl_ProjectionMatrix * test * gl_Vertex;
//gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
}
However, when I use this in place of my ModelView Matrix, It doesn't work correctly. But I have no idea what is wrong. Can anyone point me in the right direction?
Here are some pictures, although not terribly helpful.
Using the programs ModelViewMatrix
Using the Vertex Shader lookAt function