00001 #include <GL/glut.h>
00002 #include <GL/glu.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <math.h>
00006 #include <assert.h>
00007
00008 #include "config.h"
00009 #include "Cg.h"
00010 #include "texture.h"
00011
00012 #include "math.h"
00013
00014
00015
00016 static CgVertexProgram _vp_pv;
00017 static CgFragmentProgram _fp_pv;
00018
00019 static CgVertexProgram _vp_pp;
00020 static CgFragmentProgram _fp_pp;
00021
00022
00023
00024
00025
00026
00027
00028
00029 void setProgramMaterialColor( const float *mc )
00030 {
00031 CgProgram *prog = NULL;
00032 if (g_bPerPixelLighting) {
00033 prog = &_fp_pp;
00034 }
00035 else {
00036 prog = &_vp_pv;
00037 }
00038
00039
00040 float ambient[4];
00041 const float af = 0.3f;
00042 ambient[0] = mc[0] * af;
00043 ambient[1] = mc[1] * af;
00044 ambient[2] = mc[2] * af;
00045 ambient[3] = mc[3] * af;
00046 prog->setParameter( "AmbientColor", 4, ambient );
00047
00048
00049
00050 float diffuse[4];
00051 const float df = 1.0 - af;
00052 diffuse[0] = mc[0] * df;
00053 diffuse[1] = mc[1] * df;
00054 diffuse[2] = mc[2] * df;
00055 diffuse[3] = mc[3] * df;
00056 prog->setParameter( "DiffuseColor", 4, diffuse );
00057
00058
00059 prog->setParameter( "SpecularExponent", 1, &g_specularExponent );
00060 if (g_bBlinnPhong) {
00061
00062 prog->setParameter( "SpecularColor", 4, colorWhite );
00063 }
00064 else {
00065
00066 prog->setParameter( "SpecularColor", 4, colorNone );
00067 }
00068 }
00069
00070
00071 void initTorus()
00072 {
00073
00074 _vp_pv.setFilename( "vertex_program.cg" );
00075 _vp_pv.setup();
00076
00077 _fp_pv.setFilename( "fragment_program.cg" );
00078 _fp_pv.setup();
00079
00080 _vp_pp.setFilename( "vertex_program_pp.cg" );
00081 _vp_pp.setup();
00082
00083 _fp_pp.setFilename( "fragment_program_pp.cg" );
00084 _fp_pp.setup();
00085
00086 }
00087
00088 extern void computeLightPosition(float *, float *);
00089
00090 void drawTorus( const float *eyePoint )
00091 {
00092 CgVertexProgram *vp = NULL;
00093 CgFragmentProgram *fp = NULL;
00094 CgProgram *lightprog = NULL;
00095 if (g_bPerPixelLighting) {
00096 vp = &_vp_pp;
00097 fp = &_fp_pp;
00098 lightprog = &_fp_pp;
00099 }
00100 else {
00101 vp = &_vp_pv;
00102 fp = &_fp_pv;
00103 lightprog = &_vp_pv;
00104 }
00105
00106 float textureMatrix[16];
00107 float modelViewProj[16];
00108 float modelView[16];
00109 float vector[4];
00110 float bias[16];
00111 float lightFrustum[16];
00112 float lightView[16];
00113 float tex[16];
00114 float trbias[16];
00115
00116 bias[0] = .5; bias[1] = 0; bias[2] = 0; bias[3] = .5;
00117 bias[4] = 0 ; bias[5] = .5; bias[6] = 0; bias[7] =.5;
00118 bias[8] = 0 ; bias[9] = 0; bias[10] = .5; bias[11] = .5;
00119 bias[12] = 0; bias[13] = 0; bias[14] = 0; bias[15] = 1;
00120
00121
00122
00123 trbias[0] = .5; trbias[1] = 0; trbias[2] = 0; trbias[3] = 0;
00124 trbias[4] = 0 ; trbias[5] = .5; trbias[6] = 0; trbias[7] = 0;
00125 trbias[8] = 0 ; trbias[9] = 0; trbias[10] = .5; trbias[11] = 0;
00126 trbias[12] = 0.5; trbias[13] = 0.5; trbias[14] = 0.5; trbias[15] = 1;
00127
00128
00129 glGetFloatv(CG_GL_MODELVIEW_PROJECTION_MATRIX, modelViewProj);
00130
00131 glGetFloatv(GL_MODELVIEW_MATRIX, modelView);
00132
00133 vp->setParameter("bias", 16, (const float *) trbias );
00134
00135
00136
00137 glBindTexture(GL_TEXTURE_2D, TEX_ROCK );
00138
00139 glMatrixMode(GL_TEXTURE);
00140 glPushMatrix();
00141 glLoadIdentity();
00142 glMultMatrixf(trbias);
00143 glFrustum(-1, 1, -1, 1, 1, 15);
00144 gluLookAt(-3, 3, -3, 0,0,0, 0, 5,0);
00145
00146
00147
00148
00149 glMultMatrixf(ModelViewMatrix);
00150 glGetFloatv(GL_TEXTURE_MATRIX, tex);
00151 glPushMatrix();
00152 vp->setParameter("TM", 16, (const float *) tex);
00153 glPopMatrix();
00154 glPopMatrix();
00155 glLoadIdentity();
00156 glMatrixMode(GL_MODELVIEW);
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 float lightDir[3];
00177 lightDir[0] = g_lightPosEye[0];
00178 lightDir[1] = g_lightPosEye[1];
00179 lightDir[2] = g_lightPosEye[2];
00180 float l = sqrt( lightDir[0]*lightDir[0] + lightDir[1]*lightDir[1] + lightDir[2]*lightDir[2] );
00181 assert( l != 0 );
00182 lightDir[0] /= l;
00183 lightDir[1] /= l;
00184 lightDir[2] /= l;
00185
00186 vp -> setParameter ("lightPosition", 3, g_lightPos );
00187
00188 vp -> setStateMatrixParameter ("Projection", CG_GL_PROJECTION_MATRIX );
00189
00190
00191
00192
00193
00194
00195
00196 fp->bindTexture( "Texture", texture( TEX_GRASS ));
00197
00198
00199 vp->setStateMatrixParameter( "ModelviewProjection", CG_GL_MODELVIEW_PROJECTION_MATRIX );
00200 vp->setStateMatrixParameter( "ModelviewProjectionIT", CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_INVERSE_TRANSPOSE );
00201 vp->setStateMatrixParameter( "Modelview", CG_GL_MODELVIEW_MATRIX );
00202 vp->setStateMatrixParameter( "ModelviewIT", CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_INVERSE_TRANSPOSE );
00203
00204 glEnable(GL_TEXTURE_2D);
00205
00206
00207
00208
00209
00210
00211 glMatrixMode(GL_MODELVIEW);
00212 glPushMatrix();
00213 glRotatef(g_fTorusX, 0, 1, 0);
00214 glRotatef(g_fTorusY, 1, 0, 0);
00215 glPushMatrix();
00216
00217 vp->enable();
00218
00219 fp->enable();
00220
00221 glPopMatrix();
00222 glutSolidSphere(0.5f, 20, 20);
00223
00224
00225 glPopMatrix();
00226
00227
00228
00229 glPushMatrix();
00230
00231 glBegin(GL_TRIANGLES);
00232 glVertex3d( -3, -3, 3);
00233 glVertex3d( -3, 0, 3);
00234 glVertex3d( 0, 0, 3);
00235
00236 glVertex3d( 0, 0, 3);
00237 glVertex3d( 0, -3, 3);
00238 glVertex3d( -3, -3, 3);
00239
00240 glVertex3d( -3, 0, 3);
00241 glVertex3d( -3, 3, 3);
00242 glVertex3d( 0, 3, 3);
00243
00244 glVertex3d( 0, 3, 3);
00245 glVertex3d( 0, 0, 3);
00246 glVertex3d( -3, 0, 3);
00247
00248 glVertex3d( 0, -3, 3);
00249 glVertex3d( 0, 0, 3);
00250 glVertex3d( 0, 3, 3);
00251
00252 glVertex3d( 0,-3,3);
00253 glVertex3d( 0,0,3);
00254 glVertex3d( 3,0,3);
00255
00256 glVertex3d( 3,0,3);
00257 glVertex3d( 3,-3,3);
00258 glVertex3d( 0,-3,3);
00259
00260 glVertex3d( 0,0,3);
00261 glVertex3d( 0,3,3);
00262 glVertex3d( 3,3,3);
00263
00264 glVertex3d( 3,3,3);
00265 glVertex3d( 3,0,3);
00266 glVertex3d( 0,0,3);
00267 glEnd();
00268 glPopMatrix();
00269
00270 glPushMatrix();
00271 glBegin(GL_TRIANGLES);
00272 glVertex3d( -3, -3, -3);
00273 glVertex3d( -3, -3, 3);
00274 glVertex3d( 3, -3, 3);
00275
00276 glVertex3d( 3,-3, 3);
00277 glVertex3d( 3,-3, -3);
00278 glVertex3d( -3,-3, -3);
00279 glEnd();
00280 glPopMatrix();
00281 glDisable(GL_TEXTURE_2D);
00282
00283 fp->disable();
00284 vp->disable();
00285 }