Off-screen Rendering (Render to Texture) using OpenGL

((Preliminaries))
1. download the most recent version of GLEW library at http://glew.sourceforge.net/
2. copy *.h files to “C:\Program Files\Microsoft Visual Studio 8\VC\include\GL”
3. copy *.lib files to “C:\Program Files\Microsoft Visual Studio 8\VC\lib”
4. copy *.dll files to “C:\WINDOWS\system32”
((Source Code))

#include <GL/glew.h>
#include <GL/glut.h>#include <iostream>
using namespace std;#pragma comment( lib, "glew32.lib"  )
#pragma comment( lib, "glew32s.lib" )
#pragma comment( lib, "glut32.lib"  )

const int WIDTH  = 2;
const int HEIGHT = 2;

const double xMax =  10;
const double xMin = -10;
const double yMax =  10;
const double yMin = -10;
const double nearDist = 0;
const double farDist  = 100;

const double camPosX = 0;
const double camPosY = 0;
const double camPosZ = 0;
const double lookAtX = 0;
const double lookAtY = 0;
const double lookAtZ = -1;
const double upX     = 0;
const double upY     = 1;
const double upZ     = 0;

void main() {

//////////////////////
// Create a window. //
//////////////////////

//glutInit( &argc, argv );
glutCreateWindow( "RTT window" );
//glutInitWindowSize( WIDTH, HEIGHT );
glewInit();

//////////////////////////////////
// Check th framebuffer status. //
//////////////////////////////////

GLenum status = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );

switch( status ) {

case GL_FRAMEBUFFER_COMPLETE_EXT:
cout<<"GL_FRAMEBUFFER_COMPLETE_EXT!: SUCCESS"<<endl<<endl;
break;

case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
cout<<"GL_FRAMEBUFFER_UNSUPPORTED_EXT!: ERROR"<<endl;
exit(0);
break;

default:
exit(0);

}

// ID numbers for buffers.
GLuint fbo; // for 'frame buffer object'
GLuint rbo; // for 'render buffer object'

// ID numbers for textures.
GLuint renderTarget;

// Create buffers.
glGenFramebuffersEXT(  1, &fbo );
glGenRenderbuffersEXT( 1, &rbo );

// Create textures.
glGenTextures( 1, &renderTarget );
glBindTexture( GL_TEXTURE_2D, renderTarget );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );

// Set the render target.
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo );
glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, rbo );
glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, WIDTH, HEIGHT );

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );

glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, rbo, 0 );
glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, rbo );

///////////////////
// Render scene. //
///////////////////

// Set the render states.
glEnable( GL_DEPTH_TEST );
glDepthMask( 1 );
glDepthFunc( GL_LEQUAL );

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

glEnable(GL_LINE_SMOOTH);
glLineWidth(1.0);

// Draw on the buffer.
glClearColor( 0.f, 0.f, 0.f, 1.f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glViewport( 0, 0, WIDTH, HEIGHT );  // Viewport transformation

glMatrixMode( GL_PROJECTION );      // Projection transformation
glLoadIdentity();
glOrtho( xMin, xMax, yMin, yMax, nearDist, farDist );

glMatrixMode( GL_MODELVIEW );       // Viewing transformation
glLoadIdentity();
gluLookAt( camPosX, camPosY, camPosZ, lookAtX, lookAtY, lookAtZ, upX, upY, upZ );

glColor3f(1,1,0); // yellow
glTranslatef( 0, 0, -60 );
glBegin( GL_QUADS );
glVertex3f( xMin, yMin, 0 );
glVertex3f( xMax, yMin, 0 );
glVertex3f( xMax, yMax, 0 );
glVertex3f( xMin, yMax, 0 );
glEnd();

///////////////////////
// Read from buffer. //
///////////////////////

unsigned char *color = new unsigned char[WIDTH*HEIGHT*4];
float         *depth = new float[WIDTH*HEIGHT];

glReadBuffer( GL_COLOR_ATTACHMENT0_EXT );
glReadPixels( 0, 0, WIDTH, HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, color );

cout<<"Color values from color buffer:"<<endl;
cout<<"("<<(int)color[8]<<","<<(int)color[9]<<","<<(int)color[10]<<","<<(int)color[11]<<") ("
<<(int)color[12]<<","<<(int)color[13]<<","<<(int)color[14]<<","<<(int)color[15]<<")"<<endl;
cout<<"("<<(int)color[0]<<","<<(int)color[1]<<","<<(int)color[2]<<","<<(int)color[3]<<") ("
<<(int)color[4]<<","<<(int)color[5]<<","<<(int)color[6]<<","<<(int)color[7]<<")"<<endl<<endl;

glReadBuffer( GL_DEPTH_ATTACHMENT_EXT );
glReadPixels( 0, 0, WIDTH, HEIGHT, GL_DEPTH_COMPONENT, GL_FLOAT, depth );

cout<<"Depth values from z-buffer:"<<endl;
cout<<depth[2]<<" "<<depth[3]<<endl;
cout<<depth[0]<<" "<<depth[1]<<endl;

delete[] color;
delete[] depth;

/////////////////////
// Release mmemory //
/////////////////////

// Delete textures;
glDeleteTextures( 1, &renderTarget );

// Delete buffers.
glDeleteFramebuffersEXT(  1, &fbo );
glDeleteRenderbuffersEXT( 1, &rbo );

// Return to rendering to the main framebuffer instead of 'fbo'.
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );

}

 

((Result))

GL_FRAMEBUFFER_COMPLETE_EXT!: SUCCESS

Color values from color buffer:
(255,255,0,255) (255,255,0,255)
(255,255,0,255) (255,255,0,255)

Depth values from z-buffer:
0.6 0.6
0.6 0.6

답글 남기기