|
Fluid Dynamics Library
|
00001 #include <iostream> 00002 #include <algorithm> 00003 #include <iterator> 00004 #include <stdexcept> 00005 #include <assert.h> 00006 #include <math.h> 00007 00008 #include <boost/bind.hpp> 00009 #include <boost/thread.hpp> 00010 #include <boost/signal.hpp> 00011 00012 #include "logger/logger.h" 00013 #include "render/drawable.h" 00014 #include "render/glutapp.h" 00015 00016 00017 namespace fdl { 00018 00019 static GlutApp* self; 00020 00025 GlutApp::GlutApp(std::string title, int width, int height) : m_windowTitle(title), m_width(width), m_height(height) 00026 { 00027 die = 0; 00028 m_button1Down = false; 00029 m_button2Down = false; 00030 m_lastX = 0; 00031 m_lastY = 0; 00032 m_running = false; 00033 00034 m_camEye = new cg::vecmath::Vector3f(5.0, 3.0, -3.0); 00035 m_camTarget = new cg::vecmath::Vector3f(5.0, 3.0, -3.0); 00036 cg::vecmath::Vector3f up(0.0, 1.0, 0.0); 00037 m_camera = new Camera(*m_camEye, *m_camTarget, up, 30, 0.1, 1000.0); 00038 00039 m_lastMousePt = new cg::vecmath::Point2f(); 00040 m_currMousePt = new cg::vecmath::Point2f(); 00041 00042 m_currentFrame = 0; 00043 00044 self = this; 00045 00046 marcher = new fdl::CubeMarcher(); 00047 00048 headsUpDisplay = new fdl::HUD(); 00049 00051 //m.run(m.parseRawFile("aneurism.raw", 256, 256, 256), 256, 256, 256, 1, 1, 1, 37.0); 00052 } 00053 00058 GlutApp::~GlutApp() 00059 { 00060 m_running = false; 00061 } 00062 00067 int GlutApp::run() 00068 { 00069 if(!m_running){ 00070 //t1 = new boost::thread(boost::bind(&GlutApp::start,this)); 00071 //t1->join(); 00072 m_running = true; 00073 this->init(); 00074 } 00075 00076 return 0; 00077 } 00078 00079 00080 void GlutApp::setGrid(fdl::Drawable* grid) 00081 { 00082 m_grid = grid; 00083 } 00084 00085 void GlutApp::setSolver(fdl::Drawable* solver) 00086 { 00087 m_solver = solver; 00088 } 00089 00090 int GlutApp::render() 00091 { 00092 m_currentFrame++; 00093 00094 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00095 glMatrixMode(GL_MODELVIEW); 00096 glLoadIdentity(); 00097 00098 renderCamera(); 00099 00100 headsUpDisplay->draw(); 00101 00102 int limit = 1000; 00103 //glutSolidTeapot(1.0); // test 00104 //if(m_currentFrame < limit) 00105 00106 if(m_grid!=NULL) m_grid->draw(); 00107 if(m_solver!=NULL) m_solver->draw(); 00108 00109 /* 00110 fdl::CubeMarcher m; 00111 m.run(*(fdl::Grid*)m_grid, 0.5); 00112 m.draw(); 00113 */ 00114 /* 00115 if(m_currentFrame == limit){ 00116 marcher->run(*(fdl::Grid*)m_grid, 0.5); 00117 } 00118 00119 marcher->draw(); 00120 */ 00121 /* 00122 glMatrixMode(GL_MODELVIEW); 00123 glLoadIdentity(); 00124 glPushMatrix(); 00125 glScalef(0.1f, 0.1f, 0.1f); 00126 m.draw(); 00127 glPopMatrix(); 00128 */ 00129 00130 // swap drawing buffers 00131 glutSwapBuffers(); 00132 00133 return 0; 00134 } 00135 00136 void GlutApp::windowToViewport(cg::vecmath::Point2f& p) const 00137 { 00138 p.set((2.0f * p.x - m_width) / m_width, (2.0f * (m_height - p.y - 1.0) - m_height) / m_height); 00139 } 00140 00141 void GlutApp::renderCamera() 00142 { 00143 // DEBUG() << "m_camera.eye = " << m_camera->getEye(); 00144 // DEBUG() << "m_camera.target = " << m_camera->getTarget(); 00145 m_camera->updateMatrices(); 00146 glMatrixMode(GL_PROJECTION); 00147 glLoadIdentity(); 00148 float proj[16]; 00149 float view[16]; 00150 m_camera->getProjectionMatrix().toArray(proj); 00151 m_camera->getViewMatrix().toArray(view); 00152 glMultMatrixf(proj); 00153 glMultMatrixf(view); 00154 glMatrixMode(GL_MODELVIEW); 00155 glLoadIdentity(); 00156 // rotationGizmo.setCamera(mainCamera); 00157 } 00158 00159 int GlutApp::resize(int width, int height) 00160 { 00161 m_width = width; 00162 m_height = height; 00163 00164 glLoadIdentity(); 00165 float aspect = m_width / m_height; 00166 glViewport(0, 0, m_width, m_height); 00167 m_camera->setAspect(aspect); 00168 00169 // glMatrixMode(GL_PROJECTION); 00170 // glLoadIdentity(); 00171 // float aspect = 1.0;//m_width / m_height; 00172 // gluPerspective(60, aspect, 1.0, 100.0); 00173 // glOrtho(0, m_width, m_height, 0, -1.0f, 1.0f); 00174 // glMatrixMode(GL_MODELVIEW); 00175 // m_camera->setAspect(aspect); 00176 00177 return 0; 00178 } 00179 00180 int GlutApp::keyPressed(unsigned char key) 00181 { 00182 // for escape key ... 00183 if (key == 27) { 00184 die = 1; 00185 glutDestroyWindow(window); 00186 }else{ 00187 std::cout << "key pressed: " << key << std::endl; 00188 } 00189 00190 return 0; 00191 } 00192 00193 int GlutApp::keyReleased(unsigned char key) 00194 { 00195 return 0; 00196 } 00197 00198 int GlutApp::mousePressed(int button, int x, int y) 00199 { 00200 // std::cout << "mouse pressed: " << x << ", " << y << std::endl; 00201 m_lastMousePt->set(x, y); 00202 windowToViewport(*m_lastMousePt); 00203 m_button1Down = true; 00204 00205 return 0; 00206 } 00207 00208 int GlutApp::mouseReleased(int button, int x, int y) 00209 { 00210 // std::cout << "mouse released: " << x << ", " << y << std::endl; 00211 m_button1Down = false; 00212 00213 return 0; 00214 } 00215 00216 int GlutApp::mouseMoved(int x, int y) 00217 { 00218 // std::cout << "mouse moved: " << x << ", " << y << std::endl; 00219 m_currMousePt->set(x, y); 00220 windowToViewport(*m_currMousePt); 00221 m_camera->orbit(*m_lastMousePt, *m_currMousePt); 00222 *m_lastMousePt = *m_currMousePt; 00223 00224 return 0; 00225 } 00226 00231 void GlutApp::render_cb() 00232 { 00233 self->render(); 00234 } 00235 00236 00241 void GlutApp::resize_cb(int width, int height) 00242 { 00243 self->resize(width,height); 00244 } 00245 00250 void GlutApp::keyPressed_cb(unsigned char key, int x, int y) 00251 { 00252 self->keyPressed(key); 00253 } 00254 00255 00260 void GlutApp::mouse_cb(int button, int state, int x, int y) 00261 { 00262 if (state == GLUT_DOWN && button == GLUT_LEFT_BUTTON) { 00263 self->m_button1Down = true; 00264 self->m_lastX = x; 00265 self->m_lastY = y; 00266 self->mousePressed(button, x, y); 00267 } 00268 else if (state == GLUT_UP && button == GLUT_LEFT_BUTTON) { 00269 self->m_button1Down = false; 00270 self->mouseReleased(button, x, y); 00271 } 00272 else if (state == GLUT_DOWN && button == GLUT_RIGHT_BUTTON) { 00273 self->m_button2Down = true; 00274 self->m_lastX = x; 00275 self->m_lastY = y; 00276 self->mousePressed(button, x, y); 00277 } 00278 else if (state == GLUT_UP && button == GLUT_RIGHT_BUTTON) { 00279 self->m_button2Down = false; 00280 self->mouseReleased(button, x, y); 00281 } 00282 } 00283 00284 00289 void GlutApp::motion_cb(int x, int y) 00290 { 00291 if (self->m_button1Down) { 00292 double dx = (x - self->m_lastX)/2.0f; 00293 double dy = (y - self->m_lastY)/2.0f; 00294 self->m_lastX = x; 00295 self->m_lastY = y; 00296 glutPostRedisplay(); 00297 } 00298 else if (self->m_button2Down) { 00299 // double dy = (y - lastY)/10.0f; 00300 // m_distZ -= dy; 00301 // m_lastY = y; 00302 // glutPostRedisplay(); 00303 } 00304 00305 self->mouseMoved(x, y); 00306 } 00307 00308 00313 void GlutApp::init() 00314 { 00315 m_mutex.lock(); 00316 00317 int argc = 1; 00318 char* argv = (char*) m_windowTitle.c_str(); 00319 00320 glutInit(&argc, &argv); 00321 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH); 00322 glutInitWindowSize(self->m_width, self->m_height); 00323 glutInitWindowPosition(0, 0); 00324 00325 // create X11 window 00326 window = glutCreateWindow(m_windowTitle.c_str()); 00327 00328 // pass callback handler function pointers to GLUT 00329 glutDisplayFunc(&render_cb); 00330 glutIdleFunc(&render_cb); 00331 glutReshapeFunc(&resize_cb); 00332 glutKeyboardFunc(&keyPressed_cb); 00333 glutMouseFunc(&mouse_cb); 00334 glutMotionFunc(&motion_cb); 00335 00336 // init GL 00337 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 00338 glClearDepth(1.0); 00339 00340 glEnable(GL_LIGHTING); 00341 glEnable(GL_LIGHT0); 00342 glDepthFunc(GL_LESS); 00343 glEnable(GL_DEPTH_TEST); 00344 glEnable(GL_BLEND); 00345 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 00346 glShadeModel(GL_SMOOTH); 00347 00348 // begin loop 00349 glutMainLoop(); 00350 00351 m_running = false; 00352 00353 m_mutex.unlock(); 00354 } 00355 00356 void GlutApp::setEye(float x, float y, float z) 00357 { 00358 m_camEye->set(x,y,z); 00359 m_camera->setEye(*m_camEye); 00360 m_camera->updateMatrices(); 00361 } 00362 00363 void GlutApp::setTarget(float x, float y, float z) 00364 { 00365 m_camTarget->set(x,y,z); 00366 m_camera->setTarget(*m_camTarget); 00367 m_camera->updateMatrices(); 00368 } 00369 00370 }
1.7.4