shader_wrapper.cpp
Go to the documentation of this file.
00001 /* 
00002  * Copyright (c) 2011, Nico Blodow <blodow@cs.tum.edu>
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  * 
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Intelligent Autonomous Systems Group/
00014  *       Technische Universitaet Muenchen nor the names of its contributors 
00015  *       may be used to endorse or promote products derived from this software 
00016  *       without specific prior written permission.
00017  * 
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00022  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028  * POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 #include <realtime_urdf_filter/shader_wrapper.h>
00032 #include <iostream>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <resource_retriever/retriever.h>
00036 #include <GL/glu.h>
00037 
00038 namespace realtime_urdf_filter
00039 {
00040 // named constructor to compile from source
00041 template <int L1, int L2>
00042 ShaderWrapper ShaderWrapper::fromSource (GLchar const * (&v_source) [L1], GLchar const * (&f_source) [L2])
00043 {
00044   return ShaderWrapper (v_source, f_source);
00045 }
00046 
00047 // named constructor to compile from files
00048 ShaderWrapper ShaderWrapper::fromFiles (const std::string vertex_file, const std::string fragment_file)
00049 {
00050   return fromFiles (vertex_file.c_str (), fragment_file.c_str ());
00051 }
00052 ShaderWrapper ShaderWrapper::fromFiles (const char* vertex_file, const char* fragment_file)
00053 {
00054   std::string v_source = load_text_file (vertex_file);
00055   std::string f_source = load_text_file (fragment_file);
00056 
00057   const GLchar* vs[1] = {v_source.c_str () };
00058   const GLchar* fs[1] = {f_source.c_str () };
00059   return ShaderWrapper (vs, fs);
00060 }
00061 
00062 // make sure we delete everything upon deconstruction
00063 ShaderWrapper::~ShaderWrapper()
00064 {
00065   glDeleteProgram (prog);
00066   glDeleteShader (vertex_shader);
00067   glDeleteShader (fragment_shader);
00068 }
00069 
00070 // operator to get back the encapsulated program handle
00071 ShaderWrapper::operator GLuint ()
00072 {
00073   return prog;
00074 }
00075 
00076 // call operator enables the shader to be used in gl drawing calls
00077 void ShaderWrapper::operator() ()
00078 {
00079   glUseProgram (prog);
00080 }
00081 
00082 // convenience function to set a integer uniform value
00083 void ShaderWrapper::SetUniformVal1i(std::string name, GLint val)
00084 {
00085   glUniform1i(glGetUniformLocation(prog, name.c_str()), val);
00086 }
00087 
00088 // convenience function to set a float uniform value
00089 void ShaderWrapper::SetUniformVal1f(std::string name, GLfloat val)
00090 {
00091   glUniform1f(glGetUniformLocation(prog, name.c_str()), val);
00092 }
00093 
00094 // templated constructor takes two char* arrays for vertex and fragment shader source code
00095 template <int L1, int L2>
00096 ShaderWrapper::ShaderWrapper (GLchar const * (&v_source) [L1], GLchar const * (&f_source) [L2])
00097 {
00098   // compile shaders
00099   vertex_shader = compile (GL_VERTEX_SHADER, v_source);
00100   fragment_shader = compile (GL_FRAGMENT_SHADER, f_source);
00101   // link vertex and fragment shaders together
00102   prog = glCreateProgram();
00103   glAttachShader (prog, vertex_shader);
00104   glAttachShader (prog, fragment_shader);
00105   glLinkProgram (prog);
00106    
00107   GLenum error = glGetError();
00108   if(error != GL_NO_ERROR)
00109     throw std::logic_error (std::string("GL ERROR while creating shaders:").append((const char*)gluErrorString(error)));
00110 }
00111 
00112 // compile function is templated on the number of lines in the shader
00113 template <int L>
00114 GLuint ShaderWrapper::compile (GLuint type, char const * (&shader_source) [L])
00115 {
00116   GLuint shader = glCreateShader (type);
00117   glShaderSource (shader, L, shader_source, NULL);
00118   glCompileShader (shader);
00119 
00120   GLint compiled = GL_TRUE;
00121   glGetShaderiv (shader, GL_COMPILE_STATUS, &compiled);
00122   if (!compiled)
00123   {
00124     for (unsigned int i = 0; i < L; ++i)
00125       std::cerr << shader_source[i] << std::endl;
00126     GLint length = 0;
00127     glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &length);
00128     std::string log (length, ' ');
00129     glGetShaderInfoLog (shader, length, &length, &log[0]);
00130     if (type == GL_VERTEX_SHADER)
00131       throw std::logic_error (std::string("compiling vertex shader :").append(log));
00132     else if (type == GL_FRAGMENT_SHADER)
00133       throw std::logic_error (std::string("compiling fragment shader :").append(log));
00134     return -1;
00135   }
00136   return shader;
00137 }
00138 
00139 // loads a text file as a string
00140 std::string ShaderWrapper::load_text_file (std::string file_name)
00141 {
00142   resource_retriever::Retriever retriever;
00143   resource_retriever::MemoryResource res;
00144   try 
00145   {   
00146     res = retriever.get(file_name);
00147   }
00148   catch (resource_retriever::Exception& e)
00149   {   
00150     throw std::logic_error (std::string("could not open shader file: ").append(file_name));
00151     return "";
00152   }
00153   
00154   char* buf = (char*) malloc (sizeof(char) * (res.size + 1));
00155   memcpy (buf, res.data.get(), res.size);
00156   buf[res.size] = 0;
00157 
00158   return std::string (buf);
00159   
00160   //std::ifstream ifs (file_name.c_str());
00161   //std::string str ( (std::istreambuf_iterator<char> (ifs)),
00162   //                  std::istreambuf_iterator<char>());
00163   //return str;
00164 }
00165 
00166 } // end namespace
00167 
00168 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


realtime_urdf_filter
Author(s): Nico Blodow
autogenerated on Thu May 23 2013 16:50:36