【问题标题】:OpenGL (LWJGL): Render to Texture not workingOpenGL (LWJGL):渲染到纹理不起作用
【发布时间】:2018-01-11 10:02:35
【问题描述】:

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

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

编辑:翻转缓冲区后,我可以得到一些颜色来渲染到纹理,但它只是一种颜色(这让我认为它只是采样一个像素),我不知道如何得到我要渲染的实际图像。

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 之后,我可以看到一些东西,大部分只是一个蓝色的大方块。还是有进步的

【问题讨论】:

    标签: java opengl lwjgl


    【解决方案1】:

    您从未为纹理坐标 (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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 2023-03-30
      • 1970-01-01
      • 2014-08-09
      相关资源
      最近更新 更多