OpenGL将纹理四边形渲染为(0,0)

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

本文关键字:纹理 四边形 OpenGL      更新时间:2023-10-16

我有一个非常基本的renderer2D,但不能正确渲染我的基本2D四边形。它将其渲染为窗口左下角的单个像素。窗口大小为800x600

使用CCD_ 1计算正交投影矩阵。由于某些原因,它只在错误的位置渲染单个像素。更奇怪的是,如果我将Model矩阵设置为平移到屏幕中间,它会渲染屏幕中间的唯一像素

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

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

渲染器类别:

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)));