Configuring MVP matrix in OpenGL

I am trying to learn the basics of OpenGL, but I have a problem setting up transformation matrices. I created a model, a matrix of representations and projections, but I have a problem with sending them to the vertex shader.

Here is the code:

//Set up MVP
glm::mat4 model = glm::mat4();
GLint uniModel = glGetUniformLocation(program, "model");
glUniformMatrix4fv(uniModel, 1, GL_FALSE, glm::value_ptr(model));

glm::mat4 view = glm::lookAt(
    glm::vec3(2.5f, 2.5f, 2.0f),
    glm::vec3(0.0f, 0.0f, 0.0f),
    glm::vec3(0.0f, 0.0f, 1.0f));
GLint uniView = glGetUniformLocation(program, "view");
glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));

glm::mat4 proj = glm::perspective(45.0f, 800.0f / 600.0f, 1.0f, 10.0f);
GLint uniProj = glGetUniformLocation(program, "proj");
glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));

and shader:

layout (location = 0) in vec3 position;

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;

void main() {
    gl_Position = proj * view * model * vec4(position, 1.0);
}

I think that I did something wrong with setting up the uniform, because it doesnโ€™t draw anything, even if I set the model, I looked and the prog identity. Maybe I just guessed something, but I really can't find the problem.

Change: . Having solved the problem, I forgot to use it first glUseProgram().

+4
source share
1 answer

, , , , , , , , >= 0.

glGetError() , , .

, glGetProgram(GL_LINK_STATUS,...). , , glUseProgram(), .

@Vallentin, MVP, . , , :

// Set up MVP matrices

glm::mat4 model = glm::mat4();

glm::mat4 view = glm::lookAt(
    glm::vec3(2.5f, 2.5f, 2.0f),
    glm::vec3(0.0f, 0.0f, 0.0f),
    glm::vec3(0.0f, 0.0f, 1.0f));

glm::mat4 proj = glm::perspective(45.0f, 800.0f / 600.0f, 1.0f, 10.0f);

glm::mat4 mvp = proj * view * model;

glUseProgram(prog);

GLint uniMvp = glGetUniformLocation(program, "mvp");
glUniformMatrix4fv(uniMvp, 1, GL_FALSE, glm::value_ptr(mvp));

GLSL:

// Shader code

layout (location = 0) in vec3 position;
uniform mat4 mvp;

void main() {
    gl_Position = mvp * vec4(position, 1.0);
}

, , . , .

+1

Source: https://habr.com/ru/post/1525602/


All Articles