title: OpenGL - Rendering into a Texture
body: <p>I want to be able to render something into a texture, on OpenGL, so I can further use it whenever I want, without rendering everything over again. This website <a href="http://steinsoft.net/index.php?site=Programming/Code%20Snippets/OpenGL/no9" rel="nofollow">here</a> gave me the guidelines to do it, without using the FrameBuffer. I don't want to do it with the FrameBuffer Object due to compatibility issues, since this old machine is not supporting it. I have done some code, which creates my texture, renders my scene, and I then create a Quad to render the texture on it. The only problem is that the texture is being rendered like an "Alpha Mask", it means, looks like it's only taking into account the Alpha Value, maintaining my rectangle always with the same color, but just changing the transparency on pixels. Here is some code I've done so far:</p>
  
  <pre><code>void CreateTexture ()
  {
      xSize = 512;
      ySize = 512; //size of texture
  //new array
      char* colorBits = new char[ xSize * ySize * 3 ];
  //texture creation..
      glGenTextures(1,&amp;texture);
      glBindTexture(GL_TEXTURE_2D,texture);
      glTexImage2D(GL_TEXTURE_2D,0 ,3 , xSize,
                   ySize, 0 , GL_RGB,
                   GL_UNSIGNED_BYTE, colorBits);
  //you can set other texture parameters if you want
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  //clean up
      delete[] colorBits;
  }
  </code></pre>
  
  <p>Then:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>And Finally:</p>
  
  <pre><code>glEnable(GL_TEXTURE_2D);                    // Enable 2D Texture Mapping
          glBlendFunc(GL_DST_COLOR,GL_ONE);               // Set Blending Mode
          glEnable(GL_BLEND);
  
          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);
  </code></pre>
  
  <p>The DrawScene() function simply renders a rectangle with a triangle on top of it, with each vertice having different colors.. nothing special.</p>
  
  <pre><code>glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  glClear(GL_COLOR_BUFFER_BIT );//| GL_DEPTH_BUFFER_BIT);
  
  glPushMatrix();
  //    glRotatef(theta, 0.0f, 0.0f, 1.0f);
  
  glBegin(GL_QUADS);
  glColor3f(1.0f, 0.0f, 0.0f);
  glVertex3f(-1.0f, -1.0f,  1.0f);
  glColor3f(0.0f, 1.0f, 0.0f);
  glVertex3f( 1.0f, -1.0f,  1.0f);
  glColor3f(0.0f, 0.0f, 1.0f);
  glVertex3f( 1.0f,  1.0f,  1.0f);
  glColor3f(1.0f, 1.0f, 1.0f);
  glVertex3f(-1.0f,  1.0f,  1.0f);
  glEnd();
  
  glBegin(GL_TRIANGLES);
  
  glColor3f(1.0f, 0.0f, 0.0f);
  glVertex2f(0.0f,   1.0f);
  glColor3f(0.0f, 1.0f, 0.0f);
  glVertex2f(0.87f,  -0.5f);
  glColor3f(0.0f, 0.0f, 1.0f);
  glVertex2f(-0.87f, -0.5f);
  glEnd();
  glPopMatrix();
  </code></pre>