ViewerCore.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "ViewerCore.h"
  9. #include <igl/quat_to_mat.h>
  10. #include <igl/massmatrix.h>
  11. #include <Eigen/Geometry>
  12. Eigen::Matrix4f lookAt (
  13. const Eigen::Vector3f& eye,
  14. const Eigen::Vector3f& center,
  15. const Eigen::Vector3f& up)
  16. {
  17. Eigen::Vector3f f = (center - eye).normalized();
  18. Eigen::Vector3f s = f.cross(up).normalized();
  19. Eigen::Vector3f u = s.cross(f);
  20. Eigen::Matrix4f Result = Eigen::Matrix4f::Identity();
  21. Result(0,0) = s(0);
  22. Result(0,1) = s(1);
  23. Result(0,2) = s(2);
  24. Result(1,0) = u(0);
  25. Result(1,1) = u(1);
  26. Result(1,2) = u(2);
  27. Result(2,0) =-f(0);
  28. Result(2,1) =-f(1);
  29. Result(2,2) =-f(2);
  30. Result(0,3) =-s.transpose() * eye;
  31. Result(1,3) =-u.transpose() * eye;
  32. Result(2,3) = f.transpose() * eye;
  33. return Result;
  34. }
  35. Eigen::Matrix4f ortho (
  36. const float left,
  37. const float right,
  38. const float bottom,
  39. const float top,
  40. const float zNear,
  41. const float zFar
  42. )
  43. {
  44. Eigen::Matrix4f Result = Eigen::Matrix4f::Identity();
  45. Result(0,0) = 2.0f / (right - left);
  46. Result(1,1) = 2.0f / (top - bottom);
  47. Result(2,2) = - 2.0f / (zFar - zNear);
  48. Result(0,3) = - (right + left) / (right - left);
  49. Result(1,3) = - (top + bottom) / (top - bottom);
  50. Result(2,3) = - (zFar + zNear) / (zFar - zNear);
  51. return Result;
  52. }
  53. Eigen::Matrix4f frustum (
  54. const float left,
  55. const float right,
  56. const float bottom,
  57. const float top,
  58. const float nearVal,
  59. const float farVal)
  60. {
  61. Eigen::Matrix4f Result = Eigen::Matrix4f::Zero();
  62. Result(0,0) = (2.0f * nearVal) / (right - left);
  63. Result(1,1) = (2.0f * nearVal) / (top - bottom);
  64. Result(0,2) = (right + left) / (right - left);
  65. Result(1,2) = (top + bottom) / (top - bottom);
  66. Result(2,2) = -(farVal + nearVal) / (farVal - nearVal);
  67. Result(3,2) = -1.0f;
  68. Result(2,3) = -(2.0f * farVal * nearVal) / (farVal - nearVal);
  69. return Result;
  70. }
  71. Eigen::Matrix4f scale (const Eigen::Matrix4f& m,
  72. const Eigen::Vector3f& v)
  73. {
  74. Eigen::Matrix4f Result;
  75. Result.col(0) = m.col(0).array() * v(0);
  76. Result.col(1) = m.col(1).array() * v(1);
  77. Result.col(2) = m.col(2).array() * v(2);
  78. Result.col(3) = m.col(3);
  79. return Result;
  80. }
  81. Eigen::Matrix4f translate(
  82. const Eigen::Matrix4f& m,
  83. const Eigen::Vector3f& v)
  84. {
  85. Eigen::Matrix4f Result = m;
  86. 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();
  87. return Result;
  88. }
  89. void igl::ViewerCore::InitSerialization()
  90. {
  91. Add(shininess, "shininess");
  92. Add(background_color, "background_color");
  93. Add(line_color, "line_color");
  94. Add(light_position, "light_position");
  95. Add(lighting_factor, "lighting_factor");
  96. Add(trackball_angle, "trackball_angle");
  97. Add(model_zoom, "model_zoom");
  98. Add(model_translation, "model_translation");
  99. Add(model_zoom_uv, "model_zoom_uv");
  100. Add(model_translation_uv, "model_translation_uv");
  101. Add(object_scale, "object_scale");
  102. Add(camera_zoom, "camera_zoom");
  103. Add(orthographic, "orthographic");
  104. Add(camera_view_angle, "camera_view_angle");
  105. Add(camera_dnear, "camera_dnear");
  106. Add(camera_dfar, "camera_dfar");
  107. Add(camera_eye, "camera_eye");
  108. Add(camera_center, "camera_center");
  109. Add(camera_up, "camera_up");
  110. Add(show_faces, "show_faces");
  111. Add(show_lines, "show_lines");
  112. Add(invert_normals, "invert_normals");
  113. Add(show_overlay, "show_overlay");
  114. Add(show_overlay_depth, "show_overlay_depth");
  115. Add(show_vertid, "show_vertid");
  116. Add(show_faceid, "show_faceid");
  117. Add(show_texture, "show_texture");
  118. Add(point_size, "point_size");
  119. Add(line_width, "line_width");
  120. Add(is_animating, "is_animating");
  121. Add(animation_max_fps, "animation_max_fps");
  122. Add(viewport, "viewport");
  123. Add(view, "view");
  124. Add(model, "model");
  125. Add(proj, "proj");
  126. }
  127. IGL_INLINE void igl::ViewerCore::align_camera_center(
  128. const Eigen::MatrixXd& V,
  129. const Eigen::MatrixXi& F)
  130. {
  131. if(V.rows() == 0)
  132. return;
  133. get_scale_and_shift_to_fit_mesh(V,F,model_zoom,model_translation);
  134. object_scale = (V.colwise().maxCoeff() - V.colwise().minCoeff()).norm();
  135. }
  136. IGL_INLINE void igl::ViewerCore::get_scale_and_shift_to_fit_mesh(
  137. const Eigen::MatrixXd& V,
  138. const Eigen::MatrixXi& F,
  139. float& zoom,
  140. Eigen::Vector3f& shift)
  141. {
  142. if (V.rows() == 0)
  143. return;
  144. //Eigen::SparseMatrix<double> M;
  145. //igl::massmatrix(V,F,igl::MASSMATRIX_TYPE_VORONOI,M);
  146. //const auto & MV = M*V;
  147. //Eigen::RowVector3d centroid = MV.colwise().sum()/M.diagonal().sum();
  148. Eigen::RowVector3d min_point = V.colwise().minCoeff();
  149. Eigen::RowVector3d max_point = V.colwise().maxCoeff();
  150. Eigen::RowVector3d centroid = 0.5*(min_point + max_point);
  151. shift = -centroid.cast<float>();
  152. double x_scale = fabs(max_point[0] - min_point[0]);
  153. double y_scale = fabs(max_point[1] - min_point[1]);
  154. double z_scale = fabs(max_point[2] - min_point[2]);
  155. zoom = 2.0 / std::max(z_scale,std::max(x_scale,y_scale));
  156. }
  157. IGL_INLINE void igl::ViewerCore::clear_framebuffers()
  158. {
  159. glClearColor(background_color[0],
  160. background_color[1],
  161. background_color[2],
  162. 1.0f);
  163. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  164. }
  165. IGL_INLINE void igl::ViewerCore::draw(ViewerData& data, OpenGL_state& opengl)
  166. {
  167. using namespace std;
  168. using namespace Eigen;
  169. glEnable(GL_DEPTH_TEST);
  170. /* Bind and potentially refresh mesh/line/point data */
  171. if (data.dirty)
  172. {
  173. opengl.set_data(data, invert_normals);
  174. data.dirty = ViewerData::DIRTY_NONE;
  175. }
  176. opengl.bind_mesh();
  177. // Initialize uniform
  178. glViewport(viewport(0), viewport(1), viewport(2), viewport(3));
  179. model = Eigen::Matrix4f::Identity();
  180. view = Eigen::Matrix4f::Identity();
  181. proj = Eigen::Matrix4f::Identity();
  182. // Set view
  183. view = lookAt(Eigen::Vector3f(camera_eye[0], camera_eye[1], camera_eye[2]),
  184. Eigen::Vector3f(camera_center[0], camera_center[1], camera_center[2]),
  185. Eigen::Vector3f(camera_up[0], camera_up[1], camera_up[2]));
  186. float width = viewport(2);
  187. float height = viewport(3);
  188. // Set projection
  189. if (orthographic)
  190. {
  191. float length = (camera_eye - camera_center).norm();
  192. float h = tan(camera_view_angle/360.0 * M_PI) * (length);
  193. proj = ortho(-h*width/height, h*width/height, -h, h, camera_dnear, camera_dfar);
  194. }
  195. else
  196. {
  197. float fH = tan(camera_view_angle / 360.0 * M_PI) * camera_dnear;
  198. float fW = fH * (double)width/(double)height;
  199. proj = frustum(-fW, fW, -fH, fH, camera_dnear, camera_dfar);
  200. }
  201. // end projection
  202. // Set model transformation
  203. float mat[16];
  204. igl::quat_to_mat(trackball_angle.data(), mat);
  205. for (unsigned i=0;i<4;++i)
  206. for (unsigned j=0;j<4;++j)
  207. model(i,j) = mat[i+4*j];
  208. model = scale(model, Eigen::Vector3f(camera_zoom,camera_zoom,camera_zoom));
  209. model = scale(model, Eigen::Vector3f(model_zoom,model_zoom,model_zoom));
  210. model = translate(model, Eigen::Vector3f(model_translation[0],model_translation[1],model_translation[2]));
  211. // Send transformations to the GPU
  212. GLint modeli = opengl.shader_mesh.uniform("model");
  213. GLint viewi = opengl.shader_mesh.uniform("view");
  214. GLint proji = opengl.shader_mesh.uniform("proj");
  215. glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
  216. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  217. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  218. // Light parameters
  219. GLint specular_exponenti = opengl.shader_mesh.uniform("specular_exponent");
  220. GLint light_position_worldi = opengl.shader_mesh.uniform("light_position_world");
  221. GLint lighting_factori = opengl.shader_mesh.uniform("lighting_factor");
  222. GLint fixed_colori = opengl.shader_mesh.uniform("fixed_color");
  223. GLint texture_factori = opengl.shader_mesh.uniform("texture_factor");
  224. glUniform1f(specular_exponenti, shininess);
  225. Vector3f rev_light = -1.*light_position;
  226. glUniform3fv(light_position_worldi, 1, rev_light.data());
  227. glUniform1f(lighting_factori, lighting_factor); // enables lighting
  228. glUniform4f(fixed_colori, 0.0, 0.0, 0.0, 0.0);
  229. if (data.V.rows()>0)
  230. {
  231. // Render fill
  232. if (show_faces)
  233. {
  234. // Texture
  235. glUniform1f(texture_factori, show_texture ? 1.0f : 0.0f);
  236. opengl.draw_mesh(true);
  237. glUniform1f(texture_factori, 0.0f);
  238. }
  239. // Render wireframe
  240. if (show_lines)
  241. {
  242. glLineWidth(line_width);
  243. glUniform4f(fixed_colori, line_color[0], line_color[1],
  244. line_color[2], 1.0f);
  245. opengl.draw_mesh(false);
  246. glUniform4f(fixed_colori, 0.0f, 0.0f, 0.0f, 0.0f);
  247. }
  248. if (show_vertid)
  249. {
  250. textrenderer.BeginDraw(view*model, proj, viewport, object_scale);
  251. for (int i=0; i<data.V.rows(); ++i)
  252. textrenderer.DrawText(data.V.row(i), data.V_normals.row(i), to_string(i));
  253. textrenderer.EndDraw();
  254. }
  255. if (show_faceid)
  256. {
  257. textrenderer.BeginDraw(view*model, proj, viewport, object_scale);
  258. for (int i=0; i<data.F.rows(); ++i)
  259. {
  260. Eigen::RowVector3d p = Eigen::RowVector3d::Zero();
  261. for (int j=0;j<data.F.cols();++j)
  262. p += data.V.row(data.F(i,j));
  263. p /= data.F.cols();
  264. textrenderer.DrawText(p, data.F_normals.row(i), to_string(i));
  265. }
  266. textrenderer.EndDraw();
  267. }
  268. }
  269. if (show_overlay)
  270. {
  271. if (show_overlay_depth)
  272. glEnable(GL_DEPTH_TEST);
  273. else
  274. glDisable(GL_DEPTH_TEST);
  275. if (data.lines.rows() > 0)
  276. {
  277. opengl.bind_overlay_lines();
  278. modeli = opengl.shader_overlay_lines.uniform("model");
  279. viewi = opengl.shader_overlay_lines.uniform("view");
  280. proji = opengl.shader_overlay_lines.uniform("proj");
  281. glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
  282. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  283. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  284. // This must be enabled, otherwise glLineWidth has no effect
  285. glEnable(GL_LINE_SMOOTH);
  286. glLineWidth(line_width);
  287. opengl.draw_overlay_lines();
  288. }
  289. if (data.points.rows() > 0)
  290. {
  291. opengl.bind_overlay_points();
  292. modeli = opengl.shader_overlay_points.uniform("model");
  293. viewi = opengl.shader_overlay_points.uniform("view");
  294. proji = opengl.shader_overlay_points.uniform("proj");
  295. glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
  296. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  297. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  298. glPointSize(point_size);
  299. opengl.draw_overlay_points();
  300. }
  301. if (data.labels_positions.rows() > 0)
  302. {
  303. textrenderer.BeginDraw(view*model, proj, viewport, object_scale);
  304. for (int i=0; i<data.labels_positions.rows(); ++i)
  305. textrenderer.DrawText(data.labels_positions.row(i), Eigen::Vector3d(0.0,0.0,0.0),
  306. data.labels_strings[i]);
  307. textrenderer.EndDraw();
  308. }
  309. glEnable(GL_DEPTH_TEST);
  310. }
  311. }
  312. IGL_INLINE igl::ViewerCore::ViewerCore()
  313. {
  314. // Default shininess
  315. shininess = 35.0f;
  316. // Default colors
  317. background_color << 0.3f, 0.3f, 0.5f;
  318. line_color << 0.0f, 0.0f, 0.0f;
  319. // Default lights settings
  320. light_position << 0.0f, -0.30f, -5.0f;
  321. lighting_factor = 1.0f; //on
  322. // Default trackball
  323. trackball_angle << 0.0f, 0.0f, 0.0f, 1.0f;
  324. // Defalut model viewing parameters
  325. model_zoom = 1.0f;
  326. model_translation << 0,0,0;
  327. // Camera parameters
  328. camera_zoom = 1.0f;
  329. orthographic = false;
  330. camera_view_angle = 45.0;
  331. camera_dnear = 1.0;
  332. camera_dfar = 100.0;
  333. camera_eye << 0, 0, 5;
  334. camera_center << 0, 0, 0;
  335. camera_up << 0, 1, 0;
  336. // Default visualization options
  337. show_faces = true;
  338. show_lines = true;
  339. invert_normals = false;
  340. show_overlay = true;
  341. show_overlay_depth = true;
  342. show_vertid = false;
  343. show_faceid = false;
  344. show_texture = false;
  345. // Default point size / line width
  346. point_size = 15;
  347. line_width = 0.5f;
  348. is_animating = false;
  349. animation_max_fps = 30.;
  350. }
  351. IGL_INLINE void igl::ViewerCore::init()
  352. {
  353. textrenderer.Init();
  354. }
  355. IGL_INLINE void igl::ViewerCore::shut()
  356. {
  357. textrenderer.Shut();
  358. }