Browse Source

REVIEWED: LoadShader() and default locations

Reviewed locations descriptions
pull/1856/head
Ray 3 years ago
parent
commit
d4c03b47ec
3 changed files with 64 additions and 101 deletions
  1. +12
    -49
      src/core.c
  2. +26
    -26
      src/raylib.h
  3. +26
    -26
      src/rlgl.h

+ 12
- 49
src/core.c View File

@ -2330,55 +2330,14 @@ void UnloadVrStereoConfig(VrStereoConfig config)
Shader LoadShader(const char *vsFileName, const char *fsFileName)
{
Shader shader = { 0 };
shader.locs = (int *)RL_CALLOC(MAX_SHADER_LOCATIONS, sizeof(int));
// NOTE: All locations must be reseted to -1 (no location)
for (int i = 0; i < MAX_SHADER_LOCATIONS; i++) shader.locs[i] = -1;
char *vShaderStr = NULL;
char *fShaderStr = NULL;
">if (vsFileName != NULL) vShaderStr = LoadFileText(vsFileName);
">if (fsFileName != NULL) fShaderStr = LoadFileText(fsFileName);
char *vShaderStr = LoadFileText(vsFileName);
char *fShaderStr = LoadFileText(fsFileName);
shader.id = rlLoadShaderCode(vShaderStr, fShaderStr);
if (vShaderStr != NULL) UnloadFileText(vShaderStr);
if (fShaderStr != NULL) UnloadFileText(fShaderStr);
// After shader loading, we TRY to set default location names
if (shader.id > 0)
{
// Default shader attrib locations have been fixed before linking:
// vertex position location = 0
// vertex texcoord location = 1
// vertex normal location = 2
// vertex color location = 3
// vertex tangent location = 4
// vertex texcoord2 location = 5
shader = LoadShaderFromMemory(vShaderStr, fShaderStr);
// NOTE: If any location is not found, loc point becomes -1
// Get handles to GLSL input attibute locations
shader.locs[SHADER_LOC_VERTEX_POSITION] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_POSITION);
shader.locs[SHADER_LOC_VERTEX_TEXCOORD01] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD);
shader.locs[SHADER_LOC_VERTEX_TEXCOORD02] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2);
shader.locs[SHADER_LOC_VERTEX_NORMAL] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_NORMAL);
shader.locs[SHADER_LOC_VERTEX_TANGENT] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_TANGENT);
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_NORMAL] = rlGetLocationUniform(shader.id, "matNormal");
// 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.locs[SHADER_LOC_MAP_SPECULAR] = rlGetLocationUniform(shader.id, "texture1");
shader.locs[SHADER_LOC_MAP_NORMAL] = rlGetLocationUniform(shader.id, "texture2");
}
UnloadFileText(vShaderStr);
UnloadFileText(fShaderStr);
return shader;
}
@ -2388,13 +2347,16 @@ RLAPI Shader LoadShaderFromMemory(const char *vsCode, const char *fsCode)
{
Shader shader = { 0 };
shader.locs = (int *)RL_CALLOC(MAX_SHADER_LOCATIONS, sizeof(int));
// NOTE: All locations must be reseted to -1 (no location)
for (int i = 0; i < MAX_SHADER_LOCATIONS; i++) shader.locs[i] = -1;
shader.id = rlLoadShaderCode(vsCode, fsCode);
// After shader loading, we TRY to set default location names
if (shader.id > 0)
{
// Default shader attrib locations have been fixed before linking:
// Default shader attribute locations have been binded before linking:
// vertex position location = 0
// vertex texcoord location = 1
// vertex normal location = 2
@ -2416,12 +2378,13 @@ RLAPI Shader LoadShaderFromMemory(const char *vsCode, const char *fsCode)
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");
// 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.locs[SHADER_LOC_MAP_SPECULAR] = rlGetLocationUniform(shader.id, "texture1");
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");
}

+ 26
- 26
src/raylib.h View File

@ -734,32 +734,32 @@ typedef enum {
// Shader location index
typedef enum {
SHADER_LOC_VERTEX_POSITION = 0, // Shader ">location point: position
SHADER_LOC_VERTEX_TEXCOORD01, // Shader ">location point: texcoord01
SHADER_LOC_VERTEX_TEXCOORD02, // Shader ">location point: texcoord02
SHADER_LOC_VERTEX_NORMAL, // Shader ">location point: normal
SHADER_LOC_VERTEX_TANGENT, // Shader ">location point: tangent
SHADER_LOC_VERTEX_COLOR, // Shader ">location point: color
SHADER_LOC_MATRIX_MVP, // Shader ">location point: model-view-projection matrix
SHADER_LOC_MATRIX_VIEW, // Shader ">location point: view matrix
SHADER_LOC_MATRIX_PROJECTION, // Shader ">location point: projection matrix
SHADER_LOC_MATRIX_MODEL, // Shader ">location point: model matrix
SHADER_LOC_MATRIX_NORMAL, // Shader ">location point: normal matrix
SHADER_LOC_VECTOR_VIEW, // Shader ">location point: view vector
SHADER_LOC_COLOR_DIFFUSE, // Shader ">location point: diffuse color
SHADER_LOC_COLOR_SPECULAR, // Shader ">location point: specular color
SHADER_LOC_COLOR_AMBIENT, // Shader ">location point: ambient color
SHADER_LOC_MAP_ALBEDO, // Shader ">location point: albedo texture (same as: SHADER_LOC_MAP_DIFFUSE)
SHADER_LOC_MAP_METALNESS, // Shader ">location point: metalness texture (same as: SHADER_LOC_MAP_SPECULAR)
SHADER_LOC_MAP_NORMAL, // Shader ">location point: normal texture
SHADER_LOC_MAP_ROUGHNESS, // Shader ">location point: roughness texture
SHADER_LOC_MAP_OCCLUSION, // Shader ">location point: occlusion texture
SHADER_LOC_MAP_EMISSION, // Shader ">location point: emission texture
SHADER_LOC_MAP_HEIGHT, // Shader ">location point: height texture
SHADER_LOC_MAP_CUBEMAP, // Shader ">location point: cubemap texture_cube_map
SHADER_LOC_MAP_IRRADIANCE, // Shader ">location point: irradiance texture_cube_map
SHADER_LOC_MAP_PREFILTER, // Shader ">location point: prefilter texture_cube_map
SHADER_LOC_MAP_BRDF // Shader ">location point: brdf texture
SHADER_LOC_VERTEX_POSITION = 0, // Shader l">location: vertex attribute: position
SHADER_LOC_VERTEX_TEXCOORD01, // Shader l">location: vertex attribute: texcoord01
SHADER_LOC_VERTEX_TEXCOORD02, // Shader l">location: vertex attribute: texcoord02
SHADER_LOC_VERTEX_NORMAL, // Shader l">location: vertex attribute: normal
SHADER_LOC_VERTEX_TANGENT, // Shader l">location: vertex attribute: tangent
SHADER_LOC_VERTEX_COLOR, // Shader l">location: vertex attribute: color
SHADER_LOC_MATRIX_MVP, // Shader l">location: matrix uniform: model-view-projection
SHADER_LOC_MATRIX_VIEW, // Shader l">location: matrix uniform: view (camera transform)
SHADER_LOC_MATRIX_PROJECTION, // Shader l">location: matrix uniform: projection
SHADER_LOC_MATRIX_MODEL, // Shader l">location: matrix uniform: model (transform)
SHADER_LOC_MATRIX_NORMAL, // Shader l">location: matrix uniform: normal
SHADER_LOC_VECTOR_VIEW, // Shader l">location: vector uniform: view
SHADER_LOC_COLOR_DIFFUSE, // Shader l">location: vector uniform: diffuse color
SHADER_LOC_COLOR_SPECULAR, // Shader l">location: vector uniform: specular color
SHADER_LOC_COLOR_AMBIENT, // Shader l">location: vector uniform: ambient color
SHADER_LOC_MAP_ALBEDO, // Shader l">location: sampler2d texture: albedo (same as: SHADER_LOC_MAP_DIFFUSE)
SHADER_LOC_MAP_METALNESS, // Shader l">location: sampler2d texture: metalness (same as: SHADER_LOC_MAP_SPECULAR)
SHADER_LOC_MAP_NORMAL, // Shader l">location: sampler2d texture: normal
SHADER_LOC_MAP_ROUGHNESS, // Shader l">location: sampler2d texture: roughness
SHADER_LOC_MAP_OCCLUSION, // Shader l">location: sampler2d texture: occlusion
SHADER_LOC_MAP_EMISSION, // Shader l">location: sampler2d texture: emission
SHADER_LOC_MAP_HEIGHT, // Shader l">location: sampler2d texture: height
SHADER_LOC_MAP_CUBEMAP, // Shader l">location: samplerCube texture: cubemap
SHADER_LOC_MAP_IRRADIANCE, // Shader l">location: samplerCube texture: irradiance
SHADER_LOC_MAP_PREFILTER, // Shader l">location: samplerCube texture: prefilter
SHADER_LOC_MAP_BRDF // Shader l">location: sampler2d texture: brdf
} ShaderLocationIndex;
#define SHADER_LOC_MAP_DIFFUSE SHADER_LOC_MAP_ALBEDO

+ 26
- 26
src/rlgl.h View File

@ -383,32 +383,32 @@ typedef struct RenderBatch {
// Shader location point type
typedef enum {
SHADER_LOC_VERTEX_POSITION = 0, // Shader ">location point: position
SHADER_LOC_VERTEX_TEXCOORD01, // Shader ">location point: texcoord01
SHADER_LOC_VERTEX_TEXCOORD02, // Shader ">location point: texcoord02
SHADER_LOC_VERTEX_NORMAL, // Shader ">location point: normal
SHADER_LOC_VERTEX_TANGENT, // Shader ">location point: tangent
SHADER_LOC_VERTEX_COLOR, // Shader ">location point: color
SHADER_LOC_MATRIX_MVP, // Shader ">location point: model-view-projection matrix
SHADER_LOC_MATRIX_VIEW, // Shader ">location point: view matrix
SHADER_LOC_MATRIX_PROJECTION, // Shader ">location point: projection matrix
SHADER_LOC_MATRIX_MODEL, // Shader ">location point: model matrix
SHADER_LOC_MATRIX_NORMAL, // Shader ">location point: normal matrix
SHADER_LOC_VECTOR_VIEW, // Shader ">location point: view vector
SHADER_LOC_COLOR_DIFFUSE, // Shader ">location point: diffuse color
SHADER_LOC_COLOR_SPECULAR, // Shader ">location point: specular color
SHADER_LOC_COLOR_AMBIENT, // Shader ">location point: ambient color
SHADER_LOC_MAP_ALBEDO, // Shader ">location point: albedo texture (same as: SHADER_LOC_MAP_DIFFUSE)
SHADER_LOC_MAP_METALNESS, // Shader ">location point: metalness texture (same as: SHADER_LOC_MAP_SPECULAR)
SHADER_LOC_MAP_NORMAL, // Shader ">location point: normal texture
SHADER_LOC_MAP_ROUGHNESS, // Shader ">location point: roughness texture
SHADER_LOC_MAP_OCCLUSION, // Shader ">location point: occlusion texture
SHADER_LOC_MAP_EMISSION, // Shader ">location point: emission texture
SHADER_LOC_MAP_HEIGHT, // Shader ">location point: height texture
SHADER_LOC_MAP_CUBEMAP, // Shader ">location point: cubemap texture_cube_map
SHADER_LOC_MAP_IRRADIANCE, // Shader ">location point: irradiance texture_cube_map
SHADER_LOC_MAP_PREFILTER, // Shader ">location point: prefilter texture_cube_map
SHADER_LOC_MAP_BRDF // Shader ">location point: brdf texture
SHADER_LOC_VERTEX_POSITION = 0, // Shader l">location: vertex attribute: position
SHADER_LOC_VERTEX_TEXCOORD01, // Shader l">location: vertex attribute: texcoord01
SHADER_LOC_VERTEX_TEXCOORD02, // Shader l">location: vertex attribute: texcoord02
SHADER_LOC_VERTEX_NORMAL, // Shader l">location: vertex attribute: normal
SHADER_LOC_VERTEX_TANGENT, // Shader l">location: vertex attribute: tangent
SHADER_LOC_VERTEX_COLOR, // Shader l">location: vertex attribute: color
SHADER_LOC_MATRIX_MVP, // Shader l">location: matrix uniform: model-view-projection
SHADER_LOC_MATRIX_VIEW, // Shader l">location: matrix uniform: view (camera transform)
SHADER_LOC_MATRIX_PROJECTION, // Shader l">location: matrix uniform: projection
SHADER_LOC_MATRIX_MODEL, // Shader l">location: matrix uniform: model (transform)
SHADER_LOC_MATRIX_NORMAL, // Shader l">location: matrix uniform: normal
SHADER_LOC_VECTOR_VIEW, // Shader l">location: vector uniform: view
SHADER_LOC_COLOR_DIFFUSE, // Shader l">location: vector uniform: diffuse color
SHADER_LOC_COLOR_SPECULAR, // Shader l">location: vector uniform: specular color
SHADER_LOC_COLOR_AMBIENT, // Shader l">location: vector uniform: ambient color
SHADER_LOC_MAP_ALBEDO, // Shader l">location: sampler2d texture: albedo (same as: SHADER_LOC_MAP_DIFFUSE)
SHADER_LOC_MAP_METALNESS, // Shader l">location: sampler2d texture: metalness (same as: SHADER_LOC_MAP_SPECULAR)
SHADER_LOC_MAP_NORMAL, // Shader l">location: sampler2d texture: normal
SHADER_LOC_MAP_ROUGHNESS, // Shader l">location: sampler2d texture: roughness
SHADER_LOC_MAP_OCCLUSION, // Shader l">location: sampler2d texture: occlusion
SHADER_LOC_MAP_EMISSION, // Shader l">location: sampler2d texture: emission
SHADER_LOC_MAP_HEIGHT, // Shader l">location: sampler2d texture: height
SHADER_LOC_MAP_CUBEMAP, // Shader l">location: samplerCube texture: cubemap
SHADER_LOC_MAP_IRRADIANCE, // Shader l">location: samplerCube texture: irradiance
SHADER_LOC_MAP_PREFILTER, // Shader l">location: samplerCube texture: prefilter
SHADER_LOC_MAP_BRDF // Shader l">location: sampler2d texture: brdf
} ShaderLocationIndex;
#define SHADER_LOC_MAP_DIFFUSE SHADER_LOC_MAP_ALBEDO

Loading…
Cancel
Save