ViewerCore.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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/snap_to_fixed_up.h>
  11. #include <igl/look_at.h>
  12. #include <igl/frustum.h>
  13. #include <igl/ortho.h>
  14. #include <igl/massmatrix.h>
  15. #include <igl/barycenter.h>
  16. #include <Eigen/Geometry>
  17. #include <iostream>
  18. IGL_INLINE void igl::opengl::ViewerCore::align_camera_center(
  19. const Eigen::MatrixXd& V,
  20. const Eigen::MatrixXi& F)
  21. {
  22. if(V.rows() == 0)
  23. return;
  24. get_scale_and_shift_to_fit_mesh(V,F,model_zoom,model_translation);
  25. // Rather than crash on empty mesh...
  26. if(V.size() > 0)
  27. {
  28. object_scale = (V.colwise().maxCoeff() - V.colwise().minCoeff()).norm();
  29. }
  30. }
  31. IGL_INLINE void igl::opengl::ViewerCore::get_scale_and_shift_to_fit_mesh(
  32. const Eigen::MatrixXd& V,
  33. const Eigen::MatrixXi& F,
  34. float& zoom,
  35. Eigen::Vector3f& shift)
  36. {
  37. if (V.rows() == 0)
  38. return;
  39. Eigen::MatrixXd BC;
  40. if (F.rows() <= 1)
  41. {
  42. BC = V;
  43. } else
  44. {
  45. igl::barycenter(V,F,BC);
  46. }
  47. return get_scale_and_shift_to_fit_mesh(BC,zoom,shift);
  48. }
  49. IGL_INLINE void igl::opengl::ViewerCore::align_camera_center(
  50. const Eigen::MatrixXd& V)
  51. {
  52. if(V.rows() == 0)
  53. return;
  54. get_scale_and_shift_to_fit_mesh(V,model_zoom,model_translation);
  55. // Rather than crash on empty mesh...
  56. if(V.size() > 0)
  57. {
  58. object_scale = (V.colwise().maxCoeff() - V.colwise().minCoeff()).norm();
  59. }
  60. }
  61. IGL_INLINE void igl::opengl::ViewerCore::get_scale_and_shift_to_fit_mesh(
  62. const Eigen::MatrixXd& V,
  63. float& zoom,
  64. Eigen::Vector3f& shift)
  65. {
  66. if (V.rows() == 0)
  67. return;
  68. auto min_point = V.colwise().minCoeff();
  69. auto max_point = V.colwise().maxCoeff();
  70. auto centroid = (0.5*(min_point + max_point)).eval();
  71. shift.setConstant(0);
  72. shift.head(centroid.size()) = -centroid.cast<float>();
  73. zoom = 2.0 / (max_point-min_point).array().abs().maxCoeff();
  74. }
  75. IGL_INLINE void igl::opengl::ViewerCore::clear_framebuffers()
  76. {
  77. glClearColor(background_color[0],
  78. background_color[1],
  79. background_color[2],
  80. 1.0f);
  81. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  82. }
  83. IGL_INLINE void igl::opengl::ViewerCore::draw(
  84. ViewerData& data,
  85. State& opengl,
  86. bool update_matrices)
  87. {
  88. using namespace std;
  89. using namespace Eigen;
  90. if (depth_test)
  91. glEnable(GL_DEPTH_TEST);
  92. else
  93. glDisable(GL_DEPTH_TEST);
  94. glEnable(GL_BLEND);
  95. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  96. /* Bind and potentially refresh mesh/line/point data */
  97. if (data.dirty)
  98. {
  99. opengl.set_data(data, invert_normals);
  100. data.dirty = ViewerData::DIRTY_NONE;
  101. }
  102. opengl.bind_mesh();
  103. // Initialize uniform
  104. glViewport(viewport(0), viewport(1), viewport(2), viewport(3));
  105. if(update_matrices)
  106. {
  107. model = Eigen::Matrix4f::Identity();
  108. view = Eigen::Matrix4f::Identity();
  109. proj = Eigen::Matrix4f::Identity();
  110. // Set view
  111. look_at( camera_eye, camera_center, camera_up, view);
  112. float width = viewport(2);
  113. float height = viewport(3);
  114. // Set projection
  115. if (orthographic)
  116. {
  117. float length = (camera_eye - camera_center).norm();
  118. float h = tan(camera_view_angle/360.0 * M_PI) * (length);
  119. ortho(-h*width/height, h*width/height, -h, h, camera_dnear, camera_dfar,proj);
  120. }
  121. else
  122. {
  123. float fH = tan(camera_view_angle / 360.0 * M_PI) * camera_dnear;
  124. float fW = fH * (double)width/(double)height;
  125. frustum(-fW, fW, -fH, fH, camera_dnear, camera_dfar,proj);
  126. }
  127. // end projection
  128. // Set model transformation
  129. float mat[16];
  130. igl::quat_to_mat(trackball_angle.coeffs().data(), mat);
  131. for (unsigned i=0;i<4;++i)
  132. for (unsigned j=0;j<4;++j)
  133. model(i,j) = mat[i+4*j];
  134. // Why not just use Eigen::Transform<double,3,Projective> for model...?
  135. model.topLeftCorner(3,3)*=camera_zoom;
  136. model.topLeftCorner(3,3)*=model_zoom;
  137. model.col(3).head(3) += model.topLeftCorner(3,3)*model_translation;
  138. }
  139. // Send transformations to the GPU
  140. GLint modeli = opengl.shader_mesh.uniform("model");
  141. GLint viewi = opengl.shader_mesh.uniform("view");
  142. GLint proji = opengl.shader_mesh.uniform("proj");
  143. glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
  144. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  145. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  146. // Light parameters
  147. GLint specular_exponenti = opengl.shader_mesh.uniform("specular_exponent");
  148. GLint light_position_worldi = opengl.shader_mesh.uniform("light_position_world");
  149. GLint lighting_factori = opengl.shader_mesh.uniform("lighting_factor");
  150. GLint fixed_colori = opengl.shader_mesh.uniform("fixed_color");
  151. GLint texture_factori = opengl.shader_mesh.uniform("texture_factor");
  152. glUniform1f(specular_exponenti, shininess);
  153. Vector3f rev_light = -1.*light_position;
  154. glUniform3fv(light_position_worldi, 1, rev_light.data());
  155. glUniform1f(lighting_factori, lighting_factor); // enables lighting
  156. glUniform4f(fixed_colori, 0.0, 0.0, 0.0, 0.0);
  157. if (data.V.rows()>0)
  158. {
  159. // Render fill
  160. if (show_faces)
  161. {
  162. // Texture
  163. glUniform1f(texture_factori, show_texture ? 1.0f : 0.0f);
  164. opengl.draw_mesh(true);
  165. glUniform1f(texture_factori, 0.0f);
  166. }
  167. // Render wireframe
  168. if (show_lines)
  169. {
  170. glLineWidth(line_width);
  171. glUniform4f(fixed_colori, line_color[0], line_color[1],
  172. line_color[2], 1.0f);
  173. opengl.draw_mesh(false);
  174. glUniform4f(fixed_colori, 0.0f, 0.0f, 0.0f, 0.0f);
  175. }
  176. #ifdef IGL_VIEWER_WITH_NANOGUI
  177. if (show_vertid)
  178. {
  179. textrenderer.BeginDraw(view*model, proj, viewport, object_scale);
  180. for (int i=0; i<data.V.rows(); ++i)
  181. textrenderer.DrawText(data.V.row(i),data.V_normals.row(i),to_string(i));
  182. textrenderer.EndDraw();
  183. }
  184. if (show_faceid)
  185. {
  186. textrenderer.BeginDraw(view*model, proj, viewport, object_scale);
  187. for (int i=0; i<data.F.rows(); ++i)
  188. {
  189. Eigen::RowVector3d p = Eigen::RowVector3d::Zero();
  190. for (int j=0;j<data.F.cols();++j)
  191. p += data.V.row(data.F(i,j));
  192. p /= data.F.cols();
  193. textrenderer.DrawText(p, data.F_normals.row(i), to_string(i));
  194. }
  195. textrenderer.EndDraw();
  196. }
  197. #endif
  198. }
  199. if (show_overlay)
  200. {
  201. if (show_overlay_depth)
  202. glEnable(GL_DEPTH_TEST);
  203. else
  204. glDisable(GL_DEPTH_TEST);
  205. if (data.lines.rows() > 0)
  206. {
  207. opengl.bind_overlay_lines();
  208. modeli = opengl.shader_overlay_lines.uniform("model");
  209. viewi = opengl.shader_overlay_lines.uniform("view");
  210. proji = opengl.shader_overlay_lines.uniform("proj");
  211. glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
  212. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  213. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  214. // This must be enabled, otherwise glLineWidth has no effect
  215. glEnable(GL_LINE_SMOOTH);
  216. glLineWidth(line_width);
  217. opengl.draw_overlay_lines();
  218. }
  219. if (data.points.rows() > 0)
  220. {
  221. opengl.bind_overlay_points();
  222. modeli = opengl.shader_overlay_points.uniform("model");
  223. viewi = opengl.shader_overlay_points.uniform("view");
  224. proji = opengl.shader_overlay_points.uniform("proj");
  225. glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
  226. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  227. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  228. glPointSize(point_size);
  229. opengl.draw_overlay_points();
  230. }
  231. #ifdef IGL_VIEWER_WITH_NANOGUI
  232. if (data.labels_positions.rows() > 0)
  233. {
  234. textrenderer.BeginDraw(view*model, proj, viewport, object_scale);
  235. for (int i=0; i<data.labels_positions.rows(); ++i)
  236. textrenderer.DrawText(data.labels_positions.row(i), Eigen::Vector3d(0.0,0.0,0.0),
  237. data.labels_strings[i]);
  238. textrenderer.EndDraw();
  239. }
  240. #endif
  241. glEnable(GL_DEPTH_TEST);
  242. }
  243. }
  244. IGL_INLINE void igl::opengl::ViewerCore::draw_buffer(ViewerData& data,
  245. State& opengl,
  246. bool update_matrices,
  247. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  248. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  249. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
  250. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A)
  251. {
  252. assert(R.rows() == G.rows() && G.rows() == B.rows() && B.rows() == A.rows());
  253. assert(R.cols() == G.cols() && G.cols() == B.cols() && B.cols() == A.cols());
  254. unsigned x = R.rows();
  255. unsigned y = R.cols();
  256. // Create frame buffer
  257. GLuint frameBuffer;
  258. glGenFramebuffers(1, &frameBuffer);
  259. glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
  260. // Create texture to hold color buffer
  261. GLuint texColorBuffer;
  262. glGenTextures(1, &texColorBuffer);
  263. glBindTexture(GL_TEXTURE_2D, texColorBuffer);
  264. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  265. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  266. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  267. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0);
  268. // Create Renderbuffer Object to hold depth and stencil buffers
  269. GLuint rboDepthStencil;
  270. glGenRenderbuffers(1, &rboDepthStencil);
  271. glBindRenderbuffer(GL_RENDERBUFFER, rboDepthStencil);
  272. glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, x, y);
  273. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rboDepthStencil);
  274. assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
  275. glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
  276. // Clear the buffer
  277. glClearColor(background_color(0), background_color(1), background_color(2), 0.f);
  278. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  279. // Save old viewport
  280. Eigen::Vector4f viewport_ori = viewport;
  281. viewport << 0,0,x,y;
  282. // Draw
  283. draw(data,opengl,update_matrices);
  284. // Restore viewport
  285. viewport = viewport_ori;
  286. // Copy back in the given Eigen matrices
  287. GLubyte* pixels = (GLubyte*)calloc(x*y*4,sizeof(GLubyte));
  288. glReadPixels
  289. (
  290. 0, 0,
  291. x, y,
  292. GL_RGBA, GL_UNSIGNED_BYTE, pixels
  293. );
  294. int count = 0;
  295. for (unsigned j=0; j<y; ++j)
  296. {
  297. for (unsigned i=0; i<x; ++i)
  298. {
  299. R(i,j) = pixels[count*4+0];
  300. G(i,j) = pixels[count*4+1];
  301. B(i,j) = pixels[count*4+2];
  302. A(i,j) = pixels[count*4+3];
  303. ++count;
  304. }
  305. }
  306. // Clean up
  307. free(pixels);
  308. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  309. glDeleteRenderbuffers(1, &rboDepthStencil);
  310. glDeleteTextures(1, &texColorBuffer);
  311. glDeleteFramebuffers(1, &frameBuffer);
  312. }
  313. IGL_INLINE void igl::opengl::ViewerCore::set_rotation_type(
  314. const igl::opengl::ViewerCore::RotationType & value)
  315. {
  316. using namespace Eigen;
  317. using namespace std;
  318. const RotationType old_rotation_type = rotation_type;
  319. rotation_type = value;
  320. if(rotation_type == ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP &&
  321. old_rotation_type != ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP)
  322. {
  323. snap_to_fixed_up(Quaternionf(trackball_angle),trackball_angle);
  324. }
  325. }
  326. IGL_INLINE igl::opengl::ViewerCore::ViewerCore()
  327. {
  328. // Default shininess
  329. shininess = 35.0f;
  330. // Default colors
  331. background_color << 0.3f, 0.3f, 0.5f, 1.0f;
  332. line_color << 0.0f, 0.0f, 0.0f, 1.0f;
  333. // Default lights settings
  334. light_position << 0.0f, -0.30f, -5.0f;
  335. lighting_factor = 1.0f; //on
  336. // Default trackball
  337. trackball_angle = Eigen::Quaternionf::Identity();
  338. set_rotation_type(ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP);
  339. // Defalut model viewing parameters
  340. model_zoom = 1.0f;
  341. model_translation << 0,0,0;
  342. // Camera parameters
  343. camera_zoom = 1.0f;
  344. orthographic = false;
  345. camera_view_angle = 45.0;
  346. camera_dnear = 1.0;
  347. camera_dfar = 100.0;
  348. camera_eye << 0, 0, 5;
  349. camera_center << 0, 0, 0;
  350. camera_up << 0, 1, 0;
  351. // Default visualization options
  352. show_faces = true;
  353. show_lines = true;
  354. invert_normals = false;
  355. show_overlay = true;
  356. show_overlay_depth = true;
  357. show_vertid = false;
  358. show_faceid = false;
  359. show_texture = false;
  360. depth_test = true;
  361. // Default point size / line width
  362. point_size = 30;
  363. line_width = 0.5f;
  364. is_animating = false;
  365. animation_max_fps = 30.;
  366. }
  367. IGL_INLINE void igl::opengl::ViewerCore::init()
  368. {
  369. #ifdef IGL_VIEWER_WITH_NANOGUI
  370. textrenderer.Init();
  371. #endif
  372. }
  373. IGL_INLINE void igl::opengl::ViewerCore::shut()
  374. {
  375. #ifdef IGL_VIEWER_WITH_NANOGUI
  376. textrenderer.Shut();
  377. #endif
  378. }