我需要将多维数据集缩放50倍,并以3.0f
单位沿Z轴移动:
glm::mat4 model(1.0f);
model = glm::scale(model, glm::vec3(50.0f, 50.0f, 50.0f));
model = glm::translate(model, glm::vec3(0.0f, 0.0f, 3.0f));
我期望街区几乎在中心,但事实并非如此。我在做什么错了?
我的顶点着色器:
const char *vertexShaderSource = "#version 330 core\n"
"layout(location = 0) in vec3 aPos;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"void main()\n"
"{\n"
" gl_Position = projection * view * model * vec4(aPos, 1.0f);\n"
"}\n\0";
我的视图和投影矩阵:
glm::vec3 cameraPos = glm::vec3(-300.0f, 0.0f, 0.0f);
glm::vec3 cameraFront = glm::vec3(1.0f, 0.0f, 0.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
glm::mat4 projection = glm::infinitePerspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.001f);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "view"), 1, GL_FALSE, glm::value_ptr(view));
我的数组:
float vertices[] = {
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
};
unsigned int indices[] = {
0, 1, 2,
2, 3, 0,
1, 0, 4,
1, 4, 5,
3, 7, 4,
3, 4, 0,
6, 2, 1,
6, 1, 5,
6, 7, 3,
6, 3, 2,
5, 4, 7,
5, 7, 6,
};
答案 0 :(得分:1)
请参阅translate()
的glm API文档:
构建由3个分量的向量创建的平移4 * 4矩阵。
参数
m
输入矩阵乘以该转换矩阵
v
翻译向量的坐标。
这意味着模型矩阵可以表示为:
model = scale * translate
这将导致翻译乘以比例。注意,必须从左到右“读取”乘法。 (请参见GLSL Programming/Vector and Matrix Operations)
交换比例尺和翻译来解决您的问题:
glm::mat4 model(1.0f);
model = glm::translate(model, glm::vec3(0.0f, 0.0f, 3.0f));
model = glm::scale(model, glm::vec3(50.0f, 50.0f, 50.0f));