title: OpenGL - PBuffer render to Texture
body: <p>After my last post, when someone recommended me to use pBuffers, I digged a bit on Google and I found some cool examples to make Offscreen Rendering, using pbuffers. Some example, available on nVidia's website, does a simple offscreen rendering, which just renders on the pbuffer context, reads the pixels into an array and then calls the opengl functions to DrawPixels. </p>
  
  <p>I changed this example, in order to create a texture from the pixels read - Render it offscreen, read the pixels to the array, and then initialize a texture with this colorbit array. But this looks very redundant to me - We render the image, we copy it from Graphical Card memory into our memory (the array), to later copy it back to the graphical card in order to display it on screen, but just in a different rendering context. It looks kinda stupid the copies that I am making just to display the rendered texture, so I tried a different approach using glCopyTexImage2D(), which unfortunately doesn't work. I'll display code and explanations:</p>
  
  <pre><code>mypbuffer.Initialize(256, 256, false, false);
  </code></pre>
  
  <p>- The false values are for Sharing context and sharing object. They are false cause this fantastic graphical card doesn't support it.</p>
  
  <p>Then I perform the usual initializations, to enable Blending, and GL_TEXTURE_2D.</p>
  
  <pre><code>    CreateTexture();
      mypbuffer.Activate();
  
          int viewport[4];
          glGetIntegerv(GL_VIEWPORT,(int*)viewport);
          glViewport(0,0,xSize,ySize);
  
  
          DrawScene(hDC);
  
          //save data to texture using glCopyTexImage2D
          glBindTexture(GL_TEXTURE_2D,texture);
  
          glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
                           0,0, xSize, ySize, 0);
          glClearColor(.0f, 0.5f, 0.5f, 1.0f);                // Set The Clear Color To Medium Blue
          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
          glViewport(viewport[0],viewport[1],viewport[2],viewport[3]);
          // glBindTexture(GL_TEXTURE_2D,texture);
          first = false;
          mypbuffer.Deactivate();
  </code></pre>
  
  <p>- The DrawScene function is very simple, it just renders a triangle and a rectangle, which is suposed to be offscreen rendered (I HOPE). CreateTexture() creates an empty texture. The function should work, as it was tested in the previous way I described and it works.</p>
  
  <p>After this, in the main loop, i just do the following:</p>
  
  <pre><code>            glClear(GL_COLOR_BUFFER_BIT);
              glBindTexture(GL_TEXTURE_2D,texture);
  
              glRotatef(theta, 0.0f, 0.0f, 0.01f);
              glBegin(GL_QUADS);
              //Front Face
              glTexCoord2f(0.0f, 0.0f);
              glVertex3f(-0.5, -0.5f,  0.5f);
              glTexCoord2f(1.0f, 0.0f);
              glVertex3f( 0.5f, -0.5f,  0.5f);
              glTexCoord2f(1.0f, 1.0f);
              glVertex3f( 0.5f,  0.5f,  0.5f);
              glTexCoord2f(0.0f, 1.0f);
              glVertex3f(-0.5f,  0.5f,  0.5f);
              glEnd();
              SwapBuffers(hDC);
  
              theta = 0.10f;
              Sleep (1);
  </code></pre>
  
  <p>The final result is just a window with a blue background, nothing got actually Rendered. Any Idea why is this happening? My Graphical Card doesn't support the extension wgl_ARB_render_texture, but this shouldn't be a problem when calling the glCopyTexImage2D() right?</p>
  
  <p><strong>My Card doesn't support FBO either</strong></p>