opengl入门-坐标系统(3)多model3D动画

2020-06-11 16:04:17 浏览数 (2)

113.gif

这里我做了点修改,改成动态的,angle改成随时间动态递增的,改动代码如下:

代码语言:javascript复制
        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);
        }

源代码:

代码语言:javascript复制
     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);
        }

完整代码: https://learnopengl.com/code_viewer_gh.php?code=src/1.getting_started/6.3.coordinate_systems_multiple/coordinate_systems_multiple.cpp

注意:

  1. 对齐次坐标的理解,一开始对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,按照投影的逻辑来理解,移动到远处就能被相机收录进来。可以想象在拍照的时候,一个物体在相机视野外面,但是如果往后退,物体就逐渐能进入视野。
代码语言:javascript复制
    // 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)
    };

0 人点赞