From 0fe56b1674c99aa75c0dd6f45d77cbff80cf218c Mon Sep 17 00:00:00 2001 From: Marco Lizza Date: Wed, 9 Jan 2019 16:20:56 +0100 Subject: Adding basic palette-switching example using uniform arrays. --- .../resources/shaders/glsl100/palette-switch.fs | 29 ++++ .../resources/shaders/glsl120/palette-switch.fs | 27 ++++ .../resources/shaders/glsl330/palette-switch.fs | 30 ++++ examples/shaders/shaders_palette_switch.c | 160 +++++++++++++++++++++ 4 files changed, 246 insertions(+) create mode 100644 examples/shaders/resources/shaders/glsl100/palette-switch.fs create mode 100644 examples/shaders/resources/shaders/glsl120/palette-switch.fs create mode 100644 examples/shaders/resources/shaders/glsl330/palette-switch.fs create mode 100644 examples/shaders/shaders_palette_switch.c (limited to 'examples/shaders') diff --git a/examples/shaders/resources/shaders/glsl100/palette-switch.fs b/examples/shaders/resources/shaders/glsl100/palette-switch.fs new file mode 100644 index 00000000..65a7bd29 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl100/palette-switch.fs @@ -0,0 +1,29 @@ +#version 100 + +precision mediump float; + +const int colors = 8; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform ivec3 palette[colors]; + +void main() +{ + // Texel color fetching from texture sampler + vec4 texelColor = texture(texture0, fragTexCoord) * fragColor; + + // Convert the (normalized) texel color RED component (GB would work, too) + // to the palette index by scaling up from [0, 1] to [0, 255]. + int index = int(texelColor.r * 255.0); + ivec3 color = palette[index]; + + // Calculate final fragment color. Note that the palette color components + // are defined in the range [0, 255] and need to be normalized to [0, 1] + // for OpenGL to work. + gl_FragColor = vec4(color / 255.0, texelColor.a); +} diff --git a/examples/shaders/resources/shaders/glsl120/palette-switch.fs b/examples/shaders/resources/shaders/glsl120/palette-switch.fs new file mode 100644 index 00000000..b4384502 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/palette-switch.fs @@ -0,0 +1,27 @@ +#version 120 + +const int colors = 8; + +// Input fragment attributes (from fragment shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform ivec3 palette[colors]; + +void main() +{ + // Texel color fetching from texture sampler + vec4 texelColor = texture(texture0, fragTexCoord) * fragColor; + + // Convert the (normalized) texel color RED component (GB would work, too) + // to the palette index by scaling up from [0, 1] to [0, 255]. + int index = int(texelColor.r * 255.0); + ivec3 color = palette[index]; + + // Calculate final fragment color. Note that the palette color components + // are defined in the range [0, 255] and need to be normalized to [0, 1] + // for OpenGL to work. + gl_FragColor = vec4(color / 255.0, texelColor.a); +} diff --git a/examples/shaders/resources/shaders/glsl330/palette-switch.fs b/examples/shaders/resources/shaders/glsl330/palette-switch.fs new file mode 100644 index 00000000..178c42c5 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl330/palette-switch.fs @@ -0,0 +1,30 @@ +#version 330 + +const int colors = 8; + +// Input fragment attributes (from fragment shader) +in vec2 fragTexCoord; +in vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform ivec3 palette[colors]; + +// Output fragment color +out vec4 finalColor; + +void main() +{ + // Texel color fetching from texture sampler + vec4 texelColor = texture(texture0, fragTexCoord) * fragColor; + + // Convert the (normalized) texel color RED component (GB would work, too) + // to the palette index by scaling up from [0, 1] to [0, 255]. + int index = int(texelColor.r * 255.0); + ivec3 color = palette[index]; + + // Calculate final fragment color. Note that the palette color components + // are defined in the range [0, 255] and need to be normalized to [0, 1] + // for OpenGL to work. + finalColor = vec4(color / 255.0, texelColor.a); +} diff --git a/examples/shaders/shaders_palette_switch.c b/examples/shaders/shaders_palette_switch.c new file mode 100644 index 00000000..03151b66 --- /dev/null +++ b/examples/shaders/shaders_palette_switch.c @@ -0,0 +1,160 @@ +/******************************************************************************************* +* +* raylib [shaders] example - Apply a postprocessing shader to a scene +* +* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support, +* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version. +* +* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example +* on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders +* raylib comes with shaders ready for both versions, check raylib/shaders install folder +* +* This example has been created using raylib 2.x (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2015 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#if defined(PLATFORM_DESKTOP) + #define GLSL_VERSION 330 +#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB + #define GLSL_VERSION 100 +#endif + +#define MAX_PALETTES 3 +#define COLORS_PER_PALETTE 8 +#define VALUES_PER_COLOR 3 + +static const int palettes[MAX_PALETTES][COLORS_PER_PALETTE * VALUES_PER_COLOR] = { + { + 0, 0, 0, + 255, 0, 0, + 0, 255, 0, + 0, 0, 255, + 0, 255, 255, + 255, 0, 255, + 255, 255, 0, + 255, 255, 255, + }, + { + 4, 12, 6, + 17, 35, 24, + 30, 58, 41, + 48, 93, 66, + 77, 128, 97, + 137, 162, 87, + 190, 220, 127, + 238, 255, 204, + }, + { + 21, 25, 26, + 138, 76, 88, + 217, 98, 117, + 230, 184, 193, + 69, 107, 115, + 75, 151, 166, + 165, 189, 194, + 255, 245, 247, + } +}; + +static const char *paletteText[] = { + "3-BIT RGB", + "AMMO-8 (GameBoy-like)", + "RKBV (2-strip film)" +}; + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - palette-switch shader"); + + // Load shader to be used on some parts drawing + // NOTE 1: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version + // NOTE 2: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader + Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/palette-switch.fs", GLSL_VERSION)); + + // Get variable (uniform) location on the shader to connect with the program + // NOTE: If uniform variable could not be found in the shader, function returns -1 + int paletteLoc = GetShaderLocation(shader, "palette"); + + // Initial index not set, will be automatically bounded below. + int currentPalette = -1; + + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + int paletteIndex = currentPalette; + if (IsKeyPressed(KEY_RIGHT)) paletteIndex++; + else if (IsKeyPressed(KEY_LEFT)) paletteIndex--; + + if (paletteIndex >= MAX_PALETTES) paletteIndex = 0; + else if (paletteIndex < 0) paletteIndex = MAX_PALETTES - 1; + + // Send new value to the shader to be used on drawing. + // Note that we are sending RGB triplets w/o the alpha channel *only* if the current + // palette index has changed (in order to save performances). + if (currentPalette != paletteIndex) { + currentPalette = paletteIndex; + SetShaderValueArrayi(shader, paletteLoc, palettes[currentPalette], VALUES_PER_COLOR, COLORS_PER_PALETTE); + } + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + BeginShaderMode(shader); + + // Draw horizontal screen-wide rectangles with increasing "palette index". + // The used palette index is encoded in the RGB components of the pixel. + int linesPerRectangle = screenHeight / COLORS_PER_PALETTE; + int leftover = screenHeight % COLORS_PER_PALETTE; + int y = 0; + + for (int i = 0; i < COLORS_PER_PALETTE; ++i) { + int height = linesPerRectangle; + if (leftover > 0) { + height += 1; + leftover -= 1; + } + + DrawRectangle(0, y, screenWidth, height, CLITERAL{ i, i, i, 255 }); + + y += height; + } + + EndShaderMode(); + + DrawText("CURRENT PALETTE:", 10, 15, 20, RAYWHITE); + DrawText(paletteText[currentPalette], 240, 15, 20, RED); + DrawText("< >", 540, 10, 30, DARKBLUE); + + DrawFPS(700, 15); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadShader(shader); // Unload shader + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} -- cgit v1.2.3 From 0c5bee4c9addc20b8b3c443820764854c63f76d7 Mon Sep 17 00:00:00 2001 From: Marco Lizza Date: Wed, 9 Jan 2019 16:33:09 +0100 Subject: Limiting FPS to 60 for uniformity with other examples. --- examples/shaders/shaders_palette_switch.c | 1 + 1 file changed, 1 insertion(+) (limited to 'examples/shaders') diff --git a/examples/shaders/shaders_palette_switch.c b/examples/shaders/shaders_palette_switch.c index 03151b66..ad09ca73 100644 --- a/examples/shaders/shaders_palette_switch.c +++ b/examples/shaders/shaders_palette_switch.c @@ -88,6 +88,7 @@ int main() // Initial index not set, will be automatically bounded below. int currentPalette = -1; + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop -- cgit v1.2.3 From 55f8dbc755c2e31d2112e71439fef0d31e8090a6 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 10 Jan 2019 11:25:26 +0100 Subject: WARNING: Redesigned SetShaderValue() --- examples/models/models_material_pbr.c | 20 ++--- examples/models/models_skybox.c | 4 +- examples/models/rlights.h | 10 +-- examples/others/standard_lighting.c | 25 +++--- .../resources/shaders/glsl330/palette-switch.fs | 2 +- examples/shaders/shaders_custom_uniform.c | 2 +- examples/shaders/shaders_palette_switch.c | 24 +++--- examples/shaders/shaders_raymarching.c | 12 +-- src/raylib.h | 23 +++++- src/rlgl.h | 96 ++++++++++++---------- 10 files changed, 120 insertions(+), 98 deletions(-) (limited to 'examples/shaders') diff --git a/examples/models/models_material_pbr.c b/examples/models/models_material_pbr.c index 6885f753..f93c7a68 100644 --- a/examples/models/models_material_pbr.c +++ b/examples/models/models_material_pbr.c @@ -64,7 +64,7 @@ int main() // Send to material PBR shader camera view position float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z }; - SetShaderValue(model.material.shader, model.material.shader.locs[LOC_VECTOR_VIEW], cameraPos, 3); + SetShaderValue(model.material.shader, model.material.shader.locs[LOC_VECTOR_VIEW], cameraPos, UNIFORM_VEC3); //---------------------------------------------------------------------------------- // Draw @@ -148,9 +148,9 @@ static Material LoadMaterialPBR(Color albedo, float metalness, float roughness) Shader shdrBRDF = LoadShader(PATH_BRDF_VS, PATH_BRDF_FS); // Setup required shader locations - SetShaderValuei(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, 1); - SetShaderValuei(shdrIrradiance, GetShaderLocation(shdrIrradiance, "environmentMap"), (int[1]){ 0 }, 1); - SetShaderValuei(shdrPrefilter, GetShaderLocation(shdrPrefilter, "environmentMap"), (int[1]){ 0 }, 1); + SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, UNIFORM_INT); + SetShaderValue(shdrIrradiance, GetShaderLocation(shdrIrradiance, "environmentMap"), (int[1]){ 0 }, UNIFORM_INT); + SetShaderValue(shdrPrefilter, GetShaderLocation(shdrPrefilter, "environmentMap"), (int[1]){ 0 }, UNIFORM_INT); Texture2D texHDR = LoadTexture("resources/dresden_square.hdr"); Texture2D cubemap = GenTextureCubemap(shdrCubemap, texHDR, CUBEMAP_SIZE); @@ -174,14 +174,14 @@ static Material LoadMaterialPBR(Color albedo, float metalness, float roughness) SetTextureFilter(mat.maps[MAP_OCCLUSION].texture, FILTER_BILINEAR); // Enable sample usage in shader for assigned textures - SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "albedo.useSampler"), (int[1]){ 1 }, 1); - SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "normals.useSampler"), (int[1]){ 1 }, 1); - SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "metalness.useSampler"), (int[1]){ 1 }, 1); - SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "roughness.useSampler"), (int[1]){ 1 }, 1); - SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "occlusion.useSampler"), (int[1]){ 1 }, 1); + SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "albedo.useSampler"), (int[1]){ 1 }, UNIFORM_INT); + SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "normals.useSampler"), (int[1]){ 1 }, UNIFORM_INT); + SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "metalness.useSampler"), (int[1]){ 1 }, UNIFORM_INT); + SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "roughness.useSampler"), (int[1]){ 1 }, UNIFORM_INT); + SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "occlusion.useSampler"), (int[1]){ 1 }, UNIFORM_INT); int renderModeLoc = GetShaderLocation(mat.shader, "renderMode"); - SetShaderValuei(mat.shader, renderModeLoc, (int[1]){ 0 }, 1); + SetShaderValue(mat.shader, renderModeLoc, (int[1]){ 0 }, UNIFORM_INT); // Set up material properties color mat.maps[MAP_ALBEDO].color = albedo; diff --git a/examples/models/models_skybox.c b/examples/models/models_skybox.c index a6b233e3..c7f76ecf 100644 --- a/examples/models/models_skybox.c +++ b/examples/models/models_skybox.c @@ -30,11 +30,11 @@ int main() // Load skybox shader and set required locations // NOTE: Some locations are automatically set at shader loading skybox.material.shader = LoadShader("resources/shaders/skybox.vs", "resources/shaders/skybox.fs"); - SetShaderValuei(skybox.material.shader, GetShaderLocation(skybox.material.shader, "environmentMap"), (int[1]){ MAP_CUBEMAP }, 1); + SetShaderValue(skybox.material.shader, GetShaderLocation(skybox.material.shader, "environmentMap"), (int[1]){ MAP_CUBEMAP }, UNIFORM_INT); // Load cubemap shader and setup required shader locations Shader shdrCubemap = LoadShader("resources/shaders/cubemap.vs", "resources/shaders/cubemap.fs"); - SetShaderValuei(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, 1); + SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, UNIFORM_INT); // Load HDR panorama (sphere) texture Texture2D texHDR = LoadTexture("resources/dresden_square.hdr"); diff --git a/examples/models/rlights.h b/examples/models/rlights.h index 0da3e2cb..19504473 100644 --- a/examples/models/rlights.h +++ b/examples/models/rlights.h @@ -158,20 +158,20 @@ Light CreateLight(int type, Vector3 pos, Vector3 targ, Color color, Shader shade void UpdateLightValues(Shader shader, Light light) { // Send to shader light enabled state and type - SetShaderValuei(shader, light.enabledLoc, (int[1]){ light.enabled }, 1); - SetShaderValuei(shader, light.typeLoc, (int[1]){ light.type }, 1); + SetShaderValue(shader, light.enabledLoc, &light.enabled, UNIFORM_INT); + SetShaderValue(shader, light.typeLoc, &light.type, UNIFORM_INT); // Send to shader light position values float position[3] = { light.position.x, light.position.y, light.position.z }; - SetShaderValue(shader, light.posLoc, position, 3); + SetShaderValue(shader, light.posLoc, position, UNIFORM_VEC3); // Send to shader light target position values float target[3] = { light.target.x, light.target.y, light.target.z }; - SetShaderValue(shader, light.targetLoc, target, 3); + SetShaderValue(shader, light.targetLoc, target, UNIFORM_VEC3); // Send to shader light color values float diff[4] = { (float)light.color.r/(float)255, (float)light.color.g/(float)255, (float)light.color.b/(float)255, (float)light.color.a/(float)255 }; - SetShaderValue(shader, light.colorLoc, diff, 4); + SetShaderValue(shader, light.colorLoc, diff, UNIFORM_VEC4); } #endif // RLIGHTS_IMPLEMENTATION \ No newline at end of file diff --git a/examples/others/standard_lighting.c b/examples/others/standard_lighting.c index 72890436..7034aa35 100644 --- a/examples/others/standard_lighting.c +++ b/examples/others/standard_lighting.c @@ -350,9 +350,6 @@ static void GetShaderLightsLocations(Shader shader) // Set shader uniform values for lights // NOTE: It would be far easier with shader UBOs but are not supported on OpenGL ES 2.0 -// TODO: Replace glUniform1i(), glUniform1f(), glUniform3f(), glUniform4f(): -//SetShaderValue(Shader shader, int uniformLoc, float *value, int size) -//SetShaderValuei(Shader shader, int uniformLoc, int *value, int size) static void SetShaderLightsValues(Shader shader) { int tempInt[8] = { 0 }; @@ -363,20 +360,20 @@ static void SetShaderLightsValues(Shader shader) if (i < lightsCount) { tempInt[0] = lights[i]->enabled; - SetShaderValuei(shader, lightsLocs[i][0], tempInt, 1); //glUniform1i(lightsLocs[i][0], lights[i]->enabled); + SetShaderValue(shader, lightsLocs[i][0], tempInt, UNIFORM_INT); //glUniform1i(lightsLocs[i][0], lights[i]->enabled); tempInt[0] = lights[i]->type; - SetShaderValuei(shader, lightsLocs[i][1], tempInt, 1); //glUniform1i(lightsLocs[i][1], lights[i]->type); + SetShaderValue(shader, lightsLocs[i][1], tempInt, UNIFORM_INT); //glUniform1i(lightsLocs[i][1], lights[i]->type); tempFloat[0] = (float)lights[i]->diffuse.r/255.0f; tempFloat[1] = (float)lights[i]->diffuse.g/255.0f; tempFloat[2] = (float)lights[i]->diffuse.b/255.0f; tempFloat[3] = (float)lights[i]->diffuse.a/255.0f; - SetShaderValue(shader, lightsLocs[i][5], tempFloat, 4); + SetShaderValue(shader, lightsLocs[i][5], tempFloat, UNIFORM_VEC4); //glUniform4f(lightsLocs[i][5], (float)lights[i]->diffuse.r/255, (float)lights[i]->diffuse.g/255, (float)lights[i]->diffuse.b/255, (float)lights[i]->diffuse.a/255); tempFloat[0] = lights[i]->intensity; - SetShaderValue(shader, lightsLocs[i][6], tempFloat, 1); + SetShaderValue(shader, lightsLocs[i][6], tempFloat, UNIFORM_FLOAT); switch (lights[i]->type) { @@ -385,10 +382,10 @@ static void SetShaderLightsValues(Shader shader) tempFloat[0] = lights[i]->position.x; tempFloat[1] = lights[i]->position.y; tempFloat[2] = lights[i]->position.z; - SetShaderValue(shader, lightsLocs[i][2], tempFloat, 3); + SetShaderValue(shader, lightsLocs[i][2], tempFloat, UNIFORM_VEC3); tempFloat[0] = lights[i]->radius; - SetShaderValue(shader, lightsLocs[i][4], tempFloat, 1); + SetShaderValue(shader, lightsLocs[i][4], tempFloat, UNIFORM_FLOAT); //glUniform3f(lightsLocs[i][2], lights[i]->position.x, lights[i]->position.y, lights[i]->position.z); //glUniform1f(lightsLocs[i][4], lights[i]->radius); @@ -401,7 +398,7 @@ static void SetShaderLightsValues(Shader shader) tempFloat[0] = direction.x; tempFloat[1] = direction.y; tempFloat[2] = direction.z; - SetShaderValue(shader, lightsLocs[i][3], tempFloat, 3); + SetShaderValue(shader, lightsLocs[i][3], tempFloat, UNIFORM_VEC3); //glUniform3f(lightsLocs[i][3], direction.x, direction.y, direction.z); } break; @@ -410,7 +407,7 @@ static void SetShaderLightsValues(Shader shader) tempFloat[0] = lights[i]->position.x; tempFloat[1] = lights[i]->position.y; tempFloat[2] = lights[i]->position.z; - SetShaderValue(shader, lightsLocs[i][2], tempFloat, 3); + SetShaderValue(shader, lightsLocs[i][2], tempFloat, UNIFORM_VEC3); //glUniform3f(lightsLocs[i][2], lights[i]->position.x, lights[i]->position.y, lights[i]->position.z); @@ -420,11 +417,11 @@ static void SetShaderLightsValues(Shader shader) tempFloat[0] = direction.x; tempFloat[1] = direction.y; tempFloat[2] = direction.z; - SetShaderValue(shader, lightsLocs[i][3], tempFloat, 3); + SetShaderValue(shader, lightsLocs[i][3], tempFloat, UNIFORM_VEC3); //glUniform3f(lightsLocs[i][3], direction.x, direction.y, direction.z); tempFloat[0] = lights[i]->coneAngle; - SetShaderValue(shader, lightsLocs[i][7], tempFloat, 1); + SetShaderValue(shader, lightsLocs[i][7], tempFloat, UNIFORM_FLOAT); //glUniform1f(lightsLocs[i][7], lights[i]->coneAngle); } break; default: break; @@ -433,7 +430,7 @@ static void SetShaderLightsValues(Shader shader) else { tempInt[0] = 0; - SetShaderValuei(shader, lightsLocs[i][0], tempInt, 1); //glUniform1i(lightsLocs[i][0], 0); // Light disabled + SetShaderValue(shader, lightsLocs[i][0], tempInt, UNIFORM_INT); //glUniform1i(lightsLocs[i][0], 0); // Light disabled } } } diff --git a/examples/shaders/resources/shaders/glsl330/palette-switch.fs b/examples/shaders/resources/shaders/glsl330/palette-switch.fs index 178c42c5..61b532ed 100644 --- a/examples/shaders/resources/shaders/glsl330/palette-switch.fs +++ b/examples/shaders/resources/shaders/glsl330/palette-switch.fs @@ -16,7 +16,7 @@ out vec4 finalColor; void main() { // Texel color fetching from texture sampler - vec4 texelColor = texture(texture0, fragTexCoord) * fragColor; + vec4 texelColor = texture(texture0, fragTexCoord)*fragColor; // Convert the (normalized) texel color RED component (GB would work, too) // to the palette index by scaling up from [0, 1] to [0, 255]. diff --git a/examples/shaders/shaders_custom_uniform.c b/examples/shaders/shaders_custom_uniform.c index de76a376..fbfd82d0 100644 --- a/examples/shaders/shaders_custom_uniform.c +++ b/examples/shaders/shaders_custom_uniform.c @@ -79,7 +79,7 @@ int main() swirlCenter[1] = screenHeight - mousePosition.y; // Send new value to the shader to be used on drawing - SetShaderValue(shader, swirlCenterLoc, swirlCenter, 2); + SetShaderValue(shader, swirlCenterLoc, swirlCenter, UNIFORM_VEC2); UpdateCamera(&camera); // Update camera //---------------------------------------------------------------------------------- diff --git a/examples/shaders/shaders_palette_switch.c b/examples/shaders/shaders_palette_switch.c index ad09ca73..d0b56190 100644 --- a/examples/shaders/shaders_palette_switch.c +++ b/examples/shaders/shaders_palette_switch.c @@ -1,6 +1,6 @@ /******************************************************************************************* * -* raylib [shaders] example - Apply a postprocessing shader to a scene +* raylib [shaders] example - Color palette switch * * NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support, * OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version. @@ -9,10 +9,10 @@ * on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders * raylib comes with shaders ready for both versions, check raylib/shaders install folder * -* This example has been created using raylib 2.x (www.raylib.com) +* This example has been created using raylib 2.3 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * -* Copyright (c) 2015 Ramon Santamaria (@raysan5) +* Copyright (c) 2019 Ramon Santamaria (@raysan5) * ********************************************************************************************/ @@ -28,7 +28,7 @@ #define COLORS_PER_PALETTE 8 #define VALUES_PER_COLOR 3 -static const int palettes[MAX_PALETTES][COLORS_PER_PALETTE * VALUES_PER_COLOR] = { +static const int palettes[MAX_PALETTES][COLORS_PER_PALETTE*VALUES_PER_COLOR] = { { 0, 0, 0, 255, 0, 0, @@ -74,7 +74,7 @@ int main() int screenWidth = 800; int screenHeight = 450; - InitWindow(screenWidth, screenHeight, "raylib [shaders] example - palette-switch shader"); + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - color palette switch"); // Load shader to be used on some parts drawing // NOTE 1: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version @@ -106,9 +106,10 @@ int main() // Send new value to the shader to be used on drawing. // Note that we are sending RGB triplets w/o the alpha channel *only* if the current // palette index has changed (in order to save performances). - if (currentPalette != paletteIndex) { + if (currentPalette != paletteIndex) + { currentPalette = paletteIndex; - SetShaderValueArrayi(shader, paletteLoc, palettes[currentPalette], VALUES_PER_COLOR, COLORS_PER_PALETTE); + SetShaderValueV(shader, paletteLoc, palettes[currentPalette], UNIFORM_IVEC3, COLORS_PER_PALETTE); } //---------------------------------------------------------------------------------- @@ -126,14 +127,17 @@ int main() int leftover = screenHeight % COLORS_PER_PALETTE; int y = 0; - for (int i = 0; i < COLORS_PER_PALETTE; ++i) { + for (int i = 0; i < COLORS_PER_PALETTE; ++i) + { int height = linesPerRectangle; - if (leftover > 0) { + + if (leftover > 0) + { height += 1; leftover -= 1; } - DrawRectangle(0, y, screenWidth, height, CLITERAL{ i, i, i, 255 }); + DrawRectangle(0, y, screenWidth, height, (Color){ i, i, i, 255 }); y += height; } diff --git a/examples/shaders/shaders_raymarching.c b/examples/shaders/shaders_raymarching.c index d1f9d5f8..79868a2a 100644 --- a/examples/shaders/shaders_raymarching.c +++ b/examples/shaders/shaders_raymarching.c @@ -48,7 +48,7 @@ int main() int resolutionLoc = GetShaderLocation(shader, "resolution"); float resolution[2] = { screenWidth, screenHeight }; - SetShaderValue(shader, resolutionLoc, resolution, 2); + SetShaderValue(shader, resolutionLoc, resolution, UNIFORM_VEC2); float runTime = 0.0f; @@ -70,11 +70,11 @@ int main() runTime += deltaTime; // Set shader required uniform values - SetShaderValue(shader, viewEyeLoc, cameraPos, 3); - SetShaderValue(shader, viewCenterLoc, cameraTarget, 3); - SetShaderValue(shader, viewUpLoc, cameraUp, 3); - SetShaderValue(shader, deltaTimeLoc, &deltaTime, 1); - SetShaderValue(shader, runTimeLoc, &runTime, 1); + SetShaderValue(shader, viewEyeLoc, cameraPos, UNIFORM_VEC3); + SetShaderValue(shader, viewCenterLoc, cameraTarget, UNIFORM_VEC3); + SetShaderValue(shader, viewUpLoc, cameraUp, UNIFORM_VEC3); + SetShaderValue(shader, deltaTimeLoc, &deltaTime, UNIFORM_FLOAT); + SetShaderValue(shader, runTimeLoc, &runTime, UNIFORM_FLOAT); //---------------------------------------------------------------------------------- // Draw diff --git a/src/raylib.h b/src/raylib.h index 275a3b63..5e18ae00 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -676,6 +676,23 @@ typedef enum { #define LOC_MAP_DIFFUSE LOC_MAP_ALBEDO #define LOC_MAP_SPECULAR LOC_MAP_METALNESS +// Shader uniform data types +typedef enum { + UNIFORM_BOOL = 0, + UNIFORM_INT, + UNIFORM_UNIT, + UNIFORM_FLOAT, + UNIFORM_IVEC2, + UNIFORM_IVEC3, + UNIFORM_IVEC4, + UNIFORM_UVEC2, + UNIFORM_UVEC3, + UNIFORM_UVEC4, + UNIFORM_VEC2, + UNIFORM_VEC3, + UNIFORM_VEC4, +} ShaderUniformDataType; + // Material map type typedef enum { MAP_ALBEDO = 0, // MAP_DIFFUSE @@ -1229,10 +1246,8 @@ RLAPI Texture2D GetTextureDefault(void); // Get // Shader configuration functions RLAPI int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location -RLAPI void SetShaderValue(Shader shader, int uniformLoc, const float *value, int size); // Set shader uniform value (float) -RLAPI void SetShaderValuei(Shader shader, int uniformLoc, const int *value, int size); // Set shader uniform value (int) -RLAPI void SetShaderValueArray(Shader shader, int uniformLoc, const float *value, int size, int count); // Set shader uniform value (array of float/vec2/vec3/vec4) -RLAPI void SetShaderValueArrayi(Shader shader, int uniformLoc, const int *value, int size, int count); // Set shader uniform value (array of int/ivec2/ivec3/ivec4) +RLAPI void SetShaderValue(Shader shader, int uniformLoc, const void *value, int uniformType); // Set shader uniform value +RLAPI void SetShaderValueV(Shader shader, int uniformLoc, const void *value, int uniformType, int count); // Set shader uniform value vector RLAPI void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4) RLAPI void SetMatrixProjection(Matrix proj); // Set a custom projection matrix (replaces internal projection matrix) RLAPI void SetMatrixModelview(Matrix view); // Set a custom modelview matrix (replaces internal modelview matrix) diff --git a/src/rlgl.h b/src/rlgl.h index 07f18f0f..e40553bc 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -332,6 +332,23 @@ typedef unsigned char byte; LOC_MAP_PREFILTER, LOC_MAP_BRDF } ShaderLocationIndex; + + // Shader uniform data types + typedef enum { + UNIFORM_BOOL = 0, + UNIFORM_INT, + UNIFORM_UNIT, + UNIFORM_FLOAT, + UNIFORM_IVEC2, + UNIFORM_IVEC3, + UNIFORM_IVEC4, + UNIFORM_UVEC2, + UNIFORM_UVEC3, + UNIFORM_UVEC4, + UNIFORM_VEC2, + UNIFORM_VEC3, + UNIFORM_VEC4, + } ShaderUniformDataType; #define LOC_MAP_DIFFUSE LOC_MAP_ALBEDO #define LOC_MAP_SPECULAR LOC_MAP_METALNESS @@ -468,10 +485,8 @@ Texture2D GetTextureDefault(void); // Get defau // Shader configuration functions int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location -void SetShaderValue(Shader shader, int uniformLoc, const float *value, int size); // Set shader uniform value (float) -void SetShaderValuei(Shader shader, int uniformLoc, const int *value, int size); // Set shader uniform value (int) -void SetShaderValueArray(Shader shader, int uniformLoc, const float *value, int size, int count); // Set shader uniform value (array of float/vec2/vec3/vec4) -void SetShaderValueArrayi(Shader shader, int uniformLoc, const int *value, int size, int count); // Set shader uniform value (array of int/ivec2/ivec3/ivec4) +void SetShaderValue(Shader shader, int uniformLoc, const void *value, int uniformType); // Set shader uniform value +void SetShaderValueV(Shader shader, int uniformLoc, const void *value, int uniformType, int count); // Set shader uniform value vector void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4) void SetMatrixProjection(Matrix proj); // Set a custom projection matrix (replaces internal projection matrix) void SetMatrixModelview(Matrix view); // Set a custom modelview matrix (replaces internal modelview matrix) @@ -2893,49 +2908,40 @@ int GetShaderLocation(Shader shader, const char *uniformName) return location; } -// Set shader uniform value (float/vec2/vec3/vec4) -void SetShaderValue(Shader shader, int uniformLoc, const float *value, int size) -{ - SetShaderValueArray(shader, uniformLoc, value, size, 1); -} - -// Set shader uniform value (int/ivec2/ivec3/ivec4) -void SetShaderValuei(Shader shader, int uniformLoc, const int *value, int size) +// Set shader uniform value +void SetShaderValue(Shader shader, int uniformLoc, const void *value, int uniformType) { - SetShaderValueArrayi(shader, uniformLoc, value, size, 1); + SetShaderValueV(shader, uniformLoc, value, uniformType, 1); } -// Set shader uniform value (array of float/vec2/vec3/vec4) -void SetShaderValueArray(Shader shader, int uniformLoc, const float *value, int size, int count) +// Set shader uniform value vector +void SetShaderValueV(Shader shader, int uniformLoc, const void *value, int uniformType, int count) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) glUseProgram(shader.id); - if (size == 1) glUniform1fv(uniformLoc, count, value); // Shader uniform type: float[] - else if (size == 2) glUniform2fv(uniformLoc, count, value); // Shader uniform type: vec2[] - else if (size == 3) glUniform3fv(uniformLoc, count, value); // Shader uniform type: vec3[] - else if (size == 4) glUniform4fv(uniformLoc, count, value); // Shader uniform type: vec4[] - else TraceLog(LOG_WARNING, "Wrong size for shader's uniform value (1 to 4 supported)"); - + switch (uniformType) + { + case UNIFORM_BOOL: glUniform1iv(uniformLoc, count, (int *)value); break; + case UNIFORM_INT: glUniform1iv(uniformLoc, count, (int *)value); break; + case UNIFORM_UNIT: glUniform1uiv(uniformLoc, count, (unsigned int *)value); break; + case UNIFORM_FLOAT: glUniform1fv(uniformLoc, count, (float *)value); break; + case UNIFORM_IVEC2: glUniform2iv(uniformLoc, count, (int *)value); break; + case UNIFORM_IVEC3: glUniform3iv(uniformLoc, count, (int *)value); break; + case UNIFORM_IVEC4: glUniform4iv(uniformLoc, count, (int *)value); break; + case UNIFORM_UVEC2: glUniform2uiv(uniformLoc, count, (unsigned int *)value); break; + case UNIFORM_UVEC3: glUniform3uiv(uniformLoc, count, (unsigned int *)value); break; + case UNIFORM_UVEC4: glUniform4uiv(uniformLoc, count, (unsigned int *)value); break; + case UNIFORM_VEC2: glUniform2fv(uniformLoc, count, (float *)value); break; + case UNIFORM_VEC3: glUniform3fv(uniformLoc, count, (float *)value); break; + case UNIFORM_VEC4: glUniform4fv(uniformLoc, count, (float *)value); break; + default: TraceLog(LOG_WARNING, "Shader uniform could not be set data type not recognized"); + } + //glUseProgram(0); // Avoid reseting current shader program, in case other uniforms are set #endif } -// Set shader uniform value (array of int/ivec2/ivec3/ivec4) -void SetShaderValueArrayi(Shader shader, int uniformLoc, const int *value, int size, int count) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - glUseProgram(shader.id); - - if (size == 1) glUniform1iv(uniformLoc, count, value); // Shader uniform type: int[] - else if (size == 2) glUniform2iv(uniformLoc, count, value); // Shader uniform type: ivec2[] - else if (size == 3) glUniform3iv(uniformLoc, count, value); // Shader uniform type: ivec3[] - else if (size == 4) glUniform4iv(uniformLoc, count, value); // Shader uniform type: ivec4[] - else TraceLog(LOG_WARNING, "Wrong size for shader's uniform value (1 to 4 supported)"); - - //glUseProgram(0); // Avoid reseting current shader program, in case other uniforms are set -#endif -} // Set shader uniform value (matrix 4x4) void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat) @@ -4286,15 +4292,15 @@ static void SetStereoConfig(VrDeviceInfo hmd) #if defined(SUPPORT_DISTORTION_SHADER) // Update distortion shader with lens and distortion-scale parameters - SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "leftLensCenter"), leftLensCenter, 2); - SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "rightLensCenter"), rightLensCenter, 2); - SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "leftScreenCenter"), leftScreenCenter, 2); - SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "rightScreenCenter"), rightScreenCenter, 2); - - SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "scale"), scale, 2); - SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "scaleIn"), scaleIn, 2); - SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "hmdWarpParam"), hmd.lensDistortionValues, 4); - SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "chromaAbParam"), hmd.chromaAbCorrection, 4); + SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "leftLensCenter"), leftLensCenter, UNIFORM_VEC2); + SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "rightLensCenter"), rightLensCenter, UNIFORM_VEC2); + SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "leftScreenCenter"), leftScreenCenter, UNIFORM_VEC2); + SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "rightScreenCenter"), rightScreenCenter, UNIFORM_VEC2); + + SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "scale"), scale, UNIFORM_VEC2); + SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "scaleIn"), scaleIn, UNIFORM_VEC2); + SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "hmdWarpParam"), hmd.lensDistortionValues, UNIFORM_VEC4); + SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "chromaAbParam"), hmd.chromaAbCorrection, UNIFORM_VEC4); #endif // Fovy is normally computed with: 2*atan2(hmd.vScreenSize, 2*hmd.eyeToScreenDistance) -- cgit v1.2.3