Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 1 | |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <stdlib.h> |
Jakob Bornecrantz | 96c773c | 2009-02-14 01:05:13 +0100 | [diff] [blame] | 5 | |
| 6 | #ifndef WIN32 |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 7 | #include <unistd.h> |
| 8 | #include <signal.h> |
Jakob Bornecrantz | 96c773c | 2009-02-14 01:05:13 +0100 | [diff] [blame] | 9 | #endif |
| 10 | |
| 11 | #include <GL/glew.h> |
| 12 | #include <GL/glut.h> |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 13 | |
Keith Whitwell | 185ff38 | 2009-03-06 21:15:19 +0000 | [diff] [blame] | 14 | #include "readtex.c" |
| 15 | |
| 16 | |
| 17 | #define TEXTURE_FILE "../images/bw.rgb" |
| 18 | |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 19 | unsigned show_fps = 0; |
| 20 | unsigned int frame_cnt = 0; |
| 21 | void alarmhandler(int); |
| 22 | static const char *filename = NULL; |
| 23 | |
| 24 | static void usage(char *name) |
| 25 | { |
| 26 | fprintf(stderr, "usage: %s [ options ] shader_filename\n", name); |
Jakob Bornecrantz | 96c773c | 2009-02-14 01:05:13 +0100 | [diff] [blame] | 27 | #ifndef WIN32 |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 28 | fprintf(stderr, "\n" ); |
| 29 | fprintf(stderr, "options:\n"); |
| 30 | fprintf(stderr, " -fps show frames per second\n"); |
Jakob Bornecrantz | 96c773c | 2009-02-14 01:05:13 +0100 | [diff] [blame] | 31 | #endif |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 32 | } |
| 33 | |
Jakob Bornecrantz | 96c773c | 2009-02-14 01:05:13 +0100 | [diff] [blame] | 34 | #ifndef WIN32 |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 35 | void alarmhandler (int sig) |
| 36 | { |
| 37 | if (sig == SIGALRM) { |
| 38 | printf("%d frames in 5.0 seconds = %.3f FPS\n", frame_cnt, |
| 39 | frame_cnt / 5.0); |
| 40 | |
| 41 | frame_cnt = 0; |
| 42 | } |
| 43 | signal(SIGALRM, alarmhandler); |
| 44 | alarm(5); |
| 45 | } |
Jakob Bornecrantz | 96c773c | 2009-02-14 01:05:13 +0100 | [diff] [blame] | 46 | #endif |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 47 | |
| 48 | static void args(int argc, char *argv[]) |
| 49 | { |
| 50 | GLint i; |
| 51 | |
| 52 | for (i = 1; i < argc; i++) { |
| 53 | if (strcmp(argv[i], "-fps") == 0) { |
| 54 | show_fps = 1; |
| 55 | } |
| 56 | else if (i == argc - 1) { |
| 57 | filename = argv[i]; |
| 58 | } |
| 59 | else { |
| 60 | usage(argv[0]); |
| 61 | exit(1); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if (!filename) { |
| 66 | usage(argv[0]); |
| 67 | exit(1); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static void Init( void ) |
| 72 | { |
Keith Whitwell | 185ff38 | 2009-03-06 21:15:19 +0000 | [diff] [blame] | 73 | GLuint Texture; |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 74 | GLint errno; |
| 75 | GLuint prognum; |
| 76 | char buf[4096]; |
| 77 | GLuint sz; |
| 78 | FILE *f; |
| 79 | |
| 80 | if ((f = fopen(filename, "r")) == NULL) { |
| 81 | fprintf(stderr, "Couldn't open %s\n", filename); |
| 82 | exit(1); |
| 83 | } |
| 84 | |
| 85 | sz = fread(buf, 1, sizeof(buf), f); |
| 86 | if (!feof(f)) { |
| 87 | fprintf(stderr, "file too long\n"); |
| 88 | exit(1); |
| 89 | } |
| 90 | fprintf(stderr, "%.*s\n", sz, buf); |
| 91 | |
José Fonseca | e3f14f2 | 2009-06-11 13:19:34 +0100 | [diff] [blame] | 92 | if (!GLEW_ARB_fragment_program) { |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 93 | printf("Error: GL_ARB_fragment_program not supported!\n"); |
| 94 | exit(1); |
| 95 | } |
| 96 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 97 | |
| 98 | /* Setup the fragment program */ |
| 99 | glGenProgramsARB(1, &prognum); |
| 100 | glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, prognum); |
| 101 | glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, |
| 102 | sz, (const GLubyte *)buf); |
| 103 | |
| 104 | errno = glGetError(); |
| 105 | printf("glGetError = 0x%x\n", errno); |
| 106 | if (errno != GL_NO_ERROR) { |
| 107 | GLint errorpos; |
| 108 | |
| 109 | glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos); |
| 110 | printf("errorpos: %d\n", errorpos); |
| 111 | printf("glError(GL_PROGRAM_ERROR_STRING_ARB) = %s\n", |
| 112 | (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB)); |
| 113 | } |
| 114 | glEnable(GL_FRAGMENT_PROGRAM_ARB); |
| 115 | |
Keith Whitwell | 185ff38 | 2009-03-06 21:15:19 +0000 | [diff] [blame] | 116 | |
| 117 | /* Load texture */ |
| 118 | glGenTextures(1, &Texture); |
| 119 | glBindTexture(GL_TEXTURE_2D, Texture); |
| 120 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 121 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 122 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 123 | if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) { |
| 124 | printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE); |
| 125 | exit(1); |
| 126 | } |
| 127 | |
Keith Whitwell | fb8a987 | 2009-03-09 14:08:35 +0000 | [diff] [blame] | 128 | |
| 129 | glGenTextures(1, &Texture); |
| 130 | glActiveTextureARB(GL_TEXTURE0_ARB + 1); |
| 131 | glBindTexture(GL_TEXTURE_2D, Texture); |
| 132 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 133 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 134 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 135 | |
| 136 | { |
| 137 | GLubyte data[32][32]; |
| 138 | int width = 32; |
| 139 | int height = 32; |
| 140 | int i; |
| 141 | int j; |
| 142 | |
| 143 | for (i = 0; i < 32; i++) |
| 144 | for (j = 0; j < 32; j++) |
| 145 | { |
| 146 | /** |
| 147 | ** +-----------+ |
| 148 | ** | W | |
| 149 | ** | +-----+ | |
| 150 | ** | | | | |
| 151 | ** | | B | | |
| 152 | ** | | | | |
| 153 | ** | +-----+ | |
| 154 | ** | | |
| 155 | ** +-----------+ |
| 156 | **/ |
| 157 | int i2 = i - height / 2; |
| 158 | int j2 = j - width / 2; |
| 159 | int h8 = height / 8; |
| 160 | int w8 = width / 8; |
| 161 | if ( -h8 <= i2 && i2 <= h8 && -w8 <= j2 && j2 <= w8 ) { |
| 162 | data[i][j] = 0x00; |
| 163 | } else if ( -2 * h8 <= i2 && i2 <= 2 * h8 && -2 * w8 <= j2 && j2 <= 2 * w8 ) { |
| 164 | data[i][j] = 0x55; |
| 165 | } else if ( -3 * h8 <= i2 && i2 <= 3 * h8 && -3 * w8 <= j2 && j2 <= 3 * w8 ) { |
| 166 | data[i][j] = 0xaa; |
| 167 | } else { |
| 168 | data[i][j] = 0xff; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | glTexImage2D( GL_TEXTURE_2D, 0, |
| 173 | GL_ALPHA8, |
| 174 | 32, 32, 0, |
| 175 | GL_ALPHA, GL_UNSIGNED_BYTE, data ); |
| 176 | } |
| 177 | |
| 178 | |
Keith Whitwell | 185ff38 | 2009-03-06 21:15:19 +0000 | [diff] [blame] | 179 | glClearColor(.1, .3, .5, 0); |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static void Reshape(int width, int height) |
| 183 | { |
| 184 | |
| 185 | glViewport(0, 0, (GLint)width, (GLint)height); |
| 186 | |
| 187 | glMatrixMode(GL_PROJECTION); |
| 188 | glLoadIdentity(); |
| 189 | glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); |
| 190 | glMatrixMode(GL_MODELVIEW); |
| 191 | } |
| 192 | |
| 193 | static void Key(unsigned char key, int x, int y) |
| 194 | { |
| 195 | |
| 196 | switch (key) { |
| 197 | case 27: |
| 198 | exit(1); |
| 199 | default: |
Vinson Lee | cde6643 | 2009-11-18 14:41:40 -0800 | [diff] [blame] | 200 | break; |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | glutPostRedisplay(); |
| 204 | } |
| 205 | |
| 206 | static void Display(void) |
| 207 | { |
| 208 | glClear(GL_COLOR_BUFFER_BIT); |
| 209 | |
Jakob Bornecrantz | fa75293 | 2008-09-18 14:14:56 +0200 | [diff] [blame] | 210 | glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, 1.0, 1.0, 0.0, 0.0); |
| 211 | glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 1, 0.0, 0.0, 1.0, 1.0); |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 212 | glBegin(GL_TRIANGLES); |
Keith Whitwell | 103a4bd | 2009-03-23 18:37:33 +0000 | [diff] [blame] | 213 | |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 214 | glColor3f(0,0,1); |
Keith Whitwell | 103a4bd | 2009-03-23 18:37:33 +0000 | [diff] [blame] | 215 | glTexCoord3f(1,1,0); |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 216 | glVertex3f( 0.9, -0.9, -30.0); |
Keith Whitwell | 103a4bd | 2009-03-23 18:37:33 +0000 | [diff] [blame] | 217 | |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 218 | glColor3f(1,0,0); |
Keith Whitwell | 103a4bd | 2009-03-23 18:37:33 +0000 | [diff] [blame] | 219 | glTexCoord3f(1,-1,0); |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 220 | glVertex3f( 0.9, 0.9, -30.0); |
Keith Whitwell | 103a4bd | 2009-03-23 18:37:33 +0000 | [diff] [blame] | 221 | |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 222 | glColor3f(0,1,0); |
Keith Whitwell | 103a4bd | 2009-03-23 18:37:33 +0000 | [diff] [blame] | 223 | glTexCoord3f(-1,0,0); |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 224 | glVertex3f(-0.9, 0.0, -30.0); |
| 225 | glEnd(); |
| 226 | |
| 227 | glFlush(); |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 228 | if (show_fps) { |
| 229 | ++frame_cnt; |
| 230 | glutPostRedisplay(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | |
| 235 | int main(int argc, char **argv) |
| 236 | { |
| 237 | glutInit(&argc, argv); |
| 238 | glutInitWindowPosition(0, 0); |
| 239 | glutInitWindowSize(250, 250); |
| 240 | glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH); |
Keith Whitwell | 1f13545 | 2008-09-12 10:28:36 +0100 | [diff] [blame] | 241 | args(argc, argv); |
| 242 | glutCreateWindow(filename); |
Jakob Bornecrantz | 96c773c | 2009-02-14 01:05:13 +0100 | [diff] [blame] | 243 | glewInit(); |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 244 | glutReshapeFunc(Reshape); |
| 245 | glutKeyboardFunc(Key); |
| 246 | glutDisplayFunc(Display); |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 247 | Init(); |
Jakob Bornecrantz | 96c773c | 2009-02-14 01:05:13 +0100 | [diff] [blame] | 248 | #ifndef WIN32 |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 249 | if (show_fps) { |
| 250 | signal(SIGALRM, alarmhandler); |
| 251 | alarm(5); |
| 252 | } |
Jakob Bornecrantz | 96c773c | 2009-02-14 01:05:13 +0100 | [diff] [blame] | 253 | #endif |
Zack Rusin | 55c1894 | 2007-12-17 13:21:45 -0500 | [diff] [blame] | 254 | glutMainLoop(); |
| 255 | return 0; |
| 256 | } |