00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <SDL.h>
00042 #include <GL/glew.h>
00043 #include <vector>
00044
00045
00046 #include <vcg/simplex/vertex/base.h>
00047
00048
00049 #include <vcg/simplex/face/base.h>
00050
00051
00052 #include <vcg/complex/trimesh/base.h>
00053
00054
00055 #include <vcg/complex/trimesh/update/bounding.h>
00056 #include <vcg/complex/trimesh/update/normal.h>
00057
00058
00059 #include <wrap/io_trimesh/import.h>
00060
00061
00062 #include <wrap/gl/trimesh.h>
00063
00064
00065
00066
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
00079
00080
00081 class CVertex : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::Normal3f >{};
00082
00083
00084 class CFace : public Face< MyUsedTypes, face::VertexRef, face::Normal3f > {};
00085
00086
00087 class CMesh : public vcg::tri::TriMesh< vector<CVertex>, vector<CFace> > {};
00088
00089
00091
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
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(false);
00154 glPushMatrix();
00155 float d=1.0f/mesh.bbox.Diag();
00156 glScale(d);
00157 glTranslate(-glWrap.m->bbox.Center());
00158
00159
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
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
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
00232 vcg::tri::UpdateBounding<CMesh>::Box(mesh);
00233
00234 vcg::tri::UpdateNormals<CMesh>::PerVertexPerFace(mesh);
00235
00236
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