132001f49Smrg/* GL_ARB_fragment_program texture test */ 232001f49Smrg 332001f49Smrg#include <assert.h> 432001f49Smrg#include <string.h> 532001f49Smrg#include <stdio.h> 632001f49Smrg#include <stdlib.h> 732001f49Smrg#include <math.h> 832001f49Smrg#include <GL/glew.h> 932001f49Smrg#include "glut_wrap.h" 1032001f49Smrg 1132001f49Smrg#include "readtex.c" 1232001f49Smrg 1332001f49Smrg 1432001f49Smrg#define TEXTURE_FILE DEMOS_DATA_DIR "girl.rgb" 1532001f49Smrg 1632001f49Smrgstatic GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0; 1732001f49Smrg 1832001f49Smrg#define PI 3.141592 1932001f49Smrg 2032001f49Smrgstatic void Display( void ) 2132001f49Smrg{ 2232001f49Smrg glClear( GL_COLOR_BUFFER_BIT ); 2332001f49Smrg 2432001f49Smrg glPushMatrix(); 2532001f49Smrg glRotatef(Xrot, 1.0, 0.0, 0.0); 2632001f49Smrg glRotatef(Yrot, 0.0, 1.0, 0.0); 2732001f49Smrg glRotatef(Zrot, 0.0, 0.0, 1.0); 2832001f49Smrg 2932001f49Smrg glBegin(GL_POLYGON); 3032001f49Smrg glTexCoord2f(-PI, 0); glVertex2f(-1, -1); 3132001f49Smrg glTexCoord2f(PI, 0); glVertex2f( 1, -1); 3232001f49Smrg glTexCoord2f(PI, 1); glVertex2f( 1, 1); 3332001f49Smrg glTexCoord2f(-PI, 1); glVertex2f(-1, 1); 3432001f49Smrg glEnd(); 3532001f49Smrg 3632001f49Smrg glPopMatrix(); 3732001f49Smrg 3832001f49Smrg glutSwapBuffers(); 3932001f49Smrg} 4032001f49Smrg 4132001f49Smrg 4232001f49Smrgstatic void Reshape( int width, int height ) 4332001f49Smrg{ 4432001f49Smrg glViewport( 0, 0, width, height ); 4532001f49Smrg glMatrixMode( GL_PROJECTION ); 4632001f49Smrg glLoadIdentity(); 4732001f49Smrg glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 ); 4832001f49Smrg glMatrixMode( GL_MODELVIEW ); 4932001f49Smrg glLoadIdentity(); 5032001f49Smrg glTranslatef( 0.0, 0.0, -8.0 ); 5132001f49Smrg} 5232001f49Smrg 5332001f49Smrg 5432001f49Smrgstatic void SpecialKey( int key, int x, int y ) 5532001f49Smrg{ 5632001f49Smrg float step = 3.0; 5732001f49Smrg (void) x; 5832001f49Smrg (void) y; 5932001f49Smrg 6032001f49Smrg switch (key) { 6132001f49Smrg case GLUT_KEY_UP: 6232001f49Smrg Xrot += step; 6332001f49Smrg break; 6432001f49Smrg case GLUT_KEY_DOWN: 6532001f49Smrg Xrot -= step; 6632001f49Smrg break; 6732001f49Smrg case GLUT_KEY_LEFT: 6832001f49Smrg Yrot += step; 6932001f49Smrg break; 7032001f49Smrg case GLUT_KEY_RIGHT: 7132001f49Smrg Yrot -= step; 7232001f49Smrg break; 7332001f49Smrg } 7432001f49Smrg glutPostRedisplay(); 7532001f49Smrg} 7632001f49Smrg 7732001f49Smrg 7832001f49Smrgstatic void Key( unsigned char key, int x, int y ) 7932001f49Smrg{ 8032001f49Smrg (void) x; 8132001f49Smrg (void) y; 8232001f49Smrg switch (key) { 8332001f49Smrg case 27: 8432001f49Smrg exit(0); 8532001f49Smrg break; 8632001f49Smrg } 8732001f49Smrg glutPostRedisplay(); 8832001f49Smrg} 8932001f49Smrg 9032001f49Smrg 9132001f49Smrgstatic void Init( void ) 9232001f49Smrg{ 9332001f49Smrg static const char *modulate2D = 9432001f49Smrg "!!ARBfp1.0\n" 9532001f49Smrg "TEMP R0;\n" 9632001f49Smrg "MOV R0, {0,0,0,1};\n" 9732001f49Smrg "SCS R0, fragment.texcoord[0].x; \n" 9832001f49Smrg "ADD R0, R0, {1.0}.x;\n" 9932001f49Smrg "MUL R0, R0, {0.5}.x;\n" 10032001f49Smrg "MOV result.color, R0; \n" 10132001f49Smrg "END" 10232001f49Smrg ; 10332001f49Smrg GLuint modulateProg; 10432001f49Smrg GLuint Texture; 10532001f49Smrg 10632001f49Smrg if (!glutExtensionSupported("GL_ARB_fragment_program")) { 10732001f49Smrg printf("Error: GL_ARB_fragment_program not supported!\n"); 10832001f49Smrg exit(1); 10932001f49Smrg } 11032001f49Smrg printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); 11132001f49Smrg 11232001f49Smrg /* Setup the fragment program */ 11332001f49Smrg glGenProgramsARB(1, &modulateProg); 11432001f49Smrg glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, modulateProg); 11532001f49Smrg glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, 11632001f49Smrg strlen(modulate2D), (const GLubyte *)modulate2D); 11732001f49Smrg 11832001f49Smrg printf("glGetError = 0x%x\n", (int) glGetError()); 11932001f49Smrg printf("glError(GL_PROGRAM_ERROR_STRING_ARB) = %s\n", 12032001f49Smrg (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB)); 12132001f49Smrg assert(glIsProgramARB(modulateProg)); 12232001f49Smrg 12332001f49Smrg glEnable(GL_FRAGMENT_PROGRAM_ARB); 12432001f49Smrg 12532001f49Smrg /* Load texture */ 12632001f49Smrg glGenTextures(1, &Texture); 12732001f49Smrg glBindTexture(GL_TEXTURE_2D, Texture); 12832001f49Smrg glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 12932001f49Smrg glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 13032001f49Smrg glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 13132001f49Smrg if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) { 13232001f49Smrg printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE); 13332001f49Smrg exit(1); 13432001f49Smrg } 13532001f49Smrg /* XXX this enable shouldn't really be needed!!! */ 13632001f49Smrg glEnable(GL_TEXTURE_2D); 13732001f49Smrg 13832001f49Smrg glClearColor(.3, .3, .3, 0); 13932001f49Smrg} 14032001f49Smrg 14132001f49Smrg 14232001f49Smrgint main( int argc, char *argv[] ) 14332001f49Smrg{ 14432001f49Smrg glutInit( &argc, argv ); 14532001f49Smrg glutInitWindowPosition( 0, 0 ); 14632001f49Smrg glutInitWindowSize( 250, 250 ); 14732001f49Smrg glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); 14832001f49Smrg glutCreateWindow(argv[0]); 14932001f49Smrg glewInit(); 15032001f49Smrg glutReshapeFunc( Reshape ); 15132001f49Smrg glutKeyboardFunc( Key ); 15232001f49Smrg glutSpecialFunc( SpecialKey ); 15332001f49Smrg glutDisplayFunc( Display ); 15432001f49Smrg Init(); 15532001f49Smrg glutMainLoop(); 15632001f49Smrg return 0; 15732001f49Smrg} 158