Esempio n. 1
0
unsigned int GLTexture::_Load(unsigned char* data)
{
  glGenTextures(1, &glTex);
  glBindTexture(GL_TEXTURE_2D, glTex);
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  if(!data)
    glTexImage2D(GetGLType(), 0, 4, width, height, 0, GetGLFormat(), GL_UNSIGNED_BYTE, data);
  else
    gluBuild2DMipmaps(GetGLType(), 4, width, height, GetGLFormat(), GL_UNSIGNED_BYTE, data);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

  return true;
}
Esempio n. 2
0
/*virtual*/ void GL2RenderTarget::Initialize(
    const SRenderTargetParams& Params) {
  XTRACE_FUNCTION;

  m_Width = Params.Width;
  m_Height = Params.Height;
  #ifdef PANDORA
  if ((m_Height == 480) && (m_Width = 800))
  {
    m_Width = 512;
    m_Height = 256;
  }
  #endif

#ifdef HAVE_GLES
  m_Width = NPOT(m_Width);
  m_Height = NPOT(m_Height);

  glGenFramebuffers(1, &m_FrameBufferObject);
  ASSERT(m_FrameBufferObject != 0);
  glBindFramebuffer(GL_FRAMEBUFFER, m_FrameBufferObject);
  if (Params.ColorFormat != ERTF_None) {
    glGenTextures(1, &m_ColorTextureObject);
    ASSERT(m_ColorTextureObject != 0);
    glBindTexture(GL_TEXTURE_2D, m_ColorTextureObject);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_Width, m_Height, 0,
                 GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    m_ColorTexture = new GL2Texture(m_ColorTextureObject);
    glBindTexture(GL_TEXTURE_2D, 0);  // Unbind ?
  }
  if (Params.DepthStencilFormat != ERTF_None) {
      glGenRenderbuffers(1, &m_DepthStencilRenderBufferObject);
      ASSERT(m_DepthStencilRenderBufferObject != 0);
      glBindRenderbuffer(GL_RENDERBUFFER, m_DepthStencilRenderBufferObject);
      glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, m_Width,
                            m_Height);
  }
  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
                         m_ColorTextureObject, 0);
  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
                            GL_RENDERBUFFER,
                            m_DepthStencilRenderBufferObject);
  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
                            GL_RENDERBUFFER,
                            m_DepthStencilRenderBufferObject);

  const GLenum FrameBufferStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  ASSERT(FrameBufferStatus == GL_FRAMEBUFFER_COMPLETE);
  Unused(FrameBufferStatus);
#else
  // FBOs were supported in GL 2.1 only by extension, but some newer drivers
  // don't still support that extension. Use whichever is available.
  ASSERT(GLEW_EXT_framebuffer_object || GLEW_ARB_framebuffer_object);

  if (GLEW_ARB_framebuffer_object) {
    glGenFramebuffers(1, &m_FrameBufferObject);
    ASSERT(m_FrameBufferObject != 0);
    glBindFramebuffer(GL_FRAMEBUFFER, m_FrameBufferObject);
  } else if (GLEW_EXT_framebuffer_object) {
    glGenFramebuffersEXT(1, &m_FrameBufferObject);
    ASSERT(m_FrameBufferObject != 0);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_FrameBufferObject);
  }

  if (Params.ColorFormat != ERTF_None) {
    glGenTextures(1, &m_ColorTextureObject);
    ASSERT(m_ColorTextureObject != 0);
    glBindTexture(GL_TEXTURE_2D, m_ColorTextureObject);
    const GLenum ColorFormat = GetGLFormat(Params.ColorFormat);
    // The image format parameters don't necessarily match the color format,
    // but it doesn't matter because we're not providing image data here.
    glTexImage2D(GL_TEXTURE_2D, 0, ColorFormat, Params.Width, Params.Height, 0,
                 GL_BGRA, GL_UNSIGNED_BYTE, nullptr);
    m_ColorTexture = new GL2Texture(m_ColorTextureObject);
  }

  if (Params.DepthStencilFormat != ERTF_None) {
    if (GLEW_ARB_framebuffer_object) {
      glGenRenderbuffers(1, &m_DepthStencilRenderBufferObject);
      ASSERT(m_DepthStencilRenderBufferObject != 0);
      glBindRenderbuffer(GL_RENDERBUFFER, m_DepthStencilRenderBufferObject);
      const GLenum DepthStencilFormat = GetGLFormat(Params.DepthStencilFormat);
      glRenderbufferStorage(GL_RENDERBUFFER, DepthStencilFormat, Params.Width,
                            Params.Height);
    } else if (GLEW_EXT_framebuffer_object) {
      glGenRenderbuffersEXT(1, &m_DepthStencilRenderBufferObject);
      ASSERT(m_DepthStencilRenderBufferObject != 0);
      glBindRenderbufferEXT(GL_RENDERBUFFER_EXT,
                            m_DepthStencilRenderBufferObject);
      const GLenum DepthStencilFormat = GetGLFormat(Params.DepthStencilFormat);
      glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, DepthStencilFormat,
                               Params.Width, Params.Height);
    }
  }

  if (GLEW_ARB_framebuffer_object) {
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
                           m_ColorTextureObject, 0);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
                              GL_RENDERBUFFER,
                              m_DepthStencilRenderBufferObject);

    const GLenum FrameBufferStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    ASSERT(FrameBufferStatus == GL_FRAMEBUFFER_COMPLETE);
    Unused(FrameBufferStatus);
  } else if (GLEW_EXT_framebuffer_object) {
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
                              GL_TEXTURE_2D, m_ColorTextureObject, 0);
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
                                 GL_RENDERBUFFER_EXT,
                                 m_DepthStencilRenderBufferObject);
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
                                 GL_RENDERBUFFER_EXT,
                                 m_DepthStencilRenderBufferObject);

    const GLenum FrameBufferStatus =
        glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
    ASSERT(FrameBufferStatus == GL_FRAMEBUFFER_COMPLETE_EXT);
    Unused(FrameBufferStatus);
  }
#endif  //HAVE_GLES
}