title: Opengl : Render To Cubemap?
body: <p>I am trying to render 3d scene to a cubemap but the cubemap only renders the skybox, here is my RenderToCubemap function:</p>
  <pre><code>GLuint RenderToCubemap(glm::vec3 position, float resolution, float nearPlane, float farPlane)
  {
      unsigned int CM_FBO;
      unsigned int textureID;
      glGenFramebuffers(1, &amp;CM_FBO);
      glGenTextures(1, &amp;textureID);
      glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
  
      for (unsigned int i = 0; i &lt; 6; i++)
          glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, resolution, resolution, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  
      glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  
      // attach depth texture as FBO's depth buffer
      glBindFramebuffer(GL_FRAMEBUFFER, CM_FBO);
      glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureID, 0);
  
      if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
          std::cout &lt;&lt; &quot;ERROR::FRAMEBUFFER:: Framebuffer is not complete!&quot; &lt;&lt; std::endl;
      glBindFramebuffer(GL_FRAMEBUFFER, 0);
  
      glm::mat4 shadowProj = glm::perspective(glm::radians(90.0f), (float)resolution / (float)resolution, nearPlane, farPlane);
      std::vector&lt;glm::mat4&gt; shadowTransforms;
      shadowTransforms.push_back(shadowProj * glm::lookAt(position, position + glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
      shadowTransforms.push_back(shadowProj * glm::lookAt(position, position + glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
      shadowTransforms.push_back(shadowProj * glm::lookAt(position, position + glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)));
      shadowTransforms.push_back(shadowProj * glm::lookAt(position, position + glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f)));
      shadowTransforms.push_back(shadowProj * glm::lookAt(position, position + glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
      shadowTransforms.push_back(shadowProj * glm::lookAt(position, position + glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
  
      // Render scene to cubemap
      // --------------------------------
      glViewport(0, 0, (int)resolution, (int)resolution);
      glBindFramebuffer(GL_FRAMEBUFFER, CM_FBO);
  
      Camera capture_cam;
      capture_cam.temp_cam = true;
      capture_cam.projectionMatrix = shadowProj;
      capture_cam.transform.Position = position;
      for (size_t i = 0; i &lt; 6; i++)
      {
          capture_cam.viewMatrix = shadowTransforms[i];
  
          glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, textureID, 0);
          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
          RenderScene(capture_cam);
      }
  
      return textureID;
  }
  </code></pre>
  <p>RenderScene function :</p>
  <pre><code>void RenderScene(Camera&amp; cam)
  {
      glm::mat4 proj = cam.GetProjectionMatrix();
      glm::mat4 view = cam.GetViewMatrix();
      glm::vec3 viewPos = cam.transform.Position;
  
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
          Shader* shader = &amp;materials.main_shader;
  
          shader -&gt;use();
          shader -&gt;SetMat4(&quot;projection&quot;, proj);
          shader -&gt;SetMat4(&quot;view&quot;, view);
          shader -&gt;SetVec3(&quot;CamPos&quot;, viewPos);
  
          // Render all meshs
          // ----------------------------------------------------
          for (size_t i = 0; i &lt; Meshs.size(); i++)
          {
              if (Meshs[i].Material == nullptr)
                  continue;
  
              Meshs[i].Material-&gt;setShaderProperties(shader);
  
              shader-&gt;SetMat4(&quot;model&quot;, Meshs[i].Transform);
              Meshs[i]-&gt;Draw();
         }    
      
      // render skybox 
      // ----------------------------------------------------
      glDepthFunc(GL_LEQUAL);  // change depth function so depth test passes when values are equal to depth buffer's content
      Shader* bg = &amp;materials.background;
      bg-&gt;use();
      bg-&gt;SetMat4(&quot;view&quot;, view);
      bg-&gt;SetMat4(&quot;projection&quot;, proj);
      glActiveTexture(GL_TEXTURE0);
      glBindTexture(GL_TEXTURE_CUBE_MAP, envCubemap); 
      renderCube();
      glDepthFunc(GL_LESS); // set depth function back to default
  }
  </code></pre>
  <p>I am using RenderScene for main rendering and it works, but when i use it in renderToCubemap it shows only the skybox.</p>
  <p>Any help or correction?</p>