fshake3d  0.0.1
FreeformDensity3DSurfaceEditor
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines
main.cpp
Go to the documentation of this file.
00001 #include "opengl.hpp"
00002 
00003 #include "math.hpp"
00004 #include <iostream>
00005 using namespace std;
00006 
00007 #include "ui_View.hpp"
00008 #include "ui_Bin.hpp"
00009 #include "ui_CrossSplitter.hpp"
00010 #include "ui_Frame.hpp"
00011 #include "ui_App.hpp"
00012 #include "ui_fonts.hpp"
00013 #include "ui_SceneView.hpp"
00014 #include "ui_SceneEditor.hpp"
00015 
00016 #include "scene_FunctionMixer.hpp"
00017 
00018 // --- application -----------------------------------------------------------
00019 
00020 #include "options.hpp"
00021 
00022 options_config gConfig;
00023 Frame* pFrame;
00024 Scene* pScene;
00025 SceneEditor* pSceneEditor;
00026 FunctionMixer* pFunctionMixer;
00027 
00028 void init(int windowId)
00029 {
00030   pScene = new Scene();
00031   pFunctionMixer = new FunctionMixer();
00032   pFunctionMixer->setElevationGrid(new ElevationGrid(gConfig.mSize[0],gConfig.mSize[1],gConfig.mXLimits,gConfig.mYLimits));
00033   // pFunctionMixer->setPointSize(0.75);
00034   pFunctionMixer->setPointSize(0.5);
00035   // pFunctionMixer->setXSD(10.0);
00036   // pFunctionMixer->setYSD(10.0);
00037   pScene->addNode(pFunctionMixer);
00038   
00039   pFrame = new Frame(windowId);
00040 
00041   // specifies currently the cross split color
00042   pFrame->setBGColor( Vec3d(0.25,0.25,0.25) );
00043 
00044   pSceneEditor = new SceneEditor(pScene);
00045   pSceneEditor->setViewSize( 60.0 );
00046   pSceneEditor->setFocusPoint(Point3d(50.0,50.0,20.0));
00047 
00048   pFrame->setChildView( pSceneEditor );
00049 }
00050 
00051 
00052 #include "io_fmix.hpp"
00053 
00054 void load(const char* filename)
00055 {
00056   std::ifstream ifs;
00057   ifs.open(filename,std::ios::in);
00058   if ( ifs.is_open() )
00059   {
00060     cout << "loading model from file '" << filename << "' ...";
00061     io_fmix::load(ifs, *pFunctionMixer );
00062     ifs.close();
00063     cout << "done.\n";
00064   }
00065   else
00066   {
00067     cerr << "error: unable to open file '" << filename << "' for reading.\n";
00068   }
00069 }
00070 
00071 void save(const char* filename)
00072 {
00073   std::ofstream ofs;
00074   ofs.open(filename,std::ios::out);
00075   if ( ofs.is_open() )
00076   {
00077     cout << "saving model to file '" << filename << "' ... ";
00078     io_fmix::save(ofs, *pFunctionMixer );
00079     ofs.close();
00080     cout << "done.\n";
00081   }
00082   else
00083   {
00084     cerr << "error: unable to open file '" << filename << "' for writing.\n";
00085   }
00086 }
00087 
00088 #include "dialog.hpp"
00089 
00090 void load()
00091 {
00092   std::string fn;
00093   if ( dialog_load(fn) ) 
00094   {
00095     load(fn.c_str());
00096   }
00097 }
00098 
00099 void save()
00100 {
00101   std::string fn;
00102   if ( dialog_save(fn) ) 
00103   {
00104     save(fn.c_str());
00105   }
00106 }
00107 
00108 void moveLocal(const Vec3d& direction)
00109 {
00110   pSceneEditor->moveLocal(direction);
00111 }
00112 
00113 void clear()
00114 {
00115   if ( dialog_yesno("Do you want to clear ALL hotspots?") )
00116   {
00117     pFunctionMixer->clear();
00118   }
00119 }
00120 
00121 bool gLayerStateRuler = true;
00122 
00123 void toggle(int layerid)
00124 {
00125   if (layerid == 4)
00126   {
00127     gLayerStateRuler = !gLayerStateRuler;
00128     pSceneEditor->setRulersVisible( gLayerStateRuler );
00129   }
00130   else
00131     pFunctionMixer->toggleLayer(layerid);
00132 }
00133 
00134 void remove_hotspot()
00135 {
00136   pFunctionMixer->removeSelected();
00137 }
00138 
00139 void help()
00140 {
00141   static const char* help_text =
00142     "Keys:\n"
00143     "F1\tthis page\n"
00144     "W\tmove camera forward\n"
00145     "S\tmove camera backward\n"
00146     "A\tmove camera left\n"
00147     "D\tmove camera right\n"
00148     "R\tmove camera up\n"
00149     "F\tmove camera down\n"
00150     "SHIFT-S\tsave function mix\n"
00151     "SHIFT-L\tload function mix\n"
00152     "SHIFT-C\tnew function mix\n"
00153     "DELETE\tremove selected function\n"
00154     "1\ttoggle normals layer\n"
00155     "2\ttoggle grid layer\n"
00156     "3\ttoggle control point layer\n"
00157     "4\ttoggle surface layer\n"
00158     "5\ttoggle rulers\n"
00159   ;
00160 
00161   dialog_ok(help_text);
00162 }
00163 
00164 void quit()
00165 {
00166   exit(0);
00167 }
00168 
00169 void keyboard(unsigned char ch, int x, int y)
00170 {
00171   if ( (ch >= '1') && (ch <= '9') )
00172   {
00173     int layer( ch - '1' );
00174     toggle(layer);
00175   }
00176   else
00177   {
00178     switch(ch)
00179     {
00180     case 'w': moveLocal(Vec3d( 0, 0,-1)); break;  
00181     case 's': moveLocal(Vec3d( 0, 0, 1)); break;  
00182     case 'a': moveLocal(Vec3d(-1, 0, 0)); break;
00183     case 'd': moveLocal(Vec3d( 1, 0, 0)); break;
00184     case 'r': moveLocal(Vec3d( 0, 1, 0)); break;
00185     case 'f': moveLocal(Vec3d( 0,-1, 0)); break;
00186     case 'S': save(); break;
00187     case 'L': load(); break;
00188     case 'C': clear(); break;
00189     case 127: remove_hotspot(); break;
00190     case  27: quit(); break;
00191     }
00192   }
00193 }
00194 
00195 void keyboard2(int ch, int x, int y)
00196 {
00197   switch(ch)
00198   {
00199   case GLUT_KEY_F1: help(); break;
00200   }
00201 }
00202 
00203 
00204 void gl_check_error()
00205 {
00206 #if !(defined NDEBUG)
00207   GLenum err = glGetError();
00208   if (err != GL_NO_ERROR)
00209   {
00210     std::cerr << "GL Error: " << gluErrorString(err) << "\n";
00211   }
00212 #endif
00213 }
00214 
00215 // global window height
00216 int gH;
00217 // filter mouse y value 
00218 // begins at left/top in native windowing system
00219 // but begins at left/bottom in OpenGL window system
00220 inline int filter_y(int y)
00221 {
00222   return gH - y;
00223 }
00224 
00225 void display()
00226 {
00227   pFrame->display();
00228   gl_check_error();
00229   glutSwapBuffers();
00230   glutPostRedisplay();
00231 }
00232 
00233 void reshape(int w, int h)
00234 { 
00235   gH = h;
00236   pFrame->reshape(w,h);
00237 }
00238 
00239 void mouse(int button, int state, int x, int y)
00240 {
00241   y = filter_y(y);
00242   pFrame->mouse(button,state,x,y);
00243 }
00244 
00245 void motion(int x, int y)
00246 {
00247   y = filter_y(y);
00248   pFrame->motion(x,y);
00249 }
00250 
00251 int main(int argc, char* argv[])
00252 {
00253   options_parser options(argc, argv);  
00254   options.parse_all(gConfig);
00255   glutInit(&argc, argv);
00256   glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);  
00257   const int width = 800;
00258   const int height = 600;
00259   const int screenWidth = glutGet(GLUT_SCREEN_WIDTH);
00260   const int screenHeight = glutGet(GLUT_SCREEN_HEIGHT);
00261   glutInitWindowPosition( ( screenWidth - width ) / 2, (screenHeight - height ) / 2);
00262   glutInitWindowSize(width,height);
00263   int windowId = glutCreateWindow("F-Shake 3D (Version 0.0.1) - Press F1 for help");
00264   glutSetWindow(windowId);
00265   init(windowId);
00266   glutDisplayFunc(display);
00267   glutReshapeFunc(reshape);
00268   glutMouseFunc(mouse);
00269   glutMotionFunc(motion);
00270   glutKeyboardFunc(keyboard);
00271   glutSpecialFunc(keyboard2);
00272   glutMainLoop();
00273   
00274   return 0; 
00275 }