繁体   English   中英

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

[英]OpenGL (LWJGL): Render to Texture not working

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

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

编辑:翻转缓冲区后,我可以将某种颜色渲染到纹理上,但是全都是一种颜色(这使我认为它只是对一个像素进行采样),而且我不知道如何获取实际图像我想渲染以显示它。

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

编辑:翻转intBuffer为drawBuffers之后,我可以使一些东西出现,主要是一个大的蓝色方块。 尽管如此进步

您从未为纹理坐标定义任何通用顶点属性数据的数组( 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM