# OpenGL随笔十一 opengl 绘制多个3D立方体

1、定义10个平移向量，将已知立方体平移十次，产生十个立方体
//10个立方体的平移向量
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)
};

2、平移已知立方体，并绘制
for (size_t i = 0; i < 10; i++)
{
glm::mat4 modelMat;
//平移矩阵
modelMat = glm::translate(modelMat, cubePositions[i]);
//旋转矩阵
modelMat = glm::rotate(modelMat, (float)glfwGetTime(), glm::vec3(0.5f, 1.0f, 0.0f));glm::vec3(0.5f, 1.0f, 0.0f));
pshader->use();
//uniform传递texture贴图数据
glUniform1i(glGetUniformLocation(pshader->ID, “ourTextrue”), 0);
glUniform1i(glGetUniformLocation(pshader->ID, “ourFace”), 13);
//uniform传递旋转位移等变换矩阵，完成投影
glUniformMatrix4fv(glGetUniformLocation(pshader->ID, “modelMat”), 1, GL_FALSE, glm::value_ptr(modelMat));
glUniformMatrix4fv(glGetUniformLocation(pshader->ID, “viewMat”), 1, GL_FALSE, glm::value_ptr(viewMat));
glUniformMatrix4fv(glGetUniformLocation(pshader->ID, “projMat”), 1, GL_FALSE, glm::value_ptr(projMat));

		//VBO方式绘制
glDrawArrays(GL_TRIANGLES, 0, 36);
}


git链接: link.

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客