我有一个渲染器2D,是非常基本的,但没有渲染我的基本2D四个正确。 它将其呈现为窗口左下方的单个像素。 窗口大小为800x600
使用GLM::Ortho(0.0F,800.0F,0.0F,600.0F)
计算正投影矩阵。 由于某种原因,它只在错误的位置呈现单个像素。 更奇怪的是,如果我将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)));