40 const string& vertShaderFile,
41 const string& fragShaderFile,
42 const string& geomShaderFile,
43 const string& programName) :
SLObject(programName)
52 if (!geomShaderFile.empty())
73 glDetachShader(
_progID, shader->_shaderID);
102 glDetachShader(
_progID, shader->_shaderID);
139 glDetachShader(
_progID, shader->_shaderID);
147 SLbool allSuccuessfullyCompiled =
true;
150 if (!shader->createAndCompile(
nullptr))
152 allSuccuessfullyCompiled =
false;
159 if (!allSuccuessfullyCompiled)
175 if (allSuccuessfullyCompiled)
179 glAttachShader(
_progID, shader->_shaderID);
185 SL_EXIT_MSG(
"No successfully compiled shaders attached!");
189 glTransformFeedbackVaryings(
_progID,
192 GL_INTERLEAVED_ATTRIBS);
195 glGetProgramiv(
_progID, GL_LINK_STATUS, &linked);
205 _name += shader->name() +
", ";
211 SL_LOG(
"*** LINKER ERROR ***");
214 SL_LOG(
"%s", shader->name().c_str());
238 glDetachShader(
_progID, shader->_shaderID);
246 SLbool allSuccuessfullyCompiled =
true;
249 if (!shader->createAndCompile(lights))
251 allSuccuessfullyCompiled =
false;
258 if (!allSuccuessfullyCompiled)
274 if (allSuccuessfullyCompiled)
278 glAttachShader(
_progID, shader->_shaderID);
284 SL_EXIT_MSG(
"No successfully compiled shaders attached!");
290 glGetProgramiv(
_progID, GL_LINK_STATUS, &linked);
300 _name += shader->name() +
", ";
306 SL_LOG(
"*** LINKER ERROR ***");
309 SL_LOG(
"%s", shader->name().c_str());
347 SLint nextTexUnit = 0;
376 if (!lights->empty())
409 lightPosWS[i] =
SLVec4f(0, 0, 1, 1);
410 lightPosVS[i] =
SLVec4f(0, 0, 1, 1);
414 lightSpotDirWS[i] =
SLVec3f(0, 0, -1);
415 lightSpotDirVS[i] =
SLVec3f(0, 0, -1);
416 lightSpotCutoff[i] = 180.0f;
418 lightSpotExp[i] = 1.0f;
419 lightAtt[i].
set(1.0f, 0.0f, 0.0f);
421 for (
SLint ii = 0; ii < 6; ++ii)
423 lightSpace[i * 6 + ii] =
SLMat4f();
424 lightShadowMap[i * 6 + ii] =
nullptr;
426 lightCreatesShadows[i] = 0;
427 lightDoSmoothShadows[i] = 0;
428 lightSmoothShadowLevel[i] = 1;
429 lightShadowMinBias[i] = 0.001f;
430 lightShadowMaxBias[i] = 0.008f;
431 lightUsesCubemap[i] = 0;
432 lightNumCascades[i] = 0;
436 for (
SLuint i = 0; i < lights->size(); ++i)
438 SLLight* light = lights->at(i);
441 lightIsOn[i] = light->
isOn();
444 lightAmbient[i] = light->
ambient();
445 lightDiffuse[i] = light->
diffuse();
446 lightSpecular[i] = light->
specular();
452 lightAtt[i] =
SLVec3f(light->
kc(), light->
kl(), light->
kq());
459 lightUsesCubemap[i] = shadowMap && shadowMap->
useCubemap() ? 1 : 0;
460 lightNumCascades[i] = shadowMap ? shadowMap->
numCascades() : 0;
466 for (
SLint ls = 0; ls < 6; ++ls)
468 lightShadowMap[i * 6 + ls] = cascades > ls ? shadowMap->
depthBuffers()[ls] :
nullptr;
469 lightSpace[i * 6 + ls] = shadowMap->
lightSpace()[ls];
475 auto nL = (
SLint)lights->size();
489 loc =
uniform1iv(
"u_lightCreatesShadows", nL, (
SLint*)&lightCreatesShadows);
490 loc =
uniform1iv(
"u_lightDoSmoothShadows", nL, (
SLint*)&lightDoSmoothShadows);
491 loc =
uniform1iv(
"u_lightSmoothShadowLevel", nL, (
SLint*)&lightSmoothShadowLevel);
499 if (lightCreatesShadows[i])
501 if (lightNumCascades[i])
505 uniformSm = (
"u_cascadesFactor_" + std::to_string(i));
506 loc =
uniform1f(uniformSm.c_str(), lights->at(i)->shadowMap()->cascadesFactor());
507 uniformSm = (
"u_lightSpace_" + std::to_string(i));
509 for (
int j = 0; j < lightNumCascades[i]; j++)
511 uniformSm =
"u_cascadedShadowMap_" + std::to_string(i) +
"_" + std::to_string(j);
514 lightShadowMap[i * 6 + j]->
bindActive(nextTexUnit);
515 glUniform1i(loc, nextTexUnit);
524 if (lightUsesCubemap[i])
526 uniformSm = (
"u_lightSpace_" + std::to_string(i));
528 uniformSm =
"u_shadowMapCube_" + std::to_string(i);
532 uniformSm = (
"u_lightSpace_" + std::to_string(i));
534 uniformSm =
"u_shadowMap_" + std::to_string(i);
539 lightShadowMap[i * 6]->
bindActive(nextTexUnit);
540 glUniform1i(loc, nextTexUnit);
586 if (loc >= 0) glUniform1f(loc, v0);
596 if (loc >= 0) glUniform2f(loc, v0, v1);
607 if (loc >= 0) glUniform3f(loc, v0, v1, v2);
619 if (loc >= 0) glUniform4f(loc, v0, v1, v2,
v3);
627 if (loc >= 0) glUniform1i(loc, v0);
637 if (loc >= 0) glUniform2i(loc, v0, v1);
648 if (loc >= 0) glUniform3i(loc, v0, v1, v2);
660 if (loc == -1)
return false;
661 glUniform4i(loc, v0, v1, v2,
v3);
671 if (loc >= 0) glUniform1fv(loc, count, value);
681 if (loc >= 0) glUniform2fv(loc, count, value);
691 if (loc == -1)
return false;
692 glUniform3fv(loc, count, value);
702 if (loc >= 0) glUniform4fv(loc, count, value);
709 const SLint* value)
const
712 if (loc >= 0) glUniform1iv(loc, count, value);
719 const SLint* value)
const
722 if (loc >= 0) glUniform2iv(loc, count, value);
729 const SLint* value)
const
732 if (loc >= 0) glUniform3iv(loc, count, value);
739 const SLint* value)
const
742 if (loc >= 0) glUniform4iv(loc, count, value);
750 GLboolean transpose)
const
753 if (loc >= 0) glUniformMatrix2fv(loc, count, transpose, value);
761 GLboolean transpose)
const
763 glUniformMatrix2fv(loc, count, transpose, value);
770 GLboolean transpose)
const
773 if (loc >= 0) glUniformMatrix3fv(loc, count, transpose, value);
781 GLboolean transpose)
const
783 glUniformMatrix3fv(loc, count, transpose, value);
790 GLboolean transpose)
const
793 if (loc >= 0) glUniformMatrix4fv(loc, count, transpose, value);
801 GLboolean transpose)
const
803 glUniformMatrix4fv(loc, count, transpose, value);
#define SL_EXIT_MSG(message)
Uses an OpenGL framebuffer object as a depth-buffer.
char * aGLSLErrorString[]
Singleton class for global render state.
static const SLint SL_MAX_LIGHTS
max. number of used lights
vector< SLLight * > SLVLight
STL vector of light pointers.
SLMat4< SLfloat > SLMat4f
SLVec3< SLfloat > SLVec3f
SLVec4< SLfloat > SLVec4f
Toplevel holder of the assets meshes, materials, textures and shaders.
SLVGLProgram & programs()
Active or visible camera node class.
void passToUniforms(SLGLProgram *program)
Pass camera parameters to the uniform variables.
void bindActive(SLuint texUnit) const
Sets the active texture unit within the shader and binds the texture.
SLint uniformMatrix4fv(const SLchar *name, SLsizei count, const SLfloat *value, GLboolean transpose=false) const
Passes a 4x4 float matrix values py pointer to the uniform variable "name".
SLint uniform4fv(const SLchar *name, SLsizei count, const SLfloat *value) const
Passes 4 float values py pointer to the uniform variable "name".
void deleteDataGpu()
Delete all Gpu data.
SLint uniform4f(const SLchar *name, SLfloat v0, SLfloat v1, SLfloat v2, SLfloat v3) const
Passes the float values v0,v1,v2 & v3 to the uniform variable "name".
SLint uniform3fv(const SLchar *name, SLsizei count, const SLfloat *value) const
Passes 3 float values py pointer to the uniform variable "name".
SLint uniform3i(const SLchar *name, SLint v0, SLint v1, SLint v2) const
Passes the int values v0, v1 & v2 to the uniform variable "name".
SLint uniform2iv(const SLchar *name, SLsizei count, const SLint *value) const
Passes 2 int values py pointer to the uniform variable "name".
SLint uniform3iv(const SLchar *name, SLsizei count, const SLint *value) const
Passes 3 int values py pointer to the uniform variable "name".
SLint uniformMatrix3fv(const SLchar *name, SLsizei count, const SLfloat *value, GLboolean transpose=false) const
Passes a 3x3 float matrix values py pointer to the uniform variable "name".
SLint uniform2fv(const SLchar *name, SLsizei count, const SLfloat *value) const
Passes 2 float values py pointer to the uniform variable "name".
SLint uniform1f(const SLchar *name, SLfloat v0) const
Passes the float value v0 to the uniform variable "name".
void initTF(const char *writeBackAttrib[], int size)
void beginUse(SLCamera *cam, SLMaterial *mat, SLVLight *lights)
void addShader(SLGLShader *shader)
SLGLProgram::addShader adds a shader to the shader list.
SLint uniform2f(const SLchar *name, SLfloat v0, SLfloat v1) const
Passes the float values v0 & v1 to the uniform variable "name".
void addUniform1i(SLGLUniform1i *u)
add int uniform
SLuint _progID
OpenGL shader program object ID.
SLint uniform1fv(const SLchar *name, SLsizei count, const SLfloat *value) const
Passes 1 float value py pointer to the uniform variable "name".
SLint getUniformLocation(const SLchar *name) const
SLVUniform1f _uniforms1f
Vector of uniform1f variables.
void addUniform1f(SLGLUniform1f *u)
add float uniform
SLint uniform3f(const SLchar *name, SLfloat v0, SLfloat v1, SLfloat v2) const
Passes the float values v0, v1 & v2 to the uniform variable "name".
SLint uniformMatrix2fv(const SLchar *name, SLsizei count, const SLfloat *value, GLboolean transpose=false) const
Passes a 2x2 float matrix values py pointer to the uniform variable "name".
SLVGLShader _shaders
Vector of all shader objects.
void init(SLVLight *lights)
SLint passLightsToUniforms(SLVLight *lights, SLuint nextTexUnit) const
SLint uniform1iv(const SLchar *name, SLsizei count, const SLint *value) const
Passes 1 int value py pointer to the uniform variable "name".
SLVUniform1i _uniforms1i
Vector of uniform1i variables.
SLint uniform1i(const SLchar *name, SLint v0) const
Passes the int values v0 to the uniform variable "name".
SLint uniform2i(const SLchar *name, SLint v0, SLint v1) const
Passes the int values v0 & v1 to the uniform variable "name".
SLint uniform4iv(const SLchar *name, GLsizei count, const SLint *value) const
Passes 4 int values py pointer to the uniform variable "name".
SLbool _isLinked
Flag if program is linked.
SLGLProgram(SLAssetManager *am, const string &vertShaderFile, const string &fragShaderFile, const string &geomShaderFile="", const string &programName="")
Ctor with a vertex and a fragment shader filename.
void endUse()
SLGLProgram::endUse stops the shader program.
SLint uniform4i(const SLchar *name, SLint v0, SLint v1, SLint v2, SLint v3) const
Passes the int values v0, v1, v2 & v3 to the uniform variable "name".
Encapsulation of an OpenGL shader object.
Singleton class holding all OpenGL states.
static SLGLState * instance()
Public static instance getter for singleton pattern.
SLMat4f viewMatrix
matrix for the active cameras view transform
SLfloat glVersionNOf() const
void useProgram(SLuint progID)
Abstract Light class for OpenGL light sources.
virtual SLVec3f spotDirWS()=0
void createsShadows(SLbool createsShadows)
void shadowMaxBias(SLfloat maxBias)
SLbool doSoftShadows() const
void shadowMap(SLShadowMap *shadowMap)
virtual SLCol4f specular()=0
Returns normally _specularColor * _specularPower.
SLbool isAttenuated() const
virtual SLVec4f positionWS() const =0
static SLbool doColoredShadows
flag if shadows should be displayed with colors for debugging
static SLCol4f globalAmbient
static global ambient light intensity
SLfloat spotCosCut() const
static SLfloat oneOverGamma()
void isOn(const SLbool on)
SLuint softShadowLevel() const
void spotCutOffDEG(SLfloat cutOffAngleDEG)
virtual SLCol4f diffuse()=0
Returns normally _diffuseColor * _diffusePower.
virtual SLCol4f ambient()=0
Return normally _ambientColor * _ambientPower.
void shadowMinBias(SLfloat minBias)
void spotExponent(const SLfloat exp)
SLVec3< T > translation() const
Defines a standard CG material with textures and a shader program.
SLint passToUniforms(SLGLProgram *program, SLint nextTexUnit)
Passes all material parameters as uniforms to the passed shader program.
Base class for all other classes.
SLstring _name
name of an object
const SLstring & name() const
Class for standard and cascaded shadow mapping.
void numCascades(int numCascades)
SLGLVDepthBuffer depthBuffers()
void useCubemap(SLbool useCubemap)
void set(const T X, const T Y, const T Z)
static const float DEG2RAD
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
V3 v3(float x, float y, float z)