132001f49Smrg
232001f49Smrg#include <stdio.h>
332001f49Smrg#include <string.h>
432001f49Smrg#include <stdlib.h>
532001f49Smrg
632001f49Smrg#ifndef WIN32
732001f49Smrg#include <unistd.h>
832001f49Smrg#include <signal.h>
932001f49Smrg#endif
1032001f49Smrg
1132001f49Smrg#include <GL/glew.h>
1232001f49Smrg#include "glut_wrap.h"
1332001f49Smrg
1432001f49Smrg#include "readtex.c"
1532001f49Smrg
1632001f49Smrg
1732001f49Smrg#define TEXTURE_FILE DEMOS_DATA_DIR "bw.rgb"
1832001f49Smrg
1932001f49Smrgunsigned show_fps = 0;
2032001f49Smrgunsigned int frame_cnt = 0;
2132001f49Smrgvoid alarmhandler(int);
2232001f49Smrgstatic const char *filename = NULL;
2332001f49Smrg
2432001f49Smrgstatic void usage(char *name)
2532001f49Smrg{
2632001f49Smrg   fprintf(stderr, "usage: %s [ options ] shader_filename\n", name);
2732001f49Smrg#ifndef WIN32
2832001f49Smrg   fprintf(stderr, "\n" );
2932001f49Smrg   fprintf(stderr, "options:\n");
3032001f49Smrg   fprintf(stderr, "    -fps  show frames per second\n");
3132001f49Smrg#endif
3232001f49Smrg}
3332001f49Smrg
3432001f49Smrg#ifndef WIN32
3532001f49Smrgvoid alarmhandler (int sig)
3632001f49Smrg{
3732001f49Smrg   if (sig == SIGALRM) {
3832001f49Smrg      printf("%d frames in 5.0 seconds = %.3f FPS\n", frame_cnt,
3932001f49Smrg             frame_cnt / 5.0);
4032001f49Smrg
4132001f49Smrg      frame_cnt = 0;
4232001f49Smrg   }
4332001f49Smrg   signal(SIGALRM, alarmhandler);
4432001f49Smrg   alarm(5);
4532001f49Smrg}
4632001f49Smrg#endif
4732001f49Smrg
4832001f49Smrgstatic void args(int argc, char *argv[])
4932001f49Smrg{
5032001f49Smrg   GLint i;
5132001f49Smrg
5232001f49Smrg   for (i = 1; i < argc; i++) {
5332001f49Smrg      if (strcmp(argv[i], "-fps") == 0) {
5432001f49Smrg         show_fps = 1;
5532001f49Smrg      }
5632001f49Smrg      else if (i == argc - 1) {
5732001f49Smrg	 filename = argv[i];
5832001f49Smrg      }
5932001f49Smrg      else {
6032001f49Smrg	 usage(argv[0]);
6132001f49Smrg	 exit(1);
6232001f49Smrg      }
6332001f49Smrg   }
6432001f49Smrg
6532001f49Smrg   if (!filename) {
6632001f49Smrg      usage(argv[0]);
6732001f49Smrg      exit(1);
6832001f49Smrg   }
6932001f49Smrg}
7032001f49Smrg
7132001f49Smrgstatic void Init( void )
7232001f49Smrg{
7332001f49Smrg   GLuint Texture;
747ec3b29aSmrg   GLint errnum;
7532001f49Smrg   GLuint prognum;
7632001f49Smrg   char buf[50000];
7732001f49Smrg   GLuint sz;
7832001f49Smrg   FILE *f;
7932001f49Smrg
8032001f49Smrg   if ((f = fopen(filename, "r")) == NULL) {
8132001f49Smrg      fprintf(stderr, "Couldn't open %s\n", filename);
8232001f49Smrg      exit(1);
8332001f49Smrg   }
8432001f49Smrg
8532001f49Smrg   sz = fread(buf, 1, sizeof(buf), f);
8632001f49Smrg   if (!feof(f)) {
8732001f49Smrg      fprintf(stderr, "file too long\n");
8832001f49Smrg      exit(1);
8932001f49Smrg   }
9032001f49Smrg   fprintf(stderr, "%.*s\n", sz, buf);
9132001f49Smrg
9232001f49Smrg   if (!GLEW_ARB_fragment_program) {
9332001f49Smrg      printf("Error: GL_ARB_fragment_program not supported!\n");
9432001f49Smrg      exit(1);
9532001f49Smrg   }
9632001f49Smrg   printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
9732001f49Smrg
9832001f49Smrg   /* Setup the fragment program */
9932001f49Smrg   glGenProgramsARB(1, &prognum);
10032001f49Smrg   glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, prognum);
10132001f49Smrg   glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
10232001f49Smrg                      sz, (const GLubyte *)buf);
10332001f49Smrg
1047ec3b29aSmrg   errnum = glGetError();
1057ec3b29aSmrg   printf("glGetError = 0x%x\n", errnum);
1067ec3b29aSmrg   if (errnum != GL_NO_ERROR) {
10732001f49Smrg      GLint errorpos;
10832001f49Smrg
10932001f49Smrg      glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
11032001f49Smrg      printf("errorpos: %d\n", errorpos);
11132001f49Smrg      printf("glError(GL_PROGRAM_ERROR_STRING_ARB) = %s\n",
11232001f49Smrg             (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB));
11332001f49Smrg   }
11432001f49Smrg   glEnable(GL_FRAGMENT_PROGRAM_ARB);
11532001f49Smrg
11632001f49Smrg
11732001f49Smrg   /* Load texture */
11832001f49Smrg   glGenTextures(1, &Texture);
11932001f49Smrg   glBindTexture(GL_TEXTURE_2D, Texture);
12032001f49Smrg   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
12132001f49Smrg   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
12232001f49Smrg   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
12332001f49Smrg   if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
12432001f49Smrg      printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE);
12532001f49Smrg      exit(1);
12632001f49Smrg   }
12732001f49Smrg
12832001f49Smrg
12932001f49Smrg   glGenTextures(1, &Texture);
13032001f49Smrg   glActiveTextureARB(GL_TEXTURE0_ARB + 1);
13132001f49Smrg   glBindTexture(GL_TEXTURE_2D, Texture);
13232001f49Smrg   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
13332001f49Smrg   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
13432001f49Smrg   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
13532001f49Smrg
13632001f49Smrg   {
13732001f49Smrg      GLubyte data[32][32];
13832001f49Smrg      int width = 32;
13932001f49Smrg      int height = 32;
14032001f49Smrg      int i;
14132001f49Smrg      int j;
14232001f49Smrg
14332001f49Smrg      for (i = 0; i < 32; i++)
14432001f49Smrg         for (j = 0; j < 32; j++)
14532001f49Smrg	 {
14632001f49Smrg	    /**
14732001f49Smrg	     ** +-----------+
14832001f49Smrg	     ** |     W     |
14932001f49Smrg	     ** |  +-----+  |
15032001f49Smrg	     ** |  |     |  |
15132001f49Smrg	     ** |  |  B  |  |
15232001f49Smrg	     ** |  |     |  |
15332001f49Smrg	     ** |  +-----+  |
15432001f49Smrg	     ** |           |
15532001f49Smrg	     ** +-----------+
15632001f49Smrg	     **/
15732001f49Smrg	    int i2 = i - height / 2;
15832001f49Smrg	    int j2 = j - width / 2;
15932001f49Smrg	    int h8 = height / 8;
16032001f49Smrg	    int w8 = width / 8;
16132001f49Smrg	    if ( -h8 <= i2 && i2 <= h8 && -w8 <= j2 && j2 <= w8 ) {
16232001f49Smrg	       data[i][j] = 0x00;
16332001f49Smrg	    } else if ( -2 * h8 <= i2 && i2 <= 2 * h8 && -2 * w8 <= j2 && j2 <= 2 * w8 ) {
16432001f49Smrg	       data[i][j] = 0x55;
16532001f49Smrg	    } else if ( -3 * h8 <= i2 && i2 <= 3 * h8 && -3 * w8 <= j2 && j2 <= 3 * w8 ) {
16632001f49Smrg	       data[i][j] = 0xaa;
16732001f49Smrg	    } else {
16832001f49Smrg	       data[i][j] = 0xff;
16932001f49Smrg	    }
17032001f49Smrg	 }
17132001f49Smrg
17232001f49Smrg      glTexImage2D( GL_TEXTURE_2D, 0,
17332001f49Smrg                    GL_ALPHA8,
17432001f49Smrg                    32, 32, 0,
17532001f49Smrg                    GL_ALPHA, GL_UNSIGNED_BYTE, data );
17632001f49Smrg   }
17732001f49Smrg
17832001f49Smrg
17932001f49Smrg   {
18032001f49Smrg      const float Ambient[4] = { 0.0, 1.0, 0.0, 0.0 };
18132001f49Smrg      const float Diffuse[4] = { 1.0, 0.0, 0.0, 0.0 };
18232001f49Smrg      const float Specular[4] = { 0.0, 0.0, 1.0, 0.0 };
18332001f49Smrg      const float Emission[4] = { 0.0, 0.0, 0.0, 1.0 };
18432001f49Smrg      glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, Ambient);
18532001f49Smrg      glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Diffuse);
18632001f49Smrg      glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Specular);
18732001f49Smrg      glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, Emission);
18832001f49Smrg   }
18932001f49Smrg
19032001f49Smrg   glClearColor(.1, .3, .5, 0);
19132001f49Smrg   fclose(f);
19232001f49Smrg}
19332001f49Smrg
19432001f49Smrgstatic void Reshape(int width, int height)
19532001f49Smrg{
19632001f49Smrg
19732001f49Smrg    glViewport(0, 0, (GLint)width, (GLint)height);
19832001f49Smrg
19932001f49Smrg    glMatrixMode(GL_PROJECTION);
20032001f49Smrg    glLoadIdentity();
20132001f49Smrg    glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
20232001f49Smrg    glMatrixMode(GL_MODELVIEW);
20332001f49Smrg}
20432001f49Smrg
20532001f49Smrgstatic void Key(unsigned char key, int x, int y)
20632001f49Smrg{
20732001f49Smrg
20832001f49Smrg    switch (key) {
20932001f49Smrg      case 27:
21032001f49Smrg	exit(1);
21132001f49Smrg      default:
21232001f49Smrg	break;
21332001f49Smrg    }
21432001f49Smrg
21532001f49Smrg    glutPostRedisplay();
21632001f49Smrg}
21732001f49Smrg
21832001f49Smrgstatic void Draw(void)
21932001f49Smrg{
22032001f49Smrg   glClear(GL_COLOR_BUFFER_BIT);
22132001f49Smrg
22232001f49Smrg   glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, 1.0, 1.0, 0.0, 0.0);
22332001f49Smrg   glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 1, 0.0, 0.0, 1.0, 1.0);
22432001f49Smrg   glBegin(GL_TRIANGLES);
22532001f49Smrg
22632001f49Smrg   glColor3f(0,0,1);
22732001f49Smrg   glTexCoord3f(1,1,0);
22832001f49Smrg   glVertex3f( 0.9, -0.9, -30.0);
22932001f49Smrg
23032001f49Smrg   glColor3f(1,0,0);
23132001f49Smrg   glTexCoord3f(1,-1,0);
23232001f49Smrg   glVertex3f( 0.9,  0.9, -30.0);
23332001f49Smrg
23432001f49Smrg   glColor3f(0,1,0);
23532001f49Smrg   glTexCoord3f(-1,0,0);
23632001f49Smrg   glVertex3f(-0.9,  0.0, -30.0);
23732001f49Smrg   glEnd();
23832001f49Smrg
23932001f49Smrg   glFlush();
24032001f49Smrg   if (show_fps) {
24132001f49Smrg      ++frame_cnt;
24232001f49Smrg      glutPostRedisplay();
24332001f49Smrg   }
24432001f49Smrg}
24532001f49Smrg
24632001f49Smrg
24732001f49Smrgint main(int argc, char **argv)
24832001f49Smrg{
24932001f49Smrg   glutInit(&argc, argv);
25032001f49Smrg   glutInitWindowPosition(0, 0);
25132001f49Smrg   glutInitWindowSize(250, 250);
25232001f49Smrg   glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
25332001f49Smrg   args(argc, argv);
25432001f49Smrg   glutCreateWindow(filename);
25532001f49Smrg   glewInit();
25632001f49Smrg   glutReshapeFunc(Reshape);
25732001f49Smrg   glutKeyboardFunc(Key);
25832001f49Smrg   glutDisplayFunc(Draw);
25932001f49Smrg   Init();
26032001f49Smrg#ifndef WIN32
26132001f49Smrg   if (show_fps) {
26232001f49Smrg      signal(SIGALRM, alarmhandler);
26332001f49Smrg      alarm(5);
26432001f49Smrg   }
26532001f49Smrg#endif
26632001f49Smrg   glutMainLoop();
26732001f49Smrg   return 0;
26832001f49Smrg}
269