From e0720a0a5577cfb69ecb34fb4bd89e59f40f6b29 Mon Sep 17 00:00:00 2001 From: Ray Date: Fri, 25 Jun 2021 13:35:43 +0200 Subject: [PATCH] WARNING: REVIEWED: Default shader uniform names When a shader is loaded, by default, several locations are tried to be set automatically. --- .../resources/shaders/glsl100/cubemap.vs | 6 +-- .../resources/shaders/glsl100/skybox.vs | 10 ++--- .../resources/shaders/glsl330/cubemap.vs | 6 +-- .../resources/shaders/glsl330/skybox.vs | 10 ++--- src/config.h | 23 ++++++++--- src/core.c | 18 ++++----- src/rlgl.h | 40 ++++++++++++++++--- 7 files changed, 76 insertions(+), 37 deletions(-) diff --git a/examples/models/resources/shaders/glsl100/cubemap.vs b/examples/models/resources/shaders/glsl100/cubemap.vs index fd8d17e1a..6f486fbad 100644 --- a/examples/models/resources/shaders/glsl100/cubemap.vs +++ b/examples/models/resources/shaders/glsl100/cubemap.vs @@ -4,8 +4,8 @@ attribute vec3 vertexPosition; // Input uniform values -uniform mat4 projection; -uniform mat4 view; +uniform mat4 matProjection; +uniform mat4 matView; // Output vertex attributes (to fragment shader) varying vec3 fragPosition; @@ -16,5 +16,5 @@ void main() fragPosition = vertexPosition; // Calculate final vertex position - gl_Position = projection*view*vec4(vertexPosition, 1.0); + gl_Position = matProjection*matView*vec4(vertexPosition, 1.0); } diff --git a/examples/models/resources/shaders/glsl100/skybox.vs b/examples/models/resources/shaders/glsl100/skybox.vs index 0d00d54fe..e440ace3c 100644 --- a/examples/models/resources/shaders/glsl100/skybox.vs +++ b/examples/models/resources/shaders/glsl100/skybox.vs @@ -4,8 +4,8 @@ attribute vec3 vertexPosition; // Input uniform values -uniform mat4 projection; -uniform mat4 view; +uniform mat4 matProjection; +uniform mat4 matView; // Output vertex attributes (to fragment shader) varying vec3 fragPosition; @@ -16,9 +16,9 @@ void main() fragPosition = vertexPosition; // Remove translation from the view matrix - mat4 rotView = mat4(mat3(view)); - vec4 clipPos = projection*rotView*vec4(vertexPosition, 1.0); + mat4 rotView = mat4(mat3(matView)); + vec4 clipPos = matProjection*rotView*vec4(vertexPosition, 1.0); // Calculate final vertex position - gl_Position = clipPos.xyzw; + gl_Position = clipPos; } diff --git a/examples/models/resources/shaders/glsl330/cubemap.vs b/examples/models/resources/shaders/glsl330/cubemap.vs index d3565dfdc..d71f80867 100644 --- a/examples/models/resources/shaders/glsl330/cubemap.vs +++ b/examples/models/resources/shaders/glsl330/cubemap.vs @@ -4,8 +4,8 @@ in vec3 vertexPosition; // Input uniform values -uniform mat4 projection; -uniform mat4 view; +uniform mat4 matProjection; +uniform mat4 matView; // Output vertex attributes (to fragment shader) out vec3 fragPosition; @@ -16,5 +16,5 @@ void main() fragPosition = vertexPosition; // Calculate final vertex position - gl_Position = projection*view*vec4(vertexPosition, 1.0); + gl_Position = matProjection*matView*vec4(vertexPosition, 1.0); } diff --git a/examples/models/resources/shaders/glsl330/skybox.vs b/examples/models/resources/shaders/glsl330/skybox.vs index 6279bc497..f41d4692e 100644 --- a/examples/models/resources/shaders/glsl330/skybox.vs +++ b/examples/models/resources/shaders/glsl330/skybox.vs @@ -4,8 +4,8 @@ in vec3 vertexPosition; // Input uniform values -uniform mat4 projection; -uniform mat4 view; +uniform mat4 matProjection; +uniform mat4 matView; // Output vertex attributes (to fragment shader) out vec3 fragPosition; @@ -16,9 +16,9 @@ void main() fragPosition = vertexPosition; // Remove translation from the view matrix - mat4 rotView = mat4(mat3(view)); - vec4 clipPos = projection*rotView*vec4(vertexPosition, 1.0); + mat4 rotView = mat4(mat3(matView)); + vec4 clipPos = matProjection*rotView*vec4(vertexPosition, 1.0); // Calculate final vertex position - gl_Position = clipPos.xyzw; + gl_Position = clipPos; } diff --git a/src/config.h b/src/config.h index 6479d409a..4a74655ce 100644 --- a/src/config.h +++ b/src/config.h @@ -105,12 +105,23 @@ #define RL_CULL_DISTANCE_FAR 1000.0 // Default projection matrix far cull distance // Default shader vertex attribute names to set location points -#define DEFAULT_SHADER_ATTRIB_NAME_POSITION "vertexPosition" // Binded by default to shader location: 0 -#define DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD "vertexTexCoord" // Binded by default to shader location: 1 -#define DEFAULT_SHADER_ATTRIB_NAME_NORMAL "vertexNormal" // Binded by default to shader location: 2 -#define DEFAULT_SHADER_ATTRIB_NAME_COLOR "vertexColor" // Binded by default to shader location: 3 -#define DEFAULT_SHADER_ATTRIB_NAME_TANGENT "vertexTangent" // Binded by default to shader location: 4 -#define DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2 "vertexTexCoord2" // Binded by default to shader location: 5 +// NOTE: When a new shader is loaded, the following locations are tried to be set for convenience +#define DEFAULT_SHADER_ATTRIB_NAME_POSITION "vertexPosition" // Binded by default to shader location: 0 +#define DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD "vertexTexCoord" // Binded by default to shader location: 1 +#define DEFAULT_SHADER_ATTRIB_NAME_NORMAL "vertexNormal" // Binded by default to shader location: 2 +#define DEFAULT_SHADER_ATTRIB_NAME_COLOR "vertexColor" // Binded by default to shader location: 3 +#define DEFAULT_SHADER_ATTRIB_NAME_TANGENT "vertexTangent" // Binded by default to shader location: 4 +#define DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2 "vertexTexCoord2" // Binded by default to shader location: 5 + +#define DEFAULT_SHADER_UNIFORM_NAME_MVP "mvp" // model-view-projection matrix +#define DEFAULT_SHADER_UNIFORM_NAME_VIEW "matView" // view matrix +#define DEFAULT_SHADER_UNIFORM_NAME_PROJECTION "matProjection" // projection matrix +#define DEFAULT_SHADER_UNIFORM_NAME_MODEL "matModel" // model matrix +#define DEFAULT_SHADER_UNIFORM_NAME_NORMAL "matNormal" // normal matrix (transpose(inverse(matModelView)) +#define DEFAULT_SHADER_UNIFORM_NAME_COLOR "colDiffuse" // color diffuse (base tint color, multiplied by texture color) +#define DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE0 "texture0" // texture0 (texture slot active 0) +#define DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE1 "texture1" // texture1 (texture slot active 1) +#define DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE2 "texture2" // texture2 (texture slot active 2) //------------------------------------------------------------------------------------ diff --git a/src/core.c b/src/core.c index fc38203ec..a456fab20 100644 --- a/src/core.c +++ b/src/core.c @@ -2375,17 +2375,17 @@ RLAPI Shader LoadShaderFromMemory(const char *vsCode, const char *fsCode) shader.locs[SHADER_LOC_VERTEX_COLOR] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_COLOR); // Get handles to GLSL uniform locations (vertex shader) - shader.locs[SHADER_LOC_MATRIX_MVP] = rlGetLocationUniform(shader.id, "mvp"); - shader.locs[SHADER_LOC_MATRIX_VIEW] = rlGetLocationUniform(shader.id, "view"); - shader.locs[SHADER_LOC_MATRIX_PROJECTION] = rlGetLocationUniform(shader.id, "projection"); - //shader.locs[SHADER_LOC_MATRIX_MODEL] = rlGetLocationUniform(shader.id, "matModel"); - shader.locs[SHADER_LOC_MATRIX_NORMAL] = rlGetLocationUniform(shader.id, "matNormal"); + shader.locs[SHADER_LOC_MATRIX_MVP] = rlGetLocationUniform(shader.id, DEFAULT_SHADER_UNIFORM_NAME_MVP); + shader.locs[SHADER_LOC_MATRIX_VIEW] = rlGetLocationUniform(shader.id, DEFAULT_SHADER_UNIFORM_NAME_VIEW); + shader.locs[SHADER_LOC_MATRIX_PROJECTION] = rlGetLocationUniform(shader.id, DEFAULT_SHADER_UNIFORM_NAME_PROJECTION); + shader.locs[SHADER_LOC_MATRIX_MODEL] = rlGetLocationUniform(shader.id, DEFAULT_SHADER_UNIFORM_NAME_MODEL); + shader.locs[SHADER_LOC_MATRIX_NORMAL] = rlGetLocationUniform(shader.id, DEFAULT_SHADER_UNIFORM_NAME_NORMAL); // Get handles to GLSL uniform locations (fragment shader) - shader.locs[SHADER_LOC_COLOR_DIFFUSE] = rlGetLocationUniform(shader.id, "colDiffuse"); - shader.locs[SHADER_LOC_MAP_DIFFUSE] = rlGetLocationUniform(shader.id, "texture0"); // SHADER_LOC_MAP_ALBEDO - shader.locs[SHADER_LOC_MAP_SPECULAR] = rlGetLocationUniform(shader.id, "texture1"); // SHADER_LOC_MAP_METALNESS - shader.locs[SHADER_LOC_MAP_NORMAL] = rlGetLocationUniform(shader.id, "texture2"); + shader.locs[SHADER_LOC_COLOR_DIFFUSE] = rlGetLocationUniform(shader.id, DEFAULT_SHADER_UNIFORM_NAME_COLOR); + shader.locs[SHADER_LOC_MAP_DIFFUSE] = rlGetLocationUniform(shader.id, DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE0); // SHADER_LOC_MAP_ALBEDO + shader.locs[SHADER_LOC_MAP_SPECULAR] = rlGetLocationUniform(shader.id, DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE1); // SHADER_LOC_MAP_METALNESS + shader.locs[SHADER_LOC_MAP_NORMAL] = rlGetLocationUniform(shader.id, DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE2); } return shader; diff --git a/src/rlgl.h b/src/rlgl.h index fcee91fda..5d37e9a1b 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -762,22 +762,50 @@ RLAPI void rlLoadDrawQuad(void); // Load and draw a quad // Default shader vertex attribute names to set location points #ifndef DEFAULT_SHADER_ATTRIB_NAME_POSITION - #define DEFAULT_SHADER_ATTRIB_NAME_POSITION "vertexPosition" // Binded by default to shader location: 0 + #define DEFAULT_SHADER_ATTRIB_NAME_POSITION "vertexPosition" // Binded by default to shader location: 0 #endif #ifndef DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD - #define DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD "vertexTexCoord" // Binded by default to shader location: 1 + #define DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD "vertexTexCoord" // Binded by default to shader location: 1 #endif #ifndef DEFAULT_SHADER_ATTRIB_NAME_NORMAL - #define DEFAULT_SHADER_ATTRIB_NAME_NORMAL "vertexNormal" // Binded by default to shader location: 2 + #define DEFAULT_SHADER_ATTRIB_NAME_NORMAL "vertexNormal" // Binded by default to shader location: 2 #endif #ifndef DEFAULT_SHADER_ATTRIB_NAME_COLOR - #define DEFAULT_SHADER_ATTRIB_NAME_COLOR "vertexColor" // Binded by default to shader location: 3 + #define DEFAULT_SHADER_ATTRIB_NAME_COLOR "vertexColor" // Binded by default to shader location: 3 #endif #ifndef DEFAULT_SHADER_ATTRIB_NAME_TANGENT - #define DEFAULT_SHADER_ATTRIB_NAME_TANGENT "vertexTangent" // Binded by default to shader location: 4 + #define DEFAULT_SHADER_ATTRIB_NAME_TANGENT "vertexTangent" // Binded by default to shader location: 4 #endif #ifndef DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2 - #define DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2 "vertexTexCoord2" // Binded by default to shader location: 5 + #define DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2 "vertexTexCoord2" // Binded by default to shader location: 5 +#endif + +#ifndef DEFAULT_SHADER_UNIFORM_NAME_MVP + #define DEFAULT_SHADER_UNIFORM_NAME_MVP "mvp" // model-view-projection matrix +#endif +#ifndef DEFAULT_SHADER_UNIFORM_NAME_VIEW + #define DEFAULT_SHADER_UNIFORM_NAME_VIEW "matView" // view matrix +#endif +#ifndef DEFAULT_SHADER_UNIFORM_NAME_PROJECTION + #define DEFAULT_SHADER_UNIFORM_NAME_PROJECTION "matProjection" // projection matrix +#endif +#ifndef DEFAULT_SHADER_UNIFORM_NAME_MODEL + #define DEFAULT_SHADER_UNIFORM_NAME_MODEL "matModel" // model matrix +#endif +#ifndef DEFAULT_SHADER_UNIFORM_NAME_NORMAL + #define DEFAULT_SHADER_UNIFORM_NAME_NORMAL "matNormal" // normal matrix (transpose(inverse(matModelView)) +#endif +#ifndef DEFAULT_SHADER_UNIFORM_NAME_COLOR + #define DEFAULT_SHADER_UNIFORM_NAME_COLOR "colDiffuse" // color diffuse (base tint color, multiplied by texture color) +#endif +#ifndef DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE0 + #define DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE0 "texture0" // texture0 (texture slot active 0) +#endif +#ifndef DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE1 + #define DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE1 "texture1" // texture1 (texture slot active 1) +#endif +#ifndef DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE2 + #define DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE2 "texture2" // texture2 (texture slot active 2) #endif //----------------------------------------------------------------------------------