5
votes

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 here 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:

void CreateTexture ()
{
    xSize = 512;
    ySize = 512; //size of texture
//new array
    char* colorBits = new char[ xSize * ySize * 3 ];
//texture creation..
    glGenTextures(1,&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;
}

Then:

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);

And Finally:

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);

The DrawScene() function simply renders a rectangle with a triangle on top of it, with each vertice having different colors.. nothing special.

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();
2
Please edit your text to refer to the renderable surface as a FrameBuffer object in all cases - not just framebuffer, which is misleading.Ani
You seem to be blending when rendering. Is this your intention? Have you called swapBuffers to see what actually gets rendered?Ani
Done. Thanks for the attention.filipehd

2 Answers

4
votes

I've found something on nVidia website which looks useful, for someone who cannot also do offscreen rendering with FBO:

http://developer.download.nvidia.com/SDK/9.5/Samples/samples.html

This website contains one project called "Simple P-Buffer", which basically contains an implementation of a P-buffer. The idea of the sample is that you make context switching to the pBuffer, while you want to draw pixels on offscreen mode, let's say. After drawing your scene with the normal rendering functions, we use glReadPixels to read the data from the pBuffer into an array of unsigned bytes (GLubyte). After that, we do context-switching once again, setting it back to the screen context, so that you can use glReadPixels to read the content from our array.

0
votes

The method before FBOs were available was to use an alternate render buffer (see glDrawBuffer(GL_AUX0), then copy pixels from that buffer (see glReadBuffer) to the texture (see glCopyTexImage2D. Rendering directly into a texture requires FBOs.