5.opengl入门-坐标系统(3)多model3D动画
2020-06-10 本文已影响0人
天叔
113.gif
这里我做了点修改,改成动态的,angle改成随时间动态递增的,改动代码如下:
for (unsigned int i = 0; i < 10; i++) {
glm::mat4 model;
model = glm::translate(model, cubePositions[i]);
float angle = 20.0f * i + (float)glfwGetTime();
model = glm::rotate(model, angle, glm::vec3(1.0f, 0.3f, 0.5f));
ourShader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
源代码:
for (unsigned int i = 0; i < 10; i++)
{
// calculate the model matrix for each object and pass it to shader before drawing
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, cubePositions[i]);
float angle = 20.0f * i;
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
ourShader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
注意:
- 对齐次坐标的理解,一开始对glm::vec3( 2.0f, 5.0f, -15.0f),有点不理解,model的坐标范围是在-1--+1之间,x和y轴分别移动了2和5,这不是移到坐标外面了么?做了个实验,吧-15改成0确实屏幕上是空白的。但是这里z轴往后移动了15,z轴范围是-0.1~1000,2/15 = 0.13,5/15 = 0.33,按照投影的逻辑来理解,移动到远处就能被相机收录进来。可以想象在拍照的时候,一个物体在相机视野外面,但是如果往后退,物体就逐渐能进入视野。
// world space positions of our cubes
glm::vec3 cubePositions[] = {
glm::vec3( 0.0f, 0.0f, 0.0f),
glm::vec3( 2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3( 2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3( 1.3f, -2.0f, -2.5f),
glm::vec3( 1.5f, 2.0f, -2.5f),
glm::vec3( 1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};