OpenGL (LWJGL):渲染到纹理不起作用



我有一些代码应该将文本渲染到纹理,这样我就不必在每个绘制步骤中渲染每个字符,而是可以使用渲染的纹理。但是,我的代码没有按预期进行,并且纹理留空。经过几个小时的尝试,我无法弄清楚,所以我把这个问题带给你。

我相当确定问题出在下面的代码块中的某个地方,但如果您认为不是,我很乐意发布您想要的任何其他代码示例。我只是真的很想已经完成这项工作。确切的问题是创建的纹理是空白的,并且永远不会渲染到(似乎是)。我试过只在上面画一个巨大的四边形,但这似乎也不起作用。

编辑:翻转缓冲区后,我可以将一些颜色渲染到纹理中,但它们都只是一种颜色(这让我认为它只是采样一个像素),而且我不知道如何获得我想要渲染的实际图像显示在其上。

public Text(String text, int x, int y, Font font, float size, GUIComponent parent, Binding binding) {
super(null, x, y, font.getStringWidth(size, text), font.getStringHeight(size), parent, binding, false);
this.text = text;
this.font = font;
this.width = font.getStringWidth(size, text);
this.height = font.getStringHeight(size);
int fbo = glGenFramebuffers();
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
int tex = glGenTextures();
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex, 0);
IntBuffer intBuffer = BufferUtils.createIntBuffer(1);
intBuffer.put(GL_COLOR_ATTACHMENT0);
intBuffer.flip();
glDrawBuffers(intBuffer);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
throw new RuntimeException("Something really bad happened");
}
//RENDER
RenderUtil.recalibrate(width, height, 1.0f); //Does glViewport(width, height), and some matrix stuff
Camera.updateShader("textshader", "projection", false); //Update projection matrix
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
int width = 0;
float f = this.width / 1.0f;
int charWidth = 0;
for (char c : text.toCharArray()) {
font.bind(0, c % 256); // calls glBindTexture, this works, have tested
//ResourceManager.getTexture("grassroadedger1").bind(0, 0);
charWidth = font.getCharWidth(size, c);
//float[] verts = new float[] { -1f, 1f, 1f, 1f, 1f, -1f, -1f, -1f };
float[] verts = new float[] { -1.0f + (width / f), 1.0f, 1.0f + ((width + charWidth) / f), 1.0f, 1.0f + ((width + charWidth) / f), -1.0f, -1.0f + (width / f), -1.0f };
width += charWidth;
glBindBuffer(GL_ARRAY_BUFFER, vertexPointer);
glBufferSubData(GL_ARRAY_BUFFER, 0, RenderUtil.createBuffer(verts));
glVertexAttribPointer(0, 2, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, RenderUtil.getIndicesPointer());
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
//END
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
RenderUtil.recalibrate(Window.getWidth(), Window.getHeight(), LuminaEngine.getGlobalImageScale());
this.setTexture(new Texture(tex, "text_"+size+"_"+text));
}

碎片着色器

#version 330 core
in vec2 uv;
layout(location = 0) out vec4 color;
uniform sampler2D sampler;
void main(){
color = texture2D( sampler, uv );
}

顶点着色器

#version 330 core
layout(location = 0) in vec3 vertices;
layout(location = 1) in vec2 textures;
out vec2 uv;
uniform mat4 projection;
void main(){
gl_Position =  projection * vec4(vertices,1);
uv = textures;
}

编辑:翻转drawBuffers的intBuffer后,我可以显示一些东西,主要是一个大的蓝色方块。尽管如此,进展还是

您从未为纹理坐标 (in vec2 textures;) 定义通用顶点属性数据数组。

将类似以下内容添加到代码中:

int texCoordBuffer;
glGenBuffers(1, texCoordBuffer);
float[] texCoord = new float[] { 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f };
glBindBuffer(GL_ARRAY_BUFFER, texCoordBuffer);
glBufferData(GL_ARRAY_BUFFER, RenderUtil.createBuffer(texCoord), GL_STATIC_DRAW);
int tax_attr_i = 1; // layout(location = 1) in vec2 textures;
glVertexAttribPointer(tax_attr_i, 2, GL_FLOAT, false, 0, 0);

最新更新