47 VL_DEBUG_SET_OBJECT_NAME()
55 VL_DEBUG_SET_OBJECT_NAME()
72 glGetShaderiv(
handle(), GL_SHADER_SOURCE_LENGTH, &len);
75 std::vector<char> src;
77 GLint len_written = 0;
78 glGetShaderSource(
handle(), len, &len_written, &src[0]);
88 std::vector<String> lines;
90 for(
size_t i=0; i<lines.size(); ++i ) {
91 if ( lines[i].startsWith(
"#pragma VL include" ) ) {
92 String file_path = lines[i].substring( 18 ).trim();
95 new_source += include +
'\n';
98 new_source += lines[i] +
'\n';
120 std::string new_src =
"ERROR";
124 if ( source_or_path.
empty() )
137 int cn = source_or_path.
count(
'\n');
138 int cr = source_or_path.
count(
'\r');
139 int cf = source_or_path.
count(
'\f');
143 Log::error(
"GLSLShader::setSource('" + source_or_path +
"') error: file not found!\n");
152 if (new_src !=
"ERROR" && new_src !=
mSource)
170 Log::error(
"GLSLShader::compile() failed: shader source is empty!\n");
185 const char* source[] = {
mSource.c_str() };
190 glCompileShader(
handle() );
224 return status == GL_TRUE;
232 return "OpenGL Shading Language not supported.\n";
239 std::vector<char> log_buffer;
240 log_buffer.resize(max_length);
243 return &log_buffer[0];
282 VL_DEBUG_SET_OBJECT_NAME()
283 mScheduleLink =
true;
285 mProgramBinaryRetrievableHint =
false;
286 mProgramSeparable =
false;
288 resetBindingLocations();
297 void GLSLProgram::resetBindingLocations()
300 m_vl_ModelViewMatrix = -1;
301 m_vl_ProjectionMatrix = -1;
302 m_vl_ModelViewProjectionMatrix = -1;
303 m_vl_NormalMatrix = -1;
306 m_vl_VertexPosition = -1;
307 m_vl_VertexNormal = -1;
308 m_vl_VertexColor = -1;
309 m_vl_VertexSecondaryColor = -1;
310 m_vl_VertexFogCoord = -1;
311 m_vl_VertexTexCoord0 = -1;
312 m_vl_VertexTexCoord1 = -1;
313 m_vl_VertexTexCoord2 = -1;
314 m_vl_VertexTexCoord3 = -1;
315 m_vl_VertexTexCoord4 = -1;
316 m_vl_VertexTexCoord5 = -1;
317 m_vl_VertexTexCoord6 = -1;
318 m_vl_VertexTexCoord7 = -1;
319 m_vl_VertexTexCoord8 = -1;
320 m_vl_VertexTexCoord9 = -1;
321 m_vl_VertexTexCoord10 = -1;
347 glDeleteProgram(
handle());
350 resetBindingLocations();
368 if(std::find(mShaders.begin(), mShaders.end(), shader) != mShaders.end())
374 mShaders.push_back(shader);
376 detachShader(shader);
377 mShaders.push_back(shader);
389 for(
size_t i=mShaders.size(); i--;)
390 detachShader(mShaders[i].
get());
406 for(
int i=0; i<(int)mShaders.size(); ++i)
408 if (mShaders[i] == shader)
412 mShaders.erase(mShaders.begin() + i);
433 for(
size_t i = 0; i < mShaders.size(); ++i )
435 if ( mShaders[i]->
handle() )
443 mScheduleLink =
true;
454 if ( linked() && ! force_relink ) {
458 resetBindingLocations();
460 if (shaderCount() == 0) {
475 mScheduleLink = ! linkStatus();
489 if ( ! log.
empty() ) {
497 void GLSLProgram::preLink()
504 std::map<std::string, int>::iterator it = mFragDataLocation.begin();
505 while(it != mFragDataLocation.end())
514 if( Has_GL_ARB_get_program_binary )
516 VL_glProgramParameteri(
handle(), GL_PROGRAM_BINARY_RETRIEVABLE_HINT, programBinaryRetrievableHint()?GL_TRUE:GL_FALSE);
VL_CHECK_OGL();
519 if ( Has_GL_ARB_separate_shader_objects )
521 VL_glProgramParameteri(
handle(), GL_PROGRAM_SEPARABLE, programSeparable()?GL_TRUE:GL_FALSE);
VL_CHECK_OGL();
544 void GLSLProgram::postLink()
550 m_vl_WorldMatrix = glGetUniformLocation(
handle(),
"vl_WorldMatrix");
551 m_vl_ModelViewMatrix = glGetUniformLocation(
handle(),
"vl_ModelViewMatrix");
552 m_vl_ProjectionMatrix = glGetUniformLocation(
handle(),
"vl_ProjectionMatrix");
553 m_vl_ModelViewProjectionMatrix = glGetUniformLocation(
handle(),
"vl_ModelViewProjectionMatrix");
554 m_vl_NormalMatrix = glGetUniformLocation(
handle(),
"vl_NormalMatrix");
558 m_vl_VertexPosition = glGetAttribLocation(
handle(),
"vl_VertexPosition" );
559 m_vl_VertexNormal = glGetAttribLocation(
handle(),
"vl_VertexNormal" );
560 m_vl_VertexColor = glGetAttribLocation(
handle(),
"vl_VertexColor" );
561 m_vl_VertexSecondaryColor = glGetAttribLocation(
handle(),
"vl_VertexSecondaryColor" );
562 m_vl_VertexFogCoord = glGetAttribLocation(
handle(),
"vl_VertexFogCoord" );
563 m_vl_VertexTexCoord0 = glGetAttribLocation(
handle(),
"vl_VertexTexCoord0" );
564 m_vl_VertexTexCoord1 = glGetAttribLocation(
handle(),
"vl_VertexTexCoord1" );
565 m_vl_VertexTexCoord2 = glGetAttribLocation(
handle(),
"vl_VertexTexCoord2" );
566 m_vl_VertexTexCoord3 = glGetAttribLocation(
handle(),
"vl_VertexTexCoord3" );
567 m_vl_VertexTexCoord4 = glGetAttribLocation(
handle(),
"vl_VertexTexCoord4" );
568 m_vl_VertexTexCoord5 = glGetAttribLocation(
handle(),
"vl_VertexTexCoord5" );
569 m_vl_VertexTexCoord6 = glGetAttribLocation(
handle(),
"vl_VertexTexCoord6" );
570 m_vl_VertexTexCoord7 = glGetAttribLocation(
handle(),
"vl_VertexTexCoord7" );
571 m_vl_VertexTexCoord8 = glGetAttribLocation(
handle(),
"vl_VertexTexCoord8" );
572 m_vl_VertexTexCoord9 = glGetAttribLocation(
handle(),
"vl_VertexTexCoord9" );
573 m_vl_VertexTexCoord10 = glGetAttribLocation(
handle(),
"vl_VertexTexCoord10" );
590 return status == GL_TRUE;
598 return "OpenGL Shading Language not supported!\n";
604 return "GLSLProgram::infoLog(): error! GLSL program object not yet created! (" +
String(
objectName()) +
")\n";
609 std::vector<char> log_buffer;
610 log_buffer.resize( max_length + 1 );
612 return &log_buffer[0];
628 glValidateProgram(
handle() );
630 return status == GL_TRUE;
654 uniforms = uniforms ? uniforms : getUniformSet();
671 int current_glsl_program = -1;
672 glGetIntegerv(GL_CURRENT_PROGRAM, ¤t_glsl_program);
VL_CHECK_OGL();
676 for(
size_t i=0, count=uniforms->
uniforms().size(); i<count; ++i)
681 const UniformInfo* uinfo = activeUniformInfo(uniform->
name().c_str());
682 int location = uinfo ? uinfo->Location : -1;
687 std::map<std::string, ref<UniformInfo> >::const_iterator it = activeUniforms().begin();
689 for( ; it != activeUniforms().end(); ++it )
697 "GLSLProgram::applyUniformSet(): uniform '%s' not found!\n" 698 "Is the uniform variable declared but not used in your GLSL program?\n" 699 "Also double-check the spelling of the uniform variable name.\n") << uniform->
name() );
704 int location = glGetUniformLocation(
handle(), uniform->
name().c_str());
705 if (location == -1) {
714 switch(uniform->
mType)
760 vl::Log::bug(
vl::Say(
"GLSLProgram::applyUniformSet(): uniform '%s' does not contain any data! Did you forget to assign a value to it?\n") << uniform->
name() );
778 mFragDataLocation[name] = color_number;
784 mFragDataLocation.erase(name);
789 std::map<std::string, int>::const_iterator it = mFragDataLocation.find(name);
790 if (it != mFragDataLocation.end())
799 VL_CHECK(Has_GL_ARB_get_program_binary)
800 if (!Has_GL_ARB_get_program_binary)
804 binary_format = (GLenum)-1;
810 if (status == GL_FALSE)
816 binary.resize(length);
831 VL_CHECK(Has_GL_ARB_get_program_binary)
832 if (!Has_GL_ARB_get_program_binary)
844 mScheduleLink = !linkStatus();
876 for(
size_t i = 0; i < mShaders.size(); ++i ) {
877 ok &= mShaders[i]->reload();
879 return ok && linkProgram(
true );
bool reload()
Reloads the shader source and recompiles it. Returns true on success.
A shader that is intended to run on the programmable vertex processor.
bool getProgramBinary(GLenum &binary_format, std::vector< unsigned char > &binary) const
glGetProgramBinary wrapper: returns a binary representation of a program object's compiled and linked...
A simple String formatting class.
void setObjectName(const char *name)
The name of the object, by default set to the object's class name in debug builds.
static void warning(const String &message)
Use this function to provide information about situations that might lead to errors or loss of data...
bool detachShader(GLSLShader *shader)
Detaches a GLSLShader from the GLSLShader (note: it does NOT schedule a relink of the program)...
unsigned int handle() const
The handle of this OpenGL shader object as returned by glCreateShader()
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
void detachAllShaders()
Removes all the previously linked shaders and schedules a relinking.
Represents an OpenGL context, possibly a widget or a pbuffer, which can also respond to keyboard...
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
void bindAttribLocation(unsigned int index, const char *name)
Equivalent to glBindAttribLocation(handle(), index, name.c_str()) with the difference that this funct...
void splitLines(std::vector< String > &lines) const
Splits a String into a set of Strings each of which contains a line.
bool validateProgram() const
Returns true if the validation of this GLSL program is succesful, see also http://www.opengl.org/sdk/docs/man/xhtml/glValidateProgram.xml for more information.
int count(wchar_t ch, int start=0) const
Returns the number of occurrences of the given character after position 'start'.
const std::string & source() const
Returns the sources for this shader.
bool applyUniformSet(const UniformSet *uniforms=NULL) const
Applies a set of uniforms to the currently bound GLSL program.
bool compileStatus() const
Returns true if the shader has been succesfully compiled.
bool reload()
Reloads all the shaders source and recompiles them and relinks. Returns true on success.
~GLSLProgram()
Destructor. Calls deleteProgram().
Visualization Library main namespace.
void discardAllShaders()
Detaches all the shaders and deletes them (note that the GLSL Program remains still valid)...
bool linkProgram(bool force_relink=false)
Links the GLSLProgram calling glLinkProgram(handle()) only if the program needs to be linked...
void setPath(const String &path)
The path from which the shader was loaded.
static void bug(const String &message)
Use this function to provide information about programming errors: wrong parameter initialization...
void useGLSLProgram(const GLSLProgram *glsl)
Activates the given GLSLProgram or unbinds the current one if glsl is NULL.
int fragDataLocation(const char *name) const
float max(float a, float b)
void setSource(const String &source_or_path)
Sets the sources for this shader and schedules a recompilation for it. If the string passed is a file...
void createProgram()
Calls glCreateProgram() in order to acquire a GLSL program handle, see also http://www.opengl.org/sdk/docs/man/xhtml/glCreateProgram.xml for more information.
void createShader()
Creates the shader using the OpenGL function glCreateShader(), see also http://www.opengl.org/sdk/docs/man/xhtml/glCreateShader.xml for more information.
bool empty() const
Returns true if length() == 0.
void deleteShader()
Deletes the shader using the OpenGL function glDeleteShader(), see also http://www.opengl.org/sdk/docs/man/xhtml/glDeleteShader.xml for more information.
static String loadText(const String &path, EStringEncoding encoding=VL_PLATFORM_DEFAULT_ENCODING)
Loads a String from the specified path.
bool programBinary(GLenum binary_format, const std::vector< unsigned char > &binary)
glProgramBinary wrapper: loads a program object with a program binary, see also http://www.opengl.org/sdk/docs/man4/xhtml/glProgramBinary.xml
String infoLog() const
Returns the info log of this GLSL program using the OpenGL function glGetProgramInfoLog(), see also http://www.opengl.org/sdk/docs/man/xhtml/glGetProgramInfoLog.xml for more information.
void unbindFragDataLocation(const char *name)
const std::string & objectName() const
The name of the object, by default set to the object's class name.
VLCORE_EXPORT ref< VirtualFile > locateFile(const String &path)
Utility function, equivalent to vl::defFileSystem()->locateFile(path)
bool compile()
Compiles the shader, see also http://www.opengl.org/sdk/docs/man/xhtml/glCompileShader.xml for more information.
const String & path() const
The path from which the shader was loaded.
void bindFragDataLocation(int color_number, const char *name)
GLSLProgram()
Constructor.
std::string getShaderSource() const
Retrieves the shader source using glGetShaderSource()
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
static String printf(const char *fmt,...)
Returns a formatted string using the legacy printf syntax. The resulting string can be maximum 1024 +...
std::string toStdString() const
Returns a UTF8 encoded std::string.
bool attachShader(GLSLShader *shader)
Attaches the GLSLShader to this GLSLProgram.
void deleteProgram()
Deletes the GLSL program calling glDeleteProgram(handle()), see also http://www.opengl.org/sdk/docs/man/xhtml/glDeleteProgram.xml for more information.
String & clear()
Clears the string.
String infoLog() const
Returns a String object containing this shader's info log as returned by glGetShaderInfoLog(), see also http://www.opengl.org/sdk/docs/man/xhtml/glGetShaderInfoLog.xml for more information.
void apply(int index, const Camera *, OpenGLContext *ctx) const
Calls OpenGLContext::useGLSLProgram()