ViewerCore.cpp 13 KB

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