//http://stackoverflow.com/questions/12518111/how-to-load-a-bmp-on-glut-to-use-it-as-a-texture
#include <windows.h>
#include <stdio.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glut.h>
GLuint LoadTexture( const char * filename )
{
GLuint texture;
unsigned char * data;
FILE * file;
file = fopen( filename, "rb" );
data = (unsigned char*)malloc( 256 * 256 * 3 );
fseek(file,54,0);
fread( data, 256 * 256 * 3, 1, file);
fclose(file);
int i;
for( i = 0; i < 256 * 256 ; i++)
{
unsigned char* swap;
swap = data[i*3];
data[i*3] = data[i*3+2];
data[i*3+2] = swap;
}
glBindTexture( GL_TEXTURE_2D, texture );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
free( data );
}
void init(void)
{
glEnable ( GL_TEXTURE_2D );
LoadTexture("test.bmp");
}
void reshape( int w, int h )
{
glViewport ( 0, 0, w, h );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
gluPerspective ( 80, ( float ) w / ( float ) h, 1.0, 1000.0 );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ( );
}
void display( void )
{
init();
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity ( );
glPushMatrix();
glTranslatef ( 0.0, 0.0, -5.0 );
glBegin ( GL_QUADS );
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void keyboard ( unsigned char key, int x, int y )
{
if (key == 27) exit(0);
}
int main ( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
glutInitWindowSize (500, 500 );
glutCreateWindow ( "" );
glutFullScreen();
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutKeyboardFunc( keyboard );
glutIdleFunc( display );
glutMainLoop();
}
|