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

FragmentShader

    #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);
链接地址: http://www.djcxy.com/p/34043.html

上一篇: OpenGL (LWJGL): Render to Texture not working

下一篇: LWJGL Textures not rendering