glTexImage2D question

Hello. I have been trying out C# and the Tao framework lately and I've run into a problem.
I wrote a little TGA image loader that stores the pixel values into a byte array. My problem is that I can't seem to load that data into OpenGL.

I saw the Nehe examples included in Tao and noticed that they are using the Bitmap class in System.Drawing for loading image data. As far as I know, the Bitmap class can't load a Targa file, and even then I still would like to know how all the IntPtr stuff works.

Right now the only way I have to know that the TGA loader is working is by doing this simple loop:


for (int i = 0; i < texture.texels.Length; i+=3)
{
Console.WriteLine("{0} {1} {2}",texture.texels[i], texture.texels[i+1],texture.texels[i+2]);
}

where texture.texels is the byte array.
This at least tells me that there is a lot of data in my array and that I should be seeing something

Here's the code I'm using to call glTexImage2D:


unsafe
{
fixed (byte* p = texture.texels)
{
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0 , texture.texType,
texture.w, texture.h, 0, texture.texFormat, Gl.GL_UNSIGNED_BYTE, (IntPtr)p);
}
}

After doing this, I made a little shader to apply the texture and all I'm seeing is black pixels. The loop that iterates on the byte array does not show any black pixels (and the image doesn't have any) so I am assuming that there's something wrong with calling glteximage2d that way. Using fixed functionality shows the model as if there was no texturing enabled.

So, can anyone tell me hoy am I supposed to pass my texture info into glTexImage2D?
I could always just use the Bitmap and BitmapData class but I would like to know how is it working =)
Thanks!

Looks good to me... Only

Looks good to me... Only thing I can think of is using new IntPtr(p) instead of the cast.

You can try it the other way around: load a bitmap image as done in the NeHe examples and see if your renderpath works?

----------------------
Download Tao svn snapshot build
Experimental Tao rpms (OpenSUSE Build Service)

Thanks for the idea I tried

Thanks for the idea Smiling
I tried it out, loaded a bmp image with the Bitmap class and it shows on my model.

Then I call glTexImage2D again with my array, without changing the texture name and parameters, and nothing shows up =/ I am getting just white pixels since I disabled lighting.


Looks good to me... Only thing I can think of is using new IntPtr(p) instead of the cast.

I also tried that and it's still not working.
There must be something wrong with my array and the way that I am sending it.

The array just contains bytes in RGB order.
The call to the function is this

Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGB,texture.w, texture.h, 0, Gl.GL_RGB, GL_UNSIGNED_BYTE, /*Tried everything here :P*/);

I don't know what could be wrong..
Thanks for the reply!

Are you using Mono or .Net?

Are you using Mono or .Net? If it is Mono, which version? Some older versions (I think <1.2.5) had a bug where passing an IntPtr to glTexImage2D would call the "object" overload of the function.

What happens if you pass the array directly, without pinning?

Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0 , texture.texType,
texture.w, texture.h, 0, texture.texFormat, Gl.GL_UNSIGNED_BYTE, texture.texels);

If that doesn't work either, try this (to help narrow down on the cause):

byte[] texels = new byte[256*256*3];
for (int i = 0; i < texels.Length; i++)
texels[i] = 255;
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGB, 256, 256, 0, Gl.GL_RGB, GL_UNSIGNED_BYTE, texels);

Does this show a white texture as it should?

------
OpenTK

Thanks a lot for the

Thanks a lot for the replies. I just fixed it. (I am using .Net)

I did what StApostol wrote (filling a new array) and it also worked by just sending the array. Turns out that in my glteximage2d call:

Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0 , texture.texType,
texture.w, texture.h, 0, texture.texFormat, Gl.GL_UNSIGNED_BYTE, texture.texels);

I was using texture.w and texture.h as parameters when the size information was in the header and had not been written to the texture struct.. Dumb mistake. Now it works perfectly Smiling

Thanks again

Theme by La Boite a site | Powered by Drupal