map_texture.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #ifdef IGL_OPENGL_4
  2. #include "map_texture.h"
  3. #include "background_window.h"
  4. #include "../create_shader_program.h"
  5. #include "../gl.h"
  6. #include <GLFW/glfw3.h>
  7. #include <iostream>
  8. #include <string>
  9. template <typename DerivedV, typename DerivedF, typename DerivedU>
  10. IGL_INLINE bool igl::opengl::glfw::map_texture(
  11. const Eigen::MatrixBase<DerivedV> & _V,
  12. const Eigen::MatrixBase<DerivedF> & _F,
  13. const Eigen::MatrixBase<DerivedU> & _U,
  14. const unsigned char * in_data,
  15. const int w,
  16. const int h,
  17. const int nc,
  18. std::vector<unsigned char> & out_data)
  19. {
  20. int out_w = w;
  21. int out_h = h;
  22. int out_nc = nc;
  23. return map_texture(_V,_F,_U,in_data,w,h,nc,out_data,out_w,out_h,out_nc);
  24. }
  25. template <typename DerivedV, typename DerivedF, typename DerivedU>
  26. IGL_INLINE bool igl::opengl::glfw::map_texture(
  27. const Eigen::MatrixBase<DerivedV> & _V,
  28. const Eigen::MatrixBase<DerivedF> & _F,
  29. const Eigen::MatrixBase<DerivedU> & _U,
  30. const unsigned char * in_data,
  31. const int w,
  32. const int h,
  33. const int nc,
  34. std::vector<unsigned char> & out_data,
  35. int & out_w,
  36. int & out_h,
  37. int & out_nc)
  38. {
  39. const auto fail = [](const std::string msg)
  40. {
  41. std::cerr<<msg<<std::endl;
  42. glfwTerminate();
  43. return false;
  44. };
  45. // Force inputs to be RowMajor at the cost of a copy
  46. Eigen::Matrix<
  47. double,
  48. DerivedV::RowsAtCompileTime,
  49. DerivedV::ColsAtCompileTime,
  50. Eigen::RowMajor> V = _V.template cast<double>();
  51. Eigen::Matrix<
  52. double,
  53. DerivedU::RowsAtCompileTime,
  54. DerivedU::ColsAtCompileTime,
  55. Eigen::RowMajor> U = _U.template cast<double>();
  56. Eigen::Matrix<
  57. int,
  58. DerivedF::RowsAtCompileTime,
  59. DerivedF::ColsAtCompileTime,
  60. Eigen::RowMajor> F = _F.template cast<int>();
  61. const int dim = U.cols();
  62. GLFWwindow * window;
  63. if(!background_window(window))
  64. {
  65. fail("Could not initialize glfw window");
  66. }
  67. // Compile each shader
  68. std::string vertex_shader = dim == 2 ?
  69. R"(
  70. #version 330 core
  71. layout(location = 0) in vec2 position;
  72. layout(location = 1) in vec2 tex_coord_v;
  73. out vec2 tex_coord_f;
  74. void main()
  75. {
  76. tex_coord_f = vec2(tex_coord_v.x,1.-tex_coord_v.y);
  77. gl_Position = vec4( 2.*position.x-1., 2.*(1.-position.y)-1., 0.,1.);
  78. }
  79. )"
  80. :
  81. R"(
  82. #version 330 core
  83. layout(location = 0) in vec3 position;
  84. layout(location = 1) in vec2 tex_coord_v;
  85. out vec2 tex_coord_f;
  86. void main()
  87. {
  88. tex_coord_f = vec2(tex_coord_v.x,1.-tex_coord_v.y);
  89. gl_Position = vec4( 2.*position.x-1., 2.*(1.-position.y)-1., position.z,1.);
  90. }
  91. )"
  92. ;
  93. std::string fragment_shader = R"(
  94. #version 330 core
  95. layout(location = 0) out vec3 color;
  96. uniform sampler2D tex;
  97. in vec2 tex_coord_f;
  98. void main()
  99. {
  100. color = texture(tex,tex_coord_f).rgb;
  101. }
  102. )";
  103. GLuint prog_id =
  104. igl::opengl::create_shader_program(vertex_shader,fragment_shader,{});
  105. glUniform1i(glGetUniformLocation(prog_id, "tex"),0);
  106. // Generate and attach buffers to vertex array
  107. glDisable(GL_CULL_FACE);
  108. GLuint VAO = 0;
  109. glGenVertexArrays(1,&VAO);
  110. glBindVertexArray(VAO);
  111. GLuint ibo,vbo,tbo;
  112. glGenBuffers(1,&ibo);
  113. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  114. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*F.size(), F.data(), GL_STATIC_DRAW);
  115. glGenBuffers(1,&vbo);
  116. glEnableVertexAttribArray(0);
  117. glBindBuffer(GL_ARRAY_BUFFER,vbo);
  118. glBufferData(GL_ARRAY_BUFFER, sizeof(double)*U.size(), U.data(), GL_STATIC_DRAW);
  119. glVertexAttribLPointer(0, U.cols(), GL_DOUBLE, U.cols() * sizeof(GLdouble), (GLvoid*)0);
  120. glGenBuffers(1,&tbo);
  121. glEnableVertexAttribArray(1);
  122. glBindBuffer(GL_ARRAY_BUFFER,tbo);
  123. glBufferData(GL_ARRAY_BUFFER, sizeof(double)*V.size(), V.data(), GL_STATIC_DRAW);
  124. glVertexAttribLPointer(1, V.cols(), GL_DOUBLE, V.cols() * sizeof(GLdouble), (GLvoid*)0);
  125. glBindVertexArray(0);
  126. glBindBuffer(GL_ARRAY_BUFFER, 0);
  127. glBindVertexArray(0);
  128. // Prepare texture
  129. GLuint in_tex;
  130. GLenum format;
  131. {
  132. format = nc==1 ? GL_RED : (nc==3 ? GL_RGB : (nc == 4 ? GL_RGBA : GL_FALSE));
  133. glGenTextures(1, &in_tex);
  134. glBindTexture(GL_TEXTURE_2D, in_tex);
  135. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  136. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  137. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  138. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  139. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  140. glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, w, h, 0,format, GL_UNSIGNED_BYTE, in_data);
  141. }
  142. // Prepare framebuffer
  143. GLuint fb = 0;
  144. glGenFramebuffers(1, &fb);
  145. glBindFramebuffer(GL_FRAMEBUFFER, fb);
  146. GLuint out_tex;
  147. glGenTextures(1, &out_tex);
  148. glBindTexture(GL_TEXTURE_2D, out_tex);
  149. // always use float for internal storage
  150. assert(out_nc == 3);
  151. glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, out_w, out_h, 0,GL_RGB, GL_FLOAT, 0);
  152. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  153. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  154. glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, out_tex, 0);
  155. {
  156. GLenum bufs[1] = {GL_COLOR_ATTACHMENT0};
  157. glDrawBuffers(1, bufs);
  158. }
  159. if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
  160. {
  161. fail("framebuffer setup failed.");
  162. }
  163. glBindFramebuffer(GL_FRAMEBUFFER, fb);
  164. // clear screen and set viewport
  165. glClearColor(0.0,1.0,0.0,0.);
  166. glClear(GL_COLOR_BUFFER_BIT);
  167. glViewport(0,0,out_w,out_h);
  168. // Attach shader program
  169. glUseProgram(prog_id);
  170. glActiveTexture(GL_TEXTURE0 + 0);
  171. glBindTexture(GL_TEXTURE_2D, in_tex);
  172. // Draw mesh as wireframe
  173. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  174. glBindVertexArray(VAO);
  175. glDrawElements(GL_TRIANGLES, F.size(), GL_UNSIGNED_INT, 0);
  176. glBindVertexArray(0);
  177. // Write into memory
  178. assert(out_nc == 3);
  179. out_data.resize(out_nc*out_w*out_h);
  180. glBindTexture(GL_TEXTURE_2D, out_tex);
  181. glGetTexImage(GL_TEXTURE_2D, 0, format, GL_UNSIGNED_BYTE, &out_data[0]);
  182. // OpenGL cleanup
  183. glDeleteBuffers(1,&fb);
  184. glDeleteBuffers(1,&ibo);
  185. glDeleteBuffers(1,&vbo);
  186. glDeleteBuffers(1,&tbo);
  187. glDeleteTextures(1,&in_tex);
  188. glDeleteTextures(1,&out_tex);
  189. glDeleteVertexArrays(1,&VAO);
  190. glUseProgram(0);
  191. glDeleteProgram(prog_id);
  192. // GLFW cleanup
  193. glfwDestroyWindow(window);
  194. glfwTerminate();
  195. return true;
  196. }
  197. #ifdef IGL_STATIC_LIBRARY
  198. // Explicit template instantiation
  199. // generated by autoexplicit.sh
  200. template bool igl::opengl::glfw::map_texture<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, 3, 1, -1, 3>, Eigen::Matrix<double, -1, -1, 1, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, unsigned char const*, int, int, int, std::vector<unsigned char, std::allocator<unsigned char> >&);
  201. #endif
  202. #endif // IGL_OPENGL_4