1,gluPerspective()投影面在相机前方1.0位置,渲染模型与投影面比例精准可靠,反之glm::perspective()投影面在相机前方大于 1.0的位置,渲染模型与投影面比例偏小。
2, glm中矩阵也可以按列储存,与上篇说的是按行操作不要样,个人认为都是可以的。
3,Opengl 固定管线投影矩阵,模型视图矩阵传导到时shader中要注意顺序,例如,
float modelviewMatrix[16];
float projectionMatrix[16];
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.2, 0.2, 0.8, 1.0);
//Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit 100 units
//glm::mat4 Projection = glm::ortho(-4.0f/3.0f, 4.0f/3.0f, -1.0f, 1.0f, 0.1f, 100.0f);
glm::mat4 p = glm::perspective(90.0f, 1.0f, 0.1f, 100.f);
glm::mat4 v = glm::lookAt(
glm::vec3(0,0 , 3), // Camera is at (4,3,3), in World Space
glm::vec3(0, 0, 0), // and looks at the origin
glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down)
);
//Model matrix : an identity matrix (model will be at the origin)
glm::mat4 m = glm::mat4(1.0f);
m = glm::translate(m, glm::vec3(1.0f, 1.0f, 0.0f));
//m = glm::rotate(m, angleY, glm::vec3(0.0f, 0.0f, 1.0f));
//m = glm::scale(m, glm::vec3(1.0f, 1.0f, 1.0f));
//Our ModelViewProjection : multiplication of our 3 matrices
glm::mat4 mvp = p*v*m ;// Remember, matrix multiplication is the other way around
glm::mat4 mv = v*m;// Remember, matrix multiplication is the other way around
// -------------------------opengl matrix------------------------------------------
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0f, 1.0f, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
glTranslatef(1.0f, 1.0f, 0.0f);
//Get the current matrices from OpenGL
glGetFloatv(GL_MODELVIEW_MATRIX, modelviewMatrix);
glGetFloatv(GL_PROJECTION_MATRIX, projectionMatrix);
//-----------------------------------------------------------
glUseProgram(program);
//glUniformMatrix4fv(mvp_location, 1, GL_FALSE, &mv[0][0]);
//glUniformMatrix4fv(proj_location, 1, GL_FALSE, &p[0][0]);
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, &mvp[0][0]);
//glUniformMatrix4fv(mvp_location, 1, GL_FALSE, modelviewMatrix);
//glUniformMatrix4fv(proj_location, 1, GL_FALSE, projectionMatrix);
glDrawArrays(GL_TRIANGLES, 0, 6);
glUseProgram(0);
总结,只要能把opengl固定管线矩阵操作中投影矩阵和模型视图矩阵传到shaders中,就可以不用第三方的矩阵数学库了。