How can i use a texture .jpg image for an openGL background window in mac?

14,926

Solution 1

I consulted this Apple guide.

The problem is that the file is compressed AND has a header, and with your code you are even using part of the file header as an image source.

I would replace this code:

//    GLuint texture;
unsigned char * data;
FILE * file;

//The following code will read in our RAW file
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
data = (unsigned char *)malloc( width * height * 3 );
fread( data, width * height * 3, 1, file );
fclose( file );

With something more similar to this:

CFURLRef urlRef = (CFURLRef)[NSURL fileURLWithPath:@"/Users/macbook/MatrixEngineClientSample/Fighters/Sunset03.jpg"];
CGImageSourceRef myImageSourceRef = CGImageSourceCreateWithURL(url, NULL);
CGImageRef myImageRef = CGImageSourceCreateImageAtIndex(myImageSourceRef, 0, NULL);
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(myImageRef));
unsigned char *data = CFDataGetBytePtr(data);

You may need to add Core Graphics and Core Foundation to the list of your linked frameworks.

Solution 2

PNG and JPG images (as with most other image formats) require some form of decompression/decoding, so loading them raw from a file wont produce expected results. It should work with bmp or uncompressed tga images after reading the file header though :/ . Anyway, here are a few image loading libraries that should make loading images easy:

SOIL

DevIL

FreeImage

GLI

Share:
14,926
Emon
Author by

Emon

I am Md. Khaliful Islam Emon,Professional iOS developer and Technical Team Lead of a reputed Japanese company in Bangladesh. I do like late night coding and listening music.

Updated on June 05, 2022

Comments

  • Emon
    Emon almost 2 years

    I want to set a background for an openGL window for mac. background will take a jpg or png file.

    Here is my code..

    GLuint texture; //the array for our texture
    
    GLfloat angle = 0.0;
    
    GLuint LoadTexture (const char * filename, int width, int height ){
    
    //    GLuint texture;
    unsigned char * data;
    FILE * file;
    
    //The following code will read in our RAW file
    file = fopen( filename, "rb" );
    if ( file == NULL ) return 0;
    data = (unsigned char *)malloc( width * height * 3 );
    fread( data, width * height * 3, 1, file );
    fclose( file );
    
    glGenTextures( 1, &texture ); 
    glBindTexture( GL_TEXTURE_2D, texture ); 
    glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); 
    
    //    // when texture area is small, bilinear filter the closest mipmap
    //    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
    //                    GL_LINEAR_MIPMAP_NEAREST );
    //    // when texture area is large, bilinear filter the original
    //    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    //    
    //    // the texture wraps over at the edges (repeat)
    //    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    //    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
    //    
    //    //Generate the texture
    //    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,GL_RGB, GL_UNSIGNED_BYTE, data);
    
    
    
    // select modulate to mix texture with color for shading
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
    
    // when texture area is small, bilinear filter the closest mipmap
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                    GL_LINEAR_MIPMAP_NEAREST );
    // when texture area is large, bilinear filter the first mipmap
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    
    //    // the texture wraps over at the edges (repeat)
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
    
    // build our texture mipmaps
    gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
                      GL_RGB, GL_UNSIGNED_BYTE, data );
    
    free(data);
    
    return texture; //return whether it was successful
    
    }
    
    void FreeTexture( GLuint texture ){
    glDeleteTextures( 1, &texture );
    }
    
    void cube () {
    glEnable(GL_TEXTURE_2D);
    glBindTexture( GL_TEXTURE_2D, texture ); //bind the texture
    
    glPushMatrix();
    glRotatef( angle, 0.0f, 0.0f, 1.0f );
    glBegin( GL_QUADS );
    glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
    glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
    glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
    glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
    glEnd();
    glPopMatrix();
    glutSwapBuffers();
    //glutSolidCube(2);
    }
    
    void display () {
       glClearColor (0.0,0.0,0.0,1.0);
       glClear (GL_COLOR_BUFFER_BIT);
       glLoadIdentity();
       gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
       texture = LoadTexture( "/Users/macbook/MatrixEngineClientSample/Fighters/Sunset03.jpg", 256, 256  ); //load the texture
       glEnable( GL_TEXTURE_2D ); //enable 2D texturing
    //    glEnable(GL_TEXTURE_GEN_S); //enable texture coordinate generation
    //    glEnable(GL_TEXTURE_GEN_T);
      cube();
      FreeTexture( texture );
      //glutSwapBuffers();
      //angle ++;
    }
    
    void reshape (int w, int h) {
      glViewport (0, 0, (GLsizei)w, (GLsizei)h);
      glMatrixMode (GL_PROJECTION);
      //glLoadIdentity ();
      gluPerspective (50, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
      glMatrixMode (GL_MODELVIEW);
    }
    
    int main (int argc, char **argv) {
      glutInit (&argc, argv);
      glutInitDisplayMode (GLUT_DOUBLE);
      glutInitWindowSize (500, 500);
      glutInitWindowPosition (100, 100);
      glutCreateWindow ("A basic OpenGL Window");
      glutDisplayFunc (display);
      glutIdleFunc (display);
      glutReshapeFunc (reshape);
      glutMainLoop ();
      return 0;
    }
    

    EDIT

    I want to see this image as a background of the openGL window...image is below..

    enter image description here

    but it showing this...

    enter image description here

  • user1118321
    user1118321 over 11 years
    There's no need to resort to some hard-to-compile 3rd party library when the OS supports it natively. Just use the tools Apple supplies.
  • Nicolas Brown
    Nicolas Brown over 11 years
    i've never had a problem with any of them. What tools do apple provide?
  • user1118321
    user1118321 over 11 years
    The OS has built-in support for image formats. If you use Apple's, your users will automatically get support for any new formats Apple adds in OS updates, as well as bug fixes, etc. When you use a 3rd party library, you only get bug fixes when you re-link your app against the 3rd party library and re-release it.
  • Nicolas Brown
    Nicolas Brown over 11 years
    so, what tool does apple provide for loading images from files?
  • user1118321
    user1118321 over 11 years
    As mentioned above, there's NSImage, there's also CGImage. (For example, you can use CGImageCreateWithJPEGDataProvider().)
  • michaellindahl
    michaellindahl about 11 years
    To my knowledge you are trying to add Objective-C code to a C++ project. I don't think this will work.
  • Tyler
    Tyler about 10 years
    You can mix Obj-C and C++. Xcode uses filename extensions .hh and .mm for source that uses both.
  • Tyler
    Tyler about 10 years
    The following apple technical q&a provides sample code for understanding the format of the raw pixel data you have access to using code like that above: developer.apple.com/library/mac/qa/qa1509/_index.html