Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 12

Thread: Can't load .png image using opengl texture

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2010
    Posts
    20

    Question Can't load .png image using opengl texture

    Code :
        #include <stdio.h>
        #include <GL/gl.h>
        #include <GL/glut.h>
     
        #define KEY_ESCAPE 27
     
        void display();
        void keyboard(unsigned char,int,int);
        GLuint LoadTextureRAW( const char * filename, int wrap );
     
        int main(int argc, char **argv) {
            glutInit(&argc, argv);                                    
            glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH );
            glutInitWindowSize(600,400);                  
            glutCreateWindow("Opengl Test");                              
            glutDisplayFunc(display);
            glutKeyboardFunc(keyboard);
            glutMainLoop();
            return 0;
        }
        void display() {
            GLuint texture=LoadTextureRAW("ball.png",1);
            glEnable(GL_TEXTURE_2D);
            glBindTexture(GL_TEXTURE_2D,texture);
            glBegin( GL_QUADS );
            glTexCoord2d(0.0,0.0); glVertex2d(0.0,0.0);
            glTexCoord2d(1.0,0.0); glVertex2d(1.0,0.0);
            glTexCoord2d(1.0,1.0); glVertex2d(1.0,1.0);
            glTexCoord2d(0.0,1.0); glVertex2d(0.0,1.0);
            glEnd();
            glutSwapBuffers();    
        }
        // load a 256x256 RGB .RAW file as a texture
        GLuint LoadTextureRAW( const char * filename, int wrap )
        {
            GLuint texture;
            int width, height;
        //    BYTE * data;
            int *data;
            FILE * file;
     
            // open texture data
            file = fopen( filename, "rb" );
            if ( file == NULL ) return 0;
     
            // allocate buffer
            width = 256;
            height = 256;
            data = (int*)malloc( width * height * 3 );
     
            // read texture data
            fread( data, width * height * 3, 1, file );
            fclose( file );
     
            // allocate a texture name
            glGenTextures( 1, &texture );
     
            // select our current texture
            glBindTexture( GL_TEXTURE_2D, texture );
     
            // 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 );
     
            // if wrap is true, the texture wraps over at the edges (repeat)
            //       ... false, the texture ends at the edges (clamp)
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
                             wrap ? GL_REPEAT : GL_CLAMP );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
                             wrap ? GL_REPEAT : GL_CLAMP );
     
            // build our texture mipmaps
            gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
                               GL_RGB, GL_UNSIGNED_BYTE, data );
     
            // free buffer
            free( data );
     
            return texture;
        }
        void keyboard(unsigned char key, int mousePositionX, int mousePositionY) { 
            switch ( key ) {
                case KEY_ESCAPE:
                    exit ( 0 );   
                    break;      
                default:      
                    break;
            }
        }
    I followed this, http://www.nullterminator.net/gltexture.html

    What should i do?

  2. #2
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171
    Rather than just posting code and saying "What's wrong with this?" - try and explain clearly what you are trying to achive, what steps you have taken (providing relevant code where appropriate) and then explain what you are observing.

    Mearly asking "What should i do? " is not helping us help you - how do we know what's wrong unless you tell us?

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2010
    Posts
    20
    It's not loading that .png file, some say i need libpng to make the png to raw file. I can't understand how to do it.

  4. #4
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171
    Avoid PNG files? Use an art package and convert to something you can load.
    I made a choice many years ago to not rely upon someone elses work (library functions ).

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2010
    Posts
    20
    What is an art package? Why don't you want to rely upon someone elses work? What do you do then?

  6. #6
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171
    An art package like PhotoShop or Paintshop Pro.
    Why don't you want to rely upon someone elses work?
    Because quite often these pojects end up as abandonware - either not updated or not updated quickly enough. I did not want my projects to rely upon a certain feature set and then find out that that feature is no longer available. I'd rather write my own.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •