#include #include #include #include #ifndef WIN32 #include #include #endif #include #include #include "readtex.c" #define TEXTURE_FILE "../images/bw.rgb" #define TEXTURE_SIZE 4 unsigned show_fps = 0; unsigned int frame_cnt = 0; void alarmhandler(int); static const char *filename = NULL; static void usage(char *name) { fprintf(stderr, "usage: %s [ options ] shader_filename\n", name); #ifndef WIN32 fprintf(stderr, "\n" ); fprintf(stderr, "options:\n"); fprintf(stderr, " -fps show frames per second\n"); #endif } #ifndef WIN32 void alarmhandler (int sig) { if (sig == SIGALRM) { printf("%d frames in 5.0 seconds = %.3f FPS\n", frame_cnt, frame_cnt / 5.0); frame_cnt = 0; } signal(SIGALRM, alarmhandler); alarm(5); } #endif static void args(int argc, char *argv[]) { GLint i; for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-fps") == 0) { show_fps = 1; } else if (i == argc - 1) { filename = argv[i]; } else { usage(argv[0]); exit(1); } } if (!filename) { usage(argv[0]); exit(1); } } static GLboolean CheckError( int line ) { GLenum error = glGetError(); if (error) { char *err = (char *) gluErrorString( error ); fprintf( stderr, "GL Error: %s at line %d\n", err, line ); return GL_TRUE; } return GL_FALSE; } static float MitchellNetravali(float x, float B, float C) { float val = 0; float ax = fabs(x); if (ax<1.0f) { val = ((12 - 9*B - 6*C) * ax * ax * ax + (-18 + 12*B + 6*C) * ax * ax + (6 - 2*B))/6; } else if (ax<2.0f) { val = ((-B - 6*C) * ax * ax * ax + (6*B + 30*C) * ax * ax + (-12*B - 48*C) * ax + (8*B + 24*C)) / 6; } else { val = 0; } // val = ((val + 0.5) / 2); return val; } static void Init( void ) { GLuint Texture; GLint errno; GLuint prognum; char buf[4096]; GLuint sz; FILE *f; if ((f = fopen(filename, "r")) == NULL) { fprintf(stderr, "Couldn't open %s\n", filename); exit(1); } sz = fread(buf, 1, sizeof(buf), f); if (!feof(f)) { fprintf(stderr, "file too long\n"); exit(1); } fprintf(stderr, "%.*s\n", sz, buf); if (!GLEW_ARB_fragment_program) { printf("Error: GL_ARB_fragment_program not supported!\n"); exit(1); } printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); /* Load texture */ glGenTextures(1, &Texture); glBindTexture(GL_TEXTURE_2D, Texture); glEnable(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) { printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE); exit(1); } */ { const int size = TEXTURE_SIZE; GLubyte img[size*size*4]; memset(img, 0x00, sizeof(img)); GLubyte *ptr = img; #if 1 for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { *ptr++ = ((x & 1) && (y & 1)) * 0xff; *ptr++ = ((x & 1) && (y & 1)) * 0xff; *ptr++ = ((x & 1) && (y & 1)) * 0xff; *ptr++ = 0xff; } } #endif glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, img); } #if 0 glGenTextures(1, &Texture); glActiveTextureARB(GL_TEXTURE1_ARB); glBindTexture(GL_TEXTURE_2D, Texture); glEnable(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); { GLubyte data[32][32]; int width = 32; int height = 32; int i; int j; for (i = 0; i < 32; i++) for (j = 0; j < 32; j++) { /** ** +-----------+ ** | W | ** | +-----+ | ** | | | | ** | | B | | ** | | | | ** | +-----+ | ** | | ** +-----------+ **/ int i2 = i - height / 2; int j2 = j - width / 2; int h8 = height / 8; int w8 = width / 8; if ( -h8 <= i2 && i2 <= h8 && -w8 <= j2 && j2 <= w8 ) { data[i][j] = 0x00; } else if ( -2 * h8 <= i2 && i2 <= 2 * h8 && -2 * w8 <= j2 && j2 <= 2 * w8 ) { data[i][j] = 0x55; } else if ( -3 * h8 <= i2 && i2 <= 3 * h8 && -3 * w8 <= j2 && j2 <= 3 * w8 ) { data[i][j] = 0xaa; } else { data[i][j] = 0xff; } } glTexImage2D( GL_TEXTURE_2D, 0, GL_ALPHA8, 32, 32, 0, GL_ALPHA, GL_UNSIGNED_BYTE, data ); } #endif { const int size = 256; const float B = 0.3f, C = 0.3f; glGenTextures(1, &Texture); glActiveTextureARB(GL_TEXTURE2_ARB); glBindTexture(GL_TEXTURE_2D, Texture); glEnable(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); #if 0 float img[size*4]; memset(img, 0, sizeof(float)*size*4); float *ptr = img; float x, val; #define FUDGE(val) ((val + 1.0f) / 2.0f) for (int i = 0; i