ofImage and glTexImage2D

Hello all, I’m new to the boards and new to OF.
I’ve been trying to learn some OF /openGL and encountered a problem along the way. When writing along with NeHe’s lesson #7 I didn’t want to go through all the code that was presented to be used for loading an image and instead used OF’s:

  
ofImage img;  
img.loadImage("test.bmp");  

This way of loading an image seems much simpler and that’s exactly what OF is for right?
The tutorial then made a texture out of the image using this code:

  
glGenTextures(3, &texture[0]);  
//first texture  
glBindTexture(GL_TEXTURE_2D, texture[0]); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);  
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);  
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);  
  

The last line of code describes a ‘TextureImage[0]’.
Ofcourse I didn’t have a ‘TextureImage[0]’ because I Loaded my image in a different way so I thougth I would alter the code like this:

  
glTexImage2D(GL_TEXTURE_2D, 0, 3, img.getWidth(),img.getHeight(),0,GL_RGB,GL_UNSIGNED_BYTE,img.getPixels());  

The program does run like this but instead of seeing the right image/texture it shows a picture of what seems like evenly scattered pixels, almost as if the array with pixels is being fed to the glTexImage2D function but he doesn’t know how to place it right.
Now I could just use the code from the NeHe tutorial to load the image but I just don’t fully understand what all the code lines do… and I’de rather not type 50 lines of code without understanding what they’re about.

Hope anyone can help me!

Are you just trying to draw the image? You can do that just by calling:

  
img.draw(0, 0, 200, 200); // whatever the size of your image is  

If you want to get the texture from an image you can get an image using the getTextureReference() method of the ofImage instance. As a beginner, using the ofTexture is probably a lot easier than working with the raw openGL texturing calls like in the Nehe tutorials. You can also read through the ofTexture and ofImage calls to see how those calls are implemented and learn a lot about how textures work in openGL.

I went ahead and took a look at ofTexture and made it work using the following code:

  
  
ofTexture myTexture;  
myTexture.allocate(256,256,GL_RGB);  
myTexture.loadData(myImage.getPixels(),256,256,GL_RGB);  
glEnable(myTexture.getTextureData().textureTarget);  
  
//After this use glVertex to make a quad and glTexCoords to put the texture  
// on it.  
  

I found it confusing that I had to allocate the texture first and then load it.

Also I had some trouble with the glEnable because at first I was using glEnable(GL_TEXTURE_2D)
Appareantly this wasn’t the right thing to do and after some searching I found the right code, even though I don’t understand why it has to be like this.

Thank you for the tips! I’ll try to leave the complex things out of my code for now.