nythrix's picture

Help with TexImage2D

Hi,
does anyone know how to actually create textures? what do i pass to GL.TexImage2D as the last parameter? i used to pass BitmapData.Scan0 like so:

using System;
using System.Drawing;
using System.Drawing.Imaging;
...
// bmp contains a valid bitmap with correct size (power of 2)
glGenTextures( 1, out id );
glBindTexture( GL_TEXTURE_2D, id );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
Rectangle rect = new Rectangle( 0, 0, bmp.Width, bmp.Height );
BitmapData bmpData = bmp.LockBits( rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb );
glTexImage2D( GL_TEXTURE_2D, 0, 3, bmp.Width, bmp.Height, 0, GL_RGB, GL_UNSIGNED_BYTE, bmpData.Scan0 );
bmp.UnlockBits( bmpData );
bmp.Dispose();
...

This works on Tao. Any hints regarding OpenTK?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler.'s picture

The following code works:

unsafe
{
    GL.TexImage2D(GL.Enums.TextureTarget.TEXTURE_2D, 0, 3, bmp.Width, bmp.Height, 0,
        (GL.Enums.PixelFormat)GL.Enums.All.BGR, GL.Enums.PixelType.UNSIGNED_BYTE, (void*)bmpData.Scan0);
}

Unsafe code is due to a bug in 0.3.12 that has been fixed in SVN. I'll add the missing formats to GL.Enums.PixelFormat to avoid the cast from GL.Enums.All, but otherwise, you can use the same code as in Tao (swapping in the correct enums of course).

nythrix's picture

oh yes, i forgot to mention i'd like to avoid unsafe code :)
does the bugfix mean i won't have to switch to unsafe?

GL.Enums.PixelFormat.RGB is already present in 0.3.12. i couldn't find GL.Enums.TextureWrapMode.CLAMP_TO_EDGE though.

thank you.

edit: i see CLAMP_TO_EDGE is already reported. never mind then.
edit2: you're right, BGR isn't present indeed.

the Fiddler.'s picture

oh yes, i forgot to mention i'd like to avoid unsafe code :)
does the bugfix mean i won't have to switch to unsafe?

Yes, OpenTK tries to provide safe alternatives to all unsafe functions - the fix allows you to pass the BitmapData.Scan0 return value directly, just like in Tao.