Advertisement
Guest User

Untitled

a guest
May 28th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.01 KB | None | 0 0
  1. #include "define.h"
  2.  
  3. #include "fbo.h"
  4.  
  5.  
  6.  
  7.  
  8.  
  9. //-----------------------------------------------------------------------------
  10.  
  11. // Constructor
  12.  
  13. //-----------------------------------------------------------------------------
  14.  
  15. FBO::FBO()
  16.  
  17. {
  18.  
  19.     m_FrameBuffer=0;
  20.  
  21.     m_DepthRenderBuffer=0;
  22.  
  23.     m_ColorTextureID=0;
  24.  
  25.     m_DepthTextureID=0;
  26.  
  27.     m_width = 256;
  28.  
  29.     m_height = 256;
  30.  
  31.  
  32.  
  33.     //Create texture.
  34.  
  35.     glGenTextures(1, &m_ColorTextureID);
  36.  
  37.     glBindTexture(GL_TEXTURE_2D, m_ColorTextureID);
  38.  
  39.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  40.  
  41.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  42.  
  43.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  44.  
  45.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  46.  
  47.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  48.  
  49.     glBindTexture(GL_TEXTURE_2D, 0);
  50.  
  51.  
  52.  
  53.     glGenTextures(1, &m_DepthTextureID);
  54.  
  55.     glBindTexture(GL_TEXTURE_2D, m_DepthTextureID);
  56.  
  57.     glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, m_width, m_height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
  58.  
  59.     glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE,   GL_LUMINANCE);
  60.  
  61.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,   GL_LINEAR);
  62.  
  63.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,   GL_LINEAR);
  64.  
  65.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  66.  
  67.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  68.  
  69.     glBindTexture(GL_TEXTURE_2D, 0);
  70.  
  71.  
  72.  
  73.  
  74.  
  75.     //Make buffer
  76.  
  77.     glGenFramebuffers(1, &m_FrameBuffer);
  78.  
  79.     glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_FrameBuffer);
  80.  
  81.  
  82.  
  83.     glGenRenderbuffers(1, &m_DepthRenderBuffer);
  84.  
  85.     glBindRenderbuffer(GL_RENDERBUFFER_EXT, m_DepthRenderBuffer);
  86.  
  87.     glRenderbufferStorage(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, m_width, m_height);
  88.  
  89.     glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
  90.  
  91.  
  92.  
  93.     glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_ColorTextureID, 0);
  94.  
  95.     glFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT,GL_DEPTH_ATTACHMENT_EXT,GL_RENDERBUFFER_EXT, m_DepthRenderBuffer);
  96.  
  97.  
  98.  
  99.  
  100.  
  101.     glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
  102.  
  103.  
  104.  
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111. //-----------------------------------------------------------------------------
  112.  
  113. // Destructor
  114.  
  115. //-----------------------------------------------------------------------------
  116.  
  117. FBO::~FBO()
  118.  
  119. {
  120.  
  121.     glDeleteTextures(1, &m_ColorTextureID);
  122.  
  123.     glDeleteTextures(1, &m_DepthTextureID);
  124.  
  125.     glDeleteFramebuffers(1, &m_FrameBuffer);
  126.  
  127.     glDeleteRenderbuffers(1, &m_DepthRenderBuffer);
  128.  
  129. }
  130.  
  131.  
  132.  
  133.  
  134.  
  135. //-----------------------------------------------------------------------------
  136.  
  137. // Enable
  138.  
  139. //-----------------------------------------------------------------------------
  140.  
  141. void FBO::Enable(void)
  142.  
  143. {
  144.  
  145.     glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_FrameBuffer);
  146.  
  147.  
  148.  
  149.     glViewport(0,0,m_width,m_height);
  150.  
  151.     glPushMatrix();
  152.  
  153. }
  154.  
  155.  
  156.  
  157. //-----------------------------------------------------------------------------
  158.  
  159. // Disable
  160.  
  161. //-----------------------------------------------------------------------------
  162.  
  163. void FBO::Disable(void)
  164.  
  165. {
  166.  
  167.     glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
  168.  
  169.  
  170.  
  171.     glViewport(0,0,SCREENX,SCREENY);
  172.  
  173.     glPopMatrix();
  174.  
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183. //-----------------------------------------------------------------------------
  184.  
  185. // SetSize
  186.  
  187. //-----------------------------------------------------------------------------
  188.  
  189. void FBO::SetSize( int width, int height )
  190.  
  191. {
  192.  
  193.     m_width = width;
  194.  
  195.     m_height = height;
  196.  
  197.     glBindRenderbuffer(GL_RENDERBUFFER_EXT, m_DepthRenderBuffer);
  198.  
  199.     glRenderbufferStorage(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT32, m_width, m_height);
  200.  
  201.     glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
  202.  
  203.  
  204.  
  205.     //Create color texture.
  206.  
  207.     glBindTexture(GL_TEXTURE_2D, m_ColorTextureID);
  208.  
  209.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  210.  
  211.  
  212.  
  213.     glBindTexture(GL_TEXTURE_2D, m_DepthTextureID);
  214.  
  215.     glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, m_width, m_height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
  216.  
  217.  
  218.  
  219.     glBindTexture(GL_TEXTURE_2D, 0);
  220.  
  221. }
  222.  
  223.  
  224.  
  225.  
  226.  
  227. /*//-----------------------------------------------------------------------------
  228.  
  229. // GetSize
  230.  
  231. //-----------------------------------------------------------------------------
  232.  
  233. ivec2& FBO::GetSize(void)
  234.  
  235. {
  236.  
  237.     return m_size;
  238.  
  239. }*/
  240.  
  241.  
  242.  
  243.  
  244.  
  245. //-----------------------------------------------------------------------------
  246.  
  247. // GetColor
  248.  
  249. //-----------------------------------------------------------------------------
  250.  
  251. GLuint FBO::GetColor(void)
  252.  
  253. {
  254.  
  255.     return m_ColorTextureID;
  256.  
  257. }
  258.  
  259.  
  260.  
  261. //-----------------------------------------------------------------------------
  262.  
  263. // GetDepth
  264.  
  265. //-----------------------------------------------------------------------------
  266.  
  267. GLuint FBO::GetDepth(void)
  268.  
  269. {
  270.  
  271.     return m_DepthTextureID;
  272.  
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement