Guest User

nystep

a guest
Jan 10th, 2010
98
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "renderBuffer.h"
  2. #include <glew.h>
  3. #include <assert.h>
  4.  
  5.  
  6. renderBuffer::renderBuffer( bool canHasDepthBuffer )
  7. {
  8.     hasDepth = canHasDepthBuffer;
  9.  
  10.     glGenFramebuffersEXT(1, &fb);
  11.     if (hasDepth)
  12.         glGenRenderbuffersEXT(1, &depthbuff);
  13. }
  14.  
  15.  
  16. renderBuffer::~renderBuffer()
  17. {
  18.     if (hasDepth)
  19.         glDeleteRenderbuffersEXT(1, &depthbuff);
  20.     glDeleteFramebuffersEXT(1, &fb);
  21. }
  22.  
  23.  
  24. void renderBuffer::renderConfig( texture **tt, int n_textures, int layerStart, int layerEnd )
  25. {
  26.     int maxTargets;
  27.     int attachement = GL_COLOR_ATTACHMENT0_EXT;
  28.     int nTargets = (layerEnd - layerStart + 1) * n_textures;
  29.     glGetIntegerv( GL_MAX_DRAW_BUFFERS_ARB, &maxTargets );
  30.  
  31.     assert( nTargets <= maxTargets );
  32.  
  33.     GLenum drawbuffers[16] =
  34.     { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT,
  35.       GL_COLOR_ATTACHMENT4_EXT, GL_COLOR_ATTACHMENT5_EXT, GL_COLOR_ATTACHMENT6_EXT, GL_COLOR_ATTACHMENT7_EXT,
  36.       GL_COLOR_ATTACHMENT8_EXT, GL_COLOR_ATTACHMENT9_EXT, GL_COLOR_ATTACHMENT10_EXT, GL_COLOR_ATTACHMENT11_EXT,
  37.       GL_COLOR_ATTACHMENT12_EXT, GL_COLOR_ATTACHMENT13_EXT, GL_COLOR_ATTACHMENT14_EXT, GL_COLOR_ATTACHMENT15_EXT };
  38.  
  39.     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
  40.  
  41.     for (int i=0; i<n_textures; i++)
  42.     {
  43.         int target = tt[i]->target;
  44.  
  45.         if (target == GL_TEXTURE_2D)
  46.         {
  47.             glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, attachement++, target, tt[i]->textureId, 0 );
  48.         }
  49.         else if (target == GL_TEXTURE_CUBE_MAP_ARB)
  50.         {
  51.             int target = GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB;
  52.             for (int j=layerStart; j<=layerEnd; j++)
  53.             {
  54.                 glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, attachement++, target+j, tt[i]->textureId, 0 );
  55.             }
  56.         }
  57.         else if (target == GL_TEXTURE_3D)
  58.         {
  59.             for (int j=layerStart; j<=layerEnd; j++)
  60.             {
  61.                 glFramebufferTexture3DEXT( GL_FRAMEBUFFER_EXT, attachement++, target, tt[i]->textureId, 0, j );
  62.             }
  63.         }
  64.     }
  65.  
  66.     if (hasDepth)
  67.     {
  68.         glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, depthbuff );
  69.         glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, (*tt)->sx, (*tt)->sy );
  70.         glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthbuff );
  71.     }
  72.  
  73.     glDrawBuffersARB( nTargets, drawbuffers );
  74.     assert( glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) == GL_FRAMEBUFFER_COMPLETE_EXT );
  75.     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  76.     assert( glGetError() == GL_NO_ERROR );
  77. }
  78.  
  79.  
  80. void renderBuffer::startRender()
  81. {
  82.     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
  83. }
  84.  
  85.  
  86. void renderBuffer::endRender()
  87. {
  88.     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  89. }
  90.  
  91.  
  92.  
  93. static void setViewport2D()
  94. {
  95.     glMatrixMode( GL_PROJECTION );
  96.     glPushMatrix();
  97.     glLoadIdentity();
  98.     glOrtho( 0.0, 1.0, 0.0, 1.0, -1.0, 1.0 );
  99.     glMatrixMode( GL_MODELVIEW );
  100.     glPushMatrix();
  101.     glLoadIdentity();
  102. }
  103.  
  104.  
  105. static void restoreViewport()
  106. {
  107.     glMatrixMode( GL_PROJECTION );
  108.     glPopMatrix();
  109.     glMatrixMode( GL_MODELVIEW );
  110.     glPopMatrix();
  111. }
  112.  
  113.  
  114. static void drawScreenQuad()
  115. {
  116.     glBegin( GL_QUADS );
  117.     glTexCoord2f( 0.f, 0.f ); glVertex3f( 0.f, 0.f, 0.f );
  118.     glTexCoord2f( 0.f, 1.f ); glVertex3f( 0.f, 1.0, 0.f );
  119.     glTexCoord2f( 1.f, 1.f ); glVertex3f( 1.0, 1.0, 0.f );
  120.     glTexCoord2f( 1.f, 0.f ); glVertex3f( 1.0, 0.f, 0.f );
  121.     glEnd();
  122. }
  123.  
  124.  
  125. void renderTo3DTexture( shader *sh, char *layerUniformName, char* nLayersUniformName, char* layerInvSizeUniformName, texture3d *tex )
  126. {
  127.     int maxTargets;
  128.     float *layerUniform;
  129.     //float *nLayersUniform;
  130.     float *layerInvSizeUniform;
  131.  
  132.     glGetIntegerv( GL_MAX_DRAW_BUFFERS_ARB, &maxTargets );
  133.  
  134.     layerUniform = sh->GetUniform( layerUniformName );
  135.     //nLayersUniform = sh->GetUniform( nLayersUniformName );
  136.     layerInvSizeUniform = sh->GetUniform( layerInvSizeUniformName );
  137.  
  138.     //if (nLayersUniform) *nLayersUniform = float(maxTargets);
  139.     if (layerInvSizeUniform) *layerInvSizeUniform = 1.0f / tex->sz;
  140.  
  141.     renderBuffer *rb = new renderBuffer( false );
  142.  
  143.     for (int i=0; i<tex->sz; i+=maxTargets)
  144.     {
  145.         rb->renderConfig( (texture**) &tex, 1, i, i+maxTargets-1 );
  146.         if (layerUniform) *layerUniform = float(i);
  147.  
  148.         glPushAttrib( GL_VIEWPORT_BIT );
  149.         glViewport( 0, 0, tex->sx, tex->sy );
  150.         rb->startRender();
  151.         glClear( GL_COLOR_BUFFER_BIT );
  152.  
  153.         setViewport2D();
  154.         sh->Bind();
  155.         drawScreenQuad();
  156.         sh->unBind();
  157.         restoreViewport();
  158.  
  159.         rb->endRender();
  160.         glPopAttrib();
  161.     }
  162.  
  163.     delete rb;
  164. }
  165.  
  166.  
RAW Paste Data