尝试再次调用glDrawElements画出第二个箱子,只使用变换将其摆放在不同的位置。让这个箱子被摆放在窗口的左上角,并且会不断的缩放(而不是旋转)。(sin
函数在这里会很有用,不过注意使用sin
函数时应用负值会导致物体被翻转)
画图代码:
1 //the first one 2 // create transformations 3 glm::mat4 transform = glm::mat4(1.0f); // make sure to initialize matrix to identity matrix first 4 transform = glm::translate(transform, glm::vec3(0.5f, -0.5f, 0.0f)); 5 transform = glm::rotate(transform, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f)); 6 7 // get matrix's uniform location and set matrix 8 ourShader.use(); 9 unsigned int transformLoc = glGetUniformLocation(ourShader.ID, "transform"); 10 glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform)); 11 12 // render container 13 glBindVertexArray(VAO); 14 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); 15 16 //the second one 17 // create transformations 18 transform = glm::mat4(1.0f); // make sure to initialize matrix to identity matrix first 19 transform = glm::translate(transform, glm::vec3(-0.5f, 0.5f, 0.0f)); 20 float scaleAmount = sin((float)glfwGetTime()); 21 if (scaleAmount < 0) 22 transform = glm::scale(transform, glm::vec3(-scaleAmount, -scaleAmount, -scaleAmount)); 23 else 24 transform = glm::scale(transform, glm::vec3(scaleAmount, scaleAmount, scaleAmount)); 25 26 // get matrix's uniform location and set matrix 27 ourShader.use(); 28 glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform)); 29 30 // render container 31 glBindVertexArray(VAO); 32 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
2019/11/28