|
@@ -20,6 +20,27 @@ IGL_INLINE bool igl::opengl::glfw::map_texture(
|
|
|
const int h,
|
|
|
const int nc,
|
|
|
std::vector<unsigned char> & out_data)
|
|
|
+{
|
|
|
+ int out_w = w;
|
|
|
+ int out_h = h;
|
|
|
+ int out_nc = nc;
|
|
|
+ return map_texture(_V,_F,_U,in_data,w,h,nc,out_data,out_w,out_h,out_nc);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+template <typename DerivedV, typename DerivedF, typename DerivedU>
|
|
|
+IGL_INLINE bool igl::opengl::glfw::map_texture(
|
|
|
+ const Eigen::MatrixBase<DerivedV> & _V,
|
|
|
+ const Eigen::MatrixBase<DerivedF> & _F,
|
|
|
+ const Eigen::MatrixBase<DerivedU> & _U,
|
|
|
+ const unsigned char * in_data,
|
|
|
+ const int w,
|
|
|
+ const int h,
|
|
|
+ const int nc,
|
|
|
+ std::vector<unsigned char> & out_data,
|
|
|
+ int & out_w,
|
|
|
+ int & out_h,
|
|
|
+ int & out_nc)
|
|
|
{
|
|
|
const auto fail = [](const std::string msg)
|
|
|
{
|
|
@@ -43,6 +64,7 @@ IGL_INLINE bool igl::opengl::glfw::map_texture(
|
|
|
DerivedF::RowsAtCompileTime,
|
|
|
DerivedF::ColsAtCompileTime,
|
|
|
Eigen::RowMajor> F = _F.template cast<int>();
|
|
|
+ const int dim = U.cols();
|
|
|
// Set up glfw
|
|
|
if(!glfwInit()) fail("Could not initialize glfw");
|
|
|
glfwSetErrorCallback([](int id,const char* m){std::cerr<<m<<std::endl;});
|
|
@@ -56,7 +78,8 @@ IGL_INLINE bool igl::opengl::glfw::map_texture(
|
|
|
if(!window) fail("Could not create glfw window");
|
|
|
glfwMakeContextCurrent(window);
|
|
|
// Compile each shader
|
|
|
- std::string vertex_shader = R"(
|
|
|
+ std::string vertex_shader = dim == 2 ?
|
|
|
+ R"(
|
|
|
#version 330 core
|
|
|
layout(location = 0) in vec2 position;
|
|
|
layout(location = 1) in vec2 tex_coord_v;
|
|
@@ -66,7 +89,20 @@ void main()
|
|
|
tex_coord_f = vec2(tex_coord_v.x,1.-tex_coord_v.y);
|
|
|
gl_Position = vec4( 2.*position.x-1., 2.*(1.-position.y)-1., 0.,1.);
|
|
|
}
|
|
|
-)";
|
|
|
+)"
|
|
|
+ :
|
|
|
+ R"(
|
|
|
+#version 330 core
|
|
|
+layout(location = 0) in vec3 position;
|
|
|
+layout(location = 1) in vec2 tex_coord_v;
|
|
|
+out vec2 tex_coord_f;
|
|
|
+void main()
|
|
|
+{
|
|
|
+ tex_coord_f = vec2(tex_coord_v.x,1.-tex_coord_v.y);
|
|
|
+ gl_Position = vec4( 2.*position.x-1., 2.*(1.-position.y)-1., position.z,1.);
|
|
|
+}
|
|
|
+)"
|
|
|
+ ;
|
|
|
std::string fragment_shader = R"(
|
|
|
#version 330 core
|
|
|
layout(location = 0) out vec3 color;
|
|
@@ -93,7 +129,7 @@ void main()
|
|
|
glEnableVertexAttribArray(0);
|
|
|
glBindBuffer(GL_ARRAY_BUFFER,vbo);
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(double)*U.size(), U.data(), GL_STATIC_DRAW);
|
|
|
- glVertexAttribLPointer(0, U.cols(), GL_DOUBLE, V.cols() * sizeof(GLdouble), (GLvoid*)0);
|
|
|
+ glVertexAttribLPointer(0, U.cols(), GL_DOUBLE, U.cols() * sizeof(GLdouble), (GLvoid*)0);
|
|
|
glGenBuffers(1,&tbo);
|
|
|
glEnableVertexAttribArray(1);
|
|
|
glBindBuffer(GL_ARRAY_BUFFER,tbo);
|
|
@@ -124,7 +160,8 @@ void main()
|
|
|
glGenTextures(1, &out_tex);
|
|
|
glBindTexture(GL_TEXTURE_2D, out_tex);
|
|
|
// always use float for internal storage
|
|
|
- glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, w, h, 0,GL_RGB, GL_FLOAT, 0);
|
|
|
+ assert(out_nc == 3);
|
|
|
+ glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, out_w, out_h, 0,GL_RGB, GL_FLOAT, 0);
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, out_tex, 0);
|
|
@@ -140,7 +177,7 @@ void main()
|
|
|
// clear screen and set viewport
|
|
|
glClearColor(0.0,1.0,0.0,0.);
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
- glViewport(0,0,w,h);
|
|
|
+ glViewport(0,0,out_w,out_h);
|
|
|
// Attach shader program
|
|
|
glUseProgram(prog_id);
|
|
|
glActiveTexture(GL_TEXTURE0 + 0);
|
|
@@ -151,7 +188,8 @@ void main()
|
|
|
glDrawElements(GL_TRIANGLES, F.size(), GL_UNSIGNED_INT, 0);
|
|
|
glBindVertexArray(0);
|
|
|
// Write into memory
|
|
|
- out_data.resize(nc*w*h);
|
|
|
+ assert(out_nc == 3);
|
|
|
+ out_data.resize(out_nc*out_w*out_h);
|
|
|
glBindTexture(GL_TEXTURE_2D, out_tex);
|
|
|
glGetTexImage(GL_TEXTURE_2D, 0, format, GL_UNSIGNED_BYTE, &out_data[0]);
|
|
|
// OpenGL cleanup
|