// compile: c99 -o indytex indytex.c -lglut -lXmu -lGLU -lGL -lX11 // #include #include #include #include #include //#define USE_GLUT_TIMER #define WIDTH 320 #define HEIGHT 240 time_t cur_time = 0; int frames = 0; int display_state = 2; GLuint tex_id = 0; void gl_error_print(void) { GLenum err; if ((err = glGetError())) printf("glError: %d\n", err); } void keyboard_event(unsigned char key, int x, int y) { if (key == 27) exit(0); switch (key - '0') { case 0: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); display_state = 0; glDisable(GL_TEXTURE_2D); break; case 1: display_state = 1; glDisable(GL_TEXTURE_2D); break; case 2: display_state = 2; glEnable(GL_TEXTURE_2D); glColor3f(1.0, 1.0, 1.0); break; } } #ifdef USE_GLUT_TIMER void timer_event(int data) { glutPostRedisplay(); glutTimerFunc(30, timer_event, 0); } #endif void display(void) { time_t new_time; float fps; int diff; switch (display_state) { case 0: break; case 1: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_POLYGON); glColor3f(1, 0, 0); glVertex3f(-0.6, -0.75, 0.5); glColor3f(0, 1, 0); glVertex3f(0.6, -0.75, 0); glColor3f(0, 0, 1); glVertex3f(0, 0.75, 0); glEnd(); break; case 2: glBegin(GL_QUADS); glTexCoord2f(0.0, 1.0); glVertex3f(-1, -1, 0); glTexCoord2f(1.0, 1.0); glVertex3f(1.0, -1, 0); glTexCoord2f(1.0, 0.0); glVertex3f(1.0, 1.0, 0); glTexCoord2f(0.0, 0.0); glVertex3f(-1, 1.0, 0); glEnd(); break; } glutSwapBuffers(); new_time = time(NULL); if (new_time != cur_time) { diff = new_time - cur_time; fps = frames / (diff * 1.0); cur_time = new_time; frames = 0; printf("FPS: %.2f\n", fps); } else frames++; gl_error_print(); #ifndef USE_GLUT_TIMER glutPostRedisplay(); #endif } void gl_init(void) { glDisable(GL_DEPTH_TEST); glShadeModel(GL_FLAT); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); glHint(GL_FOG_HINT, GL_FASTEST); glDisable(GL_POLYGON_SMOOTH); glDisable(GL_BLEND); glDisable(GL_ALPHA_TEST); glEnable(GL_TEXTURE_2D); } void texture_init(void) { int x, y; char r, g, b; char *ptr, *tex_buf; tex_buf = malloc(256 * 256 * 3); for (y = 0; y < 256; y++) { r = y; ptr = tex_buf + 256 * 3 * y; for (x = 0; x < 256; x++, ptr += 3) { b = x; g = x / 2 + y / 2; ptr[0] = b; ptr[1] = g; ptr[2] = r; } } glGenTextures(1, &tex_id); glBindTexture(GL_TEXTURE_2D, tex_id); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 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_BASE_LEVEL, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0); glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); */ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_BGR, GL_UNSIGNED_BYTE, tex_buf); } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowPosition(600, 100); glutInitWindowSize(WIDTH, HEIGHT); glutCreateWindow("Indy Texture Performance Test"); glutDisplayFunc(display); glutKeyboardFunc(keyboard_event); #ifdef USE_GLUT_TIMER timer_event(0); #endif cur_time = time(NULL); gl_init(); texture_init(); gl_error_print(); glutMainLoop(); }