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

gliftEx1.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>
00009 using namespace std;
00010 
00011 #include <GL/glut.h>
00012 
00013 #include <vec2.h>
00014 #include <texNd.h>
00015 #include <texDataNd.h>
00016 #include <shader.h>
00017 #include <planarQuad.h>
00018 #include <shadedPrim.h>
00019 #include <multiPrim.h>
00020 #include <renderPass.h>
00021 #include <glew.h>
00022 
00023 using namespace gutz;
00024 using namespace glift;
00025 
00026 // Forward Decls
00027 class Blend;
00028 
00029 // Globals
00030 Blend*       g_blend = NULL;
00031 SingleTex*   g_greyTex = NULL;
00032 RenderPass*  g_pass1 = NULL; // The object that will draw EVERYTHING
00033 vec2i            g_winDimen(0);  // The x,y size of your window
00034 
00035 GLubyte* genGrayImageRGB( int numElts )
00036 {
00037   GLubyte* image = new GLubyte[numElts*numElts*3];
00038   
00039   unsigned long index = 0;
00040   
00041   GLubyte val = 0;
00042   for(int y=0; y < numElts; y++) {
00043     for(int x=0; x < numElts; x++) {
00044       for(int c=0; c < 3; c++) {
00045         image[index] = val;
00046         index++;
00047       }
00048     }
00049     if( y%1==0 ) {val += 35;}
00050   }
00051 
00052   return image;
00053 }
00054 
00055 // This is a testing texture setup.
00056 // - The only part of this code you will need is marked below
00057 SingleTex* setupGrayScaleGradTex( int numElts )
00058 {
00059   GLubyte* image = genGrayImageRGB( numElts );
00060   
00061   // You'll use these 2 lines, but 'image' is your framebuffer from RTRT
00062   // or any other image.
00063   // - WARNING: The 'GL_INT' must match the format of the 'image'
00064   //            (GL_INT, GL_FLOAT, GL_UNSIGNED_BYTE, etc).
00065   //            The image MUST by 'texDimen.w' x 'texDimen.h' x 3!!
00066   // - If you are using this for RGBA textures, change 'GL_RGB' to 'GL_RGBA'
00067   //   and make sure 'image' has 4 components instead of 3.
00068   MultiTexOState texState;
00069   texState << TS_S_CLAMP << TS_T_CLAMP << TS_LINEAR;
00070   TS_REPLACE->bind(GL_TEXTURE_2D);
00071 
00072   vec2i texDimen(numElts);
00073   TexDataArr1<ubyte> texData( GL_RGB, GL_UNSIGNED_BYTE, texDimen, arrayw1ub( numElts*numElts*3, image ) );
00074 
00075   SingleTex* greyTex = new Tex2D( texState, &texData );
00076   
00077   delete [] image;
00078   
00079   return greyTex;
00080 }
00081 
00082 class Color4f : public GenState
00083 {
00084 public:
00085   Color4f() {}
00086   Color4f( const vec4f& color ) {m_color = color;}
00087   void color( const vec4f& inColor) {m_color = inColor;}
00088 
00089 protected:
00090   virtual void bindDef() {
00091     glColor4fv(m_color.v());
00092   }
00093   virtual void releaseDef() {}
00094 
00095 private:
00096   vec4f m_color;
00097 };
00098 
00099 class Blend : public GenState
00100 {
00101 public:
00102   Blend() {}
00103   Blend( const vec4f& color ) {m_color = color;}
00104   void alpha( float alpha ) {m_color[3] = alpha;}
00105   
00106 protected:
00107   virtual void bindDef() {
00108     glEnable(GL_BLEND);
00109     glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00110     glColor4fv( m_color.v() );
00111   }
00112   virtual void releaseDef() {
00113     glDisable(GL_BLEND);
00114   }
00115 
00116 private:
00117   vec4f m_color;
00118 };
00119 
00120 //Blend* g_blend=NULL;
00121 
00122 void init(void) 
00123 {
00124   glew();
00125 
00126   vec2f winSize( g_winDimen.w, g_winDimen.h );     
00127     
00128   glClearColor (0.0, 0.0, 1.0, 0.0); //If we see blue background, it is the ColorBuffer
00129   
00130   // Create a large, black quad and set render state to ortho2D
00131   Color4f* red = new Color4f( vec4f(1.0, 0.0, 0.0, 0.0) );
00132   Shader*  redShader = new Shader(red);  
00133   PlanarQuad* windowSizedQuad = new PlanarQuad( vec2f(0.0f), winSize ); 
00134   ShadedPrim* bigRedQuad = new ShadedPrim(windowSizedQuad, redShader);
00135   //bigRedQuad->compile(); //Make into display list
00136   
00137   // Create the Shader to draw 'greyTex' to a quad in ortho2D mode
00138   // - Note: 'greyTex' will become any texture
00139   //         You can call 'greyTex->reset(GL_INT, image)'
00140   //         to pass in the new framebuffer from RTRT
00141           g_blend       = new Blend( vec4f(1.0, 1.0, 1.0, 0.5) );
00142                   g_greyTex     = setupGrayScaleGradTex(32); // The texture
00143   Shader* greyTexShader = new Shader( g_greyTex, g_blend );   // Turn it into a shader
00144   
00145   // Create a z-axis aligned, planar Quad with texture coordinates
00146   vec2f lowerLeft(  50, 50 );
00147   vec2f upperRight(200, 200);
00148   float z = 0.5;  
00149   bool  genTexCoords = true;
00150   PlanarQuad* smallQuad = new PlanarQuad( lowerLeft, upperRight, z, genTexCoords );
00151   //smallQuad->compile();
00152 
00153   ShadedPrim* smallTexturedQuad = new ShadedPrim( smallQuad, greyTexShader );
00154   
00155   // Create render pass...da' whole enchilada
00156   VecDrawP prims;
00157   prims.push_back( bigRedQuad );
00158   prims.push_back( smallTexturedQuad );
00159   
00160   g_pass1 = new RenderPass( prims );
00161   
00162   glerr("init()");
00163 }
00164 
00165 void display(void)
00166 {
00167   static float alpha=0.0;
00168   
00169   glerr("display","1");
00170   glClear(GL_COLOR_BUFFER_BIT);
00171 
00172   // Draw entire scene
00173   g_pass1->render();
00174   glutSwapBuffers();
00175 
00176   //Update alpha
00177   alpha += 0.025f;
00178   alpha -= int(alpha);
00179   g_blend->alpha( alpha );
00180 }
00181 
00182 void reshape (int w, int h)
00183 {
00184    g_winDimen = vec2i(w,h);
00185    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
00186    
00187    glerr("reshape","1");
00188    glMatrixMode (GL_PROJECTION);
00189    glLoadIdentity();
00190    gluOrtho2D( 0, w, 0, h );
00191    
00192    glerr("reshape","2");    
00193    glMatrixMode(GL_MODELVIEW);
00194    glLoadIdentity();
00195    //gluLookAt( 0, 0, 1, 0, 0, 0, 0, 1, 0 );
00196    glerr("reshape","3");
00197 
00198    static int count=0;
00199    int numElts = count%2==0 ? 256 : 32;   
00200    GLubyte* image = genGrayImageRGB(numElts);
00201    TexData texData( GL_TEXTURE_2D, vec3i(), vec3i(numElts), 0,
00202                                     GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, (void*)image);
00203    g_greyTex->setTexData(&texData);
00204    delete [] image;
00205    count++;
00206 }
00207 
00208 int main(int argc, char** argv)
00209 {
00210   g_winDimen = vec2i(256, 256);
00211 
00212   glutInit(&argc, argv);
00213   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
00214   glutInitWindowSize (g_winDimen.w, g_winDimen.h); 
00215   glutInitWindowPosition (100, 100);
00216   
00217   GLint mainWindow = glutCreateWindow("Texture This!");
00218   
00219   init ();
00220   glutDisplayFunc(display); 
00221   glutReshapeFunc(reshape);
00222   
00223   glErr(cerr, "main","main");
00224   glutMainLoop();
00225   return 0;
00226 }

Send questions, comments, and bug reports to:
jmk