나는 검은 질감을 가지고있다. (I have a black texture)


문제 설명

나는 검은 질감을 가지고있다. (I have a black texture)

OpenGL에서 텍스처 작업을 하는 것은 이번이 처음이지만, 지금 4개월 동안 연구하고 있습니다. 그리고 텍스처를 로드하려고 하면(정사각형이 있는 이미지만) 검은색 정사각형만 표시됩니다.\

내 텍스처 로드 코드:

  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;

다음으로 텍스처를 로드하려고 했습니다. 더 간단한 방법이지만 작동하지 않았습니다. 또한 다른 이미지를 시도하거나 내 jar 파일에 없는 텍스처를 배치하려고 했습니다.

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 매개변수:

6
        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);

나도 많은 것을 읽었습니다. 이 포럼의 다른 팁이지만 나에게도 쓸모가 없습니다.

내 결과: 결과


참조 솔루션

방법 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();

(by AryesiaRabbid76)

참조 문서

  1. I have a black texture (CC BY‑SA 2.5/3.0/4.0)

#opengl-compat #lwjgl #textures #opengl #java






관련 질문

레거시 OpenGL에서 독립적인 투명도 주문 (Order independent transparency in legacy OpenGL)

나는 검은 질감을 가지고있다. (I have a black texture)

시작/끝 각도 문제가 있는 OpenGL 타원 (OpenGL Ellipse with start/end angle issues)

행렬 기반의 OpenGL로 표시 (Displaying in openGL based on a matrix)

Python에서 OpenGL로 텍스처를 렌더링하는 것은 (Rendering a texture with OpenGL in Python is not)

OpenTK C#에서 객체 방향 가져오기 (Get orientation of object in OpenTK C#)







코멘트