unsigned short mDisplayHeight = 1000;
unsigned short mDisplayWidth = 1600;
int mVideoFlags;
// Init SDL
SDL_Init(SDL_INIT_VIDEO);
mVideoFlags = SDL_OPENGL; // Use OpenGL in SDL
mVideoFlags |= SDL_HWPALETTE; // Hardware enabled palette
const SDL_VideoInfo* sdlVideoInfo = SDL_GetVideoInfo();
if (sdlVideoInfo->hw_available)
mVideoFlags |= SDL_HWSURFACE; // Hardware enabled surfaces
else
mVideoFlags |= SDL_SWSURFACE; // Software enabled surfaces
if (sdlVideoInfo->blit_hw)
mVideoFlags |= SDL_HWACCEL; // Hardware enabled blitting
SDL_SetVideoMode(mDisplayWidth, mDisplayHeight, 32, mVideoFlags);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
// Initialize OpenGL
glewInit();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
// Create the frame buffers and textures
unsigned int mFrameBufferTexIDs[2];
unsigned int mFrameBufferObjectIDs[2];
glGenFramebuffers(1, &mFrameBufferObjectIDs[0]);
glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObjectIDs[0]);
glGenTextures(1, &mFrameBufferTexIDs[0]);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mFrameBufferTexIDs[0]);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, mDisplayWidth, mDisplayHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, mFrameBufferTexIDs[0], 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
unsigned short intermediateTexWidth = 512;
unsigned short intermediateTexHeight = 512;
glGenFramebuffers(1, &mFrameBufferObjectIDs[1]);
glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObjectIDs[1]);
glGenTextures(1, &mFrameBufferTexIDs[1]);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mFrameBufferTexIDs[1]);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, intermediateTexWidth, intermediateTexHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, mFrameBufferTexIDs[1], 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, mDisplayWidth, mDisplayHeight, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
// Framebuffer and texture arrays are as follows:
// 0 : Final output (what I want to present to the screen)
// 1 : Intermediate output (what I want to render to and then render THAT to the screen)
while(true)
{
glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObjectIDs[1]);
glLoadIdentity();
glTranslated(0, mDisplayHeight-intermediateTexHeight, 0);
// Draw a quad with no texture
glDisable(GL_TEXTURE_RECTANGLE_ARB);
glColor3f(1.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
glVertex2i(10, 10);
glVertex2i(50, 10);
glVertex2i(50, 50);
glVertex2i(10, 50);
glEnd();
glEnable(GL_TEXTURE_RECTANGLE_ARB);
// Draw the intermediate FBO onto the final FBO
glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObjectIDs[0]);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mFrameBufferTexIDs[1]);
glBegin(GL_QUADS);
glTexCoord2i(0, 0);
glVertex2i(0, 0);
glTexCoord2i(intermediateTexWidth, 0);
glVertex2i(intermediateTexWidth, 0);
glTexCoord2i(intermediateTexWidth, intermediateTexHeight);
glVertex2i(intermediateTexWidth, intermediateTexHeight);
glTexCoord2i(0, intermediateTexHeight);
glVertex2i(0, intermediateTexHeight);
glEnd();
// Present the final information to the screen
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glLoadIdentity();
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mFrameBufferTexIDs[0]);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2i(0, 0);
glVertex2i(0, 0);
glTexCoord2i(mDisplayWidth, 0);
glVertex2i(mDisplayWidth, 0);
glTexCoord2i(mDisplayWidth, mDisplayHeight);
glVertex2i(mDisplayWidth, mDisplayHeight);
glTexCoord2i(0, mDisplayHeight);
glVertex2i(0, mDisplayHeight);
glEnd();
SDL_GL_SwapBuffers();
}