Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
11.5k views
in Technique[技术] by (71.8m points)

c++ - Why isn't OpenGL drawing anything as soon as I use an orthographic projection matrix?

When I use a projection matrix as

glm::mat4 projection(1.0f);

everything works fine and it draws a rectangle, but as soon as I use

glm::mat4 projection = glm::ortho(0.0f, 800.0f, 600.0f, 0.0f, -1.0f, 1.0f);

I doesn't draw anything anymore.

Do you have any idea why?

This is my renderer code:

void Renderer::renderRect(Shader& shader, glm::vec2 pos, glm::vec2 size, glm::vec3 color, float rotation, float opacity) {
    shader.use();

    glm::mat4 model(1.0f);

    glm::translate(model, glm::vec3(pos, 0.0f));

    glm::scale(model, glm::vec3(size, 1.0f));

    glm::mat4 view(1.0f);

    glm::mat4 projection = glm::ortho(0.0f, 800.0f, 600.0f, 0.0f, -1.0f, 1.0f);
    //glm::mat4 projection(1.0f);

    shader.setMat4("model", model);
    shader.setMat4("view", view);
    shader.setMat4("projection", projection);

    shader.setVec4("color", glm::vec4(color, opacity));

    glBindVertexArray(quadVAO);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glBindVertexArray(0);
}

I am not sure where exactely the problem is, but maybe it has to do something with my vertex-shader:

#version 330 core

layout (location = 0) in vec2 pos;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    gl_Position = projection * view * model * vec4(pos.xy, 0.0, 1.0);
}

The renderRect function is called by:

renderer->renderRect(shader, glm::vec2(200.0f, 200.0f), glm::vec2(300.0f, 300.0f), glm::vec3(0.0f, 0.0f, 1.0f));

The vertices are:

float vertices[]{
        0.0f, 1.0f,
        1.0f, 0.0f,
        0.0f, 0.0f,

        0.0f, 1.0f, 
        1.0f, 0.0f,
        1.0f, 1.0f
    };
question from:https://stackoverflow.com/questions/65645025/why-isnt-opengl-drawing-anything-as-soon-as-i-use-an-orthographic-projection-ma

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found the problem. It should be:

model = glm::translate(model, glm::vec3(pos, 0.0f));
model = glm::scale(model, glm::vec3(size, 1.0f));

Otherwise the model matrix doesn't get translated and scaled.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...