Я создал программу, которая отображает 3d куб, и теперь я хотел бы изменить положение куба. Матричное умножение, которое я делаю сейчас, похоже, искажает куб вместо того, чтобы менять его положение. Искажение мало для значений в диапазоне 0,1 - 0,4 и заполняет весь экран для больших значений.3D-модель Translation deforming the object
вершинный шейдер:
#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec3 vertexColor;
uniform mat4 projectionMatrix;
uniform mat4 cameraMatrix;
uniform mat4 modelMatrix;
out vec3 fragmentColor;
void main()
{
gl_Position = projectionMatrix * cameraMatrix * modelMatrix * vec4(vertexPosition_modelspace,1);
fragmentColor = vertexColor;
}
model.cpp (обратите внимание, что modelMatrix инициализируется единичной матрицы, и я использую GLM)
void Model::SetPos(glm::vec3 coords)
{
modelMatrix[0][3] = coords[0];
modelMatrix[1][3] = coords[1];
modelMatrix[2][3] = coords[2];
}
void Model::Render()
{
// Select the right program
glUseProgram(program);
// Set the model matrix in the shader
GLuint MatrixID = glGetUniformLocation(program, "modelMatrix");
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, value_ptr(modelMatrix));
// Setup the shader color attributes
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
// Setup the shader vertex attributes
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
// Draw the model
glDrawArrays(GL_TRIANGLES, 0, triangles);
// Now disable the attributes
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}
Остальные матрицы инициализируются, как это и остаются неизменными:
cameraMatrix = glm::lookAt(pos, target, orient);
projectionMatrix = glm::perspective(45.0f, 1280.0f/720.0f, 0.1f, 100.0f);