00001
00002
00003
00004
00005
00006
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
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
00094
00095
00096 #endif
00097 }
00098
00099
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
00117 MultiTexOState texState;
00118 texState << TS_S_CLAMP << TS_T_CLAMP << TS_NEAREST;
00119
00120
00121 TS_REPLACE->bind(GL_TEXTURE_2D);
00122
00123
00124 vec2i texDimen(numElts, numElts);
00125 TexDataArr1<uchar> texData( GL_LUMINANCE, GL_UNSIGNED_BYTE, texDimen, image );
00126
00127
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
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);
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
00160
00161
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);
00172
00173
00174 g_pbuff = new PBuffer( g_dimen, GLUT_SINGLE | GLUT_RGBA, pbuffInit, true, true );
00175
00176
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
00185
00186
00187
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
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
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
00218 ShadedPrim* shadedPrimGreyTex = new ShadedPrim( quad, greyTexShader );
00219 ShadedPrim* shadedPrimDeriv = new ShadedPrim( quad, derivShader );
00220 ShadedPrim* shadedPrimPbuff = new ShadedPrim( quad, drawPbuffShader );
00221 ShadedPrim* shadedPrimTex = new ShadedPrim( quad, drawTexShader );
00222
00223
00224 g_pass1 = new RenderPass( shadedPrimDeriv, g_pbuff, true );
00225 g_pass2 = new RenderPass( shadedPrimPbuff );
00226
00227
00228
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
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
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
00266 g_glui = GLUI_Master.create_glui( "GUI", 0, 400, 50 );
00267
00268
00269 g_glui->add_button( "Quit", 0,(GLUI_Update_CB) exit);
00270
00271
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 }