00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00033 #include <AntTweakBar.h>
00034 #include <GL/glew.h>
00035 #include <GL/freeglut.h>
00036 #include <stdio.h>
00037
00039 #include <vcg/simplex/vertex/base.h>
00040 #include <vcg/simplex/face/base.h>
00041 #include <vcg/complex/complex.h>
00042 #include <vcg/complex/algorithms/update/bounding.h>
00043 #include <vcg/complex/algorithms/update/normal.h>
00044 #include <vcg/complex/algorithms/create/platonic.h>
00045
00047 #include <wrap/io_trimesh/import.h>
00048 #include <wrap/gl/trimesh.h>
00049 #include <wrap/gui/trackball.h>
00050
00052
00053 using namespace vcg;
00054 class CFace;
00055 class CVertex;
00056 struct MyUsedTypes : public UsedTypes< Use<CVertex> ::AsVertexType,
00057 Use<CFace> ::AsFaceType>{};
00058
00060 class CVertex : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::Normal3f, vcg::vertex::BitFlags>{};
00061 class CFace : public vcg::Face< MyUsedTypes, vcg::face::VertexRef, vcg::face::Normal3f, vcg::face::BitFlags > {};
00062 class CMesh : public vcg::tri::TriMesh< std::vector<CVertex>, std::vector<CFace> > {};
00063
00065 CMesh mesh;
00066
00068 char * filename = NULL;
00069
00071 vcg::GlTrimesh<CMesh> glWrap;
00073 vcg::Trackball track;
00074
00076 int width,height;
00077
00079 enum DrawMode{SMOOTH=0,PERPOINTS,WIRE,FLATWIRE,HIDDEN,FLAT};
00080
00082 DrawMode drawmode;
00083
00085 static vcg::Trackball::Button GLUT2VCG (int glut_button, int )
00086 {
00087 int vcgbt = vcg::Trackball::BUTTON_NONE;
00088
00089 switch(glut_button){
00090 case GLUT_LEFT_BUTTON: vcgbt |= vcg::Trackball::BUTTON_LEFT; break;
00091 case GLUT_MIDDLE_BUTTON: vcgbt |= vcg::Trackball::BUTTON_RIGHT; break;
00092 case GLUT_RIGHT_BUTTON: vcgbt |= vcg::Trackball::BUTTON_MIDDLE; break;
00093 }
00094
00095 int modifiers = glutGetModifiers();
00096
00097 if (modifiers & GLUT_ACTIVE_SHIFT) vcgbt |= vcg::Trackball::KEY_SHIFT;
00098 if (modifiers & GLUT_ACTIVE_CTRL) vcgbt |= vcg::Trackball::KEY_CTRL;
00099 if (modifiers & GLUT_ACTIVE_ALT) vcgbt |= vcg::Trackball::KEY_ALT;
00100
00101 return vcg::Trackball::Button (vcgbt);
00102 }
00103
00104
00105
00106
00107
00108
00109 void Display(){
00110 glViewport(0, 0, width, height);
00111 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00112
00113 if(!mesh.face.empty()){
00114 glMatrixMode(GL_PROJECTION);
00115 glLoadIdentity();
00116 gluPerspective(40, width /(float) height , 0.1, 100);
00117 glMatrixMode(GL_MODELVIEW);
00118 glLoadIdentity();
00119 gluLookAt(0,0,5, 0,0,0, 0,1,0);
00120 track.center=vcg::Point3f(0, 0, 0);
00121 track.radius= 1;
00122 track.GetView();
00123 track.Apply();
00124 glPushMatrix();
00125 float d=1.0f/mesh.bbox.Diag();
00126 vcg::glScale(d);
00127 glTranslate(-glWrap.m->bbox.Center());
00128
00129 switch(drawmode)
00130 {
00131 case SMOOTH:
00132 glWrap.Draw<vcg::GLW::DMSmooth, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
00133 break;
00134 case PERPOINTS:
00135 glWrap.Draw<vcg::GLW::DMPoints, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
00136 break;
00137 case WIRE:
00138 glWrap.Draw<vcg::GLW::DMWire, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
00139 break;
00140 case FLATWIRE:
00141 glWrap.Draw<vcg::GLW::DMFlatWire, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
00142 break;
00143 case HIDDEN:
00144 glWrap.Draw<vcg::GLW::DMHidden, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
00145 break;
00146 case FLAT:
00147 glWrap.Draw<vcg::GLW::DMFlat, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
00148 break;
00149 default:
00150 break;
00151 }
00152
00153 glPopMatrix();
00154 track.DrawPostApply();
00155 }
00156
00157
00158 TwDraw();
00159
00160
00161 glutSwapBuffers();
00162
00163
00164 glutPostRedisplay();
00165 }
00166
00167
00168 void Reshape(int _width,int _height){
00169 width = _width ;
00170 height = _height;
00171 TwWindowSize(width, height);
00172 }
00173 void Terminate(){}
00174
00175 void initMesh()
00176 {
00177
00178 vcg::tri::UpdateBounding<CMesh>::Box(mesh);
00179
00180 vcg::tri::UpdateNormals<CMesh>::PerVertexNormalizedPerFace(mesh);
00181 vcg::tri::UpdateNormals<CMesh>::PerFaceNormalized(mesh);
00182
00183 glWrap.m = &mesh;
00184 glWrap.Update();
00185 }
00186
00187 void TW_CALL loadMesh(void *)
00188 {
00189 if(filename==0) return;
00190 int err=vcg::tri::io::ImporterPLY<CMesh>::Open(mesh,(char*)filename);
00191 if(err!=0){
00192 const char* errmsg=vcg::tri::io::ImporterPLY<CMesh>::ErrorMsg(err);
00193 }
00194 else
00195 initMesh();
00196 }
00197
00198 void TW_CALL loadTetrahedron(void *){
00199 vcg::tri::Tetrahedron(mesh);
00200 initMesh();
00201 }
00202
00203 void TW_CALL loadDodecahedron(void * ){
00204 vcg::tri::Dodecahedron(mesh);
00205 initMesh();
00206 }
00207
00208 void keyReleaseEvent (unsigned char k,int x,int y)
00209 {
00210 int modifiers = glutGetModifiers();
00211 if (modifiers & GLUT_ACTIVE_CTRL)
00212 track.ButtonUp (Trackball::Button::KEY_CTRL);
00213 if (modifiers & GLUT_ACTIVE_SHIFT)
00214 track.ButtonUp ( Trackball::Button::KEY_SHIFT);
00215 if (modifiers & GLUT_ACTIVE_ALT)
00216 track.ButtonUp (Trackball::Button::KEY_ALT);
00217 }
00218 void keyPressEvent (unsigned char k,int x,int y)
00219 {
00220 int modifiers = glutGetModifiers();
00221 if (modifiers & GLUT_ACTIVE_CTRL)
00222 track.ButtonDown (Trackball::Button::KEY_CTRL);
00223 if (modifiers & GLUT_ACTIVE_SHIFT)
00224 track.ButtonDown ( Trackball::Button::KEY_SHIFT);
00225 if (modifiers & GLUT_ACTIVE_ALT)
00226 track.ButtonDown (Trackball::Button::KEY_ALT);
00227
00228 TwEventKeyboardGLUT(k,x,y);
00229 }
00230
00231 void mousePressEvent(int bt,int state,int x,int y){
00232 if(state == GLUT_DOWN)
00233 track.MouseDown ( x , height - y , GLUT2VCG (bt,state));
00234 else
00235 track.MouseUp ( x , height - y , GLUT2VCG (bt,state));
00236
00237 TwEventMouseButtonGLUT(bt,state,x,y);
00238
00239 };
00240
00241 void mouseMoveEvent (int x, int y )
00242 {
00243 if(!TwEventMouseMotionGLUT(x,y))
00244 track.MouseMove ( x , height - y );
00245 }
00246
00247
00248 void wheelEvent(int wheel, int direction, int x, int y ){
00249 track.MouseWheel(wheel*direction);
00250 }
00251
00252
00253 void TW_CALL CopyCDStringToClient(char **destPtr, const char *src)
00254 {
00255 size_t srcLen = (src!=NULL) ? strlen(src) : 0;
00256 size_t destLen = (*destPtr!=NULL) ? strlen(*destPtr) : 0;
00257
00258
00259 if( *destPtr==NULL )
00260 *destPtr = (char *)malloc(srcLen+1);
00261 else if( srcLen>destLen )
00262 *destPtr = (char *)realloc(*destPtr, srcLen+1);
00263
00264
00265 if( srcLen>0 )
00266 strncpy(*destPtr, src, srcLen);
00267 (*destPtr)[srcLen] = '\0';
00268 }
00269
00270 int main(int argc, char *argv[])
00271 {
00272
00273 TwBar *bar;
00274
00275
00276
00277 if( !TwInit(TW_OPENGL, NULL) )
00278 {
00279
00280 fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
00281 return 1;
00282 }
00283
00284 glutInit(&argc, argv);
00285 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
00286 glutInitWindowSize(640, 480);
00287 glutCreateWindow("AntTweakBar simple example using GLUT");
00288 glutCreateMenu(NULL);
00289
00290 glEnable(GL_DEPTH_TEST);
00291 glEnable(GL_LIGHT0);
00292 glEnable(GL_LIGHTING);
00293
00294
00295 glutDisplayFunc(Display);
00296 glutReshapeFunc(Reshape);
00297 atexit(Terminate);
00298
00299
00300
00301 glutMouseFunc((GLUTmousebuttonfun)mousePressEvent);
00302
00303 glutMotionFunc((GLUTmousemotionfun)mouseMoveEvent);
00304
00305 glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
00306
00307 glutKeyboardFunc((GLUTkeyboardfun)TwEventKeyboardGLUT);
00308
00309 glutSpecialFunc((GLUTspecialfun)TwEventSpecialGLUT);
00310
00311 glutKeyboardFunc(keyPressEvent);
00312 glutKeyboardUpFunc(keyReleaseEvent);
00313
00314
00315 glutMouseWheelFunc(wheelEvent);
00316 bar = TwNewBar("TweakBar");
00317
00318 TwCopyCDStringToClientFunc (CopyCDStringToClient);
00319
00320 TwAddVarRW(bar,"Input",TW_TYPE_CDSTRING,&filename," label='Filepath' group=SetMesh help=` Name of the file to load` ");
00321 TwAddButton(bar,"Load from file",loadMesh,0, " label='Load Mesh' group=SetMesh help=`load the mesh` ");
00322 TwAddButton(bar,"Use tetrahedron",loadTetrahedron,0, " label='Make Tetrahedron' group=SetMesh help=`use tetrahedron.` ");
00323 TwAddButton(bar,"Use dodecahedron",loadDodecahedron,0, " label='Make Dodecahedron' group=SetMesh help=`use dodecahedron.` ");
00324
00325
00326
00327 TwEnumVal drawmodes[6] = { {SMOOTH, "Smooth"}, {PERPOINTS, "Per Points"}, {WIRE, "Wire"}, {FLATWIRE, "FlatWire"},{HIDDEN, "Hidden"},{FLAT, "Flat"}};
00328
00329 TwType drawMode = TwDefineEnum("DrawMode", drawmodes, 6);
00330
00331 TwAddVarRW(bar, "Draw Mode", drawMode, &drawmode, " keyIncr='<' keyDecr='>' help='Change draw mode.' ");
00332
00333 glutMainLoop();
00334 }