123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428 |
- #include "Viewer.h"
- #include <igl/get_seconds.h>
- #ifdef _WIN32
- # include <windows.h>
- # undef max
- # undef min
- #endif
- // Todo: windows equivalent for `usleep`
- #include <unistd.h>
- #ifndef __APPLE__
- # define GLEW_STATIC
- # include <GL/glew.h>
- #endif
- #ifdef __APPLE__
- # include <OpenGL/gl3.h>
- # define __gl_h_ /* Prevent inclusion of the old gl.h */
- #else
- # ifdef _WIN32
- # include <windows.h>
- # endif
- # include <GL/gl.h>
- #endif
- #include <Eigen/LU>
- #define GLFW_INCLUDE_GLU
- #include <GLFW/glfw3.h>
- #include <cmath>
- #include <cstdio>
- #include <sstream>
- #include <iomanip>
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- #include <igl/project.h>
- Eigen::Matrix4f lookAt (
- const Eigen::Vector3f& eye,
- const Eigen::Vector3f& center,
- const Eigen::Vector3f& up)
- {
- Eigen::Vector3f f = (center - eye).normalized();
- Eigen::Vector3f s = f.cross(up).normalized();
- Eigen::Vector3f u = s.cross(f);
- Eigen::Matrix4f Result = Eigen::Matrix4f::Identity();
- Result(0,0) = s(0);
- Result(0,1) = s(1);
- Result(0,2) = s(2);
- Result(1,0) = u(0);
- Result(1,1) = u(1);
- Result(1,2) = u(2);
- Result(2,0) =-f(0);
- Result(2,1) =-f(1);
- Result(2,2) =-f(2);
- Result(0,3) =-s.transpose() * eye;
- Result(1,3) =-u.transpose() * eye;
- Result(2,3) = f.transpose() * eye;
- return Result;
- }
- Eigen::Matrix4f ortho (
- const float left,
- const float right,
- const float bottom,
- const float top,
- const float zNear,
- const float zFar
- )
- {
- Eigen::Matrix4f Result = Eigen::Matrix4f::Identity();
- Result(0,0) = 2.0f / (right - left);
- Result(1,1) = 2.0f / (top - bottom);
- Result(2,2) = - 2.0f / (zFar - zNear);
- Result(0,3) = - (right + left) / (right - left);
- Result(1,3) = - (top + bottom) / (top - bottom);
- Result(2,3) = - (zFar + zNear) / (zFar - zNear);
- return Result;
- }
- Eigen::Matrix4f frustum (
- const float left,
- const float right,
- const float bottom,
- const float top,
- const float nearVal,
- const float farVal)
- {
- Eigen::Matrix4f Result = Eigen::Matrix4f::Zero();
- Result(0,0) = (2.0f * nearVal) / (right - left);
- Result(1,1) = (2.0f * nearVal) / (top - bottom);
- Result(0,2) = (right + left) / (right - left);
- Result(1,2) = (top + bottom) / (top - bottom);
- Result(2,2) = -(farVal + nearVal) / (farVal - nearVal);
- Result(3,2) = -1.0f;
- Result(2,3) = -(2.0f * farVal * nearVal) / (farVal - nearVal);
- return Result;
- }
- Eigen::Matrix4f scale (const Eigen::Matrix4f& m,
- const Eigen::Vector3f& v)
- {
- Eigen::Matrix4f Result;
- Result.col(0) = m.col(0).array() * v(0);
- Result.col(1) = m.col(1).array() * v(1);
- Result.col(2) = m.col(2).array() * v(2);
- Result.col(3) = m.col(3);
- return Result;
- }
- Eigen::Matrix4f translate(
- const Eigen::Matrix4f& m,
- const Eigen::Vector3f& v)
- {
- Eigen::Matrix4f Result = m;
- Result.col(3) = m.col(0).array() * v(0) + m.col(1).array() * v(1) + m.col(2).array() * v(2) + m.col(3).array();
- return Result;
- }
- #include <limits>
- #include <cassert>
- #ifdef ENABLE_XML_SERIALIZATION
- #include "igl/xml/XMLSerializer.h"
- #endif
- #include <igl/readOBJ.h>
- #include <igl/readOFF.h>
- #include <igl/adjacency_list.h>
- #include <igl/writeOBJ.h>
- #include <igl/writeOFF.h>
- #include <igl/massmatrix.h>
- #include <igl/file_dialog_open.h>
- #include <igl/file_dialog_save.h>
- #include <igl/quat_to_mat.h>
- #include <igl/quat_mult.h>
- #include <igl/axis_angle_to_quat.h>
- #include <igl/trackball.h>
- #include <igl/snap_to_canonical_view_quat.h>
- #include <igl/unproject.h>
- #include <igl/viewer/TextRenderer.h>
- // Internal global variables used for glfw event handling
- static igl::Viewer * __viewer;
- static double highdpi = 1;
- static double scroll_x = 0;
- static double scroll_y = 0;
- static igl::TextRenderer __font_renderer;
- static void glfw_mouse_press(GLFWwindow* window, int button, int action, int modifier)
- {
- bool tw_used = TwEventMouseButtonGLFW(button, action);
- igl::Viewer::MouseButton mb;
- if (button == GLFW_MOUSE_BUTTON_1)
- mb = igl::Viewer::IGL_LEFT;
- else if (button == GLFW_MOUSE_BUTTON_2)
- mb = igl::Viewer::IGL_RIGHT;
- else //if (button == GLFW_MOUSE_BUTTON_3)
- mb = igl::Viewer::IGL_MIDDLE;
- if (action == GLFW_PRESS)
- {
- if(!tw_used)
- {
- __viewer->mouse_down(mb,modifier);
- }
- } else
- {
- // Always call mouse_up on up
- __viewer->mouse_up(mb,modifier);
- }
- }
- static void glfw_error_callback(int error, const char* description)
- {
- fputs(description, stderr);
- }
- int global_KMod = 0;
- int TwEventKeyGLFW3(int glfwKey, int glfwAction)
- {
- int handled = 0;
- // Register of modifiers state
- if (glfwAction==GLFW_PRESS)
- {
- switch (glfwKey)
- {
- case GLFW_KEY_LEFT_SHIFT:
- case GLFW_KEY_RIGHT_SHIFT:
- global_KMod |= TW_KMOD_SHIFT;
- break;
- case GLFW_KEY_LEFT_CONTROL:
- case GLFW_KEY_RIGHT_CONTROL:
- global_KMod |= TW_KMOD_CTRL;
- break;
- case GLFW_KEY_LEFT_ALT:
- case GLFW_KEY_RIGHT_ALT:
- global_KMod |= TW_KMOD_ALT;
- break;
- }
- }
- else
- {
- switch (glfwKey)
- {
- case GLFW_KEY_LEFT_SHIFT:
- case GLFW_KEY_RIGHT_SHIFT:
- global_KMod &= ~TW_KMOD_SHIFT;
- break;
- case GLFW_KEY_LEFT_CONTROL:
- case GLFW_KEY_RIGHT_CONTROL:
- global_KMod &= ~TW_KMOD_CTRL;
- break;
- case GLFW_KEY_LEFT_ALT:
- case GLFW_KEY_RIGHT_ALT:
- global_KMod &= ~TW_KMOD_ALT;
- break;
- }
- }
- // Process key pressed
- if (glfwAction==GLFW_PRESS)
- {
- int mod = global_KMod;
- int testkp = ((mod&TW_KMOD_CTRL) || (mod&TW_KMOD_ALT)) ? 1 : 0;
- if ((mod&TW_KMOD_CTRL) && glfwKey>0 && glfwKey<GLFW_KEY_ESCAPE ) // CTRL cases
- handled = TwKeyPressed(glfwKey, mod);
- else if (glfwKey>=GLFW_KEY_ESCAPE )
- {
- int k = 0;
- if (glfwKey>=GLFW_KEY_F1 && glfwKey<=GLFW_KEY_F15)
- k = TW_KEY_F1 + (glfwKey-GLFW_KEY_F1);
- else if (testkp && glfwKey>=GLFW_KEY_KP_0 && glfwKey<=GLFW_KEY_KP_9)
- k = '0' + (glfwKey-GLFW_KEY_KP_0);
- else
- {
- switch (glfwKey)
- {
- case GLFW_KEY_ESCAPE :
- k = TW_KEY_ESCAPE;
- break;
- case GLFW_KEY_UP:
- k = TW_KEY_UP;
- break;
- case GLFW_KEY_DOWN:
- k = TW_KEY_DOWN;
- break;
- case GLFW_KEY_LEFT:
- k = TW_KEY_LEFT;
- break;
- case GLFW_KEY_RIGHT:
- k = TW_KEY_RIGHT;
- break;
- case GLFW_KEY_TAB:
- k = TW_KEY_TAB;
- break;
- case GLFW_KEY_ENTER:
- k = TW_KEY_RETURN;
- break;
- case GLFW_KEY_BACKSPACE:
- k = TW_KEY_BACKSPACE;
- break;
- case GLFW_KEY_INSERT:
- k = TW_KEY_INSERT;
- break;
- case GLFW_KEY_DELETE:
- k = TW_KEY_DELETE;
- break;
- case GLFW_KEY_PAGE_UP:
- k = TW_KEY_PAGE_UP;
- break;
- case GLFW_KEY_PAGE_DOWN:
- k = TW_KEY_PAGE_DOWN;
- break;
- case GLFW_KEY_HOME:
- k = TW_KEY_HOME;
- break;
- case GLFW_KEY_END:
- k = TW_KEY_END;
- break;
- case GLFW_KEY_KP_ENTER:
- k = TW_KEY_RETURN;
- break;
- case GLFW_KEY_KP_DIVIDE:
- if (testkp)
- k = '/';
- break;
- case GLFW_KEY_KP_MULTIPLY:
- if (testkp)
- k = '*';
- break;
- case GLFW_KEY_KP_SUBTRACT:
- if (testkp)
- k = '-';
- break;
- case GLFW_KEY_KP_ADD:
- if (testkp)
- k = '+';
- break;
- case GLFW_KEY_KP_DECIMAL:
- if (testkp)
- k = '.';
- break;
- case GLFW_KEY_KP_EQUAL:
- if (testkp)
- k = '=';
- break;
- }
- }
- if (k>0)
- handled = TwKeyPressed(k, mod);
- }
- }
- return handled;
- }
- static void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int modifier)
- {
- if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
- glfwSetWindowShouldClose(window, GL_TRUE);
- if (!TwEventKeyGLFW3(key,action))
- {
- if (action == GLFW_PRESS)
- __viewer->key_down(key, modifier);
- else
- __viewer->key_up(key, modifier);
- }
- }
- static void glfw_window_size(GLFWwindow* window, int width, int height)
- {
- int w = width*highdpi;
- int h = height*highdpi;
- __viewer->resize(w, h);
- TwWindowSize(w, h);
- const auto & bar = __viewer->bar;
- // Keep AntTweakBar on right side of screen and height == opengl height
- // get the current position of a bar
- int size[2];
- TwGetParam(bar, NULL, "size", TW_PARAM_INT32, 2, size);
- int pos[2];
- // Place bar on left side of opengl rect (padded by 10 pixels)
- pos[0] = 10;//max(10,(int)width - size[0] - 10);
- // place bar at top (padded by 10 pixels)
- pos[1] = 10;
- // Set height to new height of window (padded by 10 pixels on bottom)
- size[1] = highdpi*(height-pos[1]-10);
- TwSetParam(bar, NULL, "position", TW_PARAM_INT32, 2, pos);
- TwSetParam(bar, NULL, "size", TW_PARAM_INT32, 2,size);
- }
- static void glfw_mouse_move(GLFWwindow* window, double x, double y)
- {
- if(!TwEventMousePosGLFW(x*highdpi,y*highdpi) || __viewer->down)
- {
- // Call if TwBar hasn't used or if down
- __viewer->mouse_move(x*highdpi, y*highdpi);
- }
- }
- static void glfw_mouse_scroll(GLFWwindow* window, double x, double y)
- {
- using namespace std;
- scroll_x += x;
- scroll_y += y;
- if (!TwEventMouseWheelGLFW(scroll_y))
- __viewer->mouse_scroll(y);
- }
- static void glfw_char_callback(GLFWwindow* window, unsigned int c)
- {
- if ((c & 0xff00)==0)
- TwKeyPressed(c, global_KMod);
- }
- namespace igl
- {
- void Viewer::init()
- {
- // Create a tweak bar
- bar = TwNewBar("libIGL-Viewer");
- TwDefine(" libIGL-Viewer help='This is a simple 3D mesh viewer.' "); // Message added to the help bar->
- TwDefine(" libIGL-Viewer size='200 685'"); // change default tweak bar size
- TwDefine(" libIGL-Viewer color='76 76 127' "); // change default tweak bar color
- TwDefine(" libIGL-Viewer refresh=0.5"); // change refresh rate
- // ---------------------- LOADING ----------------------
- #ifdef ENABLE_XML_SERIALIZATION
- TwAddButton(bar,"Load Scene", load_scene_cb, this, "group='Workspace'");
- TwAddButton(bar,"Save Scene", save_scene_cb, this, "group='Workspace'");
- #endif
- #ifdef ENABLE_IO
- TwAddButton(bar,"Load Mesh", open_dialog_mesh, this, "group='Mesh' key=o");
- #endif
- // ---------------------- SCENE ----------------------
- TwAddButton(bar,"Center object", align_camera_center_cb, this,
- " group='Viewing Options'"
- " label='Center object' key=A help='Set the center of the camera to the mesh center.'");
- TwAddVarRW(bar, "Zoom", TW_TYPE_FLOAT, &(options.camera_zoom),
- " min=0.05 max=50 step=0.1 keyIncr=+ keyDecr=- help='Scale the object (1=original size).' group='Scene'");
- TwAddButton(bar,"SnapView", snap_to_canonical_quaternion_cb, this,
- " group='Scene'"
- " label='Snap to canonical view' key=Z "
- " help='Snaps view to nearest canonical view.'");
- TwAddVarRW(bar,"LightDir", TW_TYPE_DIR3F, options.light_position.data(),
- " group='Scene'"
- " label='Light direction' open help='Change the light direction.' ");
- // ---------------------- DRAW OPTIONS ----------------------
- TwAddVarRW(bar, "ToggleOrthographic", TW_TYPE_BOOLCPP, &(options.orthographic),
- " group='Viewing Options'"
- " label='Orthographic view' "
- " help='Toggles orthographic / perspective view. Default: perspective.'");
- TwAddVarRW(bar, "Rotation", TW_TYPE_QUAT4F, &(options.trackball_angle),
- " group='Viewing Options'"
- " label='Rotation'"
- " help='Rotates view.'");
- TwAddVarCB(bar,"Face-based Normals/Colors", TW_TYPE_BOOLCPP, set_face_based_cb, get_face_based_cb, this,
- " group='Draw options'"
- " label='Face-based' key=T help='Toggle per face shading/colors.' ");
- TwAddVarRW(bar,"Show texture", TW_TYPE_BOOLCPP, &(options.show_texture),
- " group='Draw options'");
- TwAddVarCB(bar,"Invert Normals", TW_TYPE_BOOLCPP, set_invert_normals_cb, get_invert_normals_cb, this,
- " group='Draw options'"
- " label='Invert normals' key=i help='Invert normal directions for inside out meshes.' ");
- TwAddVarRW(bar,"ShowOverlay", TW_TYPE_BOOLCPP, &(options.show_overlay),
- " group='Draw options'"
- " label='Show overlay' key=o help='Show the overlay layers.' ");
- TwAddVarRW(bar,"ShowOverlayDepth", TW_TYPE_BOOLCPP, &(options.show_overlay_depth),
- " group='Draw options'"
- " label='Show overlay depth test' help='Enable the depth test for overlay layer.' ");
- TwAddVarRW(bar,"Background color", TW_TYPE_COLOR3F,
- options.background_color.data(),
- " help='Select a background color' colormode=hls group='Draw options'");
- TwAddVarRW(bar, "LineColor", TW_TYPE_COLOR3F,
- options.line_color.data(),
- " label='Line color' help='Select a outline color' group='Draw options'");
- TwAddVarRW(bar,"Shininess",TW_TYPE_FLOAT,&options.shininess," group='Draw options'"
- " min=1 max=128");
- // ---------------------- Overlays ----------------------
- TwAddVarRW(bar,"Wireframe", TW_TYPE_BOOLCPP, &(options.show_lines),
- " group='Overlays'"
- " label='Wireframe' key=l help='Toggle wire frame of mesh'");
- TwAddVarRW(bar,"Fill", TW_TYPE_BOOLCPP, &(options.show_faces),
- " group='Overlays'"
- " label='Fill' key=t help='Display filled polygons of mesh'");
- TwAddVarRW(bar,"ShowVertexId", TW_TYPE_BOOLCPP, &(options.show_vertid),
- " group='Overlays'"
- " label='Show Vertex Labels' key=';' help='Toggle vertex indices'");
- TwAddVarRW(bar,"ShowFaceId", TW_TYPE_BOOLCPP, &(options.show_faceid),
- " group='Overlays'"
- " label='Show Faces Labels' key='CTRL+;' help='Toggle face"
- " indices'");
- __font_renderer.Init();
- init_plugins();
- }
- Viewer::Viewer()
- {
- // Default shininess
- options.shininess = 35.0f;
- // Default colors
- options.background_color << 0.3f, 0.3f, 0.5f;
- options.line_color << 0.0f, 0.0f, 0.0f;
- // Default lights settings
- options.light_position << 0.0f, -0.30f, -5.0f;
- options.lighting_factor = 1.0f; //on
- // Default trackball
- options.trackball_angle << 0.0f, 0.0f, 0.0f, 1.0f;
- // Defalut model viewing parameters
- options.model_zoom = 1.0f;
- options.model_translation << 0,0,0;
- // Camera parameters
- options.camera_zoom = 1.0f;
- options.orthographic = false;
- options.camera_view_angle = 45.0;
- options.camera_dnear = 1.0;
- options.camera_dfar = 100.0;
- options.camera_eye << 0, 0, 5;
- options.camera_center << 0, 0, 0;
- options.camera_up << 0, 1, 0;
- // Default visualization options
- options.show_faces = true;
- options.show_lines = true;
- options.invert_normals = false;
- options.show_overlay = true;
- options.show_overlay_depth = true;
- options.show_vertid = false;
- options.show_faceid = false;
- options.show_texture = false;
- // Default point size / line width
- options.point_size = 15;
- options.line_width = 0.5f;
- data.face_based = false;
- options.is_animating = false;
- options.animation_max_fps = 30.;
- // Temporary variables initialization
- down = false;
- hack_never_moved = true;
- scroll_position = 0.0f;
- // Per face
- set_face_based(false);
- // C-style callbacks
- callback_pre_draw = 0;
- callback_post_draw = 0;
- callback_mouse_down = 0;
- callback_mouse_up = 0;
- callback_mouse_move = 0;
- callback_mouse_scroll = 0;
- callback_key_down = 0;
- callback_key_up = 0;
- callback_pre_draw_data = 0;
- callback_post_draw = 0;
- callback_mouse_down = 0;
- callback_mouse_up = 0;
- callback_mouse_move = 0;
- callback_mouse_scroll = 0;
- callback_key_down = 0;
- callback_key_up = 0;
- }
- void Viewer::init_plugins()
- {
- // Init all plugins
- for (unsigned int i = 0; i<plugins.size(); ++i)
- plugins[i]->init(this);
- }
- Viewer::~Viewer()
- {
- }
- void Viewer::shutdown_plugins()
- {
- for (unsigned int i = 0; i<plugins.size(); ++i)
- plugins[i]->shutdown();
- }
- bool Viewer::load_mesh_from_file(const char* mesh_file_name)
- {
- std::string mesh_file_name_string = std::string(mesh_file_name);
- // first try to load it with a plugin
- for (unsigned int i = 0; i<plugins.size(); ++i)
- if (plugins[i]->load(mesh_file_name_string))
- return true;
- data.clear();
- size_t last_dot = mesh_file_name_string.rfind('.');
- if (last_dot == std::string::npos)
- {
- printf("Error: No file extension found in %s\n",mesh_file_name);
- return false;
- }
- std::string extension = mesh_file_name_string.substr(last_dot+1);
- if (extension == "off" || extension =="OFF")
- {
- if (!igl::readOFF(mesh_file_name_string, data.V, data.F))
- return false;
- }
- else if (extension == "obj" || extension =="OBJ")
- {
- Eigen::MatrixXd corner_normals;
- Eigen::MatrixXi fNormIndices;
- Eigen::MatrixXd UV_V;
- Eigen::MatrixXi UV_F;
- if (!(igl::readOBJ(mesh_file_name_string, data.V, data.F, corner_normals, fNormIndices, UV_V, UV_F)))
- return false;
- }
- else
- {
- // unrecognized file type
- printf("Error: %s is not a recognized file type.\n",extension.c_str());
- return false;
- }
- data.compute_normals();
- data.uniform_colors(Eigen::Vector3d(51.0/255.0,43.0/255.0,33.3/255.0),
- Eigen::Vector3d(255.0/255.0,228.0/255.0,58.0/255.0),
- Eigen::Vector3d(255.0/255.0,235.0/255.0,80.0/255.0));
- if (data.V_uv.rows() == 0)
- data.grid_texture();
- align_camera_center();
- for (unsigned int i = 0; i<plugins.size(); ++i)
- if (plugins[i]->post_load())
- return true;
- return true;
- }
- bool Viewer::save_mesh_to_file(const char* mesh_file_name)
- {
- std::string mesh_file_name_string(mesh_file_name);
- // first try to load it with a plugin
- for (unsigned int i = 0; i<plugins.size(); ++i)
- if (plugins[i]->save(mesh_file_name_string))
- return true;
- size_t last_dot = mesh_file_name_string.rfind('.');
- if (last_dot == std::string::npos)
- {
- // No file type determined
- printf("Error: No file extension found in %s\n",mesh_file_name);
- return false;
- }
- std::string extension = mesh_file_name_string.substr(last_dot+1);
- if (extension == "off" || extension =="OFF")
- {
- return igl::writeOFF(mesh_file_name_string,data.V,data.F);
- }
- else if (extension == "obj" || extension =="OBJ")
- {
- Eigen::MatrixXd corner_normals;
- Eigen::MatrixXi fNormIndices;
- Eigen::MatrixXd UV_V;
- Eigen::MatrixXi UV_F;
- return igl::writeOBJ(mesh_file_name_string, data.V,
- data.F, corner_normals, fNormIndices, UV_V, UV_F);
- }
- else
- {
- // unrecognized file type
- printf("Error: %s is not a recognized file type.\n",extension.c_str());
- return false;
- }
- return true;
- }
- void Viewer::clear()
- {
- data.clear();
- }
- bool Viewer::key_down(unsigned char key, int modifiers)
- {
- if (callback_key_down)
- if (callback_key_down(*this,key,modifiers))
- return true;
- for (unsigned int i = 0; i<plugins.size(); ++i)
- if (plugins[i]->key_down(key, modifiers))
- return true;
- if (key == 'S')
- mouse_scroll(1);
- if (key == 'A')
- mouse_scroll(-1);
- // Why aren't these handled view AntTweakBar?
- if (key == 'z') // Don't use 'Z' because that clobbers snap_to_canonical_view_quat
- options.trackball_angle << 0.0f, 0.0f, 0.0f, 1.0f;
- if (key == 'y')
- options.trackball_angle << -sqrt(2.0f)/2.0f, 0.0f, 0.0f, sqrt(2.0f)/2.0f;
- if (key == 'x')
- options.trackball_angle << -0.5f, -0.5f, -0.5f, 0.5f;
- return false;
- }
- bool Viewer::key_up(unsigned char key, int modifiers)
- {
- if (callback_key_up)
- if (callback_key_up(*this,key,modifiers))
- return true;
- for (unsigned int i = 0; i<plugins.size(); ++i)
- if (plugins[i]->key_up(key, modifiers))
- return true;
- return false;
- }
- bool Viewer::mouse_down(MouseButton button, int modifier)
- {
- if (callback_mouse_down)
- if (callback_mouse_down(*this,button,modifier))
- return true;
- for (unsigned int i = 0; i<plugins.size(); ++i)
- if (plugins[i]->mouse_down(button,modifier))
- return true;
- down = true;
- down_mouse_x = current_mouse_x;
- down_mouse_y = current_mouse_y;
- down_translation = options.model_translation;
- // Initialization code for the trackball
- Eigen::RowVector3d center;
- if (data.V.rows() == 0)
- center << 0,0,0;
- else
- center = data.V.colwise().sum()/data.V.rows();
- Eigen::Vector3f coord = igl::project(Eigen::Vector3f(center(0),center(1),center(2)), view * model, proj, viewport);
- down_mouse_z = coord[2];
- down_rotation = options.trackball_angle;
- mouse_mode = ROTATION;
- switch (button)
- {
- case IGL_LEFT:
- mouse_mode = ROTATION;
- break;
- case IGL_RIGHT:
- mouse_mode = TRANSLATE;
- break;
- default:
- mouse_mode = NOTHING;
- break;
- }
- return true;
- }
- bool Viewer::mouse_up(MouseButton button, int modifier)
- {
- down = false;
- if (callback_mouse_up)
- if (callback_mouse_up(*this,button,modifier))
- return true;
- for (unsigned int i = 0; i<plugins.size(); ++i)
- if (plugins[i]->mouse_up(button,modifier))
- return true;
- mouse_mode = NOTHING;
- return true;
- }
- bool Viewer::mouse_move(int mouse_x, int mouse_y)
- {
- if(hack_never_moved)
- {
- down_mouse_x = mouse_x;
- down_mouse_y = mouse_y;
- hack_never_moved = false;
- }
- current_mouse_x = mouse_x;
- current_mouse_y = mouse_y;
- if (callback_mouse_move)
- if (callback_mouse_move(*this,mouse_x,mouse_y))
- return true;
- for (unsigned int i = 0; i<plugins.size(); ++i)
- if (plugins[i]->mouse_move(mouse_x, mouse_y))
- return true;
- if (down)
- {
- switch (mouse_mode)
- {
- case ROTATION :
- {
- igl::trackball(width,
- height,
- 2.0f,
- down_rotation.data(),
- down_mouse_x,
- down_mouse_y,
- mouse_x,
- mouse_y,
- options.trackball_angle.data());
- //Eigen::Vector4f snapq = options.trackball_angle;
- break;
- }
- case TRANSLATE:
- {
- //translation
- Eigen::Vector3f pos1 = igl::unproject(Eigen::Vector3f(mouse_x, viewport[3] - mouse_y, down_mouse_z), view * model, proj, viewport);
- Eigen::Vector3f pos0 = igl::unproject(Eigen::Vector3f(down_mouse_x, viewport[3] - down_mouse_y, down_mouse_z), view * model, proj, viewport);
- Eigen::Vector3f diff = pos1 - pos0;
- options.model_translation = down_translation + Eigen::Vector3f(diff[0],diff[1],diff[2]);
- break;
- }
- case ZOOM:
- {
- //float delta = 0.001f * (mouse_x - down_mouse_x + mouse_y - down_mouse_y);
- float delta = 0.001f * (mouse_x - down_mouse_x + mouse_y - down_mouse_y);
- options.camera_zoom *= 1 + delta;
- down_mouse_x = mouse_x;
- down_mouse_y = mouse_y;
- break;
- }
- default:
- break;
- }
- }
- return true;
- }
- bool Viewer::mouse_scroll(float delta_y)
- {
- scroll_position += delta_y;
- if (callback_mouse_scroll)
- if (callback_mouse_scroll(*this,delta_y))
- return true;
- for (unsigned int i = 0; i<plugins.size(); ++i)
- if (plugins[i]->mouse_scroll(delta_y))
- return true;
- // Only zoom if there's actually a change
- if(delta_y != 0)
- {
- float mult = (1.0+((delta_y>0)?1.:-1.)*0.05);
- const float min_zoom = 0.1f;
- options.camera_zoom = (options.camera_zoom * mult > min_zoom ? options.camera_zoom * mult : min_zoom);
- }
- return true;
- }
- void Viewer::draw()
- {
- using namespace std;
- using namespace Eigen;
- glClearColor(options.background_color[0],
- options.background_color[1],
- options.background_color[2],
- 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glEnable(GL_DEPTH_TEST);
- if (callback_pre_draw)
- if (callback_pre_draw(*this))
- return;
- for (unsigned int i = 0; i<plugins.size(); ++i)
- if (plugins[i]->pre_draw())
- return;
- /* Bind and potentially refresh mesh/line/point data */
- if (data.dirty)
- {
- opengl.set_data(data, options.invert_normals);
- data.dirty = ViewerData::DIRTY_NONE;
- }
- opengl.bind_mesh();
- // Initialize uniform
- glViewport(0, 0, width, height);
- model = Eigen::Matrix4f::Identity();
- view = Eigen::Matrix4f::Identity();
- proj = Eigen::Matrix4f::Identity();
- // Set view
- view = lookAt(Eigen::Vector3f(options.camera_eye[0], options.camera_eye[1], options.camera_eye[2]),
- Eigen::Vector3f(options.camera_center[0], options.camera_center[1], options.camera_center[2]),
- Eigen::Vector3f(options.camera_up[0], options.camera_up[1], options.camera_up[2]));
- // Set projection
- if (options.orthographic)
- {
- float length = (options.camera_eye - options.camera_center).norm();
- float h = tan(options.camera_view_angle/360.0 * M_PI) * (length);
- proj = ortho(-h*width/height, h*width/height, -h, h, options.camera_dnear, options.camera_dfar);
- }
- else
- {
- float fH = tan(options.camera_view_angle / 360.0 * M_PI) * options.camera_dnear;
- float fW = fH * (double)width/(double)height;
- proj = frustum(-fW, fW, -fH, fH, options.camera_dnear, options.camera_dfar);
- }
- // end projection
- // Set model transformation
- float mat[16];
- igl::quat_to_mat(options.trackball_angle.data(), mat);
- for (unsigned i=0;i<4;++i)
- for (unsigned j=0;j<4;++j)
- model(i,j) = mat[i+4*j];
- model = scale(model, Eigen::Vector3f(options.camera_zoom,options.camera_zoom,options.camera_zoom));
- model = scale(model, Eigen::Vector3f(options.model_zoom,options.model_zoom,options.model_zoom));
- model = translate(model, Eigen::Vector3f(options.model_translation[0],options.model_translation[1],options.model_translation[2]));
- // Send transformations to the GPU
- GLint modeli = opengl.shader_mesh.uniform("model");
- GLint viewi = opengl.shader_mesh.uniform("view");
- GLint proji = opengl.shader_mesh.uniform("proj");
- glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
- glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
- glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
- // Light parameters
- GLint specular_exponenti = opengl.shader_mesh.uniform("specular_exponent");
- GLint light_position_worldi = opengl.shader_mesh.uniform("light_position_world");
- GLint lighting_factori = opengl.shader_mesh.uniform("lighting_factor");
- GLint fixed_colori = opengl.shader_mesh.uniform("fixed_color");
- GLint texture_factori = opengl.shader_mesh.uniform("texture_factor");
- glUniform1f(specular_exponenti, options.shininess);
- Vector3f rev_light = -1.*options.light_position;
- glUniform3fv(light_position_worldi, 1, rev_light.data());
- glUniform1f(lighting_factori, options.lighting_factor); // enables lighting
- glUniform4f(fixed_colori, 0.0, 0.0, 0.0, 0.0);
- if (data.V.rows()>0)
- {
- // Render fill
- if (options.show_faces)
- {
- // Texture
- glUniform1f(texture_factori, options.show_texture ? 1.0f : 0.0f);
- opengl.draw_mesh(true);
- glUniform1f(texture_factori, 0.0f);
- }
- // Render wireframe
- if (options.show_lines)
- {
- glLineWidth(options.line_width);
- glUniform4f(fixed_colori, options.line_color[0], options.line_color[1],
- options.line_color[2], 1.0f);
- opengl.draw_mesh(false);
- glUniform4f(fixed_colori, 0.0f, 0.0f, 0.0f, 0.0f);
- }
- if (options.show_vertid)
- {
- __font_renderer.BeginDraw(view*model, proj, viewport, data.object_scale);
- for (int i=0; i<data.V.rows(); ++i)
- __font_renderer.DrawText(data.V.row(i), data.V_normals.row(i), to_string(i));
- __font_renderer.EndDraw();
- }
- if (options.show_faceid)
- {
- __font_renderer.BeginDraw(view*model, proj, viewport, data.object_scale);
- for (int i=0; i<data.F.rows(); ++i)
- {
- Eigen::RowVector3d p = Eigen::RowVector3d::Zero();
- for (int j=0;j<data.F.cols();++j)
- p += data.V.row(data.F(i,j));
- p /= data.F.cols();
- __font_renderer.DrawText(p, data.F_normals.row(i), to_string(i));
- }
- __font_renderer.EndDraw();
- }
- }
- if (options.show_overlay)
- {
- if (options.show_overlay_depth)
- glEnable(GL_DEPTH_TEST);
- else
- glDisable(GL_DEPTH_TEST);
- if (data.lines.rows() > 0)
- {
- opengl.bind_overlay_lines();
- modeli = opengl.shader_overlay_lines.uniform("model");
- viewi = opengl.shader_overlay_lines.uniform("view");
- proji = opengl.shader_overlay_lines.uniform("proj");
- glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
- glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
- glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
- // This must be enabled, otherwise glLineWidth has no effect
- glEnable(GL_LINE_SMOOTH);
- glLineWidth(options.line_width);
- opengl.draw_overlay_lines();
- }
- if (data.points.rows() > 0)
- {
- opengl.bind_overlay_points();
- modeli = opengl.shader_overlay_points.uniform("model");
- viewi = opengl.shader_overlay_points.uniform("view");
- proji = opengl.shader_overlay_points.uniform("proj");
- glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
- glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
- glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
- glPointSize(options.point_size);
- opengl.draw_overlay_points();
- }
- if (data.labels_positions.rows() > 0)
- {
- __font_renderer.BeginDraw(view*model, proj, viewport, data.object_scale);
- for (int i=0; i<data.labels_positions.rows(); ++i)
- __font_renderer.DrawText(data.labels_positions.row(i), Eigen::Vector3d(0.0,0.0,0.0),
- data.labels_strings[i]);
- __font_renderer.EndDraw();
- }
- glEnable(GL_DEPTH_TEST);
- }
- if (callback_post_draw)
- if (callback_post_draw(*this))
- return;
- for (unsigned int i = 0; i<plugins.size(); ++i)
- if (plugins[i]->post_draw())
- break;
- TwDraw();
- }
- bool Viewer::save_scene()
- {
- #ifdef ENABLE_XML_SERIALIZATION
- string fname = igl::file_dialog_save();
- if (fname.length() == 0)
- return false;
- ::igl::XMLSerializer serializer("Viewer");
- serializer.Add(data,"Data");
- serializer.Add(options,"Options");
- if (plugin_manager)
- for (unsigned int i = 0; i <plugin_manager->plugin_list.size(); ++i)
- serializer.Add(*(plugin_manager->plugin_list[i]),plugin_manager->plugin_list[i]->plugin_name);
- serializer.Save(fname.c_str(),true);
- #endif
- return true;
- }
- bool Viewer::load_scene()
- {
- #ifdef ENABLE_XML_SERIALIZATION
- string fname = igl::file_dialog_open();
- if (fname.length() == 0)
- return false;
- ::igl::XMLSerializer serializer("Viewer");
- serializer.Add(data,"Data");
- serializer.Add(options,"Options");
- if (plugin_manager)
- for (unsigned int i = 0; i <plugin_manager->plugin_list.size(); ++i)
- serializer.Add(*(plugin_manager->plugin_list[i]),plugin_manager->plugin_list[i]->plugin_name);
- serializer.Load(fname.c_str());
- #endif
- return true;
- }
- void Viewer::align_camera_center()
- {
- get_scale_and_shift_to_fit_mesh(data.V,data.F,options.model_zoom,options.model_translation);
- data.object_scale = (data.V.colwise().maxCoeff() - data.V.colwise().minCoeff()).norm();
- }
- void Viewer::resize(int w, int h)
- {
- width = w;
- height = h;
- viewport = Eigen::Vector4f(0,0,width,height);
- }
- void Viewer::get_scale_and_shift_to_fit_mesh(
- const Eigen::MatrixXd& V,
- const Eigen::MatrixXi& F,
- float& zoom,
- Eigen::Vector3f& shift)
- {
- if (V.rows() == 0)
- return;
- //Compute mesh centroid
- Eigen::SparseMatrix<double> M;
- igl::massmatrix(V,F,igl::MASSMATRIX_TYPE_VORONOI,M);
- const auto & MV = M*V;
- Eigen::RowVector3d centroid = MV.colwise().sum()/M.diagonal().sum();
- Eigen::RowVector3d min_point = V.colwise().minCoeff();
- Eigen::RowVector3d max_point = V.colwise().maxCoeff();
- shift = -centroid.cast<float>();
- double x_scale = fabs(max_point[0] - min_point[0]);
- double y_scale = fabs(max_point[1] - min_point[1]);
- double z_scale = fabs(max_point[2] - min_point[2]);
- zoom = 2.0/ std::max(z_scale,std::max(x_scale,y_scale));
- }
- void TW_CALL Viewer::snap_to_canonical_quaternion_cb(void *clientData)
- {
- Eigen::Vector4f snapq = static_cast<Viewer *>(clientData)->options.trackball_angle;
- igl::snap_to_canonical_view_quat<float>(snapq.data(),1,static_cast<Viewer *>(clientData)->options.trackball_angle.data());
- }
- void TW_CALL Viewer::align_camera_center_cb(void *clientData)
- {
- static_cast<Viewer *>(clientData)->align_camera_center();
- }
- void TW_CALL Viewer::save_scene_cb(void *clientData)
- {
- static_cast<Viewer *>(clientData)->save_scene();
- }
- void TW_CALL Viewer::load_scene_cb(void *clientData)
- {
- static_cast<Viewer *>(clientData)->load_scene();
- }
- void TW_CALL Viewer::set_invert_normals_cb(const void *param, void *clientData)
- {
- Viewer *viewer = static_cast<Viewer *>(clientData);
- viewer->data.dirty |= ViewerData::DIRTY_NORMAL;
- viewer->options.invert_normals = *((bool *) param);
- }
- void TW_CALL Viewer::get_invert_normals_cb(void *param, void *clientData)
- {
- *((bool *) param) = static_cast<Viewer *>(clientData)->options.invert_normals;
- }
- void TW_CALL Viewer::set_face_based_cb(const void *param, void *clientData)
- {
- Viewer *viewer = static_cast<Viewer *>(clientData);
- viewer->set_face_based(*((bool *) param));
- }
- void TW_CALL Viewer::get_face_based_cb(void *param, void *clientData)
- {
- *((bool *) param) = static_cast<Viewer *>(clientData)->data.face_based;
- }
- void TW_CALL Viewer::open_dialog_mesh(void *clientData)
- {
- std::string fname = igl::file_dialog_open();
- if (fname.length() == 0)
- return;
- static_cast<Viewer *>(clientData)->load_mesh_from_file(fname.c_str());
- }
- // Serialization
- void Viewer::Options::InitSerialization()
- {
- #ifdef ENABLE_XML_SERIALIZATION
- xmlSerializer->Add(shininess, "shininess");
- xmlSerializer->Add(background_color, "background_color");
- xmlSerializer->Add(line_color, "line_color");
- xmlSerializer->Add(light_position, "light_position");
- xmlSerializer->Add(lighting_factor, "lighting_factor");
- xmlSerializer->Add(trackball_angle, "trackball_angle");
- xmlSerializer->Add(model_zoom, "model_zoom");
- xmlSerializer->Add(model_translation, "model_translation");
- xmlSerializer->Add(model_zoom_uv, "model_zoom_uv");
- xmlSerializer->Add(model_translation_uv, "model_translation_uv");
- xmlSerializer->Add(camera_zoom, "camera_zoom");
- xmlSerializer->Add(orthographic, "orthographic");
- xmlSerializer->Add(camera_eye, "camera_eye");
- xmlSerializer->Add(camera_up, "camera_up");
- xmlSerializer->Add(camera_center, "camera_center");
- xmlSerializer->Add(camera_view_angle, "camera_view_angle");
- xmlSerializer->Add(camera_dnear, "camera_dnear");
- xmlSerializer->Add(camera_dfar, "camera_dfar");
- xmlSerializer->Add(show_overlay, "show_overlay");
- xmlSerializer->Add(show_overlay_depth, "show_overlay_depth");
- xmlSerializer->Add(show_texture, "show_texture");
- xmlSerializer->Add(show_faces, "show_faces");
- xmlSerializer->Add(show_lines, "show_lines");
- xmlSerializer->Add(show_vertid, "show_vertid");
- xmlSerializer->Add(show_faceid, "show_faceid");
- xmlSerializer->Add(point_size, "point_size");
- xmlSerializer->Add(line_width, "line_width");
- xmlSerializer->Add(invert_normals, "invert_normals");
- xmlSerializer->Add(face_based, "face_based");
- #endif
- }
- void Viewer::set_face_based(bool newvalue)
- {
- data.set_face_based(newvalue);
- }
- void Viewer::compute_normals()
- {
- data.compute_normals();
- }
- void Viewer::set_mesh(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F)
- {
- data.set_mesh(V,F);
- align_camera_center();
- }
- void Viewer::set_vertices(const Eigen::MatrixXd& V)
- {
- data.set_vertices(V);
- }
- void Viewer::set_normals(const Eigen::MatrixXd& N)
- {
- data.set_normals(N);
- }
- void Viewer::set_colors(const Eigen::MatrixXd &C)
- {
- data.set_colors(C);
- }
- void Viewer::set_uv(const Eigen::MatrixXd& UV)
- {
- data.set_uv(UV);
- }
- void Viewer::set_uv(const Eigen::MatrixXd& UV_V, const Eigen::MatrixXi& UV_F)
- {
- data.set_uv(UV_V,UV_F);
- }
- void Viewer::set_texture(
- const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& R,
- const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& G,
- const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& B)
- {
- data.set_texture(R,G,B);
- }
- void Viewer::add_points(const Eigen::MatrixXd& P, const Eigen::MatrixXd& C)
- {
- data.add_points(P,C);
- }
- void Viewer::set_edges(
- const Eigen::MatrixXd& P,
- const Eigen::MatrixXi& E,
- const Eigen::MatrixXd& C)
- {
- data.set_edges(P,E,C);
- }
- void Viewer::add_edges(const Eigen::MatrixXd& P1, const Eigen::MatrixXd& P2, const Eigen::MatrixXd& C)
- {
- data.add_edges(P1,P2,C);
- }
- void Viewer::add_label(const Eigen::VectorXd& P, const std::string& str)
- {
- data.add_label(P,str);
- }
- int Viewer::launch(std::string filename)
- {
- GLFWwindow* window;
- glfwSetErrorCallback(glfw_error_callback);
- if (!glfwInit())
- return EXIT_FAILURE;
- glfwWindowHint(GLFW_SAMPLES, 16);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
- #ifdef __APPLE__
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
- #endif
- window = glfwCreateWindow(1280, 800, "IGL Viewer", NULL, NULL);
- if (!window)
- {
- glfwTerminate();
- return EXIT_FAILURE;
- }
- glfwMakeContextCurrent(window);
- #ifndef __APPLE__
- glewExperimental = true;
- GLenum err = glewInit();
- if (GLEW_OK != err)
- {
- /* Problem: glewInit failed, something is seriously wrong. */
- fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
- }
- fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
- #endif
- #ifdef DEBUG
- int major, minor, rev;
- major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
- minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
- rev = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
- printf("OpenGL version recieved: %d.%d.%d\n", major, minor, rev);
- printf("Supported OpenGL is %s\n", (const char*)glGetString(GL_VERSION));
- printf("Supported GLSL is %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
- #endif
- glfwSetInputMode(window,GLFW_CURSOR,GLFW_CURSOR_NORMAL);
- // Initialize AntTweakBar
- TwInit(TW_OPENGL_CORE, NULL);
- // Initialize IGL viewer
- init();
- __viewer = this;
- // Register callbacks
- glfwSetKeyCallback(window, glfw_key_callback); glfwSetCursorPosCallback(window,glfw_mouse_move); glfwSetWindowSizeCallback(window,glfw_window_size); glfwSetMouseButtonCallback(window,glfw_mouse_press); glfwSetScrollCallback(window,glfw_mouse_scroll); glfwSetCharCallback(window, glfw_char_callback);
- // Handle retina displays (windows and mac)
- int width, height;
- glfwGetFramebufferSize(window, &width, &height);
- int width_window, height_window;
- glfwGetWindowSize(window, &width_window, &height_window);
- highdpi = width/width_window;
- glfw_window_size(window,width_window,height_window);
- opengl.init();
- // Load the mesh passed as input
- if (filename.size() > 0)
- load_mesh_from_file(filename.c_str());
- // Rendering loop
- while (!glfwWindowShouldClose(window))
- {
- double tic = get_seconds();
- draw();
- glfwSwapBuffers(window);
- if(options.is_animating)
- {
- glfwPollEvents();
- // In microseconds
- double duration = 1000000.*(get_seconds()-tic);
- const double min_duration = 1000000./options.animation_max_fps;
- if(duration<min_duration)
- {
- // TODO: windows equivalent
- usleep(min_duration-duration);
- }
- }
- else
- {
- glfwWaitEvents();
- }
- }
- opengl.free();
- __font_renderer.Shut();
- shutdown_plugins();
- glfwDestroyWindow(window);
- glfwTerminate();
- return EXIT_SUCCESS;
- }
- } // end namespace
|