132001f49Smrg/* GL_NV_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 "../util/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 1932001f49Smrgstatic void Display( void ) 2032001f49Smrg{ 2132001f49Smrg glClear( GL_COLOR_BUFFER_BIT ); 2232001f49Smrg 2332001f49Smrg glPushMatrix(); 2432001f49Smrg glRotatef(Xrot, 1.0, 0.0, 0.0); 2532001f49Smrg glRotatef(Yrot, 0.0, 1.0, 0.0); 2632001f49Smrg glRotatef(Zrot, 0.0, 0.0, 1.0); 2732001f49Smrg 2832001f49Smrg glBegin(GL_POLYGON); 2932001f49Smrg glColor4f(1.0, 1.0, 1.0, 1); glTexCoord2f(0, 0); glVertex2f(-1, -1); 3032001f49Smrg glColor4f(0.2, 0.2, 1.0, 1); glTexCoord2f(1, 0); glVertex2f( 1, -1); 3132001f49Smrg glColor4f(0.2, 1.0, 0.2, 1); glTexCoord2f(1, 1); glVertex2f( 1, 1); 3232001f49Smrg glColor4f(1.0, 0.2, 0.2, 1); glTexCoord2f(0, 1); glVertex2f(-1, 1); 3332001f49Smrg glEnd(); 3432001f49Smrg 3532001f49Smrg glPopMatrix(); 3632001f49Smrg 3732001f49Smrg glutSwapBuffers(); 3832001f49Smrg} 3932001f49Smrg 4032001f49Smrg 4132001f49Smrgstatic void Reshape( int width, int height ) 4232001f49Smrg{ 4332001f49Smrg glViewport( 0, 0, width, height ); 4432001f49Smrg glMatrixMode( GL_PROJECTION ); 4532001f49Smrg glLoadIdentity(); 4632001f49Smrg glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 ); 4732001f49Smrg glMatrixMode( GL_MODELVIEW ); 4832001f49Smrg glLoadIdentity(); 4932001f49Smrg glTranslatef( 0.0, 0.0, -8.0 ); 5032001f49Smrg} 5132001f49Smrg 5232001f49Smrg 5332001f49Smrgstatic void SpecialKey( int key, int x, int y ) 5432001f49Smrg{ 5532001f49Smrg float step = 3.0; 5632001f49Smrg (void) x; 5732001f49Smrg (void) y; 5832001f49Smrg 5932001f49Smrg switch (key) { 6032001f49Smrg case GLUT_KEY_UP: 6132001f49Smrg Xrot += step; 6232001f49Smrg break; 6332001f49Smrg case GLUT_KEY_DOWN: 6432001f49Smrg Xrot -= step; 6532001f49Smrg break; 6632001f49Smrg case GLUT_KEY_LEFT: 6732001f49Smrg Yrot += step; 6832001f49Smrg break; 6932001f49Smrg case GLUT_KEY_RIGHT: 7032001f49Smrg Yrot -= step; 7132001f49Smrg break; 7232001f49Smrg } 7332001f49Smrg glutPostRedisplay(); 7432001f49Smrg} 7532001f49Smrg 7632001f49Smrg 7732001f49Smrgstatic void Key( unsigned char key, int x, int y ) 7832001f49Smrg{ 7932001f49Smrg (void) x; 8032001f49Smrg (void) y; 8132001f49Smrg switch (key) { 8232001f49Smrg case 27: 8332001f49Smrg exit(0); 8432001f49Smrg break; 8532001f49Smrg } 8632001f49Smrg glutPostRedisplay(); 8732001f49Smrg} 8832001f49Smrg 8932001f49Smrg 9032001f49Smrgstatic void Init( void ) 9132001f49Smrg{ 9232001f49Smrg static const char *modulate2D = 9332001f49Smrg "!!FP1.0\n" 9432001f49Smrg "TEX R0, f[TEX0], TEX0, 2D; \n" 9532001f49Smrg "MUL o[COLR], R0, f[COL0]; \n" 9632001f49Smrg "END" 9732001f49Smrg ; 9832001f49Smrg GLuint modulateProg; 9932001f49Smrg GLuint Texture; 10032001f49Smrg 10132001f49Smrg if (!glutExtensionSupported("GL_NV_fragment_program")) { 10232001f49Smrg printf("Error: GL_NV_fragment_program not supported!\n"); 10332001f49Smrg exit(1); 10432001f49Smrg } 10532001f49Smrg printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); 10632001f49Smrg 10732001f49Smrg /* Setup the fragment program */ 10832001f49Smrg glGenProgramsNV(1, &modulateProg); 10932001f49Smrg glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, modulateProg, 11032001f49Smrg strlen(modulate2D), 11132001f49Smrg (const GLubyte *) modulate2D); 11232001f49Smrg printf("glGetError = 0x%x\n", (int) glGetError()); 11332001f49Smrg printf("glError(GL_PROGRAM_ERROR_STRING_NV) = %s\n", 11432001f49Smrg (char *) glGetString(GL_PROGRAM_ERROR_STRING_NV)); 11532001f49Smrg assert(glIsProgramNV(modulateProg)); 11632001f49Smrg 11732001f49Smrg glBindProgramNV(GL_FRAGMENT_PROGRAM_NV, modulateProg); 11832001f49Smrg glEnable(GL_FRAGMENT_PROGRAM_NV); 11932001f49Smrg 12032001f49Smrg /* Load texture */ 12132001f49Smrg glGenTextures(1, &Texture); 12232001f49Smrg glBindTexture(GL_TEXTURE_2D, Texture); 12332001f49Smrg glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 12432001f49Smrg glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 12532001f49Smrg glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 12632001f49Smrg if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) { 12732001f49Smrg printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE); 12832001f49Smrg exit(1); 12932001f49Smrg } 13032001f49Smrg /* XXX this enable shouldn't really be needed!!! */ 13132001f49Smrg glEnable(GL_TEXTURE_2D); 13232001f49Smrg 13332001f49Smrg glClearColor(.3, .3, .3, 0); 13432001f49Smrg} 13532001f49Smrg 13632001f49Smrg 13732001f49Smrgint main( int argc, char *argv[] ) 13832001f49Smrg{ 13932001f49Smrg glutInit( &argc, argv ); 14032001f49Smrg glutInitWindowPosition( 0, 0 ); 14132001f49Smrg glutInitWindowSize( 250, 250 ); 14232001f49Smrg glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); 14332001f49Smrg glutCreateWindow(argv[0]); 14432001f49Smrg glewInit(); 14532001f49Smrg glutReshapeFunc( Reshape ); 14632001f49Smrg glutKeyboardFunc( Key ); 14732001f49Smrg glutSpecialFunc( SpecialKey ); 14832001f49Smrg glutDisplayFunc( Display ); 14932001f49Smrg Init(); 15032001f49Smrg glutMainLoop(); 15132001f49Smrg return 0; 15232001f49Smrg} 153