1

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

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Aryesia
  • 152
  • 7
  • `glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE)` makes not any sense. `GL_GENERATE_MIPMAP` is not a valid texture parameter. – Rabbid76 Apr 12 '20 at 05:30
  • @Rabbid76 if I remove it the problem still remains – Aryesia Apr 12 '20 at 05:42
  • Yes I know, that doesn't solve the issue, it was just a side note. I don't know whats wrong in your code. It seem to be the same issue in all of your last questions. – Rabbid76 Apr 12 '20 at 05:45
  • @Rabbid76 As I can see, you have a lot of experience working with OpenGL. Do not know what is my problem with a black texture? I can’t solve this problem for the fourth day. I visited over 100 different pages in several different forums, but nothing from there helped me ... – Aryesia Apr 12 '20 at 05:51
  • I've a lot of OpenGL knowledge, but my Java knowledge is poor. I can't see any issue in the OpenGL instructions. – Rabbid76 Apr 12 '20 at 05:53

1 Answers1

1

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();
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    Thanks. It really helped me. There are still a few problems, but they are absolutely not connected with this. The main thing is that I see my texture. I didn’t think that OpenGL requires special loading of textures. – Aryesia Apr 12 '20 at 06:52