我有黑色的质感 [英] I have a black texture

查看:76
本文介绍了我有黑色的质感的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次在OpenGL中使用纹理,尽管我已经研究了4个月了.当我尝试加载纹理(仅带有正方形的图像)时,我只会得到一个黑色正方形.\

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.\

我的纹理加载代码:

  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文件中

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

纹理显示代码:

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

我的opengl参数:

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

我的结果:

我也尝试在具有不同视频卡的另一台计算机上运行它,但是结果保持不变

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

推荐答案

可能的问题是读取png文件:

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

我找到了一个cde代码段( LWJGL3 Texture ),循环读取文件:

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

或者使用 PNGDecoder:
(另请参见使用纯OpenGL加载PNG文件使用TWL的PNGDecoder加载PNG图片)

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

这篇关于我有黑色的质感的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆