Selaa lähdekoodia

wrl writer, texture fbo creation

Former-commit-id: 10fa8a481ffefcd53c7a018b83d457ee86d9d7bf
Alec Jacobson 10 vuotta sitten
vanhempi
commit
73c2e9f935

+ 44 - 0
include/igl/init_render_to_texture.cpp

@@ -0,0 +1,44 @@
+#include "init_render_to_texture.h"
+#include <cassert>
+
+IGL_INLINE void igl::init_render_to_texture(
+  const size_t width,
+  const size_t height,
+  GLuint & tex_id,
+  GLuint & fbo_id,
+  GLuint & dfbo_id)
+{
+  using namespace std;
+  // Delete if already exists
+  glDeleteTextures(1,&tex_id);
+  glDeleteFramebuffersEXT(1,&fbo_id);
+  glDeleteFramebuffersEXT(1,&dfbo_id);
+  // http://www.opengl.org/wiki/Framebuffer_Object_Examples#Quick_example.2C_render_to_texture_.282D.29
+  glGenTextures(1, &tex_id);
+  glBindTexture(GL_TEXTURE_2D, tex_id);
+  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
+  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
+  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+  //NULL means reserve texture memory, but texels are undefined
+  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, width, height, 0, GL_BGRA, GL_FLOAT, NULL);
+  glBindTexture(GL_TEXTURE_2D, 0);
+  glGenFramebuffersEXT(1, &fbo_id);
+  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_id);
+  //Attach 2D texture to this FBO
+  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex_id, 0);
+  glGenRenderbuffersEXT(1, &dfbo_id);
+  glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, dfbo_id);
+  glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height);
+  //Attach depth buffer to FBO (for this example it's not really needed, but if
+  //drawing a 3D scene it would be necessary to attach something)
+  glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, dfbo_id);
+  //Does the GPU support current FBO configuration?
+  GLenum status;
+  status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
+  assert(status == GL_FRAMEBUFFER_COMPLETE_EXT);
+  // Unbind to clean up
+  glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
+  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
+}
+

+ 28 - 0
include/igl/init_render_to_texture.h

@@ -0,0 +1,28 @@
+#ifndef IGL_INIT_RENDER_TO_TEXTURE_H
+#define IGL_INIT_RENDER_TO_TEXTURE_H
+#include "igl_inline.h"
+#include "OpenGL_convenience.h"
+#include <cstdlib>
+namespace igl
+{
+  // Create a texture+framebuffer+depthbuffer triplet bound for rendering into
+  // the texture;
+  //
+  // Inputs:
+  //   width  image width
+  //   height image height
+  // Outputs:
+  //   tex_id  id of the texture
+  //   fbo_id  id of the frame buffer object
+  //   dfbo_id  id of the depth frame buffer object
+  IGL_INLINE void init_render_to_texture(
+    const size_t width,
+    const size_t height,
+    GLuint & tex_id,
+    GLuint & fbo_id,
+    GLuint & dfbo_id);
+}
+#ifndef IGL_STATIC_LIBRARY
+#  include "init_render_to_texture.cpp"
+#endif
+#endif

+ 51 - 0
include/igl/writeWRL.cpp

@@ -0,0 +1,51 @@
+#include "writeWRL.h"
+#include <iostream>
+#include <fstream>
+template <typename DerivedV, typename DerivedF>
+IGL_INLINE bool igl::writeWRL(
+  const std::string & str,
+  const Eigen::PlainObjectBase<DerivedV> & V,
+  const Eigen::PlainObjectBase<DerivedF> & F)
+{
+  using namespace std;
+  using namespace Eigen;
+  assert(V.cols() == 3 && "V should have 3 columns");
+  assert(F.cols() == 3 && "F should have 3 columns");
+  ofstream s(str);
+  if(!s.is_open())
+  {
+    cerr<<"IOError: writeWRL() could not open "<<str<<endl;
+    return false;
+  }
+  // Append column of -1 to F
+  Matrix<typename DerivedF::Scalar,Dynamic,4> FF(F.rows(),4);
+  FF.leftCols(3) = F;
+  FF.col(3).setConstant(-1);
+
+  s<<R"(
+DEF default Transform {
+translation 0 0 0
+children [
+Shape {
+geometry DEF default-FACES IndexedFaceSet {
+ccw TRUE
+)"<<
+    V.format(
+      IOFormat(
+        FullPrecision,
+        DontAlignCols,
+        " ",",\n","","",
+        "coord DEF default-COORD Coordinate { point [ \n","]\n}\n"))<<
+    FF.format(
+      IOFormat(
+        FullPrecision,
+        DontAlignCols,
+        ",","\n","","",
+        "coordIndex [ \n"," ]\n"))<<
+    "}\n}\n]\n}\n";
+  return true;
+}
+
+#ifdef IGL_STATIC_LIBRARY
+template bool igl::writeWRL<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
+#endif

+ 24 - 0
include/igl/writeWRL.h

@@ -0,0 +1,24 @@
+#ifndef IGL_WRITE_WRL_H
+#define IGL_WRITE_WRL_H
+#include "igl_inline.h"
+#include <Eigen/Core>
+#include <string>
+namespace igl
+{
+  // Write mesh to a .wrl file
+  //
+  // Inputs:
+  //   str  path to .wrl file
+  //   V  #V by 3 list of vertex positions
+  //   F  #F by 3 list of triangle indices
+  // Returns true iff succes
+  template <typename DerivedV, typename DerivedF>
+  IGL_INLINE bool writeWRL(
+    const std::string & str,
+    const Eigen::PlainObjectBase<DerivedV> & V,
+    const Eigen::PlainObjectBase<DerivedF> & F);
+}
+#ifndef IGL_STATIC_LIBRARY
+#include "writeWRL.cpp"
+#endif
+#endif