Wednesday, March 14, 2018

View matrix in opengl


Sorry for my clumsy question. But I don't know where I am wrong at creating view matrix.


I have the following code:


 createMatrix(vec4f(xAxis.x, xAxis.y, xAxis.z, dot(xAxis,eye)), 
vec4f( yAxis.x_, yAxis.y_, yAxis.z_, dot(yAxis,eye)),
vec4f(-zAxis.x_, -zAxis.y_, -zAxis.z_, -dot(zAxis,eye)),
vec4f(0, 0, 0, 1)); //column1, column2,...

I have tried to transpose it, but with no success. I have also tried to use gluLookAt(...) with success. At the reference page, I watched the remarks about the to-be-created matrix, and it seems the same as mine. Where I am wrong?




Answer



In openGl matrices are transposed in memory. So transpose the matrix is OK. But your code doesn't look correct.


So you are in OpenGl. OpenGl uses right handed coordinate system. And for RH is lookat function defined like this:


zaxis = normal(cameraPosition - cameraTarget)
xaxis = normal(cross(cameraUpVector, zaxis))
yaxis = cross(zaxis, xaxis)

xaxis.x yaxis.x zaxis.x 0
xaxis.y yaxis.y zaxis.y 0
xaxis.z yaxis.z zaxis.z 0

-dot(xaxis, cameraPosition) -dot(yaxis, cameraPosition) -dot(zaxis, cameraPosition) 1

Especialy take care about zaxis = normal(cameraPosition - cameraTarget) because it is the only difference between RH and LH system.


Save it transposed and that's it


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...