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;
tex_widthandtex_height? 32 bytes is not much data for an image... It would be enough for something like 2x4 pixels.glTexImage2D (...)does not work that way, it wants RGB triplets (1-byte per component, 3 components). For what you are discussing to work, it seems you have 4-bit palette entries. So some sort of 16 color image.