opengl中的矩阵和向量

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wodownload2/article/details/89377755

如下代码:
glm::mat4 sphereModel;
sphereModel = glm::translate(sphereModel, glm::vec3(0, 0, -10.0f));
输出:
_cprintf("%f %f %f %f\n", sphereModel[0][0], sphereModel[1][0], sphereModel[2][0], sphereModel[3][0]);
_cprintf("%f %f %f %f\n", sphereModel[0][1], sphereModel[1][1], sphereModel[2][1], sphereModel[3][1]);
_cprintf("%f %f %f %f\n", sphereModel[0][2], sphereModel[1][2], sphereModel[2][2], sphereModel[3][2]);
_cprintf("%f %f %f %f\n", sphereModel[0][3], sphereModel[1][3], sphereModel[2][3], sphereModel[3][3]);

在这里插入图片描述

用它来平移点(0,0,0,1)的写法:
glm::vec4 vvv = sphereModel * glm::vec4(0, 0, 0, 1);
_cprintf("%f %f %f %f\n", vvv.x, vvv.y, vvv.z, vvv.w);

所以opengl中矩阵和向量的相乘,矩阵在左,向量在右。opengl中的向量使用的是列向量表示。
在这里插入图片描述

参考网址:http://www.cnblogs.com/graphics/archive/2012/08/02/2616017.html

opengl中的lookAt矩阵:
glm::mat4 viewMatrix = glm::lookAt(glm::vec3(0, 0, 3), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
_cprintf("%f %f %f %f\n", viewMatrix[0][0], viewMatrix[1][0], viewMatrix[2][0], viewMatrix[3][0]);
_cprintf("%f %f %f %f\n", viewMatrix[0][1], viewMatrix[1][1], viewMatrix[2][1], viewMatrix[3][1]);
_cprintf("%f %f %f %f\n", viewMatrix[0][2], viewMatrix[1][2], viewMatrix[2][2], viewMatrix[3][2]);
_cprintf("%f %f %f %f\n", viewMatrix[0][3], viewMatrix[1][3], viewMatrix[2][3], viewMatrix[3][3]);
输出:
在这里插入图片描述
首先站在(0,0,3)位置,看的是(0,0,0)位置。那么view矩阵的构建如下:
up向量为(0,1,0)
cameraDirection=position - target = (0,0,3)
right=upXcameraDirection=(3,0,0)
up=cameraDirectionXright=(0,9,0)
单位化之后:
right = (1,0,0)
up=(0,1,0)
cameraDirectioin = (0,0,1)
而z的为啥是-3呢?
参考:https://learnopengl-cn.github.io/01 Getting started/09 Camera/

猜你喜欢

转载自blog.csdn.net/wodownload2/article/details/89377755
今日推荐