Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';t从字节数组(JOGL)创建纹理_Java_Opengl_Textures_Jogl - Fatal编程技术网

Java Can';t从字节数组(JOGL)创建纹理

Java Can';t从字节数组(JOGL)创建纹理,java,opengl,textures,jogl,Java,Opengl,Textures,Jogl,我正在尝试将C#应用程序移植到Java,目前正在翻译OpenGL代码。在C#应用程序中使用了OpenTK,在Java应用程序中我选择了JOGL。当我尝试创建纹理时,程序会抛出一个异常。以下是导致故障的代码: BMD0.Model.ModelData.Material.MatDef mat = (BMD0.Model.ModelData.Material.MatDef) model.model.mdlData[0].material.material[i];

我正在尝试将C#应用程序移植到Java,目前正在翻译OpenGL代码。在C#应用程序中使用了OpenTK,在Java应用程序中我选择了JOGL。当我尝试创建纹理时,程序会抛出一个异常。以下是导致故障的代码:

            BMD0.Model.ModelData.Material.MatDef mat = (BMD0.Model.ModelData.Material.MatDef) model.model.mdlData[0].material.material[i];
            ImageData tmp_tex = Nsbtx.getTexture(tex, mat.texID, mat.palID).getImageData();
            gl.glBindTexture(GL2.GL_TEXTURE_2D, texturesID.get(i));
            ByteBuffer tmp_tex_data = ByteBuffer.allocate(tmp_tex.data.length);
            tmp_tex_data.put(tmp_tex.data);
            tmp_tex_data.flip();
            gl.glTexImage2D(texturesID.get(i), 0, GL2.GL_RGB, tmp_tex.width, tmp_tex.height, 0, GL2.GL_RGB, GL2.GL_UNSIGNED_BYTE, tmp_tex_data);
            texturesGL.put(i, texturesID.get(i));
例外情况如下:

java.lang.IndexOutOfBoundsException: Required 192 remaining bytes in buffer, only had 32
我正在使用SWT的ImageData加载RAM中的一个图像(它不是一个外部文件),它只有32字节长!为什么JOGL期望这么多字节

这是我第一个使用OpenGL的程序,所以我不是专家

编辑:这是对应的C#代码:


好的,我已经解决了所有问题,取消索引并生成RGBA(也有alpha值)值

BMD0.Model.ModelData.Material.MatDef mat=(BMD0.Model.ModelData.Material.MatDef)Model.Model.mdlData[0].Material.Material[i];
ImageData tmp_tex=Nsbtx.getTexture(tex,mat.texID,mat.palID).getImageData();
gl.glBindTexture(GL2.gl_TEXTURE_2D,texturesID.get(i));
ByteBuffer tmp_tex_data=ByteBuffer.allocate(tmp_tex.height*tmp_tex.width*4);
PaletteData pal=tmp_tex.palette;
对于(int h=0;h
什么是
tex\u宽度
tex\u高度
?32字节对于一个图像来说不是很多数据。。。对于2x4像素的东西来说就足够了。它们分别是纹理宽度和纹理高度。是的,图像应为4x4像素2x4像素在此处无法正常工作,但行宽为2会导致8位RGB图像数据的对齐问题。2像素*3字节表示第二行、第三行和第四行从3字节边界开始,而不是4字节边界。第二行和第四行可能未对齐。也就是说,每个3字节的4x4像素是48字节,而不是32.8*8*3=192,这解释了为什么缓冲区的长度预期为192字节。您将不得不取消对该图像的调用,因为
glTexImage2D(…)
不能以这种方式工作,它需要RGB三元组(每个组件1字节,3个组件)。对于您正在讨论的工作,似乎有4位调色板条目。所以是某种16色图像。Andon与解释非常接近,这样的测试在将数据传递给OpenGL之前由JOGL内部完成,比崩溃要好。顺便说一句,请使用Buffers.newDirectByteBuffer()而不是将间接NIO缓冲区传递给JOGL。无论如何,您应该按照gouessej的建议使用GLBuffers,好的,所以我将尝试它们;)当你感到准备好并且自信时,你也可以“升级”到采样器,如果你需要如何做的帮助,请访问jogl论坛/irc;)
        int id = GL.GenTexture();
        GL.BindTexture(TextureTarget.Texture2D, id);

        Bitmap bmp = BTX0.GetTexture(pluginHost, tex, num_tex, num_pal);
        System.Drawing.Imaging.BitmapData bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

        GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp_data.Width, bmp_data.Height, 0,
            OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmp_data.Scan0);

        bmp.UnlockBits(bmp_data);

        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Nearest);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Nearest);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureWrapMode.Repeat);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)TextureWrapMode.Repeat);
        return id;
            BMD0.Model.ModelData.Material.MatDef mat = (BMD0.Model.ModelData.Material.MatDef) model.model.mdlData[0].material.material[i];
            ImageData tmp_tex = Nsbtx.getTexture(tex, mat.texID, mat.palID).getImageData();
            gl.glBindTexture(GL2.GL_TEXTURE_2D, texturesID.get(i));
            ByteBuffer tmp_tex_data = ByteBuffer.allocate(tmp_tex.height * tmp_tex.width * 4);
            PaletteData pal = tmp_tex.palette;
            for (int h = 0; h < tmp_tex.height; h++) {
                for (int w = 0; w < tmp_tex.width; w++) {
                    tmp_tex_data.put((byte) pal.getRGB(tmp_tex.getPixel(w, h)).red);
                    tmp_tex_data.put((byte) pal.getRGB(tmp_tex.getPixel(w, h)).green);
                    tmp_tex_data.put((byte) pal.getRGB(tmp_tex.getPixel(w, h)).blue);
                    tmp_tex_data.put((byte) tmp_tex.getAlpha(w, h));
                }
            }
            tmp_tex_data.flip();
            gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, tmp_tex.width, tmp_tex.height, 0, GL2.GL_RGBA, GL2.GL_UNSIGNED_BYTE, tmp_tex_data);
            texturesGL.put(i, texturesID.get(i));