arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

rtt.cpp

Go to the documentation of this file.
00001 // 6/14/02      Aaron Lefohn    Scientific Computing and Imaging Institute      
00002 // School of Computing          University of Utah
00003 //
00004 // This is a simple app to test render-to-texture
00005 // - This version developed on ATI Radeon 8500
00006 // - This program has been converted to OOGL objects.
00007 
00008 #include <iostream.h>
00009 #include <GL/glui.h>
00010 
00011 #include <glew.h>
00012 
00013 #include <vec2.h>
00014 #include <vec3.h>
00015 #include <pbuffGlift.h>
00016 #include <texNd.h>
00017 #include <multiTex.h>
00018 #include <shader.h>
00019 #include <pixelShaderATI8K.h>
00020 #include <texCoordPerturb.h>
00021 #include <planarQuad.h>
00022 #include <shadedPrim.h>
00023 #include <renderPass.h>
00024 #include <texDataNd.h>
00025 
00026 using namespace gutz;
00027 using namespace glift;
00028 
00029 GLUI*            g_glui=NULL;
00030 RenderPass*      g_pass1 = NULL;
00031 RenderPass*      g_pass2 = NULL;
00032 SingleTex*       g_greyTex = NULL;
00033 PBuffer*         g_pbuff = NULL;
00034 DrawableGLI* g_prim = NULL;
00035 vec2i            g_dimen(0);
00036 
00037 class RenderState2D : public GenState
00038 {
00039 protected:
00040         virtual void bindDef()
00041         {
00042                 glerr("drawQuad","1");
00043                 glDisable(GL_DEPTH_TEST);
00044                 glMatrixMode(GL_PROJECTION);
00045                 glPushMatrix();
00046                         glLoadIdentity();
00047                         gluOrtho2D(0.0, (GLdouble)g_dimen.w, 0.0, (GLdouble)g_dimen.h);
00048                         //glOrtho(0.0, (GLdouble)g_dimen.w, 0.0, (GLdouble)g_dimen.h, 1.0, -1.0 );
00049                         glMatrixMode(GL_MODELVIEW);
00050                         glPushMatrix();
00051                         glLoadIdentity();
00052         }
00053 
00054         virtual void releaseDef()
00055         {
00056                         glPopMatrix();
00057                         glMatrixMode(GL_PROJECTION);
00058                 glPopMatrix();
00059                 glMatrixMode(GL_MODELVIEW);
00060                 glEnable(GL_DEPTH_TEST);
00061                 glerr("RenderState2D","release()");
00062         }
00063 };
00064 
00065 class GlutSphere : public DrawableGLI
00066 {
00067 protected:
00068         virtual void drawDef() { glutSolidSphere (1.0, 30, 20); }
00069 };
00070 
00071 void multTwoShaderATI()
00072 {
00073 #ifdef GLIFT_ATI
00074         glSampleMapATI( GL_REG_0_ATI, GL_TEXTURE0_ARB, GL_SWIZZLE_STR_ATI ); 
00075 
00076         glColorFragmentOp2ATI( GL_ADD_ATI, GL_REG_0_ATI, GL_NONE, GL_HALF_BIT_ATI,      
00077                                                    GL_REG_0_ATI, GL_NONE, GL_NONE,
00078                                                    GL_REG_0_ATI, GL_NONE, GL_NONE );
00079 
00080 #endif
00081 }
00082 
00083 void diffShaderATI()
00084 {
00085 #ifdef GLIFT_ATI
00086         glSampleMapATI( GL_REG_0_ATI, GL_TEXTURE0_ARB, GL_SWIZZLE_STR_ATI );
00087         glSampleMapATI( GL_REG_1_ATI, GL_TEXTURE1_ARB, GL_SWIZZLE_STR_ATI );
00088 
00089         glColorFragmentOp2ATI( GL_SUB_ATI, GL_REG_0_ATI, GL_NONE, GL_2X_BIT_ATI | GL_SATURATE_BIT_ATI,
00090                                                    GL_REG_0_ATI, GL_NONE, GL_NONE,
00091                                                    GL_REG_1_ATI, GL_NONE, GL_NONE );
00092 
00093 /*      glColorFragmentOp1ATI( GL_MOV_ATI, GL_REG_0_ATI, GL_NONE, GL_NONE,
00094                                                    GL_REG_1_ATI, GL_NONE, GL_NONE );
00095 */
00096 #endif
00097 }
00098 
00099 // This is a testing texture used to generate various grayscale images
00100 SingleTex* setupGrayScaleGradTex( PBuffer* pbuff )
00101 {
00102         int numElts = 256;
00103         arrayo1ub image(numElts*numElts);
00104 
00105         unsigned long index = 0;
00106         
00107         GLubyte val = 0;
00108         for(int y=0; y < numElts; y++) {
00109                 for(int x=0; x < numElts; x++) {
00110                         image[index] = val;
00111                         index++;
00112                 }
00113                 if( y%10==0 ) {val += 50;}
00114         }
00115 
00116         // Setup the texture object state
00117         MultiTexOState texState;                                
00118         texState << TS_S_CLAMP << TS_T_CLAMP << TS_NEAREST;
00119 
00120         // Setup the texture unit state (default to texUnit 0)
00121         TS_REPLACE->bind(GL_TEXTURE_2D);
00122 
00123         // Setup the data wrapper
00124         vec2i texDimen(numElts, numElts);
00125         TexDataArr1<uchar> texData( GL_LUMINANCE, GL_UNSIGNED_BYTE, texDimen, image );
00126         
00127         // Setup the texture
00128         SingleTex* greyTex = new Tex2D( texState, &texData, pbuff );
00129 
00130 
00131         return greyTex;
00132 }
00133 
00134 
00135 void pbuffInit()
00136 {
00137         glMatrixMode (GL_PROJECTION);
00138         glLoadIdentity();
00139         //glOrtho( 0, g_dimen.w, 0, g_dimen.h, +1, -10);
00140     gluPerspective( 90.0, 1.0, 1.0, -10.0);
00141     gluLookAt( 0, 0, 3, 0, 0, 0, 0, 1, 0 );
00142 
00143         glMatrixMode(GL_MODELVIEW);
00144         glLoadIdentity();
00145 
00146         glClearColor (1.0, 0.0, 0.0, 0.0); // If we see red background, it is the PBuffer
00147         glShadeModel (GL_SMOOTH);
00148         glEnable(GL_DEPTH_TEST);
00149 
00150         GLfloat mat_specular[] = { 1.0, 0.5, 0.5, 1.0 };
00151         GLfloat light_position[] = { 0.0, 0.0, +5, 1.0 };
00152         glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_specular);
00153         glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
00154         glMaterialf(GL_FRONT, GL_SHININESS, 25.0);
00155         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00156         glEnable(GL_LIGHTING);
00157         glEnable(GL_LIGHT0);
00158 
00159         //glColorMaterial(GL_FRONT, GL_DIFFUSE);
00160         //glColor4fv(mat_specular);
00161         //glEnable(GL_COLOR_MATERIAL);
00162 
00163         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00164         glEnableClientState(GL_VERTEX_ARRAY);
00165 }
00166 
00167 void init(void) 
00168 {  
00169         glew();
00170   
00171         glClearColor (0.0, 0.0, 1.0, 0.0); //If we see blue background, it is the ColorBuffer
00172 
00173         // Create the pbuffer
00174         g_pbuff = new PBuffer( g_dimen, GLUT_SINGLE | GLUT_RGBA, pbuffInit, true, true );
00175 
00176         // Create the Shader for pass that draws 'greyTex' to pbuffer
00177         PixelShader*    multTwoShader   = NULL;
00178         #ifdef GLIFT_ATI
00179                 multTwoShader = new PixelShaderATI8K( multTwoShaderATI, g_pbuff );
00180         #endif
00181                                         g_greyTex               = setupGrayScaleGradTex( NULL );
00182         RenderState2D*  render2D                = new RenderState2D();
00183         Shader*                 greyTexShader   = new Shader( g_greyTex, new ProgShader(1, multTwoShader), render2D );
00184         //Shader*                       greyTexShader   = new Shader( greyTex, attribs );
00185         //greyTexShader->compile();
00186 
00187         // Create Shader for pass that draws contents of Pbuffer to color buffer
00188         MultiTexOState texState;
00189         texState << TS_S_CLAMP << TS_R_CLAMP << TS_NEAREST;
00190         SingleTex* pbuffTex                 = new Tex2D( texState, g_pbuff);
00191         Shader*    drawPbuffShader  = new Shader( pbuffTex, render2D );
00192 
00193         // Create Shader for derivative shader
00194         TexTrans texShift0( 0, vec2f(-1.0, +1.0), true );
00195         TexTrans texShift1( 1, vec2f(+1.0, -1.0), true );
00196         VecTexTrans               vecTexShift;
00197         vecTexShift.push_back( texShift0 );
00198         vecTexShift.push_back( texShift1 );
00199 
00200         TexCoordPerturb*  texPerturb   = new TexCoordPerturb( 2, vecTexShift );
00201         
00202         PixelShader*   diffShader   = NULL;
00203         #ifdef GLIFT_ATI
00204                 diffShader = new PixelShaderATI8K( diffShaderATI, g_pbuff );
00205         #endif
00206         ProgShader* progShader   = new ProgShader( 2, diffShader, texPerturb );
00207         MultiTex*       greyTextures = new MultiTex( VecTexP(2, g_greyTex) );
00208         Shader*         derivShader  = new Shader( greyTextures, progShader, render2D );
00209 
00210         Shader*  drawTexShader = new Shader( g_greyTex, render2D );
00211 
00212         // Create the primitives
00213         PlanarQuad* quad           = new PlanarQuad( vec2f(0.0f), vec2f(g_dimen.w, g_dimen.h), -1.0f, true );
00214         GlutSphere* glutSphere = new GlutSphere();
00215         g_prim = glutSphere;  
00216  
00217         // Create the shadedPrims
00218         ShadedPrim* shadedPrimGreyTex = new ShadedPrim( quad, greyTexShader );// The shaded prim for pass 1
00219         ShadedPrim* shadedPrimDeriv       = new ShadedPrim( quad, derivShader );
00220         ShadedPrim* shadedPrimPbuff   = new ShadedPrim( quad, drawPbuffShader );// The shaded prim for pass 2
00221         ShadedPrim* shadedPrimTex         = new ShadedPrim( quad, drawTexShader );
00222  
00223         // Create the render passes
00224         g_pass1 = new RenderPass( shadedPrimDeriv, g_pbuff, true );//glutSphere, g_pbuff, true );//new RenderPass( shadedPrimTex );//
00225         g_pass2 = new RenderPass( shadedPrimPbuff );//  shadedPrimPbuff ); //shadedPrimDeriv );//
00226   
00227         //g_pass1->compile();  
00228         //g_pass2->compile();  
00229 
00230         glerr("init()","");
00231 }
00232 
00233 void display(void)
00234 {
00235         glerr("display","1");
00236         glClear(GL_COLOR_BUFFER_BIT);
00237 
00238         g_pass1->render();
00239         g_pass2->render();
00240 
00241         glFlush();
00242         //glutSwapBuffers();
00243 }
00244 
00245 void reshape (int w, int h)
00246 {
00247    g_dimen = vec2i(w,h);
00248 
00249    glerr("reshape","1");
00250    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
00251    glMatrixMode (GL_PROJECTION);
00252    glLoadIdentity();
00253    //glOrtho( 0, w, 0, h, -1, -10);
00254    gluPerspective( 90.0, 1.0, 1, -10.0);
00255    gluLookAt( 0, 0, 3, 0, 0, -5, 0, 1, 0 );
00256    
00257    glErr("reshape","2");    
00258    glMatrixMode(GL_MODELVIEW);
00259    glLoadIdentity();
00260    glErr("reshape","3");
00261 }
00262 
00263 void setupGluiStuff( GLint& mainWindow )
00264 {       
00265         // Create GLUI control window
00266         g_glui = GLUI_Master.create_glui( "GUI", 0, 400, 50 );
00267 
00268         //Quit Button
00269         g_glui->add_button( "Quit", 0,(GLUI_Update_CB) exit);
00270         
00271         // which is the main gfx window!
00272         g_glui->set_main_gfx_window(mainWindow);
00273 }
00274 
00275 int main(int argc, char** argv)
00276 {
00277         g_dimen = vec2i(256, 256);
00278         glutInit(&argc, argv);
00279         glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA);
00280         glutInitWindowSize (g_dimen.w, g_dimen.h); 
00281         glutInitWindowPosition (100, 100);
00282 
00283         GLint mainWindow = glutCreateWindow("Texture This!");
00284         GLUI_Master.set_glutIdleFunc( NULL ); 
00285                    
00286         init ();
00287         glutDisplayFunc(display); 
00288         glutReshapeFunc(reshape);
00289 
00290         setupGluiStuff( mainWindow );
00291 
00292         glErr("main","main");
00293         glutMainLoop();
00294         return 0;
00295 }

Send questions, comments, and bug reports to:
jmk