132001f49Smrg/* Test GL_NV_fragment_program */
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
1232001f49Smrg
1332001f49Smrgstatic void Display( void )
1432001f49Smrg{
1532001f49Smrg   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
1632001f49Smrg
1732001f49Smrg   glPushMatrix();
1832001f49Smrg
1932001f49Smrg   glColor4f(0, 0.5, 0, 1);
2032001f49Smrg   glColor4f(0, 1, 0, 1);
2132001f49Smrg   glBegin(GL_POLYGON);
2232001f49Smrg   glVertex2f(-1, -1);
2332001f49Smrg   glVertex2f( 1, -1);
2432001f49Smrg   glVertex2f( 0,  1);
2532001f49Smrg   glEnd();
2632001f49Smrg
2732001f49Smrg   glPopMatrix();
2832001f49Smrg
2932001f49Smrg   glutSwapBuffers();
3032001f49Smrg}
3132001f49Smrg
3232001f49Smrg
3332001f49Smrgstatic void Reshape( int width, int height )
3432001f49Smrg{
3532001f49Smrg   glViewport( 0, 0, width, height );
3632001f49Smrg   glMatrixMode( GL_PROJECTION );
3732001f49Smrg   glLoadIdentity();
3832001f49Smrg   glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
3932001f49Smrg   glMatrixMode( GL_MODELVIEW );
4032001f49Smrg   glLoadIdentity();
4132001f49Smrg   glTranslatef( 0.0, 0.0, -15.0 );
4232001f49Smrg}
4332001f49Smrg
4432001f49Smrg
4532001f49Smrgstatic void Key( unsigned char key, int x, int y )
4632001f49Smrg{
4732001f49Smrg   (void) x;
4832001f49Smrg   (void) y;
4932001f49Smrg   switch (key) {
5032001f49Smrg      case 27:
5132001f49Smrg         exit(0);
5232001f49Smrg         break;
5332001f49Smrg   }
5432001f49Smrg   glutPostRedisplay();
5532001f49Smrg}
5632001f49Smrg
5732001f49Smrg
5832001f49Smrgstatic void Init( void )
5932001f49Smrg{
6032001f49Smrg#if 0
6132001f49Smrg   static const char *prog0 =
6232001f49Smrg      "!!FP1.0\n"
6332001f49Smrg      "MUL   o[COLR], R0, f[WPOS]; \n"
6432001f49Smrg      "ADD   o[COLH], H3, f[TEX0]; \n"
6532001f49Smrg      "ADD_SAT o[COLH], H3, f[TEX0]; \n"
6632001f49Smrg      "ADDX o[COLH], H3, f[TEX0]; \n"
6732001f49Smrg      "ADDHC o[COLH], H3, f[TEX0]; \n"
6832001f49Smrg      "ADDXC o[COLH], H3, f[TEX0]; \n"
6932001f49Smrg      "ADDXC_SAT o[COLH], H30, f[TEX0]; \n"
7032001f49Smrg      "MUL   o[COLR].xy, R0.wzyx, f[WPOS]; \n"
7132001f49Smrg      "MUL   o[COLR], H0, f[WPOS]; \n"
7232001f49Smrg      "MUL   o[COLR], -H0, f[WPOS]; \n"
7332001f49Smrg      "MOV   RC, H1; \n"
7432001f49Smrg      "MOV   HC, H2; \n"
7532001f49Smrg      "END \n"
7632001f49Smrg      ;
7732001f49Smrg#endif
7832001f49Smrg
7932001f49Smrg   /* masked updates, defines, declarations */
8032001f49Smrg   static const char *prog1 =
8132001f49Smrg      "!!FP1.0\n"
8232001f49Smrg      "DEFINE foo = {1, 2, 3, 4}; \n"
8332001f49Smrg      "DEFINE foo2 = 5; \n"
8432001f49Smrg      "DECLARE foo3 = {5, 6, 7, 8}; \n"
8532001f49Smrg      "DECLARE bar = 3; \n"
8632001f49Smrg      "DECLARE bar2; \n"
8732001f49Smrg      "DECLARE bar3 = bar; \n"
8832001f49Smrg      "#DECLARE bar4 = { a, b, c, d }; \n"
8932001f49Smrg      "MOV o[COLR].xy,   R0; \n"
9032001f49Smrg      "MOV o[COLR] (NE), R0; \n"
9132001f49Smrg      "MOV o[COLR] (NE.wzyx), R0; \n"
9232001f49Smrg      "MOV o[COLR].xy (NE.wzyx), R0; \n"
9332001f49Smrg      "MOV RC.x (EQ), R1.x; \n"
9432001f49Smrg      "KIL NE; \n"
9532001f49Smrg      "KIL EQ.xyxy; \n"
9632001f49Smrg      "END \n"
9732001f49Smrg      ;
9832001f49Smrg
9932001f49Smrg   /* texture instructions */
10032001f49Smrg   static const char *prog2 =
10132001f49Smrg      "!!FP1.0\n"
10232001f49Smrg      "TEX R0, f[TEX0], TEX0, 2D; \n"
10332001f49Smrg      "TEX R1, f[TEX1], TEX1, CUBE; \n"
10432001f49Smrg      "TEX R2, f[TEX2], TEX2, 3D; \n"
10532001f49Smrg      "TXP R3, f[TEX3], TEX3, RECT; \n"
10632001f49Smrg      "TXD R3, R2, R1, f[TEX3], TEX3, RECT; \n"
10732001f49Smrg      "MUL o[COLR], R0, f[COL0]; \n"
10832001f49Smrg      "END \n"
10932001f49Smrg      ;
11032001f49Smrg
11132001f49Smrg   /* test negation, absolute value */
11232001f49Smrg   static const char *prog3 =
11332001f49Smrg      "!!FP1.0\n"
11432001f49Smrg      "MOV R0, -R1; \n"
11532001f49Smrg      "MOV R0, +R1; \n"
11632001f49Smrg      "MOV R0, |-R1|; \n"
11732001f49Smrg      "MOV R0, |+R1|; \n"
11832001f49Smrg      "MOV R0, -|R1|; \n"
11932001f49Smrg      "MOV R0, +|R1|; \n"
12032001f49Smrg      "MOV R0, -|-R1|; \n"
12132001f49Smrg      "MOV R0, -|+R1|; \n"
12232001f49Smrg      "MOV o[COLR], R0; \n"
12332001f49Smrg      "END \n"
12432001f49Smrg      ;
12532001f49Smrg
12632001f49Smrg   /* literal constant sources */
12732001f49Smrg   static const char *prog4 =
12832001f49Smrg      "!!FP1.0\n"
12932001f49Smrg      "DEFINE Pi = 3.14159; \n"
13032001f49Smrg      "MOV R0, {1, -2, +3, 4}; \n"
13132001f49Smrg      "MOV R0, 5; \n"
13232001f49Smrg      "MOV R0, -5; \n"
13332001f49Smrg      "MOV R0, +5; \n"
13432001f49Smrg      "MOV R0, Pi; \n"
13532001f49Smrg      "MOV o[COLR], R0; \n"
13632001f49Smrg      "END \n"
13732001f49Smrg      ;
13832001f49Smrg
13932001f49Smrg   /* change the fragment color in a simple way */
14032001f49Smrg   static const char *prog10 =
14132001f49Smrg      "!!FP1.0\n"
14232001f49Smrg      "DEFINE blue = {0, 0, 1, 0};\n"
14332001f49Smrg      "DECLARE color; \n"
14432001f49Smrg      "MOV R0, f[COL0]; \n"
14532001f49Smrg      "#ADD o[COLR], R0, f[COL0]; \n"
14632001f49Smrg      "#ADD o[COLR], blue, f[COL0]; \n"
14732001f49Smrg      "#ADD o[COLR], {1, 0, 0, 0}, f[COL0]; \n"
14832001f49Smrg      "ADD o[COLR], color, f[COL0]; \n"
14932001f49Smrg      "END \n"
15032001f49Smrg      ;
15132001f49Smrg
15232001f49Smrg   GLuint progs[20];
15332001f49Smrg
15432001f49Smrg   if (!glutExtensionSupported ("GL_NV_fragment_program")) {
15532001f49Smrg	   printf("Sorry, this program requires GL_NV_fragment_program\n");
15632001f49Smrg	   exit(1);
15732001f49Smrg   }
15832001f49Smrg
15932001f49Smrg   glGenProgramsNV(20, progs);
16032001f49Smrg   assert(progs[0]);
16132001f49Smrg   assert(progs[1]);
16232001f49Smrg   assert(progs[0] != progs[1]);
16332001f49Smrg
16432001f49Smrg#if 0
16532001f49Smrg   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[0],
16632001f49Smrg                   strlen(prog0),
16732001f49Smrg                   (const GLubyte *) prog0);
16832001f49Smrg   assert(glIsProgramNV(progs[0]));
16932001f49Smrg#endif
17032001f49Smrg
17132001f49Smrg   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[1],
17232001f49Smrg                   strlen(prog1),
17332001f49Smrg                   (const GLubyte *) prog1);
17432001f49Smrg   assert(glIsProgramNV(progs[1]));
17532001f49Smrg
17632001f49Smrg   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[2],
17732001f49Smrg                   strlen(prog2),
17832001f49Smrg                   (const GLubyte *) prog2);
17932001f49Smrg   assert(glIsProgramNV(progs[2]));
18032001f49Smrg   glBindProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[2]);
18132001f49Smrg
18232001f49Smrg   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[3],
18332001f49Smrg                   strlen(prog3),
18432001f49Smrg                   (const GLubyte *) prog3);
18532001f49Smrg   assert(glIsProgramNV(progs[3]));
18632001f49Smrg   glBindProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[3]);
18732001f49Smrg
18832001f49Smrg   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[4],
18932001f49Smrg                   strlen(prog4),
19032001f49Smrg                   (const GLubyte *) prog4);
19132001f49Smrg   assert(glIsProgramNV(progs[4]));
19232001f49Smrg   glBindProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[4]);
19332001f49Smrg
19432001f49Smrg
19532001f49Smrg   /* a real program */
19632001f49Smrg   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[10],
19732001f49Smrg                   strlen(prog10),
19832001f49Smrg                   (const GLubyte *) prog10);
19932001f49Smrg   assert(glIsProgramNV(progs[10]));
20032001f49Smrg   glBindProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[10]);
20132001f49Smrg
20232001f49Smrg   glProgramNamedParameter4fNV(progs[10],
20332001f49Smrg                               strlen("color"), (const GLubyte *) "color",
20432001f49Smrg                               1, 0, 0, 1);
20532001f49Smrg
20632001f49Smrg   glEnable(GL_FRAGMENT_PROGRAM_NV);
20732001f49Smrg   glEnable(GL_ALPHA_TEST);
20832001f49Smrg   glAlphaFunc(GL_ALWAYS, 0.0);
20932001f49Smrg
21032001f49Smrg   printf("glGetError = %d\n", (int) glGetError());
21132001f49Smrg}
21232001f49Smrg
21332001f49Smrg
21432001f49Smrgint main( int argc, char *argv[] )
21532001f49Smrg{
21632001f49Smrg   glutInit( &argc, argv );
21732001f49Smrg   glutInitWindowPosition( 0, 0 );
21832001f49Smrg   glutInitWindowSize( 250, 250 );
21932001f49Smrg   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
22032001f49Smrg   glutCreateWindow(argv[0]);
22132001f49Smrg   glewInit();
22232001f49Smrg   glutReshapeFunc( Reshape );
22332001f49Smrg   glutKeyboardFunc( Key );
22432001f49Smrg   glutDisplayFunc( Display );
22532001f49Smrg   Init();
22632001f49Smrg   glutMainLoop();
22732001f49Smrg   return 0;
22832001f49Smrg}
229