Advertisement
Guest User

RenderTarget.cpp

a guest
Jun 18th, 2012
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1. #include "RenderTarget.h"
  2. #include "Context.h"
  3. #include <math.h>
  4. #include <stdio.h>
  5.  
  6. #include "Helper.h" // remove this
  7.  
  8. RenderTarget::Viewport RenderTarget::viewport;
  9.  
  10. RenderTarget::RenderTarget(GLint width, GLint height, GLint texCount)
  11. {
  12.     rot = 0;
  13.  
  14.     size.x = width;
  15.     size.y = height;
  16.     this->texCount = texCount;
  17.     windowSize.x = Context::getWindowWidth();
  18.     windowSize.y = Context::getWindowHeight();
  19.  
  20.     // Create and initialize framebuffer
  21.     texRT = new GLuint[texCount];
  22.  
  23.     glGenFramebuffers(1, &frameBufferRT);
  24.     glBindFramebuffer(GL_FRAMEBUFFER, frameBufferRT);
  25.    
  26.     glGenTextures(texCount, texRT);
  27.     for(int i = 0; i < texCount; i ++)
  28.     {
  29.         glBindTexture(GL_TEXTURE_2D, texRT[i]);
  30.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  31.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  32.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  33.         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, texRT[i], 0);
  34.     }
  35.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  36.  
  37.     printf("+ RenderTarget added\n");
  38. }
  39.  
  40. RenderTarget::~RenderTarget()
  41. {
  42.     printf("- RenderTarget deleted\n");
  43.     delete[] texRT;
  44. }
  45.  
  46. void RenderTarget::Begin()
  47. {
  48.     // Setup framebuffer
  49.     glGetIntegerv(GL_VIEWPORT, viewport.vp);
  50.     glViewport(0, size.y - windowSize.y, windowSize.x, windowSize.y);
  51.     glBindFramebuffer(GL_FRAMEBUFFER, frameBufferRT);
  52.  
  53.     if (texCount > 1)
  54.     {
  55.         GLenum* buffers = new GLenum[texCount];
  56.         for (GLuint i = 0; i < texCount; i++)
  57.         {
  58.             buffers[i] = GL_COLOR_ATTACHMENT0 + i;
  59.         }
  60.         glDrawBuffers(texCount, buffers);
  61.         delete[] buffers;
  62.     }
  63.  
  64.     // Set blend-mode
  65.     glDisable(GL_BLEND);
  66.  
  67.     // Clear the buffer
  68.     glClearColor(0.0, 0.0, 0.0, 0.0);
  69.     glClear(GL_COLOR_BUFFER_BIT);
  70. }
  71.  
  72. void RenderTarget::End()
  73. {
  74.     // Reset blend-mode
  75.     glEnable(GL_BLEND);
  76.  
  77.     // Reset framebuffer
  78.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  79.  
  80.     // Reset viewport
  81.     glViewport(viewport.x, viewport.y, viewport.width, viewport.height);
  82. }
  83.  
  84. void RenderTarget::Draw(GLint x, GLint y)
  85. {
  86.     Draw(x, y, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0, 0, size.x, size.y);
  87. }
  88.  
  89. void RenderTarget::Draw(GLint x, GLint y, GLfloat alpha)
  90. {
  91.     Draw(x, y, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, alpha, 0, 0, size.x, size.y);
  92. }
  93.  
  94. void RenderTarget::Draw(GLint x, GLint y, GLfloat rotation, GLfloat scaleX, GLfloat scaleY, GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha, GLint xx, GLint yy, GLint w, GLint h)
  95. {
  96.     // Draw the framebuffer-texture
  97.     // Bind texture
  98.     glBindTexture(GL_TEXTURE_2D, texRT[0]);
  99.    
  100.     // Push the matrix
  101.     glPushMatrix();
  102.  
  103.     // Rotate and translate the quad
  104.     glTranslatef(x, y, 0.0f);
  105.     glRotatef(rotation, 0.0f, 0.0f, 1.0f);
  106.  
  107.     // Calculate coordinates
  108.     GLdouble coX1, coX2, coY1, coY2;
  109.     coX1 = (GLdouble)xx / size.x;
  110.     coY1 = (GLdouble)yy / size.y;
  111.     coX2 = (GLdouble)(xx + w) / size.x;
  112.     coY2 = (GLdouble)(yy + h) / size.y;
  113.  
  114.     // Testing blend functions
  115.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  116.  
  117.     // Draw the textured quad
  118.     glBegin(GL_QUADS);
  119.     glColor4f(red, green, blue, alpha);
  120.    
  121.     glTexCoord2d(coX1, coY2); glVertex2d(0,             0);
  122.     glTexCoord2d(coX2, coY2); glVertex2d(w * scaleX,    0);
  123.     glTexCoord2d(coX2, coY1); glVertex2d(w * scaleX,    h * scaleY);
  124.     glTexCoord2d(coX1, coY1); glVertex2d(0,             h * scaleY);
  125.     //glTexCoord2d(coX1, coY2); glVertex2d(lenDirX(5, rot * 2), lenDirY(5, rot * 3));
  126.     //glTexCoord2d(coX2, coY2); glVertex2d(w * scaleX + lenDirX(5, rot),    lenDirX(5, rot * 4));
  127.     //glTexCoord2d(coX2, coY1); glVertex2d(w * scaleX + lenDirY(5, rot * 4),    h * scaleY + lenDirX(5, rot));
  128.     //glTexCoord2d(coX1, coY1); glVertex2d(lenDirX(-5, rot * 3),                h * scaleY + lenDirX(-5, rot * 2));
  129.     glEnd();
  130.  
  131.     // Return to default blend-function
  132.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  133.  
  134.     // Pop the matrix
  135.     glPopMatrix();
  136.  
  137.     rot += 0.5f;
  138. }
  139.  
  140. // Get size of rendertarget
  141. Point RenderTarget::GetSize()
  142. {
  143.     return size;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement