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

torus.cpp File Reference

#include <GL/glut.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include "config.h"
#include "Cg.h"
#include "texture.h"

Go to the source code of this file.

Functions

void setProgramMaterialColor (const float *mc)
void initTorus ()
void computeLightPosition (float *, float *)
void drawTorus (const float *eyePoint)

Variables

CgVertexProgram _vp_pv
CgFragmentProgram _fp_pv
CgVertexProgram _vp_pp
CgFragmentProgram _fp_pp


Function Documentation

void computeLightPosition float *  ,
float * 
 

Referenced by setupCamera().

void drawTorus const float *  eyePoint  ) 
 

Definition at line 90 of file torus.cpp.

References _fp_pp, _fp_pv, _vp_pp, _vp_pv, CgFragmentProgram::bindTexture(), CgFragmentProgram::disable(), CgProgram::disable(), CgProgram::enable(), CgFragmentProgram::enable(), g_bPerPixelLighting, g_fTorusX, g_fTorusY, g_lightPos, g_lightPosEye, ModelViewMatrix, CgProgram::setParameter(), CgVertexProgram::setStateMatrixParameter(), TEX_GRASS, TEX_ROCK, and texture().

Referenced by renderScene().

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 //transposed bias
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 //vp->setParameter("eye", 16, (const float *) ModelViewMatrix);
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 //glTranslatef(-5, -5, -5);
00146 //glRotatef(135, 0, 1, 0);
00147 //glRotatef(135, 1, 0, 0);
00148 //glRotatef(135, 0, 0, 1);
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 //vp->setParameter("lightFrustum", 16, (const float *) lightFrustum);
00159 //vp->setParameter("lightView", 16, (const float *) lightView);
00160 
00161   // Set the current modelview-projection matrix to the vertex program
00162 //  vp->setStateMatrixParameter( "Modelview", CG_GL_MODELVIEW_MATRIX );
00163 
00164 //vp-> setParameter("Kd", 1, &Kd );
00165 
00166 //lightprog -> setParameter("Kd", 1, &Kd);
00167 //vp -> setStateMatrixParameter("modelViewProj", CG_GL_MODELVIEW_PROJECTION_MATRIX );
00168 
00169 
00170 //vp -> setParameter("ModelViewProj", 16, modelViewProj );
00171   // Set normal transformation (inverse-transpose modelview)
00172 //  vp->setStateMatrixParameter( "NormalTransform", CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_INVERSE_TRANSPOSE );
00173 
00174 
00175   // Set light direction
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 //  lightprog->setParameter( "LightDir", 3, lightDir );
00186 vp -> setParameter ("lightPosition", 3,  g_lightPos );
00187 
00188 vp -> setStateMatrixParameter ("Projection", CG_GL_PROJECTION_MATRIX ); 
00189 
00190 
00191 
00192   // Set color state
00193   //setProgramMaterialColor( colorRed );
00194 
00195   // Set the texture to the fragment program
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 //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00206 //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00207 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00208 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
00209 
00210   // Render the torus
00211 glMatrixMode(GL_MODELVIEW);
00212 glPushMatrix();
00213 glRotatef(g_fTorusX, 0, 1, 0);
00214 glRotatef(g_fTorusY, 1, 0, 0);
00215 glPushMatrix();
00216   // Enable vertex program
00217   vp->enable();
00218   // Enable fragment program
00219   fp->enable();
00220   
00221 glPopMatrix();
00222 glutSolidSphere(0.5f, 20, 20);
00223 
00224 //  glutSolidTorus( 0.4f, 1.0f, 17,20 );
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   // Restore state
00283   fp->disable();
00284   vp->disable();
00285 }

void initTorus  ) 
 

Definition at line 71 of file torus.cpp.

References _fp_pp, _fp_pv, _vp_pp, _vp_pv, CgProgram::setFilename(), CgVertexProgram::setup(), and CgFragmentProgram::setup().

Referenced by initGL().

00072 {
00073   // Load CG objects
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 }

void setProgramMaterialColor const float *  mc  ) 
 

Definition at line 29 of file torus.cpp.

References _fp_pp, _vp_pv, colorNone, colorWhite, g_bBlinnPhong, g_bPerPixelLighting, g_specularExponent, and CgProgram::setParameter().

00030 {
00031   CgProgram *prog = NULL;
00032   if (g_bPerPixelLighting) {
00033     prog = &_fp_pp;
00034   }
00035   else {
00036     prog = &_vp_pv;
00037   }
00038 
00039   // Ambient material color
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   // Diffuse material color
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     // Specular color is white
00062     prog->setParameter( "SpecularColor", 4, colorWhite );
00063   }
00064   else {
00065     // Specular color is black, so specular term disabled
00066     prog->setParameter( "SpecularColor", 4, colorNone );
00067   }
00068 }


Variable Documentation

CgFragmentProgram _fp_pp [static]
 

Definition at line 20 of file torus.cpp.

Referenced by drawTorus(), initTorus(), and setProgramMaterialColor().

CgFragmentProgram _fp_pv [static]
 

Definition at line 17 of file torus.cpp.

Referenced by drawTorus(), and initTorus().

CgVertexProgram _vp_pp [static]
 

Definition at line 19 of file torus.cpp.

Referenced by drawTorus(), and initTorus().

CgVertexProgram _vp_pv [static]
 

Definition at line 16 of file torus.cpp.

Referenced by drawTorus(), initTorus(), and setProgramMaterialColor().


Generated on Thu Jan 20 02:47:13 2005 for Projective_Texture_Effect_Demo by doxygen 1.3.6