Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.51 KB | None | 0 0
  1. ////////////// TEXTURE.CPP //////////////////
  2.  
  3. #include "Common.h"
  4. #include "Graphics.h"
  5. #include "Texture.h"
  6.  
  7. Texture::Texture(TexType type, const char* file /* = NULL */)
  8. {
  9.     m_ID = 0;
  10.     m_File = file ? file : String("");
  11.  
  12.     // load from file
  13.     if (type == eTexType_Image && file)
  14.     {
  15.         static bool initializedTIL = false;
  16.         if(!initializedTIL)
  17.         {
  18.             til::TIL_Init();
  19.             initializedTIL = true;
  20.         }
  21.  
  22.         til::Image* image = til::TIL_Load(file, TIL_FILE_ADDWORKINGDIR | TIL_DEPTH_A8B8G8R8);
  23.  
  24.         glEnable(GL_TEXTURE_2D);
  25.         glGenTextures(1, &m_ID);
  26.         glBindTexture(GL_TEXTURE_2D, m_ID);
  27.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  28.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  29.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  30.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  31.         glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
  32.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image->GetWidth(), image->GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image->GetPixels());
  33.         glBindTexture(GL_TEXTURE_2D, 0);
  34.  
  35.         til::TIL_Release(image);
  36.     }
  37.  
  38.     // create render target
  39.     else if(type == eTexType_RenderTarget)
  40.     {
  41.         glEnable(GL_TEXTURE_2D);
  42.         glGenTextures(1, &m_ID);
  43.         glBindTexture(GL_TEXTURE_2D, m_ID);
  44.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  45.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  46.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  47.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  48.         glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
  49.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, GRAPHICS_SCRWIDTH, GRAPHICS_SCRHEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  50.         glBindTexture(GL_TEXTURE_2D, 0);
  51.     }
  52.  
  53.     // create depth buffer
  54.     else if(type == eTexType_DepthTarget)
  55.     {
  56.         glEnable(GL_TEXTURE_2D);
  57.         glGenTextures(1, &m_ID);
  58.         glBindTexture(GL_TEXTURE_2D, m_ID);
  59.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  60.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  61.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  62.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  63.         glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
  64.         glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GRAPHICS_SCRWIDTH, GRAPHICS_SCRHEIGHT, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
  65.         glBindTexture(GL_TEXTURE_2D, 0);
  66.     }
  67. }
  68.  
  69. Texture::~Texture(void)
  70. {
  71.     glDeleteTextures(1, &m_ID);
  72. }
  73.  
  74. void Texture::Bind()
  75. {
  76.     glBindTexture(GL_TEXTURE_2D, m_ID);
  77. }
  78.  
  79. void Texture::Unbind()
  80. {
  81.     glBindTexture(GL_TEXTURE_2D, 0);
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88. ////////////// RENDERTARGET.CPP //////////////////
  89.  
  90. #include "Common.h"
  91. #include "Texture.h"
  92. #include "Graphics.h"
  93. #include "Application.h"
  94. #include "RenderTarget.h"
  95.  
  96. RenderTarget::RenderTarget(int type)
  97. {
  98.     m_Active = false;
  99.     m_DepthTexture = NULL;
  100.     m_ColorTexture = NULL;
  101.  
  102.     glGenFramebuffers(1, &m_FboID);
  103.     glBindFramebuffer(GL_FRAMEBUFFER, m_FboID);
  104.  
  105.     if(type & eRenderTargetType_Color)
  106.     {
  107.         m_ColorTexture = new Texture(eTexType_RenderTarget);
  108.         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_ColorTexture->GetID(), 0);
  109.     }
  110.  
  111.     if(type & eRenderTargetType_Depth)
  112.     {
  113.         m_DepthTexture = new Texture(eTexType_DepthTarget);
  114.         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_DepthTexture->GetID(), 0);
  115.     }
  116.  
  117.     GLenum bufferStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  118.     if(bufferStatus != GL_FRAMEBUFFER_COMPLETE)
  119.     {
  120.         MessageBoxA(NULL, "Failed to create FBO", "Error", MB_OK);
  121.         Application::Get()->Stop();
  122.     }
  123.  
  124.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  125. }
  126.  
  127. RenderTarget::~RenderTarget(void)
  128. {
  129.     SAFE_DELETE(m_ColorTexture);
  130.     SAFE_DELETE(m_DepthTexture);
  131.     glDeleteFramebuffers(1, &m_FboID);
  132. }
  133.  
  134. void RenderTarget::Enable()
  135. {
  136.     glBindTexture(GL_TEXTURE_2D, 0);
  137.     glBindFramebuffer(GL_FRAMEBUFFER, m_FboID);
  138.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  139.  
  140.     m_Active = true;
  141. }
  142.  
  143. void RenderTarget::Disable()
  144. {
  145.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  146.     glEnable(GL_TEXTURE_2D);
  147.     m_ColorTexture->Bind();
  148.  
  149.     glColor3f(1.f, 1.f, 1.f);
  150.     glBegin(GL_QUADS);
  151.         glTexCoord2f(1.f,   0.f);
  152.         glVertex2f(1.0f, 1.0f);
  153.  
  154.         glTexCoord2f(0.f, 0.f);
  155.         glVertex2f(0.0f, 1.0f);
  156.  
  157.         glTexCoord2f(0.f, 1.f  );
  158.         glVertex2f(0.0f, 0.0f);
  159.  
  160.         glTexCoord2f(1.f,   1.f  );
  161.         glVertex2f(1.0f, 0.0f);
  162.     glEnd();
  163.  
  164.     m_ColorTexture->Unbind();
  165.     m_Active = false;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement