example/example.cpp

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 #include <GL/glut.h>
00007 
00008 #include <stdlib.h>
00009 #include <math.h>
00010 #include <iostream>
00011 #include <fstream>
00012 
00013 #include "faogenlib.h"
00014 
00016 struct plane_vertex
00017 {
00018         float pos[3];
00019         float normal[3];
00020         GLubyte color[4];       
00021 };
00022 
00023 int const plane_size = 100;
00024 plane_vertex plane_geometry[plane_size*plane_size];
00025 unsigned short plane_indices[(plane_size-1)*(plane_size-1)*2*3];
00026 
00028 void build_plane();
00029 
00031 float cube_vertices[] = 
00032 {
00033         -0.5f, -0.5f, 0.f,
00034         0.5f, -0.5f, 0.f,
00035         -0.5f, 0.5f, 0.f,
00036         0.5f, 0.5f, 0.f,
00037         -0.5f, -0.5f, 1.f,
00038         0.5f, -0.5f, 1.f,
00039         -0.5f, 0.5f, 1.f,
00040         0.5f, 0.5f, 1.f,
00041         -0.5f, -0.5f, 1.f,
00042         0.5f, -0.5f, 1.f,
00043         -0.5f, 0.5f, 1.f,
00044         0.5f, 0.5f, 1.f,
00045         -0.5f, -0.5f, 0.f,
00046         0.5f, -0.5f, 0.f,
00047         -0.5f, -0.5f, 1.f,
00048         0.5f, -0.5f, 1.f,
00049         -0.5f, -0.5f, 0.f,
00050         -0.5f, 0.5f, 0.f,
00051         -0.5f, 0.5f, 1.f,
00052         -0.5f, 0.5f, 0.f,
00053         0.5f, 0.5f, 0.f,
00054         0.5f, 0.5f, 1.f,
00055         0.5f, -0.5f, 0.f,
00056         0.5f, 0.5f, 0.f
00057 };
00058 
00059 float cube_texcoords[] = 
00060 {
00061         0.3360113f, 1.f, 
00062         0.f, 0.6720225f, 
00063         0.3360113f, 0.6720225f, 
00064         0.3279775f, 0.6720225f, 
00065         0.6639888f, 0.3279775f, 
00066         0.f, 1.f, 
00067         0.3279775f, 0.3279775f, 
00068         0.3279775f, 1.f, 
00069         0.f, 0.3360113f, 
00070         0.3279775f, 0.3360113f, 
00071         0.f, 0.6639888f, 
00072         0.3279775f, 0.6639888f, 
00073         0.3360113f, 0.3360113f, 
00074         0.6639888f, 0.3360113f, 
00075         0.3360113f, 0.6639888f, 
00076         0.6639888f, 0.6639888f, 
00077         0.6639888f, 0.f, 
00078         0.3360113f, 0.f, 
00079         0.3360113f, 0.3279775f, 
00080         0.3279775f, 0.f, 
00081         0.f, 0.f, 
00082         0.f, 0.3279775f, 
00083         0.6639888f, 1.f, 
00084         0.6639888f, 0.6720225f
00085 };
00086 
00087 float cube_normals[] = 
00088 {
00089         0.f, 0.f, -1.f,
00090         1.f, 0.f, 0.f,
00091         0.f, 0.f, -1.f,
00092         1.f, 0.f, 0.f,
00093         -1.f, 0.f, 0.f,
00094         1.f, 0.f, 0.f,
00095         0.f, 1.f, 0.f,
00096         1.f, 0.f, 0.f,
00097         0.f, 0.f, 1.f,
00098         0.f, 0.f, 1.f,
00099         0.f, 0.f, 1.f,
00100         0.f, 0.f, 1.f,
00101         0.f, -1.f, 0.f,
00102         0.f, -1.f, 0.f,
00103         0.f, -1.f, 0.f,
00104         0.f, -1.f, 0.f,
00105         -1.f, 0.f, 0.f,
00106         -1.f, 0.f, 0.f,
00107         -1.f, 0.f, 0.f,
00108         0.f, 1.f, 0.f,
00109         0.f, 1.f, 0.f,
00110         0.f, 1.f, 0.f,
00111         0.f, 0.f, -1.f,
00112         0.f, 0.f, -1.f, 
00113 };
00114 
00115 unsigned short cube_indices[6*2*3] = 
00116 {
00117         1-1, 3-1, 24-1,
00118         24-1, 23-1, 1-1,
00119         9-1, 10-1, 12-1,
00120         12-1, 11-1, 9-1,
00121         13-1, 14-1, 16-1,
00122         16-1, 15-1, 13-1,
00123         2-1, 4-1, 8-1,
00124         8-1, 6-1, 2-1,
00125         21-1, 20-1, 7-1,
00126         7-1, 22-1, 21-1,
00127         18-1, 17-1, 5-1,
00128         5-1, 19-1, 18-1,
00129 };
00130 
00131 float cube_matrix[16] = 
00132 {
00133         0.8639584f, -0.5035632f, 0.f, 0.f,
00134         0.5035632f, 0.8639584f, 0.f, 0.f,
00135         0.f, 0.f, 1.f, 0.f, 
00136         0.f, 0.f, -0.25f, 1.f
00137 };
00138 
00139 GLuint cube_texture = 0;
00140 
00141 float bottom_occludder[] = 
00142 {
00143         -5.f, -5.f, 0.f,
00144         5.f, -5.f, 0.f,
00145         -5.f, 5.f, 0.f,
00146         5.f, 5.f, 0.f,
00147         0.f, 0.f, -5.f,
00148 };
00149 
00150 unsigned short bottom_indices[] = 
00151 {
00152         0, 4, 1,
00153         1, 4, 3,
00154         3, 4, 2,
00155         2, 4, 0
00156 };
00157 
00159 bool generate_example()
00160 {
00161         // First we need to initialize faogenlib
00162         if( faogenlib::initialize() )
00163         {
00164                 // Now define scene:
00165 
00166                 // plane object - horizontal grid, for which per-vertex AO will be calculated
00167                 faogenlib::obj_handle plane = faogenlib::create_object_vn( 
00168                         &plane_geometry[0].pos[0], sizeof(plane_vertex),
00169                         &plane_geometry[0].normal[0], sizeof(plane_vertex), 
00170                         sizeof(plane_geometry)/sizeof(plane_geometry[0]),
00171                         faogenlib::INDEX_U16, plane_indices, sizeof(plane_indices)/sizeof(plane_indices[0]) );
00172 
00173                 // cube - contains texcoords
00174                 faogenlib::obj_handle cube = faogenlib::create_object_vnt( 
00175                         cube_vertices, 3*sizeof(float),
00176                         cube_normals, 3*sizeof(float), 
00177                         cube_texcoords, 2*sizeof(float),
00178                         24,
00179                         faogenlib::INDEX_U16, cube_indices, sizeof(cube_indices)/sizeof(cube_indices[0]) );
00180 
00181                 // faogenlib allows to change transformation matrix for any object in scene
00182                 faogenlib::set_object_matrix( cube, cube_matrix );
00183 
00184                 // bottom geometry - only vertex positions
00185                 faogenlib::obj_handle bottom = faogenlib::create_object_v( 
00186                         bottom_occludder, 3*sizeof(float),
00187                         5,
00188                         faogenlib::INDEX_U16, bottom_indices, sizeof(bottom_indices)/sizeof(bottom_indices[0]) );
00189 
00190                 unsigned int const quality = 10;
00191                 unsigned int iterations = faogenlib::num_iterations( quality );
00192                 unsigned int step = iterations/50;
00193                 if( step == 0 )
00194                         step = 1;
00195 
00196                 unsigned int texture_size = 256;
00197                 
00198                 // generate AO texture for cube
00199                 if( faogenlib::start_ao_gen_tex( cube, 1024, 0.01f, 1.f, quality, true, texture_size, texture_size, 2, false ) )
00200                 {
00201                         std::cout << "cube\t:";
00202                         do
00203                         {
00204                                 faogenlib::ao_tex_generate( step );
00205                                 std::cout << '.';
00206                         }
00207                         while( !faogenlib::results_ready() );
00208 
00209                         float const * result = faogenlib::get_tex_results( 16 );
00210 
00211                         glGenTextures( 1, &cube_texture );
00212                         glBindTexture( GL_TEXTURE_2D, cube_texture );
00213                         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
00214                         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
00215                         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, 0x812F/*GL_CLAMP_TO_EDGE*/ );
00216                         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, 0x812F/*GL_CLAMP_TO_EDGE*/ );
00217                         glTexImage2D( GL_TEXTURE_2D, 0, GL_LUMINANCE8, texture_size, texture_size, 0, GL_LUMINANCE, GL_FLOAT, result );
00218 
00219                         std::cout << "done\n";
00220                 }
00221 
00222                 // generate per-vertex AO for plane
00223                 if( faogenlib::start_ao_gen_vert( plane, 1024, 0.01f, 1.f, quality, true, 2, false ) )
00224                 {
00225                         std::cout << "plane\t:";
00226                         do
00227                         {
00228                                 faogenlib::ao_vert_generate( step );
00229                                 std::cout << '.';
00230                         }
00231                         while( !faogenlib::results_ready() );
00232 
00233                         float const * result = faogenlib::get_vert_results();
00234                         for( int i = 0; i < sizeof(plane_geometry)/sizeof(plane_geometry[0]); i++ )
00235                         {
00236                                 unsigned char value = result[i] < 0.f ? 0 : result[i] > 1.f ? 255 : (unsigned char)(result[i]*255.f);
00237                                 plane_geometry[i].color[0] = value;
00238                                 plane_geometry[i].color[1] = value;
00239                                 plane_geometry[i].color[2] = value;
00240                                 plane_geometry[i].color[3] = 255;
00241                         }
00242 
00243                         std::cout << "done\n";
00244                 }
00245 
00246                 faogenlib::shutdown();
00247                 return true;
00248         }
00249         return false;
00250 }
00251 
00252 void build_plane()
00253 {
00254         plane_vertex * v = plane_geometry;
00255         for( int j = 0; j < plane_size; j++ )
00256         {
00257                 float y = ( float(j)/float(plane_size-1) - 0.5f )*10.f;
00258                 for( int i = 0; i < plane_size; i++ )
00259                 {
00260                         float x = ( float(i)/float(plane_size-1) - 0.5f )*10.f;
00261                         v->pos[0] = x;
00262                         v->pos[1] = y;
00263                         v->pos[2] = 0.f;
00264                         v->normal[0] = 0.f;
00265                         v->normal[1] = 0.f;
00266                         v->normal[2] = 1.f;
00267                         v->color[0] = 0;
00268                         v->color[1] = 0;
00269                         v->color[2] = 0;
00270                         v->color[3] = 0;
00271                         v++;
00272                 }
00273         }
00274 
00275         unsigned short * idx = plane_indices;
00276         for( int j = 0; j < plane_size-1; j++ )
00277         {
00278                 for( int i = 0; i < plane_size-1; i++ )
00279                 {
00280                         *idx++ = (unsigned short)(j*plane_size + i);
00281                         *idx++ = (unsigned short)((j+1)*plane_size + i+1);
00282                         *idx++ = (unsigned short)((j+1)*plane_size + i);
00283                         *idx++ = (unsigned short)(j*plane_size + i);
00284                         *idx++ = (unsigned short)(j*plane_size + i+1);
00285                         *idx++ = (unsigned short)((j+1)*plane_size + i+1);
00286                 }
00287         }
00288 }
00289 
00290 void reshape( int w, int h )
00291 {
00292         glViewport( 0, 0, w, h );
00293         glMatrixMode( GL_PROJECTION );
00294         glLoadIdentity();
00295         gluPerspective( 60., double(w)/double(h), 0.5, 100. );
00296         glMatrixMode( GL_MODELVIEW );
00297 }
00298 
00299 void key( unsigned char key, int x, int y )
00300 {
00301         switch( key )
00302         {
00303         case '\033':    // Escape
00304                 glDeleteTextures( 1, &cube_texture );
00305                 std::exit( 0 );
00306                 break;
00307         }
00308 }
00309 
00310 void idle()
00311 {
00312         static float angle = 0.f;
00313         static float t = 0.f;
00314         t += 0.001f;
00315         if( t > 2.f*3.14159265358979323846f )
00316                 t = 0.f;
00317 
00318         angle += 0.01f;
00319         if( angle >= 360.f )
00320                 angle = 0.f;
00321         glLoadIdentity();
00322         glTranslatef( 0.f, 0.f, -8.f );
00323         glRotatef( (1.f + sinf( t ))*0.5f*-45.f-45.f, 1.f, 0.f, 0.f );
00324         glRotatef( angle, 0.f, 0.f, 1.f );
00325 
00326         glutPostRedisplay();
00327 }
00328 
00329 void display()
00330 {
00331         glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
00332 
00333         glEnableClientState( GL_VERTEX_ARRAY );
00334         glVertexPointer( 3, GL_FLOAT, sizeof(plane_vertex), &plane_geometry[0].pos[0] );
00335 
00336         glEnableClientState( GL_COLOR_ARRAY );
00337         glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(plane_vertex), &plane_geometry[0].color[0] );
00338 
00339         glDrawElements( GL_TRIANGLES, 
00340                 sizeof(plane_indices)/sizeof(plane_indices[0]), GL_UNSIGNED_SHORT, plane_indices );
00341 
00342         glDisableClientState( GL_COLOR_ARRAY );
00343 
00344         glColor3f( 1.f, 1.f, 1.f );
00345 
00346         glEnable( GL_TEXTURE_2D );
00347         glPushMatrix();
00348         glMultMatrixf( cube_matrix );
00349 
00350         glVertexPointer( 3, GL_FLOAT, 3*sizeof(float), cube_vertices );
00351         glEnableClientState( GL_TEXTURE_COORD_ARRAY );
00352         glTexCoordPointer( 2, GL_FLOAT, 2*sizeof(float), cube_texcoords );
00353         glDrawElements( GL_TRIANGLES, 
00354                 sizeof(cube_indices)/sizeof(cube_indices[0]), GL_UNSIGNED_SHORT, cube_indices );
00355         glDisableClientState( GL_TEXTURE_COORD_ARRAY );
00356 
00357         glPopMatrix();
00358         glDisable( GL_TEXTURE_2D );
00359 
00360         glVertexPointer( 3, GL_FLOAT, 3*sizeof(float), bottom_occludder );
00361         glDrawElements( GL_TRIANGLES, 
00362                 sizeof(bottom_indices)/sizeof(bottom_indices[0]), GL_UNSIGNED_SHORT, bottom_indices );
00363 
00364         glDisableClientState( GL_VERTEX_ARRAY );
00365 
00366         glutSwapBuffers();
00367 }
00368 
00369 int main( int argc, char * argv[] )
00370 {
00371         glutInit( &argc, argv );
00372         glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB );
00373         glutCreateWindow( "faogenlib example" );
00374         glutReshapeWindow( 800, 600 );
00375         glutReshapeFunc( reshape );
00376         glutKeyboardFunc( key );
00377         
00378         build_plane();
00379         if( !generate_example() )
00380                 return 1;
00381 
00382         glClearColor( 0.5f, 0.5f, 0.6f, 1.f );
00383         glMatrixMode( GL_MODELVIEW );
00384 
00385         glEnable( GL_CULL_FACE );
00386         glEnable( GL_DEPTH_TEST );
00387 
00388         glutIdleFunc(idle);
00389         glutDisplayFunc(display);
00390         glutMainLoop(); 
00391 
00392         return 0;
00393 }

Generated on Fri Aug 15 20:07:46 2008 for faogenlib by  doxygen 1.4.6-NO