Loading Texture

cloke
Unregistered
 
Post: #1
I've been reading through the forums and Apples dev site, but I am stumped. I am trying to load an image using QT graphics import and then load the image as a GL texture. Below is my QT graphics importer. This works without an issue.

Code:
typedef struct {
        size_t width;
        size_t height;
        size_t bitsPerComponent;
        size_t bitsPerPixel;
        size_t bytesPerRow;
        size_t size;
        CGImageAlphaInfo ai;
        unsigned char *data;
    } BitmapInfo;

OSErr GetBitmapFileFromPStr( Str255 str, BitmapInfo *bi )
{
    FSSpec fss = ReadFile( str );
    OSErr  err;
    GraphicsImportComponent gi;
    err = GetGraphicsImporterForFile(&fss, &gi);
    readBitmapInfo(gi, bi);
    getBitmapData(gi, bi);

    CloseComponent(gi);
    return err;
}


The trouble I am having is then loading the image into a texture. I am really new with OpenGL so any help would be greatly appreciated. The code below is modified from NeHe, but I'm not sure what I am doing wrong.

Code:
    BitmapInfo      *TextureImage;
GLuint             texture;    
memset(TextureImage, 0, sizeof(void *)*2);
GetBitmapFileFromPStr( "\pFont.sgi", TextureImage );
    
glGenTextures(1, &texture);                    // Create The Texture

glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->width, TextureImage->height, 0,
GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
        
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);


I'm not sure what I may be doing wrong.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
aside from the fact that that snippet is missing all the relevant code, why not search the forums? This question is answered at least twice a week...
Quote this message in a reply
Member
Posts: 156
Joined: 2002.11
Post: #3
You may want to download the source code from http://www.angelfire.com/mac2/yexi/Basic...Struct.dmg

There, you will find a texture loader that works with JPG, PNG, or TGA.
Quote this message in a reply
cloke
Unregistered
 
Post: #4
Thanks for the above link. I was able to take my code to create a CGImage and use the above code to pass that into a GL Texture. It's cool to be able to take any QT supported file format and create the GL texture from that.
Quote this message in a reply
Member
Posts: 156
Joined: 2002.11
Post: #5
Just one thing: when creating the data buffer for the CGImage context, use calloc instead of malloc. Recently, I've tried to load some small PNG files and I was getting some noise because the buffer was not zeroed.

So, on LoadGLTextureFromCGImage():

Code:
//----------------------------------------------------------------------
    // Allocate memory block for bitmap buffer:
    
    data_buffer = (GLubyte *) calloc( bitmap_byteCount, sizeof( GLubyte ) );
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  iPhone texture loading issue yotryu 0 3,405 Mar 11, 2009 03:40 AM
Last Post: yotryu
  Texture Mapping: Loading a texture from a .bmp file? ishrock 5 6,108 Dec 13, 2008 09:27 AM
Last Post: ThemsAllTook
  OpenGL Texture Loading & Sprites corporatenewt 2 11,062 Jan 30, 2008 12:39 PM
Last Post: ynda20
  Texture Loading in Cocoa... dave05 3 8,073 Dec 11, 2007 03:12 AM
Last Post: DoG
  Cocoa Texture Loading Code Problem Nick 1 3,053 Oct 28, 2005 11:44 PM
Last Post: OneSadCookie