Przeglądaj źródła

more fixes to Viewer.cpp

Former-commit-id: 772366b553af359970099523664c36832a85928a
Alec Jacobson 11 lat temu
rodzic
commit
4fbf0da74b

+ 16 - 3
include/igl/viewer/TODOs.txt

@@ -1,7 +1,19 @@
-- Rewrite in libigl style
-- zoom with pan rather than scaling
-- don't zoom on horizontal scale
+- rewrite in libigl style
+- remove use of double underscores (http://stackoverflow.com/a/224420/148668)
+- document inputs and outputs to all functions
+- document all member fields
+- document all classes
+- light direction is backwards
+- remove global variables (not threadsafe)
+- encapsulate (in igl namespace) and move static/global functions, use lambdas?
+- preface macros with "IGL_"
 - trackball mouseup captured by tweakbar
+- zoom with pan rather than scaling
+- refresh draw while resizing
+- use constructor initializer list rather than complicated constructor
++ resize TwBar with window
++ trackball should be able to drag over TwBar
++ don't zoom on horizontal scale
 + remove global `using namespace std`
 + remove `#define IGL_HEADER_ONLY`
 + guard `#undef max`
@@ -10,3 +22,4 @@
 + missing `#include <fstream>`
 + fix all -Wunused-but-set-variable
 + makefile for libiglviewer.a
++ Viewer.h should include Viewer.cpp

+ 47 - 19
include/igl/viewer/Viewer.cpp

@@ -315,20 +315,26 @@ static TextRenderer __font_renderer;
 
 static void glfw_mouse_press(GLFWwindow* window, int button, int action, int modifier)
 {
-  if (!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;
+  bool tw_used = TwEventMouseButtonGLFW(button, action);
+  igl::Viewer::MouseButton mb;
 
-    if (action == GLFW_PRESS)
+  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
-      __viewer->mouse_up(mb,modifier);
+    }
+  } else
+  {
+    // Always call mouse_up on up
+    __viewer->mouse_up(mb,modifier);
   }
 
 }
@@ -504,12 +510,29 @@ static void glfw_window_size(GLFWwindow* window, int width, int height)
   __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] = 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))
+  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)
@@ -517,7 +540,6 @@ static void glfw_mouse_scroll(GLFWwindow* window, double x, double y)
   using namespace std;
   scroll_x += x;
   scroll_y += y;
-  cout<<"scroll: "<<x<<","<<y<<endl;
 
   if (!TwEventMouseWheelGLFW(scroll_y))
     __viewer->mouse_scroll(y);
@@ -633,7 +655,7 @@ namespace igl
     options.line_color << 0.0f, 0.0f, 0.0f;
 
     // Default lights settings
-    options.light_position << 0.0f, 0.30f, 5.0f;
+    options.light_position << 0.0f, -0.30f, -5.0f;
 
     // Default trackball
     options.trackball_angle << 0.0f, 0.0f, 0.0f, 1.0f;
@@ -1082,9 +1104,13 @@ namespace igl
         if (plugin_manager->plugin_list[i]->mouse_scroll(delta_y))
           return true;
 
-    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);
+    // 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;
   }
 
@@ -1724,6 +1750,7 @@ namespace igl
   void Viewer::draw()
   {
     using namespace std;
+    using namespace Eigen;
     glClearColor(options.background_color[0],
                  options.background_color[1],
                  options.background_color[2],
@@ -1804,7 +1831,8 @@ namespace igl
     GLint texture_factori       = opengl.shader_mesh.uniform("texture_factor");
 
     glUniform1f(specular_exponenti, options.shininess);
-    glUniform3fv(light_position_worldi, 1, options.light_position.data());
+    Vector3f rev_light = -1.*options.light_position;
+    glUniform3fv(light_position_worldi, 1, rev_light.data());
     glUniform1f(lighting_factori, 1.0f); // enables lighting
     glUniform4f(fixed_colori, 0.0, 0.0, 0.0, 0.0);
 

+ 5 - 0
include/igl/viewer/Viewer.h

@@ -577,4 +577,9 @@ namespace igl
 
 
 } // end namespace
+
+#ifdef IGL_HEADER_ONLY
+#  include "Viewer.cpp"
+#endif
+
 #endif

+ 0 - 1
tutorial/102_DrawMesh/main.cpp

@@ -1,4 +1,3 @@
-#define IGL_HEADER_ONLY
 #include <igl/readOFF.h>
 #include <igl/viewer/Viewer.h>
 

+ 4 - 4
tutorial/cmake/FindLIBIGL.cmake

@@ -24,10 +24,10 @@ FIND_PATH(LIBIGL_INCLUDE_DIR igl/readOBJ.h
 
 if(LIBIGL_INCLUDE_DIR)
    set(LIBIGL_FOUND TRUE)
-#   add_definitions(-DIGL_HEADER_ONLY)
-   set(LIBIGL_SOURCES
-      ${LIBIGL_INCLUDE_DIR}/igl/viewer/Viewer.cpp
-   )
+   add_definitions(-DIGL_HEADER_ONLY)
+   #set(LIBIGL_SOURCES
+   #   ${LIBIGL_INCLUDE_DIR}/igl/viewer/Viewer.cpp
+   #)
 endif(LIBIGL_INCLUDE_DIR)
 
 if(LIBIGL_FOUND)