arbfptexture.c revision 32001f49
1/* GL_ARB_fragment_program texture test */
2
3#include <assert.h>
4#include <string.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <math.h>
8#include <GL/glew.h>
9#include "glut_wrap.h"
10
11#include "readtex.c"
12
13
14#define TEXTURE_FILE DEMOS_DATA_DIR "girl.rgb"
15
16static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
17
18
19static void Display( void )
20{
21   glClear( GL_COLOR_BUFFER_BIT );
22
23   glPushMatrix();
24   glRotatef(Xrot, 1.0, 0.0, 0.0);
25   glRotatef(Yrot, 0.0, 1.0, 0.0);
26   glRotatef(Zrot, 0.0, 0.0, 1.0);
27
28   glBegin(GL_POLYGON);
29#define Q 2
30   glColor4f(1.0, 1.0, 1.0, 1);   glTexCoord4f(0, 0, 0, Q);   glVertex2f(-1, -1);
31   glColor4f(0.2, 0.2, 1.0, 1);   glTexCoord4f(1, 0, 0, Q);   glVertex2f( 1, -1);
32   glColor4f(0.2, 1.0, 0.2, 1);   glTexCoord4f(1, 1, 0, Q);   glVertex2f( 1,  1);
33   glColor4f(1.0, 0.2, 0.2, 1);   glTexCoord4f(0, 1, 0, Q);   glVertex2f(-1,  1);
34   glEnd();
35
36   glPopMatrix();
37
38   glutSwapBuffers();
39}
40
41
42static void Reshape( int width, int height )
43{
44   glViewport( 0, 0, width, height );
45   glMatrixMode( GL_PROJECTION );
46   glLoadIdentity();
47   glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
48   glMatrixMode( GL_MODELVIEW );
49   glLoadIdentity();
50   glTranslatef( 0.0, 0.0, -8.0 );
51}
52
53
54static void SpecialKey( int key, int x, int y )
55{
56   float step = 3.0;
57   (void) x;
58   (void) y;
59
60   switch (key) {
61      case GLUT_KEY_UP:
62         Xrot += step;
63         break;
64      case GLUT_KEY_DOWN:
65         Xrot -= step;
66         break;
67      case GLUT_KEY_LEFT:
68         Yrot += step;
69         break;
70      case GLUT_KEY_RIGHT:
71         Yrot -= step;
72         break;
73   }
74   glutPostRedisplay();
75}
76
77
78static void Key( unsigned char key, int x, int y )
79{
80   (void) x;
81   (void) y;
82   switch (key) {
83      case 27:
84         exit(0);
85         break;
86   }
87   glutPostRedisplay();
88}
89
90
91static void Init( void )
92{
93   static const char *modulate2D =
94      "!!ARBfp1.0\n"
95      "TEMP R0;\n"
96      "TEX R0, fragment.texcoord[0], texture[0], 2D; \n"
97      "MUL result.color, R0, fragment.color; \n"
98      "END"
99      ;
100   GLuint modulateProg;
101   GLuint Texture;
102
103   if (!glutExtensionSupported("GL_ARB_fragment_program")) {
104      printf("Error: GL_ARB_fragment_program not supported!\n");
105      exit(1);
106   }
107   printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
108
109   /* Setup the fragment program */
110   glGenProgramsARB(1, &modulateProg);
111   glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, modulateProg);
112   glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
113                        strlen(modulate2D), (const GLubyte *)modulate2D);
114
115   printf("glGetError = 0x%x\n", (int) glGetError());
116   printf("glError(GL_PROGRAM_ERROR_STRING_ARB) = %s\n",
117          (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB));
118   assert(glIsProgramARB(modulateProg));
119
120   glEnable(GL_FRAGMENT_PROGRAM_ARB);
121
122   /* Load texture */
123   glGenTextures(1, &Texture);
124   glBindTexture(GL_TEXTURE_2D, Texture);
125   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
126   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
127   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
128   if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
129      printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE);
130      exit(1);
131   }
132   /* XXX this enable shouldn't really be needed!!! */
133   glEnable(GL_TEXTURE_2D);
134
135   glClearColor(.3, .3, .3, 0);
136}
137
138
139int main( int argc, char *argv[] )
140{
141   glutInit( &argc, argv );
142   glutInitWindowPosition( 0, 0 );
143   glutInitWindowSize( 250, 250 );
144   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
145   glutCreateWindow(argv[0]);
146   glewInit();
147   glutReshapeFunc( Reshape );
148   glutKeyboardFunc( Key );
149   glutSpecialFunc( SpecialKey );
150   glutDisplayFunc( Display );
151   Init();
152   glutMainLoop();
153   return 0;
154}
155