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> |
| 5 | #define GL_GLEXT_PROTOTYPES |
| 6 | #include <GL/glut.h> |
| 7 | #include <unistd.h> |
| 8 | #include <signal.h> |
| 9 | |
| 10 | unsigned show_fps = 0; |
| 11 | unsigned int frame_cnt = 0; |
| 12 | void alarmhandler(int); |
| 13 | static const char *filename = NULL; |
| 14 | |
| 15 | static void usage(char *name) |
| 16 | { |
| 17 | fprintf(stderr, "usage: %s [ options ] shader_filename\n", name); |
| 18 | fprintf(stderr, "\n" ); |
| 19 | fprintf(stderr, "options:\n"); |
| 20 | fprintf(stderr, " -fps show frames per second\n"); |
| 21 | } |
| 22 | |
| 23 | void alarmhandler (int sig) |
| 24 | { |
| 25 | if (sig == SIGALRM) { |
| 26 | printf("%d frames in 5.0 seconds = %.3f FPS\n", frame_cnt, |
| 27 | frame_cnt / 5.0); |
| 28 | |
| 29 | frame_cnt = 0; |
| 30 | } |
| 31 | signal(SIGALRM, alarmhandler); |
| 32 | alarm(5); |
| 33 | } |
| 34 | |
| 35 | static void args(int argc, char *argv[]) |
| 36 | { |
| 37 | GLint i; |
| 38 | |
| 39 | for (i = 1; i < argc; i++) { |
| 40 | if (strcmp(argv[i], "-fps") == 0) { |
| 41 | show_fps = 1; |
| 42 | } |
| 43 | else if (i == argc - 1) { |
| 44 | filename = argv[i]; |
| 45 | } |
| 46 | else { |
| 47 | usage(argv[0]); |
| 48 | exit(1); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if (!filename) { |
| 53 | usage(argv[0]); |
| 54 | exit(1); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static void Init( void ) |
| 59 | { |
| 60 | GLint errno; |
| 61 | GLuint prognum; |
| 62 | char buf[4096]; |
| 63 | GLuint sz; |
| 64 | FILE *f; |
| 65 | |
| 66 | if ((f = fopen(filename, "r")) == NULL) { |
| 67 | fprintf(stderr, "Couldn't open %s\n", filename); |
| 68 | exit(1); |
| 69 | } |
| 70 | |
| 71 | sz = fread(buf, 1, sizeof(buf), f); |
| 72 | if (!feof(f)) { |
| 73 | fprintf(stderr, "file too long\n"); |
| 74 | exit(1); |
| 75 | } |
| 76 | fprintf(stderr, "%.*s\n", sz, buf); |
| 77 | |
| 78 | if (!glutExtensionSupported("GL_ARB_fragment_program")) { |
| 79 | printf("Error: GL_ARB_fragment_program not supported!\n"); |
| 80 | exit(1); |
| 81 | } |
| 82 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 83 | |
| 84 | /* Setup the fragment program */ |
| 85 | glGenProgramsARB(1, &prognum); |
| 86 | glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, prognum); |
| 87 | glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, |
| 88 | sz, (const GLubyte *)buf); |
| 89 | |
| 90 | errno = glGetError(); |
| 91 | printf("glGetError = 0x%x\n", errno); |
| 92 | if (errno != GL_NO_ERROR) { |
| 93 | GLint errorpos; |
| 94 | |
| 95 | glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos); |
| 96 | printf("errorpos: %d\n", errorpos); |
| 97 | printf("glError(GL_PROGRAM_ERROR_STRING_ARB) = %s\n", |
| 98 | (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB)); |
| 99 | } |
| 100 | glEnable(GL_FRAGMENT_PROGRAM_ARB); |
| 101 | |
| 102 | glClearColor(.3, .3, .3, 0); |
| 103 | } |
| 104 | |
| 105 | static void Reshape(int width, int height) |
| 106 | { |
| 107 | |
| 108 | glViewport(0, 0, (GLint)width, (GLint)height); |
| 109 | |
| 110 | glMatrixMode(GL_PROJECTION); |
| 111 | glLoadIdentity(); |
| 112 | glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); |
| 113 | glMatrixMode(GL_MODELVIEW); |
| 114 | } |
| 115 | |
| 116 | static void Key(unsigned char key, int x, int y) |
| 117 | { |
| 118 | |
| 119 | switch (key) { |
| 120 | case 27: |
| 121 | exit(1); |
| 122 | default: |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | glutPostRedisplay(); |
| 127 | } |
| 128 | |
| 129 | static void Display(void) |
| 130 | { |
| 131 | glClear(GL_COLOR_BUFFER_BIT); |
| 132 | |
| 133 | glBegin(GL_TRIANGLES); |
| 134 | glColor3f(0,0,1); |
| 135 | glVertex3f( 0.9, -0.9, -30.0); |
| 136 | glColor3f(1,0,0); |
| 137 | glVertex3f( 0.9, 0.9, -30.0); |
| 138 | glColor3f(0,1,0); |
| 139 | glVertex3f(-0.9, 0.0, -30.0); |
| 140 | glEnd(); |
| 141 | |
| 142 | glFlush(); |
| 143 | |
| 144 | if (show_fps) { |
| 145 | ++frame_cnt; |
| 146 | glutPostRedisplay(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | |
| 151 | int main(int argc, char **argv) |
| 152 | { |
| 153 | glutInit(&argc, argv); |
| 154 | glutInitWindowPosition(0, 0); |
| 155 | glutInitWindowSize(250, 250); |
| 156 | glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH); |
| 157 | glutCreateWindow(argv[0]); |
| 158 | glutReshapeFunc(Reshape); |
| 159 | glutKeyboardFunc(Key); |
| 160 | glutDisplayFunc(Display); |
| 161 | args(argc, argv); |
| 162 | Init(); |
| 163 | if (show_fps) { |
| 164 | signal(SIGALRM, alarmhandler); |
| 165 | alarm(5); |
| 166 | } |
| 167 | glutMainLoop(); |
| 168 | return 0; |
| 169 | } |