1/* Test GL_NV_fragment_program */
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
12
13static void Display( void )
14{
15   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
16
17   glPushMatrix();
18
19   glColor4f(0, 0.5, 0, 1);
20   glColor4f(0, 1, 0, 1);
21   glBegin(GL_POLYGON);
22   glVertex2f(-1, -1);
23   glVertex2f( 1, -1);
24   glVertex2f( 0,  1);
25   glEnd();
26
27   glPopMatrix();
28
29   glutSwapBuffers();
30}
31
32
33static void Reshape( int width, int height )
34{
35   glViewport( 0, 0, width, height );
36   glMatrixMode( GL_PROJECTION );
37   glLoadIdentity();
38   glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
39   glMatrixMode( GL_MODELVIEW );
40   glLoadIdentity();
41   glTranslatef( 0.0, 0.0, -15.0 );
42}
43
44
45static void Key( unsigned char key, int x, int y )
46{
47   (void) x;
48   (void) y;
49   switch (key) {
50      case 27:
51         exit(0);
52         break;
53   }
54   glutPostRedisplay();
55}
56
57
58static void Init( void )
59{
60#if 0
61   static const char *prog0 =
62      "!!FP1.0\n"
63      "MUL   o[COLR], R0, f[WPOS]; \n"
64      "ADD   o[COLH], H3, f[TEX0]; \n"
65      "ADD_SAT o[COLH], H3, f[TEX0]; \n"
66      "ADDX o[COLH], H3, f[TEX0]; \n"
67      "ADDHC o[COLH], H3, f[TEX0]; \n"
68      "ADDXC o[COLH], H3, f[TEX0]; \n"
69      "ADDXC_SAT o[COLH], H30, f[TEX0]; \n"
70      "MUL   o[COLR].xy, R0.wzyx, f[WPOS]; \n"
71      "MUL   o[COLR], H0, f[WPOS]; \n"
72      "MUL   o[COLR], -H0, f[WPOS]; \n"
73      "MOV   RC, H1; \n"
74      "MOV   HC, H2; \n"
75      "END \n"
76      ;
77#endif
78
79   /* masked updates, defines, declarations */
80   static const char *prog1 =
81      "!!FP1.0\n"
82      "DEFINE foo = {1, 2, 3, 4}; \n"
83      "DEFINE foo2 = 5; \n"
84      "DECLARE foo3 = {5, 6, 7, 8}; \n"
85      "DECLARE bar = 3; \n"
86      "DECLARE bar2; \n"
87      "DECLARE bar3 = bar; \n"
88      "#DECLARE bar4 = { a, b, c, d }; \n"
89      "MOV o[COLR].xy,   R0; \n"
90      "MOV o[COLR] (NE), R0; \n"
91      "MOV o[COLR] (NE.wzyx), R0; \n"
92      "MOV o[COLR].xy (NE.wzyx), R0; \n"
93      "MOV RC.x (EQ), R1.x; \n"
94      "KIL NE; \n"
95      "KIL EQ.xyxy; \n"
96      "END \n"
97      ;
98
99   /* texture instructions */
100   static const char *prog2 =
101      "!!FP1.0\n"
102      "TEX R0, f[TEX0], TEX0, 2D; \n"
103      "TEX R1, f[TEX1], TEX1, CUBE; \n"
104      "TEX R2, f[TEX2], TEX2, 3D; \n"
105      "TXP R3, f[TEX3], TEX3, RECT; \n"
106      "TXD R3, R2, R1, f[TEX3], TEX3, RECT; \n"
107      "MUL o[COLR], R0, f[COL0]; \n"
108      "END \n"
109      ;
110
111   /* test negation, absolute value */
112   static const char *prog3 =
113      "!!FP1.0\n"
114      "MOV R0, -R1; \n"
115      "MOV R0, +R1; \n"
116      "MOV R0, |-R1|; \n"
117      "MOV R0, |+R1|; \n"
118      "MOV R0, -|R1|; \n"
119      "MOV R0, +|R1|; \n"
120      "MOV R0, -|-R1|; \n"
121      "MOV R0, -|+R1|; \n"
122      "MOV o[COLR], R0; \n"
123      "END \n"
124      ;
125
126   /* literal constant sources */
127   static const char *prog4 =
128      "!!FP1.0\n"
129      "DEFINE Pi = 3.14159; \n"
130      "MOV R0, {1, -2, +3, 4}; \n"
131      "MOV R0, 5; \n"
132      "MOV R0, -5; \n"
133      "MOV R0, +5; \n"
134      "MOV R0, Pi; \n"
135      "MOV o[COLR], R0; \n"
136      "END \n"
137      ;
138
139   /* change the fragment color in a simple way */
140   static const char *prog10 =
141      "!!FP1.0\n"
142      "DEFINE blue = {0, 0, 1, 0};\n"
143      "DECLARE color; \n"
144      "MOV R0, f[COL0]; \n"
145      "#ADD o[COLR], R0, f[COL0]; \n"
146      "#ADD o[COLR], blue, f[COL0]; \n"
147      "#ADD o[COLR], {1, 0, 0, 0}, f[COL0]; \n"
148      "ADD o[COLR], color, f[COL0]; \n"
149      "END \n"
150      ;
151
152   GLuint progs[20];
153
154   if (!glutExtensionSupported ("GL_NV_fragment_program")) {
155	   printf("Sorry, this program requires GL_NV_fragment_program\n");
156	   exit(1);
157   }
158
159   glGenProgramsNV(20, progs);
160   assert(progs[0]);
161   assert(progs[1]);
162   assert(progs[0] != progs[1]);
163
164#if 0
165   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[0],
166                   strlen(prog0),
167                   (const GLubyte *) prog0);
168   assert(glIsProgramNV(progs[0]));
169#endif
170
171   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[1],
172                   strlen(prog1),
173                   (const GLubyte *) prog1);
174   assert(glIsProgramNV(progs[1]));
175
176   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[2],
177                   strlen(prog2),
178                   (const GLubyte *) prog2);
179   assert(glIsProgramNV(progs[2]));
180   glBindProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[2]);
181
182   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[3],
183                   strlen(prog3),
184                   (const GLubyte *) prog3);
185   assert(glIsProgramNV(progs[3]));
186   glBindProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[3]);
187
188   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[4],
189                   strlen(prog4),
190                   (const GLubyte *) prog4);
191   assert(glIsProgramNV(progs[4]));
192   glBindProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[4]);
193
194
195   /* a real program */
196   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[10],
197                   strlen(prog10),
198                   (const GLubyte *) prog10);
199   assert(glIsProgramNV(progs[10]));
200   glBindProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[10]);
201
202   glProgramNamedParameter4fNV(progs[10],
203                               strlen("color"), (const GLubyte *) "color",
204                               1, 0, 0, 1);
205
206   glEnable(GL_FRAGMENT_PROGRAM_NV);
207   glEnable(GL_ALPHA_TEST);
208   glAlphaFunc(GL_ALWAYS, 0.0);
209
210   printf("glGetError = %d\n", (int) glGetError());
211}
212
213
214int main( int argc, char *argv[] )
215{
216   glutInit( &argc, argv );
217   glutInitWindowPosition( 0, 0 );
218   glutInitWindowSize( 250, 250 );
219   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
220   glutCreateWindow(argv[0]);
221   glewInit();
222   glutReshapeFunc( Reshape );
223   glutKeyboardFunc( Key );
224   glutDisplayFunc( Display );
225   Init();
226   glutMainLoop();
227   return 0;
228}
229