/* * Copyright (C) 2010 Nick Bowler * * License WTFPL2: Do What The Fuck You Want To Public License, version 2. * This is free software: you are free to do what the fuck you want to. * There is NO WARRANTY, to the extent permitted by law. * * In case the above is not acceptable, you may alternately choose the terms * of the MIT license: http://opensource.org/licenses/mit-license.php. */ #include #include #include #include #include #ifndef XRES # define XRES 32 #endif #ifndef YRES # define YRES 32 #endif static float pixbuf[YRES][XRES]; void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); for (unsigned i = 0; i < YRES; i++) { for (unsigned j = 0; j < XRES; j++) { if (isfinite(pixbuf[i][j])) { glPointSize(pixbuf[i][j]); glBegin(GL_POINTS); glVertex2d(j, i); glEnd(); } } } glutSwapBuffers(); } void reshape(int width, int height) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0, 0, width, height); glOrtho(0, XRES, 0, YRES, -1, 1); glMatrixMode(GL_MODELVIEW); } void drawTeapot(size_t w, size_t h) { glPushAttrib(GL_VIEWPORT_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT); glViewport(0, 0, w, h); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPerspective(40.0, 1, 0.1, 10.0); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glTranslated(0, 0, -5); glColor3d(1, 1, 1); glutSolidTeapot(1); glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glPopAttrib(); } void rasterTeapot(float *pixels, size_t w, size_t h) { GLuint fbo, buffers[2]; glGenFramebuffersEXT(1, &fbo); glGenRenderbuffersEXT(2, buffers); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, buffers[0]); glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB8, w, h); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, buffers[1]); glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, w, h); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, buffers[0]); glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, buffers[1]); drawTeapot(w, h); glPixelTransferf(GL_DEPTH_SCALE, -1); glPixelTransferf(GL_DEPTH_BIAS, 1); glReadPixels(0, 0, w, h, GL_DEPTH_COMPONENT, GL_FLOAT, pixels); for (unsigned i = 0; i < w*h; i++) { if (pixels[i] == 0) pixels[i] = -INFINITY; pixels[i] *= 1200; pixels[i] -= 11; } glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); glDeleteFramebuffersEXT(1, &fbo); glDeleteRenderbuffersEXT(2, buffers); } int main(int argc, char **argv) { GLenum err; int window; glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); window = glutCreateWindow("GLTest"); if (window == 0) return EXIT_FAILURE; glutSetWindow(window); err = glewInit(); if (err != GLEW_OK) { fprintf(stderr, "glewInit: %s\n", glewGetErrorString(err)); return EXIT_FAILURE; } glEnable(GL_DEPTH_TEST); glEnable(GL_POINT_SMOOTH); rasterTeapot(pixbuf[0], XRES, YRES); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; }