void Bamboo::RN_Deferred::ItlPreRenderChildren()
{
    glBindFramebuffer(GL_FRAMEBUFFER, m_nFBO);

    ItlPushFBO(m_nFBO);

    //set viewport size
    glViewport(0,0, m_nWidth, m_nHeight);

    // clear buffers
   // glClearColor(0.0, 0.0, 0.0, 0.0);
    //glClearDepth(1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    ItlGetGraphicCore()->GetShaderManager()->PushActiveShader();
    if (m_bLayeredFBO)
      ItlGetGraphicCore()->GetShaderManager()->ActivateShader("deferred_pass_cm");
    else
      ItlGetGraphicCore()->GetShaderManager()->ActivateShader("deferred_pass");

    GLuint l_nUseParallax = ItlGetGraphicCore()->GetShaderManager()->GetUniform("nUseParallax");

    assert (l_nUseParallax != -1);
    glUniform1i(l_nUseParallax, s_nUseParallax % 2);

    ItlPushViewportInformation(m_nWidth, m_nHeight);

}
/*!
 *  This method is called internally BEFORE rendering the children,
 *  and binds and clears the fbo as needed.
 */
void Bamboo::RN_FBO::ItlPreRenderChildren()
{
    //bind fbo
    glBindFramebuffer(GL_FRAMEBUFFER, m_nFramebuffer);

    //set viewport size
    glViewport(0,0, m_iWidth, m_iHeight);

    // clear buffers
   // glClearColor(0.0, 0.0, 0.0, 0.0);
    //glClearDepth(1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    ///store our fbo-id on the stack, to allow "deeper" SceneObject_FBOs to rebind our fbo after finish drawing
    ItlPushFBO(m_nFramebuffer);

    ItlPushViewportInformation(m_iWidth, m_iHeight);
}
void Bamboo::RN_Deferred::ItlRender()
{
  double test[3];
//  glVertex3dv(test);

  if (m_bLayeredFBO == false)
    {
      glBindFramebuffer(GL_FRAMEBUFFER, m_nFBO);
      ItlPushFBO(m_nFBO);
      ItlPushViewportInformation(m_nWidth, m_nHeight);

      glViewport(0,0, m_nWidth, m_nHeight);
      GLenum tDrawBuffers[1] = { GL_COLOR_ATTACHMENT0 };
      glDrawBuffers(1, tDrawBuffers);
      //glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      //glClear(GL_COLOR_BUFFER_BIT);
      glEnable(GL_STENCIL_TEST);
      glStencilFunc(GL_EQUAL, 0, 1);
      glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);


      // first, we search for all lights
      for (unsigned int a=0; a < m_vspSpotLights.size(); a++)
      {
          glClear(GL_STENCIL_BUFFER_BIT);

          std::shared_ptr<Bamboo::RN_SpotLight> spLight (m_vspSpotLights[a]);
          spLight->SetTextureLocation("color_texture", m_nAlbedoDrawBuffer);
          spLight->SetTextureLocation("normal_texture", m_nNormalDrawBuffer);
          spLight->SetTextureLocation("tangent_texture", m_nTangentDrawBuffer);
          spLight->SetTextureLocation("normalmap_texture", m_nNormalMapDrawBuffer);
          spLight->SetTextureLocation("depth_texture", m_nDepthDrawBuffer);
          spLight->SetTextureLocation("position_texture", m_nPositionDrawBuffer);
          spLight->SetTextureLocation("specular_texture", m_nSpecularDrawBuffer);
          spLight->SetTextureLocation("displace_texture", m_nDisplaceDrawBuffer);
          //spLight->SetTextureLocation("position_texture", m_nPositionDrawBuffer);

          TItlRenderInfo *pCurrentRenderInfo = m_pCurrentRenderInfo.get();

          //store old matrices
          glm::mat4 SavedModelViewProjectionMatrix = pCurrentRenderInfo->ModelViewProjectionMatrix;
          glm::mat4 SavedInverseModelViewProjectionMatrix = pCurrentRenderInfo->InverseModelViewProjectionMatrix;
          glm::mat4 SavedModelMatrix = pCurrentRenderInfo->ModelMatrix;
          glm::mat4 SavedModelViewProjectionMatrix_ForFrustumCulling = pCurrentRenderInfo->ModelViewProjectionMatrix_ForFrustumCulling;

          //update matrices by multiplying with our matrices
          pCurrentRenderInfo->ModelViewProjectionMatrix = pCurrentRenderInfo->ModelViewProjectionMatrix * m_mTransformMatrixToWorld;
          pCurrentRenderInfo->InverseModelViewProjectionMatrix = pCurrentRenderInfo->InverseModelViewProjectionMatrix * m_mTransformMatrixFromWorld;
          pCurrentRenderInfo->ModelMatrix = pCurrentRenderInfo->ModelMatrix * m_mTransformMatrixToWorld;
          pCurrentRenderInfo->ModelViewProjectionMatrix_ForFrustumCulling = pCurrentRenderInfo->ModelViewProjectionMatrix_ForFrustumCulling * m_mTransformMatrixToWorld;

          spLight->Render(this);

          //write old matrices
          pCurrentRenderInfo->ModelViewProjectionMatrix = SavedModelViewProjectionMatrix;
          pCurrentRenderInfo->InverseModelViewProjectionMatrix = SavedInverseModelViewProjectionMatrix;
          pCurrentRenderInfo->ModelMatrix = SavedModelMatrix;
          pCurrentRenderInfo->ModelViewProjectionMatrix_ForFrustumCulling = SavedModelViewProjectionMatrix_ForFrustumCulling;
      }

      glDisable(GL_STENCIL_TEST);

      GLenum tDrawBuffers2[7] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 , GL_COLOR_ATTACHMENT3, GL_COLOR_ATTACHMENT4 , GL_COLOR_ATTACHMENT5, GL_COLOR_ATTACHMENT6};
      glDrawBuffers(7, tDrawBuffers2);


      ItlPostRenderChildren();

      // ---BEGIN ---- this is just for debugging!

      static Bamboo::RN_PostEffect rPostEffectNode("directwrite");

      GLuint nTextureToShow;

     // std::cout << s_DebugDeferredTexture << std::endl;

      switch (s_DebugDeferredTexture % 6)
      {
      case 0:
          nTextureToShow = m_nAlbedoDrawBuffer;
          break;
      case 1:
          nTextureToShow = m_nDisplaceDrawBuffer;
          break;
      case 2:
          nTextureToShow = m_nSpecularDrawBuffer;
          break;
      case 3:
          nTextureToShow = m_nNormalMapDrawBuffer;
          break;
      case 4:
          nTextureToShow = m_nTangentDrawBuffer;
          break;
      case 5:
          nTextureToShow = m_nSpecularDrawBuffer;
          break;
      }

      rPostEffectNode.SetTexture("texture1", nTextureToShow );

      rPostEffectNode.Render();
    }
  else
    {
      static bool bAlreadyPrinted = false;
      if (bAlreadyPrinted == false)
        {
          Logger::debug() << "Texture id of cube map: " << m_nAlbedoDrawBuffer << Logger::endl;
          bAlreadyPrinted = true;
        }
    }



}