Datriot's picture

Loading Textures

I'm pretty new to OpenGL, as I've been a DirectX use for the most part. I must say that texture loading and usage is quite different in OpenGL and I'm not sure how to go about loading and displaying one ( preferably on a quad ).
There's some tutorials out there, but one catered for OpenTK would be helpful so help would be appreciated. :)


Comment viewing options

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

A good place to start would be getting the data off of the hard drive and into memory. You can use the System.Drawing name space for this purpose:

//check if the file exists
if(System.IO.File.Exists(_Name + ".png"))
    //make a bitmap out of the file on the disk
    Bitmap TextureBitmap = new Bitmap(_Name + ".png");
    //get the data out of the bitmap
    System.Drawing.Imaging.BitmapData TextureData = 
            new System.Drawing.Rectangle(0,0,TextureBitmap.Width,TextureBitmap.Height),
    //Code to get the data to the OpenGL Driver
    //generate one texture and put its ID number into the "Texture" variable
    GL.GenTextures(1,out Texture);
    //tell OpenGL that this is a 2D texture
    //the following code sets certian parameters for the texture
    //load the data by telling OpenGL to build mipmaps out of the bitmap data
        (int)PixelInternalFormat.Three, TextureBitmap.Width, TextureBitmap.Height,
	PixelFormat.Bgr, PixelType.UnsignedByte,
    //free the bitmap data (we dont need it anymore because it has been passed to the OpenGL driver
Thats basically it. It's quite simple considering that not everything in the above code is essential to loading a texture.
Datriot's picture

Thanks Stevo, that's exactly what I needed, I got the texture to load, now I can just slap it onto a quad. I'll have to experiment a bit more and find out all the different settings and stuff you can have.
Thanks again!

Inertia's picture

Welcome Datriot :)

The OpenTK distribution also includes a couple of examples you can take a look at. They reside in the folder Source/Examples/Tutorial

Stevo14's picture

Yea, experimenting will help you find out what is possible. If you don't need mipmaps, you can alternatively load the texture using "GL.TexImage2D(/*..params...*/);" and replace "TextureMinFilter.LinearMipmapLinear" with just "TextureMinFilter.Linear".

Datriot's picture

Ah, didn't notice the examples in the folder, sorry about that.
Thanks for all the help guys, it's going pretty well. :)

Inertia's picture

No problem, it's a bit hidden.

I should probably point at http://www.glprogramming.com/red/ too, which is a quality book to get started with OpenGL.

the Fiddler's picture

A couple of observations:

  • IIRC, you can pass a filename directly to the Bitmap constructor (no need for an intermediate image).
  • Don't forget to call the UnlockBits method, to free the resources used by LockBits.
Stevo14's picture

Thanks Fiddler, I'll update it here and in my own code.

Datriot's picture

Thanks guys, I've been going over that tutorial that Inertia posted and I came across the glReadPixels() and glDrawPixels(). Are they are faster and more effective way of using image data to create a 2D game?

the Fiddler's picture

Actually, no, they are the slowest and least efficient way to do 2D graphics (unless you are rendering video frames, that is) :)

The way to go is to load everything into textures/texture sheets with glTex* and glSubTex* functions and apply the textures on quads. Same result, just way faster.