PDA

View Full Version : Camera Matrix does not behave as expected



stimulate
12-18-2016, 09:41 AM
I try to render a simple grid using `glBegin(GL_LINES)`.
I have a `class Camera`, which provides values like this:


float farPlane = 100.0f;
float nearPlane= 0.1f;

float screenRatio = 1,42857; //width/height = 1000/700
float frustum = 70.0f;

glm::vec3 position = glm::vec3(3.0f, 3.0f, 3.0f);

glm::vec3 UP = glm::vec3(0.0f, 1.0f, 0.0f);
glm::mat4 view = glm::lookAt(position, position - glm::normalize(position), UP); // makes the lookAt vector always point towards (0, 0, 0)
glm::mat4 projection = glm::perspective(frustum, screenRatio, nearPlane, farPlane);

using the `view` and `projection` matrices, i transform every vertex from my Grid model and render it.


glm::mat4 viewProjectionMatrix = Graphic::camera->getViewProjectionMatrix(); // returns Camera::projection * Camera::view
glBegin(GL_LINES);
for (unsigned int i = 0; i < vertexNum; ++i) {
glColor3f(vertexArray[i].normal.x, vertexArray[i].normal.y, vertexArray[i].normal.z); // normal vector is used for color in this case
glm::vec4 translatedPosition(viewProjectionMatrix*gridTransf ormationMatrix*(glm::vec4(vertexArray[i].position, 1.0f)));
glVertex3f(translatedPosition.x, translatedPosition.y, translatedPosition.z);
}
glEnd();

but this is what i see when i move the camera along the line: (0,0,0) + u*(1,1,1)
http://i.imgur.com/PrcDcLs.gifv
(you can find the camera cooridnates in the console)

It appears as if it was not even perspective projection but orthographic.

GClements
12-19-2016, 07:08 AM
float screenRatio = 1,42857; //width/height = 1000/700


That comma should be a period.





float frustum = 70.0f;



glm::mat4 projection = glm::perspective(frustum, screenRatio, nearPlane, farPlane);


While GLM tends to mimic the legacy OpenGL matrix functions, one area where they differ is that GLM uses radians for angles whereas OpenGL uses degrees. (Actually, whether GLM uses degrees or radians depends upon the version and compiler switches; but modern versions use radians by default).