Reputation: 215
I'm trying to port a C# application to Java, and I'm currently translating OpenGL code. In the C# app was used OpenTK, in the Java one I chose JOGL. When I try to create textures, the program throws an exception. Here is the code which is causing troubles:
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));
The exception is this:
java.lang.IndexOutOfBoundsException: Required 192 remaining bytes in buffer, only had 32
I'm using SWT's ImageData to load an image I have in the RAM (it's not an external file) and it's only 32 byte long! Why is JOGL expecting so much bytes?!
This is one of my first program with OpenGL, so I'm not that expert...
EDIT: This is the corresponding C# code:
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;
Upvotes: 2
Views: 721
Reputation: 215
Ok, I've solved by un-indexing everything and making RGBA (there was also alpha values) values.
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));
Upvotes: 2