/* * Compile with: * * gcc -g -Wall -o gl-texture gl-texture.c `pkg-config --cflags --libs gl glu` -lglut * * Based on: * Simple GLUT program to measure triangle strip rendering speed. * Brian Paul February 15, 1997 This file is in the public domain. */ #include #include #include #include #include #define SIZE 64 static void Display( void ) { glClearColor( 1.0, 1.0, 1.0, 1.0 ); glClear( GL_COLOR_BUFFER_BIT ); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2f(0, 0); glTexCoord2f(SIZE, 0); glVertex2f(SIZE, 0); glTexCoord2f(SIZE, SIZE); glVertex2f(SIZE, SIZE); glTexCoord2f(0, SIZE); glVertex2f(0, SIZE); glEnd(); glFinish(); glutSwapBuffers(); } static void Reshape( int width, int height ) { glViewport( 0, 0, width, height ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho(0.0, width, 0.0, height, -1.0, 1.0); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); } static void Key( unsigned char key, int x, int y ) { (void) x; (void) y; switch (key) { case 27: exit(0); break; } glutPostRedisplay(); } static void LoadTex() { GLubyte *pixels; int x, y; pixels = (GLubyte *) malloc(3*SIZE*SIZE); for (y = 0; y < SIZE; ++y) for (x = 0; x < SIZE; ++x) { if ((x / 4 + y / 4) % 2 == 0) { pixels[(y*SIZE+x)*3+0] = 0; pixels[(y*SIZE+x)*3+1] = 0; pixels[(y*SIZE+x)*3+2] = 0; } else { pixels[(y*SIZE+x)*3+0] = 255; pixels[(y*SIZE+x)*3+1] = 255; pixels[(y*SIZE+x)*3+2] = 255; } } glEnable(GL_TEXTURE_RECTANGLE_ARB); glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB8, SIZE, SIZE, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels); glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } static void Init( int argc, char *argv[] ) { LoadTex(); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE); glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); glEnable(GL_BLEND); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); } int main( int argc, char *argv[] ) { glutInit( &argc, argv ); glutInitWindowSize( (int) SIZE, (int) SIZE ); glutInitWindowPosition( 0, 0 ); glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); glutCreateWindow( argv[0] ); Init( argc, argv ); Reshape( SIZE, SIZE ); glutKeyboardFunc( Key ); glutDisplayFunc( Display ); glutMainLoop(); return 0; }