1 /* Test GL_ARB_fragment_program */
8 #define GL_GLEXT_PROTOTYPES
13 static void Display( void )
15 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
19 glColor4f(0, 0.5, 0, 1);
20 glColor4f(0, 1, 0, 1);
33 static void Reshape( int width
, int height
)
35 glViewport( 0, 0, width
, height
);
36 glMatrixMode( GL_PROJECTION
);
38 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
39 glMatrixMode( GL_MODELVIEW
);
41 glTranslatef( 0.0, 0.0, -15.0 );
45 static void Key( unsigned char key
, int x
, int y
)
57 static void load_program(const char *prog
, GLuint prognum
)
60 GLint errorpos
, errno
;
62 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, prognum
);
63 glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
64 strlen(prog
), (const GLubyte
*) prog
);
66 assert(glIsProgramARB(prognum
));
68 printf("glGetError = %d\n", errno
);
69 if (errno
!= GL_NO_ERROR
)
71 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errorpos
);
72 printf("errorpos: %d\n", errorpos
);
73 printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
));
75 for (a
=-10; a
<10; a
++)
77 if ((errorpos
+a
< 0) || (errorpos
+a
>= strlen(prog
))) continue;
78 printf("%c", prog
[errorpos
+a
]);
85 static void Init( void )
87 static const char *prog0
=
89 "TEMP R0, RC, HC, H0, H1, H2, H3, H30 ;\n"
90 "MUL result.color, R0, fragment.position; \n"
91 "ADD result.color, H3, fragment.texcoord; \n"
92 "ADD_SAT result.color, H3, fragment.texcoord; \n"
93 "MUL result.color.xy, R0.wzyx, fragment.position; \n"
94 "MUL result.color, H0, fragment.position; \n"
95 "MUL result.color, -H0, fragment.position; \n"
100 /* masked updates, defines, declarations */
101 static const char *prog1
=
103 "PARAM foo = {1., 2., 3., 4.}; \n"
104 "PARAM foo2 = 5.; \n"
105 "PARAM foo3 = {5., 6., 7., 8.}; \n"
107 "TEMP R0, R1, RC, EQ, NE, bar2; \n"
108 "ALIAS bar3 = bar; \n"
109 "MOV result.color.xy, R0; \n"
110 "MOV result.color, R0; \n"
111 "MOV result.color.xyzw, R0; \n"
112 "MOV result.color.xy, R0; \n"
119 /* texture instructions */
120 static const char *prog2
=
122 "TEMP R0, R1, R2, R3;\n"
123 "TEX R0, fragment.texcoord, texture[0], 2D; \n"
124 "TEX R1, fragment.texcoord[1], texture[1], CUBE; \n"
125 "TEX R2, fragment.texcoord[2], texture[2], 3D; \n"
126 "TXP R3, fragment.texcoord[3], texture[3], RECT; \n"
127 "MUL result.color, R0, fragment.color; \n"
131 /* test negation, absolute value */
132 static const char *prog3
=
137 "MOV result.color, R0; \n"
141 /* literal constant sources */
142 static const char *prog4
=
145 "PARAM Pi = 3.14159; \n"
146 "MOV R0, {1., -2., +3., 4.}; \n"
151 "MOV result.color, R0; \n"
155 /* change the fragment color in a simple way */
156 static const char *prog10
=
158 "PARAM blue = {0., 0., 1., 0.};\n"
159 "PARAM color = {1., 0., 0., 1.};\n"
161 "MOV R0, fragment.color; \n"
162 "#ADD result.color, R0, fragment.color; \n"
163 "#ADD result.color, blue, fragment.color; \n"
164 "#ADD result.color, {1., 0., 0., 0.}, fragment.color; \n"
165 "ADD result.color, color, fragment.color; \n"
171 glGenProgramsARB(20, progs
);
174 assert(progs
[0] != progs
[1]);
177 printf("program 0:\n");
178 load_program(prog0
, progs
[0]);
179 printf("program 1:\n");
180 load_program(prog1
, progs
[1]);
181 printf("program 2:\n");
182 load_program(prog2
, progs
[2]);
183 printf("program 3:\n");
184 load_program(prog3
, progs
[3]);
185 printf("program 4:\n");
186 load_program(prog4
, progs
[4]);
187 printf("program 10:\n");
188 load_program(prog10
, progs
[5]);
191 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
192 glEnable(GL_ALPHA_TEST
);
193 glAlphaFunc(GL_ALWAYS
, 0.0);
197 int main( int argc
, char *argv
[] )
199 glutInit( &argc
, argv
);
200 glutInitWindowPosition( 0, 0 );
201 glutInitWindowSize( 250, 250 );
202 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
203 glutCreateWindow(argv
[0]);
204 glutReshapeFunc( Reshape
);
205 glutKeyboardFunc( Key
);
206 glutDisplayFunc( Display
);