Implementing a LookAt function in the Vertex Shader with OpenGL

余生长醉 提交于 2019-12-08 12:35:12

问题


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

来源:https://stackoverflow.com/questions/15535347/implementing-a-lookat-function-in-the-vertex-shader-with-opengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!