OpenGL中立方体的位置
Posted
技术标签:
【中文标题】OpenGL中立方体的位置【英文标题】:Location of cube in OpenGL 【发布时间】:2019-11-03 09:56:20 【问题描述】:这段代码的输出是 25 个小熊旋转。 我面临的问题是每个立方体的位置,我不知道如何使它们排成一行,例如每 5 个立方体排成一行等等。
注意:我已经添加了立方体的位置。
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)
;
while (!glfwWindowShouldClose(mainWindow))
glfwPollEvents();
glClearColor(0.0f, 0.1f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Camera transformation
glm::mat4 projection = glm::mat4(1.0f);
glm::mat4 view = glm::mat4(1.0f);
projection = glm::perspective(glm::radians(45.0f), (float)WIDTH / (float)HEIGHT, 0.1f, 100.0f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
//Get uniform locations
GLint viewlLoc = glGetUniformLocation(ourShader.Program, "view");
glUniformMatrix4fv(viewlLoc, 1, GL_FALSE, glm::value_ptr(view));
GLint projlLoc = glGetUniformLocation(ourShader.Program, "projection");
glUniformMatrix4fv(projlLoc, 1, GL_FALSE, glm::value_ptr(projection));
// Call Shader Program
ourShader.Use();
glBindVertexArray(VAO);
for(unsigned int i = 0; i < 25; i++)
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, cubePositions[i]);
float angle = 20.0f * i;
angle = glfwGetTime() * 25.0f;
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glfwSwapBuffers(mainWindow);
glfwPollEvents();
【问题讨论】:
什么是cubePositions[i]
转换矩阵model = glm::translate(model, cubePositions[i]);
的计算行?
【参考方案1】:
使用glm::translate
为每个单独的立方体的模型矩阵添加转换,具体取决于立方体的索引。例如:
for(unsigned int i = 0; i < 25; i++)
float dist = ; // distance between the cubes
float x = (i % 5) * dist;
float y = (i / 5) * dist;
float angle = 20.0f * i;
angle = glfwGetTime() * 25.0f;
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vector3(x, y, 0.0f));
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
注意,您必须定义立方体之间的距离 (dist
)。
注意,矩阵的串联(乘法*
)不是Commutative
。
如果要旋转整个立方体块,则必须在旋转之前进行平移:
model = rotate * translate
由于glm::rotate
分别为glm::translate
,定义一个矩阵并将输入矩阵乘以新矩阵,glm::rotate
必须在glm::translate
之前完成:
glm::mat4 model = glm::mat4(1.0f);
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
model = glm::translate(model, glm::vector3(x, y, 0.0f));
您可以使用算法计算cubePositions
:
#include <vector>
std::vector<glm::vec3> cubePositions;
for(unsigned int i = 0; i < 25; i++)
float dist = 5.0f; // distance between the cubes
float x = (i % 5) * dist;
float y = (i / 5) * dist;
cubePositions.push_back(glm::vec3(x, y, 0.0f));
【讨论】:
我添加了 cubePositions[i] 让它更清晰。 @Noura 请阅读答案。您必须根据其索引计算立方体的位置。不要使用你困惑的cubePositions
。但是你可以计算立方体的位置。请参阅我现在添加的答案的最后一部分。以上是关于OpenGL中立方体的位置的主要内容,如果未能解决你的问题,请参考以下文章
在opengl es android中的随机位置绘制多个立方体