Set background in C++ using OpenGl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • computerfox
    Contributor
    • Mar 2010
    • 276

    Set background in C++ using OpenGl

    Hello everyone,

    I've been working on this application and I wanted to set an image as a background. I am using OpenGL, more specifically I'm on a Mac.

    My code:
    Code:
    void glBackground(char* filename){
    	//Set background image
            glEnable(GL_TEXTURE_2D);
            int width, height;
            unsigned char* data;
            FILE* file;
            width=2000;
            height=2000;
    	GLuint texture;
           	data=(unsigned char *)malloc( width * height * 3 );
            file=fopen(filename,"rb");
            fread(data, width * height * 3, 1, file);
            fclose(file);
    
    	glBindTexture( GL_TEXTURE_2D, texture );
    	
    	glGenTextures( 1, &texture );
        	glBindTexture( GL_TEXTURE_2D, texture );
        	glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
    
        	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR );
        	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
    	
        	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
        	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
    	
        	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,GL_RGB, GL_UNSIGNED_BYTE, data);
    	
    	
    	glBegin(GL_POLYGON);
    	glTexCoord2f(-1.-1,0.0); glVertex2f(-1, -1);
    	glTexCoord2f(1.-1,0.0); glVertex2f(1, -1);
    	glTexCoord2f(1.1,0.0); glVertex2f(1, 1);
    	glTexCoord2f(-1.1,0.0); glVertex2f(-1,1);
    	glEnd();
    
    	glFlush();
    }
    My problem:
    It doesn't load the image. Instead it just gives a splat of dots with random colors.

    Any ideas?
Working...