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 #ifndef REALTIME_PERCEPTION_SHADER_WRAPPER_H_ 00032 #define REALTIME_PERCEPTION_SHADER_WRAPPER_H_ 00033 00034 #include <fstream> 00035 #include <stdexcept> 00036 #define GL3_PROTOTYPES 1 00037 #include <GL3/gl3.h> 00038 00039 namespace realtime_urdf_filter 00040 { 00041 00042 // this class wraps loading, compiling and enabling shader programs 00043 class ShaderWrapper 00044 { 00045 00046 public: 00047 // named constructor to compile from source 00048 template <int L1, int L2> 00049 static ShaderWrapper fromSource (GLchar const * (&v_source) [L1], GLchar const * (&f_source) [L2]); 00050 00051 // named constructor to compile from files 00052 static ShaderWrapper fromFiles (const std::string vertex_file, const std::string fragment_file); 00053 static ShaderWrapper fromFiles (const char* vertex_file, const char* fragment_file); 00054 00055 // make sure we delete everything upon deconstruction 00056 ~ShaderWrapper(); 00057 00058 // operator to get back the encapsulated program handle 00059 operator GLuint(); 00060 00061 // call operator enables the shader to be used in gl drawing calls 00062 void operator() (); 00063 void SetUniformVal1i(std::string name, GLint val); 00064 void SetUniformVal1f(std::string name, GLfloat val); 00065 00066 private: 00067 // templated constructor takes two char* arrays for vertex and fragment shader source code 00068 template <int L1, int L2> 00069 ShaderWrapper (GLchar const * (&v_source) [L1], GLchar const * (&f_source) [L2]); 00070 00071 // compile function is templated on the number of lines in the shader 00072 template <int L> 00073 GLuint compile (GLuint type, char const * (&shader_source) [L]); 00074 00075 // loads a text file as a string 00076 static std::string load_text_file (std::string file_name); 00077 00078 // handles to the shaders and the linked program 00079 GLuint vertex_shader, fragment_shader, prog; 00080 }; 00081 00082 } // end namespace 00083 00084 #endif