Search code examples
javaopengltextureslwjglopengl-compat

I have a black texture


This is my first time working with textures in OpenGL, although I've been studying it for 4 months now. And when I try to load a texture (Just an image with a square) I get just a black square.\

My texture load code:

  byte[] pixelData = new byte[0];
        try {
            BufferedImage bi = ImageIO.read(getClass().getResource(TEXTURE_FILES));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bi, "png", baos);
            baos.flush();
            pixelData = baos.toByteArray();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ByteBuffer byteBuffer = ByteBuffer.wrap(pixelData);
        int texId = glGenTextures();
        glBindTexture(GL_TEXTURE_2D, texId);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 500, 500, 0,
                GL_RGB, GL_UNSIGNED_BYTE, byteBuffer);

        return texId;

I tried loading the texture with a simpler method, but it did not work. Also I tried another image or to place the texture not in my jar file

Texture show code:

                glEnable(GL_TEXTURE_2D);
                glColor4f(1f, 1f, 1f, 1f);
                glBindTexture(GL_TEXTURE_2D, texId);
                glBegin(GL_QUADS);
                glTexCoord2f(0, 0);
                glVertex2f(-1, -1);
                glTexCoord2f(1, 0);
                glVertex2f(1, -1);
                glTexCoord2f(1, 1);
                glVertex2f(1, 1);
                glTexCoord2f(0, 1);
                glVertex2f(-1, 1);
                glEnd();
                glDisable(GL_TEXTURE_2D);

My opengl paramters:

        glEnable(GL_ALPHA_TEST);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_COLOR_MATERIAL);
        glEnable(GL_TEXTURE_2D);
        glEnable(GL_BLEND);
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        glEnable(GL_NORMALIZE);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glShadeModel(GL_SMOOTH);
        glColorMask (true, true, true, true);
        glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);

I also read many other tips from this forum, but they are useless for me too

My result: result

I tried also to run it on another computer with a different video card, but the result remains the same


Solution

  • Possibly th issue is reading the png file:

    BufferedImage bi = ImageIO.read(getClass().getResource(TEXTURE_FILES));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(bi, "png", baos);
    baos.flush();
    pixelData = baos.toByteArray();
    baos.close();
    

    I've found a cde snippet (LWJGL3 Texture) where the file is read in a loop:

    InputStream is = getClass().getResourceAsStream(TEXTURE_FILES);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int read1 = is.read();
    while (read1 != -1) { 
        baos.write(read1);
        read1 = is.read();
    }
    byte[] pixelData= baos.toByteArray();
    baos.close();
    ByteBuffer byteBuffer = ByteBuffer.wrap(pixelData);
    

    Alternatively a PNGDecoder is used:
    (See also Load a PNG file with pure OpenGL and Loading PNG images with TWL's PNGDecoder)

    InputStream in = getClass().getResourceAsStream(TEXTURE_FILES);
    PNGDecoder decoder = new PNGDecoder(in);
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(3*decoder.getWidth()*decoder.getHeight());
    decoder.decode(byteBuffer, decoder.getWidth()*3, PNGDecoder.Format.RGB);
    byteBuffer.flip();