trimesh_sdl.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002  * VCGLib                                                            o o     *
00003  * Visual and Computer Graphics Library                            o     o   *
00004  *                                                                _   O  _   *
00005  * Copyright(C) 2004                                                \/)\/    *
00006  * Visual Computing Lab                                            /\/|      *
00007  * ISTI - Italian National Research Council                           |      *
00008  *                                                                    \      *
00009  * All rights reserved.                                                      *
00010  *                                                                           *
00011  * This program is free software; you can redistribute it and/or modify      *   
00012  * it under the terms of the GNU General Public License as published by      *
00013  * the Free Software Foundation; either version 2 of the License, or         *
00014  * (at your option) any later version.                                       *
00015  *                                                                           *
00016  * This program is distributed in the hope that it will be useful,           *
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019  * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020  * for more details.                                                         *
00021  *                                                                           *
00022  ****************************************************************************/
00023 /****************************************************************************
00024   History
00025 
00026 $Log: not supported by cvs2svn $
00027 Revision 1.4  2007/03/17 13:22:55  ganovelli
00028 some more comment added
00029 
00030 Revision 1.3  2006/11/21 17:44:06  cignoni
00031 Update to the latest version of the trackball
00032 
00033 Revision 1.2  2005/11/22 17:50:15  cignoni
00034 Refactored the sample code.
00035 Shortened a lot and removed all unused unnecessary stuff
00036 
00037 Revision 1.1  2005/09/21 10:29:33  cignoni
00038 Initial Relase
00039 
00040 ****************************************************************************/
00041 #include <SDL.h>
00042 #include <GL/glew.h>
00043 #include <vector>
00044 
00045 /*include the base definition for the vertex */
00046 #include <vcg/simplex/vertex/base.h>
00047 
00048 /*include the base definition for the face */
00049 #include <vcg/simplex/face/base.h>
00050 
00051 /*include the base definition for the trimesh*/
00052 #include <vcg/complex/complex.h>
00053 
00054 /*include the algorihm that update bounding box and normals*/
00055 #include <vcg/complex/algorithms/update/bounding.h>
00056 #include <vcg/complex/algorithms/update/normal.h>
00057 
00058 /*include the importer from disk*/
00059 #include <wrap/io_trimesh/import.h>
00060 
00061 /*include wrapping of the trimesh towards opengl*/
00062 #include <wrap/gl/trimesh.h>
00063 
00064 /*include the trackball: NOTE, you the implementation of the trackball is found in the files:
00065 wrap/gui/trackball.cpp and wrap/gui/trackmode.cpp. You should include these files in your solution
00066 otherwise you'll get linking errors */
00067 #include <wrap/gui/trackball.h>
00068 
00069 using namespace vcg;
00070 using namespace std;
00071 
00072 
00073 class CFace;
00074 class CVertex;
00075 struct MyUsedTypes : public UsedTypes<  Use<CVertex>            ::AsVertexType,
00076                                                                                                                                                                 Use<CFace>                      ::AsFaceType>{};
00077 
00078 /* define a vertex passing the attributes you want it to have. Each attributes has its own class.
00079 Check vcg/simplex/vertex/component.h to find out the existing attributes. Note: then you could 
00080 also personalized attributes */
00081 class CVertex : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::Normal3f >{};
00082 
00083 /*same as for the vertes */ 
00084 class CFace   : public Face<   MyUsedTypes, face::VertexRef, face::Normal3f > {};
00085 
00086 /*the mesh is a container of vertices and a container of faces */ 
00087 class CMesh   : public vcg::tri::TriMesh< vector<CVertex>, vector<CFace> > {};
00088 
00089 
00091 // Globals: the mesh, the OpenGL wrapper to draw the mesh and the trackball.
00092 CMesh mesh;
00093 vcg::GlTrimesh<CMesh> glWrap;
00094 vcg::Trackball track;
00095 int drawMode;
00096 int width =1024;
00097 int height = 768;
00098 
00100 
00101 
00102 void initGL()
00103 {
00104   glClearColor(0, 0, 0, 0); 
00105   glEnable(GL_LIGHTING);
00106   glEnable(GL_LIGHT0);
00107   glEnable(GL_NORMALIZE);
00108   glEnable(GL_COLOR_MATERIAL);
00109   glEnable(GL_CULL_FACE);
00110   glEnable(GL_DEPTH_TEST);
00111 }
00112 
00113 
00114 void myReshapeFunc(GLsizei w, GLsizei h)
00115 {
00116   SDL_SetVideoMode(w, h, 0, SDL_OPENGL|SDL_RESIZABLE|SDL_DOUBLEBUF);
00117         width=w;
00118   height=h;
00119   glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
00120   initGL();
00121 }
00122 
00123 bool initSDL(const std::string &str) {
00124   /* Initialize SDL for video output */
00125   if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
00126     fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
00127     exit(1);
00128   }
00129   if ( SDL_SetVideoMode(width, height, 0, SDL_OPENGL|SDL_RESIZABLE) == NULL ) {
00130     fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError());
00131     SDL_Quit();
00132     exit(2);
00133   }
00134   
00135   SDL_WM_SetCaption(str.c_str(), str.c_str());  
00136   myReshapeFunc(width, height);
00137   return true;
00138 }
00139 
00140 void display(){
00141     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00142     glMatrixMode(GL_PROJECTION);
00143     glLoadIdentity();
00144     gluPerspective(40, width/(float)height, 0.1, 100);
00145     glMatrixMode(GL_MODELVIEW);
00146     glLoadIdentity();
00147     gluLookAt(0,0,5,   0,0,0,   0,1,0);    
00148 
00149     track.center=Point3f(0, 0, 0);
00150     track.radius= 1;
00151 
00152                 track.GetView();
00153         track.Apply();
00154     glPushMatrix();
00155     float d=1.0f/mesh.bbox.Diag();
00156     glScale(d);
00157                 glTranslate(-glWrap.m->bbox.Center());  
00158 
00159                 // the trimesh drawing calls
00160                 switch(drawMode)
00161                 {
00162                   case 0: glWrap.Draw<vcg::GLW::DMSmooth,   vcg::GLW::CMNone,vcg::GLW::TMNone> ();break;
00163                   case 1: glWrap.Draw<vcg::GLW::DMPoints,   vcg::GLW::CMNone,vcg::GLW::TMNone> ();break;
00164                   case 2: glWrap.Draw<vcg::GLW::DMWire,     vcg::GLW::CMNone,vcg::GLW::TMNone> ();break;
00165                   case 3: glWrap.Draw<vcg::GLW::DMFlatWire, vcg::GLW::CMNone,vcg::GLW::TMNone> ();break;
00166                   case 4: glWrap.Draw<vcg::GLW::DMHidden,   vcg::GLW::CMNone,vcg::GLW::TMNone> ();break;
00167                   case 5: glWrap.Draw<vcg::GLW::DMFlat,     vcg::GLW::CMNone,vcg::GLW::TMNone> ();break;
00168                   default: break;
00169                 }
00170     glPopMatrix();
00171     track.DrawPostApply();
00172     SDL_GL_SwapBuffers();
00173 }
00174 
00175 // The Event Loop Processor
00176 int sdl_idle() {
00177   bool quit=false;
00178         SDL_Event event;
00179         while( !quit ) {  
00180     SDL_WaitEvent(&event);
00181     switch( event.type ) {
00182       case SDL_QUIT:  quit = true; break; 
00183       case SDL_VIDEORESIZE :                    myReshapeFunc(event.resize.w,event.resize.h);                   break;
00184       case SDL_KEYDOWN:                                        
00185                           switch(event.key.keysym.sym) {
00186                             case SDLK_RCTRL:
00187                             case SDLK_LCTRL: track.ButtonDown(vcg::Trackball::KEY_CTRL); break;
00188                             case SDLK_q: exit(0); break;        
00189                             case SDLK_SPACE: drawMode=((drawMode+1)%6); printf("Current Mode %i\n",drawMode); break;    
00190                           }  break;
00191       case SDL_KEYUP: 
00192                           switch(event.key.keysym.sym) {
00193                             case SDLK_RCTRL:
00194                             case SDLK_LCTRL: track.ButtonUp(vcg::Trackball::KEY_CTRL); break;
00195                           }     break;
00196       case SDL_MOUSEBUTTONDOWN:   
00197               switch(event.button.button) {
00198           case SDL_BUTTON_WHEELUP:    track.MouseWheel( 1); break;
00199           case SDL_BUTTON_WHEELDOWN:  track.MouseWheel(-1); break;
00200           case SDL_BUTTON_LEFT:       track.MouseDown(event.button.x, (height - event.button.y), vcg::Trackball::BUTTON_LEFT); break;
00201           case SDL_BUTTON_RIGHT:            track.MouseDown(event.button.x, (height - event.button.y), vcg::Trackball::BUTTON_RIGHT);break;
00202         } break;
00203       case SDL_MOUSEBUTTONUP:          
00204               switch(event.button.button) {
00205           case SDL_BUTTON_LEFT:       track.MouseUp(event.button.x, (height - event.button.y), vcg::Trackball::BUTTON_LEFT); break;
00206           case SDL_BUTTON_RIGHT:            track.MouseUp(event.button.x, (height - event.button.y), vcg::Trackball::BUTTON_RIGHT);break;
00207         } break;
00208       case SDL_MOUSEMOTION: 
00209               while(SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_MOUSEMOTIONMASK));
00210               track.MouseMove(event.button.x, (height - event.button.y));
00211               break;  
00212       case SDL_VIDEOEXPOSE:
00213       default: break;
00214       }
00215                 display();
00216         }
00217 
00218   SDL_Quit();
00219   return -1;
00220 }
00221 
00222 
00223 
00224 int main(int argc, char *argv[]) {      
00225         // Generic loading of the mesh from disk
00226         if(vcg::tri::io::Importer<CMesh>::Open(mesh,argv[1])!=0) {
00227       fprintf(stderr,"Error reading file %s\n",argv[1]);
00228                         exit(0);
00229                 }
00230 
00231   // Initialize the mesh itself
00232         vcg::tri::UpdateBounding<CMesh>::Box(mesh);      // update bounding box
00233   //vcg::tri::UpdateNormals<CMesh>::PerVertexNormalizePerFaceNormalized(mesh); // update Normals
00234   vcg::tri::UpdateNormals<CMesh>::PerVertexPerFace(mesh); // update Normals
00235         
00236         // Initialize the wrapper
00237   glWrap.m = &mesh;
00238   glWrap.Update();
00239         
00240   initSDL("SDL_minimal_viewer");
00241   initGL();
00242   sdl_idle();
00243         exit(0);
00244 }
00245 
00246 
00247 


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:38:25