Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

FBO_Filter.h

Go to the documentation of this file.
00001 
00019 #ifndef __FBO_FILTER_H
00020 #define __FBO_FILTER_H
00021 
00022 #include <GL/gl.h>
00023 #include <GL/glext.h>
00024 #include <GL/glu.h>
00025 #include <GL/glut.h>
00026 #include <stdio.h>
00027 #include <assert.h>
00028 #include <Cg/cgGL.h>
00029 
00030 #include <openvidia/errutil.h>
00031 
00036 class FBO_Filter {
00037 
00038 private :
00039     CGprogram cgProgram;
00040     CGprofile cgProfile;
00041     CGcontext cgContext;
00042 
00043     GLuint fb, oTex;
00044     int tWidth; int tHeight;
00045     int previousViewportDims[4] ;
00046     float depth;
00047 
00048     CGprogram load_cgprogram(CGprofile prof, char *name) 
00049     {
00050         fprintf(stderr, "loading %s\n", name);
00051         return cgCreateProgramFromFile( cgContext, CG_SOURCE,
00052                                     name, prof, "FragmentProgram", 0);
00053     }
00054 
00055     void renderBegin() 
00056     {
00057       //since we might be operating on downsampled images, we need 
00058       // to reshape our current drawing area. Record the previous
00059       // settings first though. 
00060    
00061       glGetIntegerv(GL_VIEWPORT, previousViewportDims );
00062 
00063       glViewport(0, 0, (GLsizei) tWidth, (GLsizei) tHeight );
00064       glMatrixMode(GL_PROJECTION);
00065       glPushMatrix();
00066       glLoadIdentity();
00067 
00068       float scale = 1.0;
00069       glFrustum(0.0, 1.0/scale,  0.0/scale, 1.0/scale,   1.0/scale,   100.0);
00070       gluLookAt(0.0,0.0,0.0,  0.0, 0.0,  depth,   0.0,   1.0, 0.0);
00071       glMatrixMode(GL_MODELVIEW);
00072       glPushMatrix();
00073       glLoadIdentity();
00074     }
00075 
00076     //return the rendering state to what it was before.
00077     void renderEnd() 
00078     {
00079       glViewport( previousViewportDims[0], previousViewportDims[1],
00080                   previousViewportDims[2], previousViewportDims[3] );
00081       glMatrixMode(GL_PROJECTION);
00082       glPopMatrix();
00083       glMatrixMode(GL_MODELVIEW);
00084       glPopMatrix();
00085     }
00086 
00087     void drawQuadTex()
00088     {
00089       glBegin(GL_QUADS);
00090 
00091        glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0f, (float)tHeight);
00092        glVertex3f(0.0, 0.0, depth);
00093 
00094         glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0f, 0);
00095         glVertex3f(0.0, 1.0, depth);
00096 
00097         glMultiTexCoord2fARB(GL_TEXTURE0_ARB, (float)tWidth , 0);
00098         glVertex3f(1.0, 1.0, depth);
00099 
00100         glMultiTexCoord2fARB(GL_TEXTURE0_ARB, (float)tWidth , (float)tHeight);
00101         glVertex3f(1.0, 0.0, depth);
00102     
00103       glEnd();
00104     }
00105 
00106     void drawQuadFBT()
00107     {
00108       glBegin(GL_QUADS);
00109 
00110         glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0f, 0);
00111         glVertex3f(0.0, 0.0, depth);
00112     
00113         glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0f, (float)tHeight);
00114         glVertex3f(0.0, 1.0, depth);
00115 
00116         glMultiTexCoord2fARB(GL_TEXTURE0_ARB, (float)tWidth , (float)tHeight);
00117         glVertex3f(1.0, 1.0, depth);
00118 
00119        glMultiTexCoord2fARB(GL_TEXTURE0_ARB, (float)tWidth , 0);
00120        glVertex3f(1.0, 0.0, depth);
00121 
00122       glEnd();
00123     }
00124 
00125 
00126 public :
00127     
00128     FBO_Filter( CGprofile cgp, 
00129                 char *name, 
00130 
00131 
00132                 GLuint outputTex, 
00133 
00134                  int W, 
00135                  int H )  
00136     {
00137         depth = -1.0;
00138         //Load the Cg Program.
00139         cgProfile = cgp;
00140         cgContext = cgCreateContext();
00141         cgProgram  = load_cgprogram(cgProfile, name );
00142         cgGLLoadProgram( cgProgram );
00143 
00144         tWidth = W; tHeight = H;
00145 
00146         //create the framebuffer object.
00147         oTex = outputTex ;
00148         glGenFramebuffersEXT( 1, &fb );
00149         glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fb );
00150         glBindTexture(GL_TEXTURE_RECTANGLE_NV, oTex );
00151         glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
00152               GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_NV, oTex, 0);
00153         CHECK_FRAMEBUFFER_STATUS()
00154         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
00155     }
00156 
00162 
00215     GLuint apply( GLuint iTex,    
00216                   bool FBOtex  )  
00217 
00218     {
00219         //XXX all this frambuffer binding is suboptimal, but will work for now.
00220         glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fb );
00221         glBindTexture(GL_TEXTURE_RECTANGLE_NV, iTex );
00222         glEnable(GL_TEXTURE_RECTANGLE_NV);
00223 
00224         renderBegin();
00225         cgGLEnableProfile(cgProfile);
00226         cgGLBindProgram(cgProgram);
00227         ( FBOtex ? drawQuadFBT() : drawQuadTex() );
00228         cgGLDisableProfile(cgProfile);
00229         renderEnd();
00230 
00231         glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0);
00232   
00233         return oTex ;
00234     }
00235 };
00236 
00237 #endif

Generated on Mon Jun 27 14:54:29 2005 for OPENVIDIA by  doxygen 1.4.0