Browse Source

compile shaders and random quaternions

Former-commit-id: ec7cc9dc3c2332b1a0558919b5322c5bf9b40608
Alec Jacobson 10 years ago
parent
commit
79c81a6dd8

+ 33 - 0
include/igl/compile_and_link_program.cpp

@@ -0,0 +1,33 @@
+#include "compile_and_link_program.h"
+#include "compile_shader.h"
+#include "report_gl_error.h"
+#include <iostream>
+#include <cassert>
+
+IGL_INLINE GLuint igl::compile_and_link_program(
+  const char * v_str, const char * f_str)
+{
+  GLuint vid = compile_shader(GL_VERTEX_SHADER,v_str);
+  GLuint fid = compile_shader(GL_FRAGMENT_SHADER,f_str);
+
+  GLuint prog_id = glCreateProgram();
+  assert(prog_id != 0 && "Failed to create shader.");
+  glAttachShader(prog_id,vid);
+  igl::report_gl_error("glAttachShader (vid): ");
+  glAttachShader(prog_id,fid);
+  igl::report_gl_error("glAttachShader (fid): ");
+
+  glLinkProgram(prog_id);
+  igl::report_gl_error("glLinkProgram: ");
+
+  GLint status;
+  glGetProgramiv(prog_id, GL_LINK_STATUS, &status);
+  if (status != GL_TRUE)
+  {
+    char buffer[512];
+    glGetProgramInfoLog(prog_id, 512, NULL, buffer);
+    std::cerr << "Linker error: " << std::endl << buffer << std::endl;
+    prog_id = 0;
+  }
+  return prog_id;
+}

+ 19 - 0
include/igl/compile_and_link_program.h

@@ -0,0 +1,19 @@
+#ifndef IGL_COMPILE_AND_LINK_PROGRAM_H
+#define IGL_COMPILE_AND_LINK_PROGRAM_H
+#include "igl_inline.h"
+#include "OpenGL_convenience.h"
+namespace igl
+{
+  // Compile and link very simple vertex/fragment shaders
+  //
+  // Inputs:
+  //   v_str  string of vertex shader contents
+  //   f_str  string of fragment shader contents
+  // Returns id of program
+  IGL_INLINE GLuint compile_and_link_program(
+    const char * v_str, const char * f_str);
+}
+#ifndef IGL_STATIC_LIBRARY
+#  include "compile_and_link_program.cpp"
+#endif
+#endif

+ 28 - 0
include/igl/compile_shader.cpp

@@ -0,0 +1,28 @@
+#include "compile_shader.h"
+#include "report_gl_error.h"
+#include <iostream>
+
+IGL_INLINE GLuint igl::compile_shader(const GLint type, const char * str)
+{
+  GLuint id = glCreateShader(type);
+  igl::report_gl_error("glCreateShader: ");
+  glShaderSource(id,1,&str,NULL);
+  igl::report_gl_error("glShaderSource: ");
+  glCompileShader(id);
+  igl::report_gl_error("glCompileShader: ");
+
+  GLint status;
+  glGetShaderiv(id, GL_COMPILE_STATUS, &status);
+  if (status != GL_TRUE)
+  {
+    char buffer[512];
+    if (type == GL_VERTEX_SHADER)
+      std::cerr << "Vertex shader:" << std::endl;
+    else if (type == GL_FRAGMENT_SHADER)
+      std::cerr << "Fragment shader:" << std::endl;
+    std::cerr << str << std::endl << std::endl;
+    glGetShaderInfoLog(id, 512, NULL, buffer);
+    std::cerr << "Error: " << std::endl << buffer << std::endl;
+  }
+  return id;
+}

+ 26 - 0
include/igl/compile_shader.h

@@ -0,0 +1,26 @@
+#ifndef IGL_COMPILE_SHADER_H
+#define IGL_COMPILE_SHADER_H
+#include "OpenGL_convenience.h"
+#include "igl_inline.h"
+namespace igl
+{
+  // Compile a shader given type and string of shader code
+  // 
+  // Inputs:
+  //   type  either GL_VERTEX_SHADER or GL_FRAGMENT_SHADER
+  //   str  contents of shader code
+  // Returns result of glCreateShader (id of shader)
+  //
+  // Example:
+  //     GLuint vid = compile_shader(GL_VERTEX_SHADER,vertex_shader.c_str());
+  //     GLuint fid = compile_shader(GL_FRAGMENT_SHADER,fragment_shader.c_str());
+  //     GLuint prog_id = glCreateProgram();
+  //     glAttachShader(prog_id,vid);
+  //     glAttachShader(prog_id,fid);
+  //     glLinkProgram(prog_id);
+  IGL_INLINE GLuint compile_shader(const GLint type, const char * str);
+}
+#ifndef IGL_STATIC_LIBRARY
+#  include "compile_shader.cpp"
+#endif
+#endif

+ 23 - 0
include/igl/random_quaternion.cpp

@@ -0,0 +1,23 @@
+#include "random_quaternion.h"
+
+template <typename Scalar>
+IGL_INLINE Eigen::Quaternion<Scalar> igl::random_quaternion()
+{
+  // http://mathproofs.blogspot.com/2005/05/uniformly-distributed-random-unit.html
+  const auto & unit_rand = []()->Scalar
+  {
+    return ((Scalar)rand() / (Scalar)RAND_MAX);
+  };
+  const Scalar t0 = 2.*M_PI*unit_rand();
+  const Scalar t1 = acos(1.-2.*unit_rand());
+  const Scalar t2 = 0.5*(M_PI*unit_rand() + acos(unit_rand()));
+  return Eigen::Quaternion<Scalar>(
+    1.*sin(t0)*sin(t1)*sin(t2),
+    1.*cos(t0)*sin(t1)*sin(t2),
+    1.*cos(t1)*sin(t2),
+    1.*cos(t2));
+}
+
+#ifdef IGL_STATIC_LIBRARY
+// Explicit template specialization
+#endif

+ 14 - 0
include/igl/random_quaternion.h

@@ -0,0 +1,14 @@
+#ifndef IGL_RANDOM_QUATERNION_H
+#define IGL_RANDOM_QUATERNION_H
+#include "igl_inline.h"
+#include <Eigen/Geometry>
+namespace igl
+{
+  // Return a random quaternion via uniform sampling of the 4-sphere
+  template <typename Scalar>
+  IGL_INLINE Eigen::Quaternion<Scalar> random_quaternion();
+}
+#ifndef IGL_STATIC_LIBRARY
+#include "random_quaternion.cpp"
+#endif
+#endif