Main Page | Class List | File List | Class Members | File Members

depth_of_field.cpp File Reference

#include <GL/glut.h>
#include <GL/glu.h>
#include <stdlib.h>
#include <math.h>
#include "jitter.h"
#include <stdio.h>

Go to the source code of this file.

Defines

#define PI_   3.14159265358979323846

Enumerations

enum  {
  JITTER_2 = 0, JITTER_3, JITTER_4, JITTER_8,
  JITTER_15, JITTER_24, JITTER_66, JITTER_N
}

Functions

void glutString (char *pstr)
void accFrustum (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far, GLdouble pixdx, GLdouble pixdy, GLdouble eyedx, GLdouble eyedy, GLdouble focus)
void accPerspective (GLdouble fovy, GLdouble aspect, GLdouble near, GLdouble far, GLdouble pixdx, GLdouble pixdy, GLdouble eyedx, GLdouble eyedy, GLdouble focus)
void init (void)
void renderTeapot (GLfloat x, GLfloat y, GLfloat z, GLfloat ambr, GLfloat ambg, GLfloat ambb, GLfloat difr, GLfloat difg, GLfloat difb, GLfloat specr, GLfloat specg, GLfloat specb, GLfloat shine)
void display (void)
void print_jitter_points (int n)
void reshape (int w, int h)
void keyboard (unsigned char key, int x, int y)
void timerCallback (int)
void keyboardUp (unsigned char key, int, int)
int main (int argc, char **argv)

Variables

float z = 5.0
bool changed = false
float AngleX = 0 AngleY=0
bool RotateUp = false RotateDown = false RotateLeft = false RotateRight = false
float CameraX = 0 CameraY = 0 CameraZ = 5.0f
int LastTime = 0
int TimerDelay
float FPS = 0
int njitters [JITTER_N] = { 2, 3, 4, 8, 15, 24, 66 }
jitter_pointjitters [JITTER_N]
int current_jitter = JITTER_8
GLuint teapotList


Define Documentation

#define PI_   3.14159265358979323846
 

Definition at line 59 of file depth_of_field.cpp.

Referenced by accPerspective().


Enumeration Type Documentation

anonymous enum
 

Enumeration values:
JITTER_2 
JITTER_3 
JITTER_4 
JITTER_8 
JITTER_15 
JITTER_24 
JITTER_66 
JITTER_N 

Definition at line 97 of file depth_of_field.cpp.


Function Documentation

void accFrustum GLdouble  left,
GLdouble  right,
GLdouble  bottom,
GLdouble  top,
GLdouble  near,
GLdouble  far,
GLdouble  pixdx,
GLdouble  pixdy,
GLdouble  eyedx,
GLdouble  eyedy,
GLdouble  focus
 

Definition at line 122 of file depth_of_field.cpp.

Referenced by accPerspective().

00125 {
00126    GLdouble xwsize, ywsize; 
00127    GLdouble dx, dy;
00128    GLint viewport[4];
00129 
00130    glGetIntegerv (GL_VIEWPORT, viewport);
00131         
00132    xwsize = right - left;
00133    ywsize = top - bottom;
00134         
00135    dx = -(pixdx*xwsize/(GLdouble) viewport[2] + eyedx*near/focus);
00136    dy = -(pixdy*ywsize/(GLdouble) viewport[3] + eyedy*near/focus);
00137         
00138    glMatrixMode(GL_PROJECTION);
00139    glLoadIdentity();
00140    glFrustum (left + dx, right + dx, bottom + dy, top + dy, near, far);
00141    glMatrixMode(GL_MODELVIEW);
00142    glLoadIdentity();
00143    glTranslatef (-eyedx, -eyedy, 0.0);
00144 }

void accPerspective GLdouble  fovy,
GLdouble  aspect,
GLdouble  near,
GLdouble  far,
GLdouble  pixdx,
GLdouble  pixdy,
GLdouble  eyedx,
GLdouble  eyedy,
GLdouble  focus
 

Definition at line 159 of file depth_of_field.cpp.

References accFrustum(), and PI_.

Referenced by display().

00162 {
00163    GLdouble fov2,left,right,bottom,top;
00164 
00165    fov2 = ((fovy*PI_) / 180.0) / 2.0;
00166 
00167    top = near / (cos(fov2) / sin(fov2));
00168    bottom = -top;
00169 
00170    right = top * aspect;
00171    left = -right;
00172 
00173    accFrustum (left, right, bottom, top, near, far,
00174                pixdx, pixdy, eyedx, eyedy, focus);
00175 }

void display void   ) 
 

Definition at line 260 of file depth_of_field.cpp.

References accPerspective(), changed, current_jitter, jitters, njitters, renderTeapot(), jitter_point::x, and z.

Referenced by main().

00261 {
00262    int jitter;
00263    GLint viewport[4];
00264 
00265 //    glColor3f( 1.0f, 1.0f, 1.0f );
00266 
00267    glClearColor(0.0, 0.0, 0.0, 0.0);
00268    glClearAccum(0.0, 0.0, 0.0, 0.0); 
00269 
00270    glGetIntegerv (GL_VIEWPORT, viewport);
00271   // glClear(GL_ACCUM_BUFFER_BIT);
00272 //glClear(GL_COLOR_BUFFER_BIT);
00273       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ACCUM_BUFFER_BIT);
00274 
00275 if (changed) 
00276         {
00277         changed = false;
00278       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00279       accPerspective (45.0, 
00280          (GLdouble) viewport[2]/(GLdouble) viewport[3], 
00281          1.0, 15.0, 0.0, 0.0,
00282          0, 0, z);
00283       renderTeapot (-1.1, -0.5, -4.5, 0.1745, 0.01175, 
00284                     0.01175, 0.61424, 0.04136, 0.04136, 
00285                     0.727811, 0.626959, 0.626959, 0.6);
00286       renderTeapot (-0.5, -0.5, -5.0, 0.24725, 0.1995, 
00287                     0.0745, 0.75164, 0.60648, 0.22648, 
00288                     0.628281, 0.555802, 0.366065, 0.4);
00289       renderTeapot (0.2, -0.5, -5.5, 0.19225, 0.19225, 
00290                     0.19225, 0.50754, 0.50754, 0.50754, 
00291                     0.508273, 0.508273, 0.508273, 0.4);
00292       renderTeapot (1.0, -0.5, -6.0, 0.0215, 0.1745, 0.0215, 
00293                     0.07568, 0.61424, 0.07568, 0.633, 
00294                     0.727811, 0.633, 0.6);
00295       renderTeapot (1.8, -0.5, -6.5, 0.0, 0.1, 0.06, 0.0, 
00296                     0.50980392, 0.50980392, 0.50196078, 
00297                     0.50196078, 0.50196078, .25);
00298        glAccum (GL_ACCUM, 1.0);
00299        glAccum (GL_RETURN, 1.0);
00300        
00301        
00302        //and now making depth of field
00303         }
00304         else
00305         {//if there are no changes in camera position,
00306         // then accumulate to the buffer
00307 
00308    jitter_point *tmp;
00309    for (jitter = 0; jitter < njitters[current_jitter]; jitter++) {
00310       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00311       tmp = (jitters[current_jitter] + jitter);
00312       accPerspective (45.0, 
00313          (GLdouble) viewport[2]/(GLdouble) viewport[3], 
00314          1.0, 15.0, 0.0, 0.0,
00315          .33 * tmp->x, .33 * tmp -> y, z);
00316 //        glMatrixMode(GL_PROJECTION);
00317         //  glTranslated(CameraX, CameraY, CameraZ);
00318 //      glRotated( AngleY, 0, 1, 0);
00319           glMatrixMode(GL_MODELVIEW);
00320 //        glTranslated(-CameraX, -CameraY, -CameraZ);
00321                 glPushMatrix();
00322 /*      ruby, gold, silver, emerald, and cyan teapots   */
00323       renderTeapot (-1.1, -0.5, -4.5, 0.1745, 0.01175, 
00324                     0.01175, 0.61424, 0.04136, 0.04136, 
00325                     0.727811, 0.626959, 0.626959, 0.6);
00326       renderTeapot (-0.5, -0.5, -5.0, 0.24725, 0.1995, 
00327                     0.0745, 0.75164, 0.60648, 0.22648, 
00328                     0.628281, 0.555802, 0.366065, 0.4);
00329       renderTeapot (0.2, -0.5, -5.5, 0.19225, 0.19225, 
00330                     0.19225, 0.50754, 0.50754, 0.50754, 
00331                     0.508273, 0.508273, 0.508273, 0.4);
00332       renderTeapot (1.0, -0.5, -6.0, 0.0215, 0.1745, 0.0215, 
00333                     0.07568, 0.61424, 0.07568, 0.633, 
00334                     0.727811, 0.633, 0.6);
00335       renderTeapot (1.8, -0.5, -6.5, 0.0, 0.1, 0.06, 0.0, 
00336                     0.50980392, 0.50980392, 0.50196078, 
00337                     0.50196078, 0.50196078, .25);
00338                 glPopMatrix();
00339       glAccum (GL_ACCUM, (1.0f/njitters[current_jitter]) ); //0.125
00340    }//end for
00341    glAccum (GL_RETURN, 1.0);
00342    }//end if 
00343    
00344     
00345 /*       // Display framerate
00346 glPushMatrix();
00347     char buf[100];
00348     sprintf( buf, "READY %0.2f fps,  X=%f, Y=%f, Z=%f, alpha=%f, beta=%f ", FPS, CameraX, CameraY, CameraZ, 
00349         AngleX, AngleY );
00350     glMatrixMode( GL_PROJECTION );
00351     glLoadIdentity();
00352     glMatrixMode( GL_MODELVIEW );
00353     glLoadIdentity();
00354     glDisable( GL_DEPTH_TEST );
00355     glDisable( GL_LIGHTING );
00356     glColor3f( 1.0f, 1.0f, 1.0f );
00357     glRasterPos2f( -1,-1 );
00358     glutString( buf );
00359     glEnable( GL_DEPTH_TEST );
00360     glEnable(GL_LIGHTING);
00361 glPopMatrix();
00362   */ 
00363    glutSwapBuffers();
00364 }

void glutString char *  pstr  ) 
 

Definition at line 88 of file depth_of_field.cpp.

00089 {
00090   while (*pstr!=char(0)) {
00091     glutBitmapCharacter( GLUT_BITMAP_8_BY_13, int( *pstr ));
00092     pstr++;
00093   }
00094 }

void init void   ) 
 

Definition at line 177 of file depth_of_field.cpp.

References j15, j2, j24, j3, j4, j66, j8, jitters, and teapotList.

Referenced by main().

00178 {
00179 jitters[0] = j2;
00180 jitters[1] = j3;
00181 jitters[2] = j4;
00182 jitters[3] = j8;
00183 jitters[4] = j15;
00184 jitters[5] = j24;
00185 jitters[6] = j66;
00186 
00187 GLenum performance = GL_FASTEST; //GL_NICEST
00188 glHint(GL_POINT_SMOOTH_HINT, performance );
00189 glHint(GL_LINE_SMOOTH_HINT, performance );
00190 glHint(GL_POLYGON_SMOOTH_HINT, performance );
00191 glHint(GL_FOG_HINT, performance );
00192 glHint(GL_PERSPECTIVE_CORRECTION_HINT, performance );
00193 
00194 /*
00195   glEnable(GL_DEPTH_TEST);
00196   glDepthFunc(GL_LESS);
00197   glShadeModel(GL_SMOOTH);
00198   glEnable(GL_LIGHTING);
00199   glDisable( GL_CULL_FACE );
00200   glEnable( GL_NORMALIZE );
00201   */
00202    GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
00203    GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
00204    GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
00205    GLfloat position[] = { 0.0, 3.0, 3.0, 0.0 };
00206     
00207    GLfloat lmodel_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
00208    GLfloat local_view[] = { 0.0 };
00209 
00210    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
00211    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
00212    glLightfv(GL_LIGHT0, GL_POSITION, position);
00213     
00214    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
00215    glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
00216 
00217    glFrontFace (GL_CW);
00218    glEnable(GL_LIGHTING);
00219    glEnable(GL_LIGHT0);
00220    glEnable(GL_AUTO_NORMAL);
00221    glEnable(GL_NORMALIZE);
00222    glEnable(GL_DEPTH_TEST);
00223 
00224    glClearColor(0.0, 0.0, 0.0, 0.0);
00225    glClearAccum(0.0, 0.0, 0.0, 0.0); 
00226 /*  make teapot display list */
00227    teapotList = glGenLists(1);
00228    glNewList (teapotList, GL_COMPILE);
00229    glutSolidTeapot (0.5);
00230    glEndList ();
00231 }

void keyboard unsigned char  key,
int  x,
int  y
 

Definition at line 384 of file depth_of_field.cpp.

References changed, current_jitter, JITTER_15, JITTER_2, JITTER_24, JITTER_3, JITTER_4, JITTER_66, JITTER_8, print_jitter_points(), and z.

Referenced by main().

00385 {
00386    switch (key) {
00387       case 27:
00388       case 'q':
00389          exit(0);
00390          break;
00391           case ' ':
00392                  glutPostRedisplay();
00393                  break;
00394           case 'a':
00395                 RotateLeft = true;
00396                 RotateRight = false;
00397                 break;
00398           case 'd':
00399                 RotateRight = true;
00400                 RotateLeft = false;
00401                 break;
00402           case 'n': 
00403                 //increase focus
00404                 z+= 0.1;
00405                 changed = true;
00406                 break;
00407           case 'm': z-=0.1;
00408                 changed = true;
00409                 break;
00410          case '1': 
00411                 changed = true; 
00412                 current_jitter = JITTER_2; 
00413                 printf(" From now will use %d jitter points. \n", 2);
00414                 print_jitter_points(JITTER_2);
00415                 break;
00416          case '2': 
00417                 changed = true; 
00418                 current_jitter = JITTER_3; 
00419                 printf(" From now will use %d jitter points. \n", 3);
00420                 print_jitter_points(JITTER_3);
00421                 break;
00422          case '3': 
00423                 changed = true; 
00424                 current_jitter = JITTER_4; 
00425                 printf(" From now will use %d jitter points. \n", 4);
00426                 print_jitter_points(JITTER_4);
00427                 break;
00428          case '4': 
00429                 changed = true; 
00430                 current_jitter = JITTER_8; 
00431                 printf(" From now will use %d jitter points. \n", 8);
00432                 print_jitter_points(JITTER_8);
00433                 break;
00434          case '5': 
00435                 changed = true; 
00436                 current_jitter = JITTER_15; 
00437                 printf(" From now will use %d jitter points. \n", 15);
00438                 print_jitter_points(JITTER_15);
00439                 break;
00440          case '6': 
00441                 changed = true; 
00442                 current_jitter = JITTER_24; 
00443                 printf(" From now will use %d jitter points. \n", 24);
00444                 print_jitter_points(JITTER_24);
00445                 break;
00446          case '7': 
00447                 changed = true; 
00448                 current_jitter = JITTER_66; 
00449                 printf(" From now will use %d jitter points. \n", 66);
00450                 print_jitter_points(JITTER_66);
00451                 break;
00452    }
00453 }

void keyboardUp unsigned char  key,
int  ,
int 
 

Definition at line 479 of file depth_of_field.cpp.

Referenced by main().

00480 {
00481 switch (key)
00482         {
00483         case 'a':
00484                 RotateLeft = false;
00485                 break;
00486         case 'd': RotateRight = false; break;
00487         }
00488 
00489 }

int main int  argc,
char **  argv
 

Definition at line 491 of file depth_of_field.cpp.

References display(), init(), keyboard(), keyboardUp(), LastTime, reshape(), timerCallback(), and TimerDelay.

00492 {
00493 
00494 printf(" Keys: \n");
00495 printf("\tn/m  -- increase/decrease focus distance \n");
00496 printf("\t1..7 -- change number of jitter points   \n");
00497 printf("\tq    -- to Quit.\n");
00498 
00499    glutInit(&argc, argv);
00500    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB
00501                         | GLUT_ACCUM | GLUT_DEPTH); 
00502    glutInitWindowSize (400, 400);
00503    glutInitWindowPosition (100, 100);
00504    glutCreateWindow (argv[0]);
00505    init();
00506    glutReshapeFunc(reshape);
00507    glutDisplayFunc(display);
00508    glutKeyboardFunc(keyboard);
00509    glutKeyboardUpFunc(keyboardUp);
00510    LastTime = glutGet (GLUT_ELAPSED_TIME );
00511    glutTimerFunc ( TimerDelay, timerCallback, 0);
00512    glutMainLoop();
00513    return 0;
00514 }

void print_jitter_points int  n  ) 
 

Definition at line 366 of file depth_of_field.cpp.

References jitters, njitters, jitter_point::x, and jitter_point::y.

Referenced by keyboard().

00367 {
00368 jitter_point * tmp;
00369    for (int jitter = 0; jitter < njitters[n]; jitter++) 
00370       {
00371       tmp = (jitters[n] + jitter);
00372       printf(" coordinates of %d-jitter point: x = %f, y = %f \n", jitter + 1, tmp->x, tmp->y );
00373       }
00374 
00375 }

void renderTeapot GLfloat  x,
GLfloat  y,
GLfloat  z,
GLfloat  ambr,
GLfloat  ambg,
GLfloat  ambb,
GLfloat  difr,
GLfloat  difg,
GLfloat  difb,
GLfloat  specr,
GLfloat  specg,
GLfloat  specb,
GLfloat  shine
 

Definition at line 233 of file depth_of_field.cpp.

References teapotList, and z.

Referenced by display().

00237 {
00238    GLfloat mat[4];
00239 
00240    glPushMatrix();
00241    glTranslatef (x, y, z);
00242    mat[0] = ambr; mat[1] = ambg; mat[2] = ambb; mat[3] = 1.0;   
00243    glMaterialfv (GL_FRONT, GL_AMBIENT, mat);
00244    mat[0] = difr; mat[1] = difg; mat[2] = difb; 
00245    glMaterialfv (GL_FRONT, GL_DIFFUSE, mat);
00246    mat[0] = specr; mat[1] = specg; mat[2] = specb;
00247    glMaterialfv (GL_FRONT, GL_SPECULAR, mat);
00248    glMaterialf (GL_FRONT, GL_SHININESS, shine*128.0);
00249    glCallList(teapotList);
00250    glPopMatrix();
00251 }

void reshape int  w,
int  h
 

Definition at line 377 of file depth_of_field.cpp.

References changed.

Referenced by main().

00378 {
00379 changed = true;
00380    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
00381    glutPostRedisplay();
00382 }

void timerCallback int   )  [static]
 

Definition at line 455 of file depth_of_field.cpp.

References FPS, LastTime, timerCallback(), and TimerDelay.

Referenced by main(), and timerCallback().

00456 {
00457 
00458 int nc = glutGet(GLUT_ELAPSED_TIME);
00459 float fSec = float(nc-LastTime) / 1000.0f;
00460   // Compute average frame rate from the last twenty frames
00461   FPS = (19.0f*FPS + 1.0f / fSec) / 20.0f;
00462   LastTime = nc;
00463   
00464   float fCamArc = fSec * 180.0f;
00465   if (RotateLeft) AngleY -= fCamArc/4;
00466   if (RotateRight) AngleY += fCamArc/4;
00467   
00468   glutPostRedisplay();
00469   glutTimerFunc(TimerDelay, timerCallback, 0);
00470 
00471 }


Variable Documentation

float AngleX = 0 AngleY=0
 

Definition at line 78 of file depth_of_field.cpp.

float CameraX = 0 CameraY = 0 CameraZ = 5.0f
 

Definition at line 81 of file depth_of_field.cpp.

bool changed = false
 

Definition at line 73 of file depth_of_field.cpp.

Referenced by display(), keyboard(), and reshape().

int current_jitter = JITTER_8
 

Definition at line 103 of file depth_of_field.cpp.

Referenced by display(), and keyboard().

float FPS = 0
 

Definition at line 85 of file depth_of_field.cpp.

Referenced by timerCallback().

jitter_point* jitters[JITTER_N]
 

Definition at line 101 of file depth_of_field.cpp.

Referenced by display(), init(), and print_jitter_points().

int LastTime = 0
 

Definition at line 83 of file depth_of_field.cpp.

Referenced by main(), and timerCallback().

int njitters[JITTER_N] = { 2, 3, 4, 8, 15, 24, 66 }
 

Definition at line 99 of file depth_of_field.cpp.

Referenced by display(), and print_jitter_points().

bool RotateUp = false RotateDown = false RotateLeft = false RotateRight = false
 

Definition at line 79 of file depth_of_field.cpp.

GLuint teapotList
 

Definition at line 105 of file depth_of_field.cpp.

Referenced by init(), and renderTeapot().

int TimerDelay
 

Definition at line 84 of file depth_of_field.cpp.

Referenced by main(), and timerCallback().

float z = 5.0
 

Definition at line 71 of file depth_of_field.cpp.

Referenced by display(), keyboard(), and renderTeapot().


Generated on Thu Jan 20 02:47:04 2005 for Depth_of_Field_Effect_Demo by doxygen 1.3.6