Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing a LookAt function in the Vertex Shader with OpenGL
    primarykey
    data
    text
    <p>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 <a href="https://stackoverflow.com/a/6802424">https://stackoverflow.com/a/6802424</a></p> <pre><code>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; } </code></pre> <p>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?</p> <p>Here are some pictures, although not terribly helpful.</p> <p>Using the programs ModelViewMatrix</p> <p><img src="https://i.imgur.com/a3fi26k.png" width="400" alt="Using the programs ModelViewMatrix"></p> <p>Using the Vertex Shader lookAt function</p> <p><img src="https://i.imgur.com/09oolMP.png" width="400" alt="Using the Vertex Shader lookAt function"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload