1 将图像逆时针旋转90度,然后缩放0.5倍
2 glm::mat4 trans;
3
4 将90度装换成弧度制,绕z轴旋转,所以旋转分量放在z分量处
5 trans = glm::rotate(trans, glm::radians(90.0f), glm::vec3(0.0, 0.0, 1.0));
6 trans = glm::translate(trans, glm::vec3(-0.5f, -0.5f, 0.0f));
7
8 trans = glm::scale(trans, glm::vec3(0.5, 0.5, 0.5)); //将变换过得的单位矩阵乘以向量vec3
9
10 unsigned int transformLoc = glGetUniformLocation(ourShader.ID, "transform");//获取uniform的位置
11
12 将trans绑定到顶点着色器中的uniform,然后利用uniform实施变换
13 第一个参数是uniform的位置,第二个参数是所传矩阵的个数,
14 第三个参数是询问我们是否需要对矩阵进行置换(行与列交换),这里是不希望,所以是GL_FALSE
15 第四个参数将trans通过glm里的value_ptr函数转化成我们需要的矩阵类型
16 glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans));
1 glm::mat4 trans;
2 //如果先旋转在平移,那么平移时会按照图像旋转后的新位置进行平移,所以最终结果有点出人意外
3 trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f)); //利用时间函数旋转
4 trans = glm::translate(trans, glm::vec3(0.5f, 0.5f, 0.0f));
5
6 //trans = glm::scale(trans, glm::vec3(2.0, 2.0, 2.0));
7
8 //glUseProgram(shaderProgram);
9 glUseProgram(ourShader.ID);
10
11 unsigned int uniformLocation = glGetUniformLocation(ourShader.ID, "transform");
12 glUniformMatrix4fv(uniformLocation, 1, GL_FALSE, glm::value_ptr(trans));
创建第二个图像,移至左上角并按时间随机缩放
1 glm::mat4 trans1;
2 trans1 = glm::translate(trans1, glm::vec3(-0.5f, 0.5f, 0.0f));
3 GLfloat scaleMount = sin(glfwGetTime());
4 trans1 = glm::scale(trans1, glm::vec3(scaleMount, scaleMount, scaleMount));
5
6 //glUseProgram(ourShader.ID);
7 glUniformMatrix4fv(uniformLocation, 1, GL_FALSE, glm::value_ptr(trans1));
8 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);