【发布时间】:2020-07-07 06:34:58
【问题描述】:
我正在编写一个基本的纹理批处理渲染器,但不知何故,它只渲染一个纹理而不是四个。下面的代码有什么问题吗?我找不到任何问题。我正在使用 GLEW 和 GLFW 进行窗口化。
VertexBuffer、VertexArray、IndexBuffer、Shader 和 Texture 类已经过测试并且可以正常工作。
void BatchRenderObjects(vector<ObjectInstance> texq, GLuint tex_id, Shader shader)
{
int current_index = 0;
int array_index = 0;
const size_t vb_size = 20 * (texq.size());
const size_t ib_size = 6 * texq.size();
const size_t texslot_size = 32;
VertexBuffer VBO(GL_ARRAY_BUFFER);
IndexBuffer IBO;
VertexArray VAO;
GLfloat *vertex_buffer = new GLfloat[vb_size];
GLuint *index_buffer = new GLuint[ib_size];
{
unsigned int offset = 0;
for (int i = 0; i < ib_size; i += 6)
{
index_buffer[i + 0] = 0 + offset;
index_buffer[i + 1] = 1 + offset;
index_buffer[i + 2] = 2 + offset;
index_buffer[i + 3] = 2 + offset;
index_buffer[i + 4] = 3 + offset;
index_buffer[i + 5] = 0 + offset;
offset += 4;
}
}
{
for (int i = 0; i < texq.size(); i++)
{
vertex_buffer[current_index + 0] = texq[i].coords[0];
vertex_buffer[current_index + 1] = texq[i].coords[1];
vertex_buffer[current_index + 2] = texq[i].coords[2];
vertex_buffer[current_index + 3] = texq[i].tex_coords[0];
vertex_buffer[current_index + 4] = texq[i].tex_coords[1];
vertex_buffer[current_index + 5] = texq[i].coords[3];
vertex_buffer[current_index + 6] = texq[i].coords[4];
vertex_buffer[current_index + 7] = texq[i].coords[5];
vertex_buffer[current_index + 8] = texq[i].tex_coords[2];
vertex_buffer[current_index + 9] = texq[i].tex_coords[3];
vertex_buffer[current_index + 10] = texq[i].coords[6];
vertex_buffer[current_index + 11] = texq[i].coords[7];
vertex_buffer[current_index + 12] = texq[i].coords[8];
vertex_buffer[current_index + 13] = texq[i].tex_coords[4];
vertex_buffer[current_index + 14] = texq[i].tex_coords[5];
vertex_buffer[current_index + 15] = texq[i].coords[9];
vertex_buffer[current_index + 16] = texq[i].coords[10];
vertex_buffer[current_index + 17] = texq[i].coords[11];
vertex_buffer[current_index + 18] = texq[i].tex_coords[6];
vertex_buffer[current_index + 19] = texq[i].tex_coords[7];
current_index = current_index + 20;
}
}
// Setup vertex buffer, index buffer and vertex array
{
VAO.Bind();
VBO.BufferData(vb_size, vertex_buffer, GL_STATIC_DRAW);
VBO.VertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
VBO.VertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
IBO.BufferData(ib_size, index_buffer, GL_STATIC_DRAW);
VAO.Unbind();
}
shader.Use();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex_id);
glUniform1i(glGetUniformLocation(shader.GetProgramID(), "u_Texture"), 0);
VAO.Bind();
glDrawElements(GL_TRIANGLES, 6 * texq.size(), GL_UNSIGNED_INT, 0);
//glDrawArrays(GL_TRIANGLES, 0, 24);
VAO.Unbind();
return;
}
这实际上是非常基本的。它从结构中生成顶点缓冲区和索引缓冲区,并在屏幕上绘制纹理。这是ObjectInstance 结构。
struct ObjectInstance
{
float coords[12];
float tex_coords[8];
};
最后是我的 main 函数的 sn-p。
ObjectInstance t1 = { {-1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f},
{1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};
ObjectInstance t2 = { {-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f},
{1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};
ObjectInstance t3 = { {0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f},
{1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};
ObjectInstance t4 = { {0.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
{1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};
std::vector<ObjectInstance> test_vector;
test_vector.push_back(t1);
test_vector.push_back(t2);
test_vector.push_back(t3);
test_vector.push_back(t4);
我在游戏循环中使用适当的参数调用该函数。
最后是我的顶点和片段着色器。 顶点着色器:
#version 330 core
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 texCoord;
out vec2 TexCoord;
void main()
{
gl_Position = vec4(position, 1.0f);
TexCoord = texCoord;
}
片段着色器
#version 330 core
in vec2 TexCoord;
out vec4 color; // the color outputted
uniform sampler2D u_Texture;
void main()
{
color = texture(u_Texture, TexCoord);
}
不,我有一个要绘制 4 次的纹理。它只是我想在屏幕的不同坐标中绘制的单个纹理。但是,显然我只在左下角打印了其中一个。你知道我为什么会收到这个错误吗?
【问题讨论】:
-
@VictorGubin 首先,非常感谢您的好意。不,我有一个要绘制 4 次的纹理。它只是我想在屏幕的不同坐标中绘制的单个纹理。但是,显然我只在左下角渲染了一个。我必须在不同的坐标处渲染 4 个相同的纹理。你知道我为什么会出现这种奇怪的行为吗?谢谢:)
-
@VictorGubin 我解决了这个问题.. 非常感谢!问题是 vb_size 和 ib_size 分配给了不正确的大小。由于 OpenGL 需要一个字节大小,我不得不将值乘以 sizeof(GLfloat)
标签: c++ opengl opengl-3 opengl-4