1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4 *
* (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
*******************************************************************************
* SOFA :: Framework *
* *
* Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
* H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza, *
* M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <sofa/helper/gl/GLSLShader.h>
#include <stdlib.h>
#include <math.h>
#include <fstream>
#include <iostream>
namespace sofa
{
namespace helper
{
namespace gl
{
bool GLSLShader::InitGLSL()
{
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
return false;
}
// fprintf(stdout, "GLSLShader: Using GLEW %s\n", glewGetString(GLEW_VERSION));
// Make sure find the GL_ARB_shader_objects extension so we can use shaders.
if(!GLEW_ARB_shader_objects)
{
fprintf(stderr, "Error: GL_ARB_shader_objects extension not supported!\n");
return false;
}
// Make sure we support the GLSL shading language 1.0
if(!GLEW_ARB_shading_language_100)
{
fprintf(stderr, "Error: GL_ARB_shading_language_100 extension not supported!\n");
return false;
}
// Return a success!
return true;
}
GLSLShader::GLSLShader()
{
m_hVertexShader = 0; //NULL;
m_hGeometryShader = 0; //NULL;
m_hFragmentShader = 0; //NULL;
m_hProgramObject = 0; //NULL;
geometry_input_type = -1;
geometry_output_type = -1;
geometry_vertices_out = -1;
header = "";
}
GLSLShader::~GLSLShader()
{
// BUGFIX: if the GL context is gone, this can crash the application on exit -- Jeremie A.
//Release();
}
void GLSLShader::AddHeader(const std::string &header)
{
this->header += header;
this->header += "\n";
}
void GLSLShader::AddDefineMacro(const std::string &name, const std::string &value)
{
AddHeader("#define " + name + " " + value);
}
/// This function loads and returns a text file for our shaders
std::string GLSLShader::LoadTextFile(const std::string& strFile)
{
// Open the file passed in
std::ifstream fin(strFile.c_str());
// Make sure we opened the file correctly
if(!fin)
return "";
std::string strLine = "";
std::string strText = "";
// Go through and store each line in the text file within a "string" object
while(std::getline(fin, strLine))
{
strText = strText + "\n" + strLine;
}
// Close our file
fin.close();
// Return the text file's data
return strText;
}
/// This function compiles a shader and check the log
bool GLSLShader::CompileShader(GLint target, const std::string& source, GLhandleARB& shader)
{
const char* stype = "";
if (target == GL_VERTEX_SHADER_ARB) stype = "vertex";
else if (target == GL_FRAGMENT_SHADER_ARB) stype = "fragment";
#ifdef GL_GEOMETRY_SHADER_EXT
else if (target == GL_GEOMETRY_SHADER_EXT) stype = "geometry";
#endif
shader = glCreateShaderObjectARB(target);
const char* src = source.c_str();
glShaderSourceARB(shader, 1, &src, NULL);
glCompileShaderARB(shader);
GLint compiled = 0, length = 0, laux = 0;
glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &compiled);
if (!compiled) std::cerr << "ERROR: Compilation of "<<stype<<" shader failed:\n"<<src<<std::endl;
// else std::cout << "Compilation of "<<stype<<" shader OK" << std::endl;
glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
if (length)
{
GLcharARB *logString = (GLcharARB *)malloc((length+1) * sizeof(GLcharARB));
glGetInfoLogARB(shader, length, &laux, logString);
std::cerr << logString << std::endl;
free(logString);
}
return (compiled!=0);
}
/// This function loads a vertex and fragment shader file
void GLSLShader::InitShaders(const std::string& strVertex, const std::string& strGeometry, const std::string& strFragment)
{
// Make sure the user passed in at least a vertex and fragment shader file
if(!strVertex.length() || !strFragment.length())
return;
// If any of our shader pointers are set, let's free them first.
if(m_hVertexShader || m_hGeometryShader || m_hFragmentShader || m_hProgramObject)
Release();
bool ready = true;
// Now we load and compile the shaders from their respective files
ready &= CompileShader( GL_VERTEX_SHADER_ARB, header + LoadTextFile(strVertex), m_hVertexShader );
if (!strGeometry.empty())
{
#ifdef GL_GEOMETRY_SHADER_EXT
ready &= CompileShader( GL_GEOMETRY_SHADER_EXT, LoadTextFile(strGeometry), m_hGeometryShader );
#else
std::cerr << "SHADER ERROR: GL_GEOMETRY_SHADER_EXT not defined. Please use a recent version of GLEW.\n";
ready = false;
#endif
}
ready &= CompileShader( GL_FRAGMENT_SHADER_ARB, header + LoadTextFile(strFragment), m_hFragmentShader );
if (!ready)
{
std::cerr << "SHADER compilation failed.\n";
return;
}
// Next we create a program object to represent our shaders
m_hProgramObject = glCreateProgramObjectARB();
// We attach each shader we just loaded to our program object
glAttachObjectARB(m_hProgramObject, m_hVertexShader);
#ifdef GL_GEOMETRY_SHADER_EXT
if (m_hGeometryShader)
glAttachObjectARB(m_hProgramObject, m_hGeometryShader);
#endif
glAttachObjectARB(m_hProgramObject, m_hFragmentShader);
#ifdef GL_GEOMETRY_SHADER_EXT
if (m_hGeometryShader)
{
if (geometry_input_type != -1) glProgramParameteriEXT(m_hProgramObject, GL_GEOMETRY_INPUT_TYPE_EXT, geometry_input_type );
if (geometry_output_type != -1) glProgramParameteriEXT(m_hProgramObject, GL_GEOMETRY_OUTPUT_TYPE_EXT, geometry_output_type );
if (geometry_vertices_out != -1) glProgramParameteriEXT(m_hProgramObject, GL_GEOMETRY_VERTICES_OUT_EXT, geometry_vertices_out );
}
#endif
// Our last init function is to link our program object with OpenGL
glLinkProgramARB(m_hProgramObject);
GLint linked = 0, length = 0, laux = 0;
glGetObjectParameterivARB(m_hProgramObject, GL_OBJECT_LINK_STATUS_ARB, &linked);
if (!linked) std::cerr << "ERROR: Link of program shader failed:\n"<<std::endl;
// else std::cout << "Link of program shader OK" << std::endl;
glGetObjectParameterivARB(m_hProgramObject, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
if (length)
{
GLcharARB *logString = (GLcharARB *)malloc((length+1) * sizeof(GLcharARB));
glGetInfoLogARB(m_hProgramObject, length, &laux, logString);
std::cerr << logString << std::endl;
free(logString);
}
// Now, let's turn off the shader initially.
glUseProgramObjectARB(0);
}
void GLSLShader::SetInt(GLint variable, int newValue) { if (variable!=-1) glUniform1iARB(variable, newValue); }
void GLSLShader::SetInt2(GLint variable, int i1, int i2) { if (variable!=-1) glUniform2iARB(variable, i1, i2); }
void GLSLShader::SetInt3(GLint variable, int i1, int i2, int i3) { if (variable!=-1) glUniform3iARB(variable, i1, i2, i3); }
void GLSLShader::SetInt4(GLint variable, int i1, int i2, int i3, int i4) { if (variable!=-1) glUniform4iARB(variable, i1, i2, i3, i4); }
void GLSLShader::SetFloat(GLint variable, float newValue) { if (variable!=-1) glUniform1fARB(variable, newValue); }
void GLSLShader::SetFloat2(GLint variable, float v0, float v1) { if (variable!=-1) glUniform2fARB(variable, v0, v1); }
void GLSLShader::SetFloat3(GLint variable, float v0, float v1, float v2) { if (variable!=-1) glUniform3fARB(variable, v0, v1, v2); }
void GLSLShader::SetFloat4(GLint variable, float v0, float v1, float v2, float v3) { if (variable!=-1) glUniform4fARB(variable, v0, v1, v2, v3); }
void GLSLShader::SetIntVector(GLint variable, GLsizei count, const GLint *value) { if (variable!=-1) glUniform1ivARB(variable, count, value); }
void GLSLShader::SetIntVector2(GLint variable, GLsizei count, const GLint *value) { if (variable!=-1) glUniform2ivARB(variable, count, value); }
void GLSLShader::SetIntVector3(GLint variable, GLsizei count, const GLint *value) { if (variable!=-1) glUniform3ivARB(variable, count, value); }
void GLSLShader::SetIntVector4(GLint variable, GLsizei count, const GLint *value) { if (variable!=-1) glUniform4ivARB(variable, count, value); }
void GLSLShader::SetFloatVector(GLint variable, GLsizei count, const float *value) { if (variable!=-1) glUniform1fvARB(variable, count, value); }
void GLSLShader::SetFloatVector2(GLint variable, GLsizei count, const float *value){ if (variable!=-1) glUniform2fvARB(variable, count, value); }
void GLSLShader::SetFloatVector3(GLint variable, GLsizei count, const float *value){ if (variable!=-1) glUniform3fvARB(variable, count, value); }
void GLSLShader::SetFloatVector4(GLint variable, GLsizei count, const float *value){ if (variable!=-1) glUniform4fvARB(variable, count, value); }
// These 2 functions turn on and off our shader
void GLSLShader::TurnOn() { if (m_hProgramObject) glUseProgramObjectARB(m_hProgramObject); }
void GLSLShader::TurnOff() { if (m_hProgramObject) glUseProgramObjectARB(0); }
/// This function returns a variable ID for a shader variable
GLint GLSLShader::GetVariable(std::string strVariable)
{
// If we don't have an active program object, let's return -1
if(!m_hProgramObject)
return -1;
// This returns the variable ID for a variable that is used to find
// the address of that variable in memory.
return glGetUniformLocationARB(m_hProgramObject, strVariable.c_str());
}
GLint GLSLShader::GetAttributeVariable(std::string strVariable)
{
// If we don't have an active program object, let's return -1
if(!m_hProgramObject)
return -1;
// This returns the variable ID for a variable that is used to find
// the address of that variable in memory.
return glGetAttribLocationARB(m_hProgramObject, strVariable.c_str());
}
/// This function frees all of our shader data
void GLSLShader::Release()
{
// If our vertex shader pointer is valid, free it
if(m_hVertexShader)
{
glDetachObjectARB(m_hProgramObject, m_hVertexShader);
glDeleteObjectARB(m_hVertexShader);
m_hVertexShader = 0; //NULL;
}
// If our geometry shader pointer is valid, free it
if(m_hGeometryShader)
{
glDetachObjectARB(m_hProgramObject, m_hGeometryShader);
glDeleteObjectARB(m_hGeometryShader);
m_hGeometryShader = 0; //NULL;
}
// If our fragment shader pointer is valid, free it
if(m_hFragmentShader)
{
glDetachObjectARB(m_hProgramObject, m_hFragmentShader);
glDeleteObjectARB(m_hFragmentShader);
m_hFragmentShader = 0; //NULL;
}
// If our program object pointer is valid, free it
if(m_hProgramObject)
{
glDeleteObjectARB(m_hProgramObject);
m_hProgramObject = 0; //NULL;
}
}
} // namespace gl
} // namespace helper
} // namespace sofa
|