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 <glad/glad.h>
9 #include "glut_wrap.h"
10 
11 #include "readtex.c"
12 
13 
14 #define TEXTURE_FILE DEMOS_DATA_DIR "girl.rgb"
15 
16 static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
17 
18 #define PI 3.141592
19 
Display(void)20 static void Display( void )
21 {
22    glClear( GL_COLOR_BUFFER_BIT );
23 
24    glPushMatrix();
25    glRotatef(Xrot, 1.0, 0.0, 0.0);
26    glRotatef(Yrot, 0.0, 1.0, 0.0);
27    glRotatef(Zrot, 0.0, 0.0, 1.0);
28 
29    glBegin(GL_POLYGON);
30    glTexCoord2f(-PI, 0);   glVertex2f(-1, -1);
31    glTexCoord2f(PI, 0);   glVertex2f( 1, -1);
32    glTexCoord2f(PI, 1);   glVertex2f( 1,  1);
33    glTexCoord2f(-PI, 1);   glVertex2f(-1,  1);
34    glEnd();
35 
36    glPopMatrix();
37 
38    glutSwapBuffers();
39 }
40 
41 
Reshape(int width,int height)42 static 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 
SpecialKey(int key,int x,int y)54 static 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 
Key(unsigned char key,int x,int y)78 static 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 
Init(void)91 static void Init( void )
92 {
93    static const char *modulate2D =
94       "!!ARBfp1.0\n"
95       "TEMP R0;\n"
96       "MOV R0, {0,0,0,1};\n"
97       "SCS R0, fragment.texcoord[0].x; \n"
98       "ADD R0, R0, {1.0}.x;\n"
99       "MUL R0, R0, {0.5}.x;\n"
100       "MOV result.color, R0; \n"
101       "END"
102       ;
103    GLuint modulateProg;
104    GLuint Texture;
105 
106    if (!glutExtensionSupported("GL_ARB_fragment_program")) {
107       printf("Error: GL_ARB_fragment_program not supported!\n");
108       exit(1);
109    }
110    printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
111 
112    /* Setup the fragment program */
113    glGenProgramsARB(1, &modulateProg);
114    glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, modulateProg);
115    glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
116                         strlen(modulate2D), (const GLubyte *)modulate2D);
117 
118    printf("glGetError = 0x%x\n", (int) glGetError());
119    printf("glError(GL_PROGRAM_ERROR_STRING_ARB) = %s\n",
120           (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB));
121    assert(glIsProgramARB(modulateProg));
122 
123    glEnable(GL_FRAGMENT_PROGRAM_ARB);
124 
125    /* Load texture */
126    glGenTextures(1, &Texture);
127    glBindTexture(GL_TEXTURE_2D, Texture);
128    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
129    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
130    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
131    if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
132       printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE);
133       exit(1);
134    }
135    /* XXX this enable shouldn't really be needed!!! */
136    glEnable(GL_TEXTURE_2D);
137 
138    glClearColor(.3, .3, .3, 0);
139 }
140 
141 
main(int argc,char * argv[])142 int main( int argc, char *argv[] )
143 {
144    glutInit( &argc, argv );
145    glutInitWindowPosition( 0, 0 );
146    glutInitWindowSize( 250, 250 );
147    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
148    glutCreateWindow(argv[0]);
149    gladLoadGL();
150    glutReshapeFunc( Reshape );
151    glutKeyboardFunc( Key );
152    glutSpecialFunc( SpecialKey );
153    glutDisplayFunc( Display );
154    Init();
155    glutMainLoop();
156    return 0;
157 }
158