New to LWJGL - Need help with textures

Started by Prof_Kewig, March 09, 2016, 04:25:48

Previous topic - Next topic

Prof_Kewig

I am working on a GUI for a game. I'm using STB's true type loader to write text to a 512 x 512 ByteBuffer, then trying to display that generated texture. Everything seems to check out, but no text shows. I've verified the texture coordinates and the vertex coordinates. Here's the basic gist of my GUI:

      
    private static final int vertex_id = 0;
    private static final int texture_id = 1;

@Override
    protected void initAttributes() {
        GL20.glBindAttribLocation(program_id, vertex_id, "vertex");
        GL20.glBindAttribLocation(program_id, texture_id, "tex_coord");
    }

    @Override
    protected void initTextures() {
        gui_tex = GL20.glGetUniformLocation(program_id, "gui_bg");
    }

    @Override
    protected void initUniforms() {
        debug();
        fg_id = GL20.glGetUniformLocation(program_id, "fg_color");
        bg_id = GL20.glGetUniformLocation(program_id, "bg_color");
    }

    @Override
    public void begin_drawing(Lighting lights, Camera player) {
        GL20.glUseProgram(program_id);

        GL11.glEnable(GL11.GL_CULL_FACE);
        GL11.glCullFace(GL11.GL_BACK);
        GL11.glFrontFace(GL11.GL_CCW);

        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

        GL11.glDisable(GL11.GL_DEPTH_TEST);

        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);

        vertices.clear();
        indices.clear();
        for (Widget widget: widgets) {
            vertices.put(widget.getVertices());
            indices.put(widget.getIndices());
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, text_ids.get(widget.getID()));
            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RED, widget.getWidth(),
                    widget.getHeight(), 0, GL11.GL_RED, GL11.GL_UNSIGNED_BYTE, widget.getImageData());
//            STBImageWrite.stbi_write_png("test.png", widget.getWidth(), widget.getHeight(),
//                    STBImage.STBI_grey, widget.getImageData(), 0);
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        }
        vertices.rewind();
        indices.rewind();

        load_VBO(vertices);
        load_IBO(indices);
        GL20.glEnableVertexAttribArray(vertex_id);
        GL20.glEnableVertexAttribArray(texture_id);
        GL20.glVertexAttribPointer(vertex_id, 3, GL11.GL_FLOAT, false, STRIDE, VERTEX_OFFSET);
        GL20.glVertexAttribPointer(texture_id, 2, GL11.GL_FLOAT, false, STRIDE, TEXTURE_OFFSET);
        GL20.glUniform1i(gui_tex, 0);
    }

    @Override
    public void redraw() {
        int start; int end = -1, offset = 0;
        for (Widget widget: widgets) {
            int i_cnt = widget.getIndices().length;
            start = end + 1;
            end = start + widget.getVertices().length / 8;


            GL13.glActiveTexture(GL13.GL_TEXTURE0);
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, text_ids.get(widget.getID()));

            fg.clear();
            bg.clear();
            widget.getFG().put(fg).rewind();
            widget.getBG().put(bg).rewind();
            GL20.glUniform4fv(fg_id, fg);
            GL20.glUniform4fv(bg_id, bg);

            GL12.glDrawRangeElements(GL11.GL_TRIANGLES, start, end, i_cnt, GL11.GL_UNSIGNED_INT, offset);
            offset += i_cnt;
        }

    }

    @Override
    public void end_drawing() {
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        GL20.glDisableVertexAttribArray(vertex_id);
        GL20.glDisableVertexAttribArray(texture_id);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        GL20.glUseProgram(0);
        if (frame == 0) {
            readback.clear();
            GL11.glReadPixels(0, 0, 800, 600, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, readback);
            readback.rewind();
            STBImageWrite.stbi_write_png("FrameCapture.png", 800, 600, 4, readback, 0);
        }
    }


Here's my vertex shader (yes, it's backwards compatible to OpenGL 2.0):

#version 120

attribute vec3 vertex;
attribute vec2 tex_coord;
varying vec2 v_tex_coord;

void main() {
    v_tex_coord = tex_coord;
    gl_Position = vec4(vertex.xy, 0.0, 1.0);
}


And my fragment shader:

#version 120

uniform sampler2D gui_bg;

uniform vec4 fg_color;
uniform vec4 bg_color;
varying vec2 v_tex_coord;

void main() {
    float tex_bg = texture2D(gui_bg, v_tex_coord).r;
    gl_FragColor = fg_color * tex_bg + bg_color * (1.0 - tex_bg);
//    gl_FragColor = texture2D(gui_bg, v_tex_coord);
//    gl_FragColor = vec4(v_tex_coord, 0, 1);
}


The two commented lines here were for debugging - my attempts to track down why the text doesn't show up. The texture is loaded. I was able to capture it back with glGetTexImage. The texture just doesn't render in the widget on screen. The vertices check out, and the texture co-ordinates check out.

Prof_Kewig

Here's an example of the texture read back in and a screen capture (sorry - the latter is upside down)