daily pastebin goal
57%
SHARE
TWEET

Untitled

a guest Mar 13th, 2009 343 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifdef _WIN32
  2.         #include "windows.h"
  3. #endif
  4.  
  5. #include <GL/glut.h>
  6. #include <GL/glext.h>
  7.  
  8. #include <stdio.h>
  9.  
  10. PFNGLGENFRAMEBUFFERSPROC           glGenFramebuffers=0;
  11. PFNGLDELETEFRAMEBUFFERSPROC        glDeleteFramebuffers=0;
  12. PFNGLBINDFRAMEBUFFERPROC           glBindFramebuffer=0;
  13. PFNGLCHECKFRAMEBUFFERSTATUSPROC    glCheckFramebufferStatus=0;
  14. PFNGLFRAMEBUFFERRENDERBUFFERPROC   glFramebufferRenderbuffer=0;
  15.  
  16. PFNGLGENRENDERBUFFERSPROC          glGenRenderbuffers=0;
  17. PFNGLDELETERENDERBUFFERSPROC       glDeleteRenderbuffers=0;
  18. PFNGLBINDRENDERBUFFERPROC          glBindRenderbuffer=0;
  19. PFNGLRENDERBUFFERSTORAGEPROC       glRenderbufferStorage=0;
  20. PFNGLFRAMEBUFFERTEXTURE2DPROC      glFramebufferTexture2D=0;
  21.  
  22.  
  23. void checkFBO() {
  24.     GLenum status;
  25.     status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  26.     if (status != GL_FRAMEBUFFER_COMPLETE) {
  27.         printf("Error: %d", status);
  28.     }
  29. }
  30.  
  31. GLuint fboId;
  32. GLuint texId;
  33. GLuint depthId;
  34.  
  35. int width = 256;
  36. int height = 256;
  37.  
  38. void init() {
  39.     char *exStr =(char*)glGetString(GL_EXTENSIONS);
  40.     if (strstr(exStr,"GL_ARB_framebuffer_object")==NULL) {
  41.         printf("GL_ARB_framebuffer_object not supported.\n");
  42.         exit(1);
  43.     } if (strstr(exStr,"GL_ARB_texture_float")==NULL) {
  44.         printf("GL_ARB_texture_float not supported.\n");
  45.         exit(1);
  46.     }
  47. #ifdef _WIN32
  48.     #define GET_PROC_ADDRESS(x) wglGetProcAddress(x);
  49. #else
  50.     #define GET_PROC_ADDRESS(x) glXGetProcAddress((const GLubyte*)x);
  51. #endif
  52.  
  53.     glGenFramebuffers= (PFNGLGENFRAMEBUFFERSPROC)  GET_PROC_ADDRESS("glGenFramebuffers");
  54.     glBindFramebuffer= (PFNGLBINDFRAMEBUFFERPROC)  GET_PROC_ADDRESS("glBindFramebuffer");
  55.     glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC) GET_PROC_ADDRESS("glDeleteFramebuffers");
  56.     glCheckFramebufferStatus= (PFNGLCHECKFRAMEBUFFERSTATUSPROC)  GET_PROC_ADDRESS("glCheckFramebufferStatus");
  57.     glGenRenderbuffers=(PFNGLGENRENDERBUFFERSPROC)GET_PROC_ADDRESS("glGenRenderbuffers");
  58.     glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC) GET_PROC_ADDRESS("glBindRenderbuffer");
  59.     glRenderbufferStorage= (PFNGLRENDERBUFFERSTORAGEPROC) GET_PROC_ADDRESS("glRenderbufferStorage");
  60.     glDeleteRenderbuffers= (PFNGLDELETERENDERBUFFERSPROC)  GET_PROC_ADDRESS("glDeleteRenderbuffers");
  61.     glFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFERPROC) GET_PROC_ADDRESS("glFramebufferRenderbuffer");
  62.     glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC)GET_PROC_ADDRESS("glFramebufferTexture2D");
  63. }
  64.  
  65. void initFBO() {
  66.     glPushAttrib(GL_TEXTURE_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
  67.     glGenTextures(1, &texId);
  68.     glBindTexture(GL_TEXTURE_2D, texId);
  69.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  70.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  71.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  72.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  73.  
  74.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
  75.  
  76.     glGenRenderbuffers(1, &depthId);
  77.     glBindRenderbuffer(GL_RENDERBUFFER, depthId);
  78.     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
  79.  
  80.     glGenFramebuffers(1, &fboId);
  81.     glBindFramebuffer(GL_FRAMEBUFFER, fboId);
  82.     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texId, 0);
  83.     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthId);
  84.  
  85.     glDrawBuffer(GL_COLOR_ATTACHMENT0);
  86.     glReadBuffer(GL_NONE);
  87.     glViewport(0, 0, width, height);
  88.     checkFBO();
  89.  
  90.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  91.     glPopAttrib();
  92. }
  93.  
  94. static void display(void) {
  95.     glPushAttrib(GL_VIEWPORT_BIT | GL_COLOR_BUFFER_BIT);
  96.     glBindFramebuffer(GL_FRAMEBUFFER, fboId);
  97.     glDrawBuffer(GL_COLOR_ATTACHMENT0);
  98.     glViewport(0, 0, width, height);
  99.     glClearColor(0.2, 0.1, 0.3, 1.0);
  100.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  101.  
  102.     glLoadIdentity();
  103.     glTranslatef(0.0, 0.0, -5.0);
  104.     glEnable(GL_DEPTH_TEST);
  105.  
  106.     glutSolidSphere(1.0, 32, 32);
  107.  
  108.     glPopAttrib();
  109.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  110.  
  111.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  112.  
  113.     glLoadIdentity();
  114.     glTranslatef(0.0, 0.0, -3.0);
  115.  
  116.     glBindTexture(GL_TEXTURE_2D, texId);
  117.     glEnable(GL_TEXTURE_2D);
  118.     glDisable(GL_DEPTH_TEST);
  119.  
  120.     glBegin(GL_QUADS);
  121.     glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.5f, -0.5f);
  122.     glTexCoord2f(1.0f, 0.0f); glVertex2f(0.5f, -0.5f);
  123.     glTexCoord2f(1.0f, 1.0f); glVertex2f(0.5f, 0.5f);
  124.     glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.5f, 0.5f);
  125.     glEnd();
  126.  
  127.     glDisable(GL_TEXTURE_2D);
  128.     glBegin(GL_LINE_LOOP);
  129.     glVertex2f(-0.5f, -0.5f);
  130.     glVertex2f(0.5f, -0.5f);
  131.     glVertex2f(0.5f, 0.5f);
  132.     glVertex2f(-0.5f, 0.5f);
  133.     glEnd();
  134.  
  135.     glutSwapBuffers();
  136. }
  137.  
  138. static void reshape(int w, int h) {
  139.    glViewport(0, 0, w, h);
  140.  
  141.    glMatrixMode(GL_PROJECTION);
  142.    glLoadIdentity();
  143.    glOrtho(-1.0, 1.0, -1.0, 1.0, 0.01, 10.0);
  144.    glMatrixMode(GL_MODELVIEW);
  145.    glLoadIdentity();
  146. }
  147.  
  148. static void key(unsigned char key, int x, int y) {
  149.     switch (key) {
  150.         case 27 :
  151.         case 'q':
  152.             exit(0);
  153.             break;
  154.     }
  155.     glutPostRedisplay();
  156. }
  157.  
  158. static void idle(void) {
  159.     glutPostRedisplay();
  160. }
  161.  
  162. int main(int argc, char *argv[]) {
  163.     glutInit(&argc, argv);
  164.     glutInitWindowSize(640,480);
  165.     glutInitWindowPosition(10,10);
  166.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  167.  
  168.     glutCreateWindow("OpenGL-Example");
  169.  
  170.     init();
  171.     initFBO();
  172.  
  173.     glutReshapeFunc(reshape);
  174.     glutDisplayFunc(display);
  175.     glutKeyboardFunc(key);
  176.     glutIdleFunc(idle);
  177.  
  178.     glutMainLoop();
  179.  
  180.     glDeleteFramebuffers(1, &fboId);
  181.     glDeleteTextures(1, &texId);
  182.     glDeleteRenderbuffers(1, &depthId);
  183.  
  184.     return 0;
  185. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top