OpenGL 将纹理四边形渲染为 (0,0) 处的单个像素?

OpenGL rendering textured quad as a single pixel at (0,0)?

我有一个非常基本的 renderer2D,但不能正确渲染我的基本 2D 四边形。它将它呈现为 window 左下角的单个像素。 window 尺寸为 800x600

使用glm::ortho(0.0f, 800.0f, 0.0f, 600.0f)计算正交投影矩阵。 由于某种原因,它只在错误的位置呈现单个像素。更奇怪的是,如果我将 Model 矩阵设置为平移到屏幕中间,它会在屏幕中间呈现那个单独的像素

我在渲染器中这样称呼它 class :

m_Renderer2D.RenderQuad(glm::vec3(400.0f, 300.0f, 1.0f), &m_CrosshairTexture, &m_Camera2D);

渲染器class:

    class Renderer2D    
    {
        public:
    
            Renderer2D();
        
            void RenderQuad(const glm::vec2& position, GLClasses::Texture* texture, OrthographicCamera* camera);
    
        private : 
    
            GLClasses::VertexBuffer m_VBO;
            GLClasses::VertexArray m_VAO;
            GLClasses::IndexBuffer m_IBO;
            GLClasses::Shader m_DefaultShader;
    };
    
cpp file : 

    Renderer2D::Renderer2D() : m_VBO(GL_ARRAY_BUFFER)
    {
        GLuint index_buffer[6] = { 0,1,2,2,3,0 };

        m_VAO.Bind();
        m_VBO.Bind();
        m_IBO.Bind();
        m_VBO.VertexAttribPointer(0, 3, GL_FLOAT, 0, 5 * sizeof(GLfloat), (void*)0);
        m_VBO.VertexAttribPointer(0, 2, GL_FLOAT, 0, 5 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
        m_IBO.BufferData(6 * sizeof(GLuint), index_buffer, GL_STATIC_DRAW);
        m_VAO.Unbind();

        m_DefaultShader.CreateShaderProgramFromFile("Shaders/2DElementShaderVert.glsl", "Shaders/2DElementShaderFrag.glsl");
        m_DefaultShader.CompileShaders();
    }

    void Renderer2D::RenderQuad(const glm::vec2& position, GLClasses::Texture* texture, OrthographicCamera* camera)
    {
        glDisable(GL_DEPTH_TEST);

        const std::array<GLfloat, 8> texture_coords = texture->GetTextureCoords();

        float x, y, w, h;

        x = position.x;
        y = position.y;
        w = position.x + texture->GetWidth(); // The width and height of the texture is correct
        h = position.y + texture->GetHeight();

    GLfloat Vertices[] = {
        w, y, 0.0f, texture_coords[0], texture_coords[1],
        w, h, 0.0f, texture_coords[2], texture_coords[3],
        x, h, 0.0f, texture_coords[4], texture_coords[5],
        x, y, 0.0f, texture_coords[6], texture_coords[7],
    };

        m_DefaultShader.Use();

        glm::mat4 proj = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f);

        texture->Bind(1);
        m_DefaultShader.SetMatrix4("u_Projection", proj, 0);
        m_DefaultShader.SetMatrix4("u_View", camera->GetViewMatrix(), 0);
        m_DefaultShader.SetMatrix4("u_Model", glm::mat4(1.0f), 0);
        m_DefaultShader.SetInteger("u_Texture", 1, 0);
        
        // Draw the 2D quad 
        m_VAO.Bind();
        m_VBO.BufferData(20 * sizeof(GLfloat), Vertices, GL_STATIC_DRAW);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)0);
        m_VAO.Unbind();

        glEnable(GL_DEPTH_TEST);
    }

着色器:

顶点着色器:

#version 330 core

layout(location = 0) in vec3 a_Position;
layout(location = 1) in vec2 a_TexCoord;

out vec2 v_TexCoord;

uniform mat4 u_Projection;
uniform mat4 u_View;
uniform mat4 u_Model;

void main()
{
    gl_Position = u_Projection * u_View * u_Model * vec4(a_Position, 1.0f);
    v_TexCoord = a_TexCoord;
}

片段着色器:

#version 330 core

in vec2 v_TexCoord;
out vec4 o_Color;

uniform sampler2D u_Texture;

void main()
{
    o_Color = texture(u_Texture, v_TexCoord);
}

布局位置将顶点坐标关联到属性索引 0,将纹理坐标关联到属性索引 1:

layout(location = 0) in vec3 a_Position;
layout(location = 1) in vec2 a_TexCoord;

当您指定通用顶点属性数据的数组时,顶点属性数组 0 被指定了两次,但是您错过了指定顶点属性数组 1:

m_VBO.VertexAttribPointer(0, 3, GL_FLOAT, 0, 5 * sizeof(GLfloat), (void*)0);
m_VBO.VertexAttribPointer(0, 2, GL_FLOAT, 0, 5 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));

因此纹理坐标覆盖了顶点坐标,根本没有指定纹理坐标属性。

纹理坐标的属性索引必须是 1 而不是 0:

m_VBO.VertexAttribPointer(0, 2, GL_FLOAT, 0, 5 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));

m_VBO.VertexAttribPointer(1, 2, GL_FLOAT, 0, 5 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));