gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:20:08 28 4
gpt4 key购买 nike

我有一个非常基本的renderer2D,但没有正确渲染我的基本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))); 

关于c++ - OpenGL将纹理四边形渲染为(0,0)的单个像素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62775195/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com