aboutsummaryrefslogtreecommitdiff
path: root/examples/shaders
diff options
context:
space:
mode:
authorRay <raysan5@gmail.com>2019-05-06 09:41:54 +0200
committerRay <raysan5@gmail.com>2019-05-06 09:41:54 +0200
commit2ddc4bacba8fc6809de924c3d6912d4812226231 (patch)
treea352d0ad95c55f77c3442897920bfa150c4055ef /examples/shaders
parent3e0e91013fa9bd57349de2a95506953d5991ddcc (diff)
downloadraylib-2ddc4bacba8fc6809de924c3d6912d4812226231.tar.gz
raylib-2ddc4bacba8fc6809de924c3d6912d4812226231.zip
Example review
Diffstat (limited to 'examples/shaders')
-rw-r--r--examples/shaders/resources/shaders/glsl100/palette_switch.fs (renamed from examples/shaders/resources/shaders/glsl100/palette-switch.fs)58
-rw-r--r--examples/shaders/resources/shaders/glsl120/palette_switch.fs (renamed from examples/shaders/resources/shaders/glsl120/palette-switch.fs)54
-rw-r--r--examples/shaders/resources/shaders/glsl330/palette_switch.fs (renamed from examples/shaders/resources/shaders/glsl330/palette-switch.fs)60
-rw-r--r--examples/shaders/shaders_palette_switch.c60
4 files changed, 106 insertions, 126 deletions
diff --git a/examples/shaders/resources/shaders/glsl100/palette-switch.fs b/examples/shaders/resources/shaders/glsl100/palette_switch.fs
index 65a7bd29..7f09137e 100644
--- a/examples/shaders/resources/shaders/glsl100/palette-switch.fs
+++ b/examples/shaders/resources/shaders/glsl100/palette_switch.fs
@@ -1,29 +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);
-}
+#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
index b4384502..ab3f79c7 100644
--- a/examples/shaders/resources/shaders/glsl120/palette-switch.fs
+++ b/examples/shaders/resources/shaders/glsl120/palette_switch.fs
@@ -1,27 +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);
-}
+#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
index 61b532ed..7c8a488c 100644
--- a/examples/shaders/resources/shaders/glsl330/palette-switch.fs
+++ b/examples/shaders/resources/shaders/glsl330/palette_switch.fs
@@ -1,30 +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);
-}
+#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
index d0b56190..a2fa0b27 100644
--- a/examples/shaders/shaders_palette_switch.c
+++ b/examples/shaders/shaders_palette_switch.c
@@ -29,7 +29,7 @@
#define VALUES_PER_COLOR 3
static const int palettes[MAX_PALETTES][COLORS_PER_PALETTE*VALUES_PER_COLOR] = {
- {
+ { // 3-BIT RGB
0, 0, 0,
255, 0, 0,
0, 255, 0,
@@ -39,7 +39,7 @@ static const int palettes[MAX_PALETTES][COLORS_PER_PALETTE*VALUES_PER_COLOR] = {
255, 255, 0,
255, 255, 255,
},
- {
+ { // AMMO-8 (GameBoy-like)
4, 12, 6,
17, 35, 24,
30, 58, 41,
@@ -49,7 +49,7 @@ static const int palettes[MAX_PALETTES][COLORS_PER_PALETTE*VALUES_PER_COLOR] = {
190, 220, 127,
238, 255, 204,
},
- {
+ { // RKBV (2-strip film)
21, 25, 26,
138, 76, 88,
217, 98, 117,
@@ -85,8 +85,8 @@ int main()
// 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;
+ int currentPalette = 0;
+ int lineHeight = screenHeight/COLORS_PER_PALETTE;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
@@ -96,21 +96,15 @@ int main()
{
// Update
//----------------------------------------------------------------------------------
- int paletteIndex = currentPalette;
- if (IsKeyPressed(KEY_RIGHT)) paletteIndex++;
- else if (IsKeyPressed(KEY_LEFT)) paletteIndex--;
+ if (IsKeyPressed(KEY_RIGHT)) currentPalette++;
+ else if (IsKeyPressed(KEY_LEFT)) currentPalette--;
- if (paletteIndex >= MAX_PALETTES) paletteIndex = 0;
- else if (paletteIndex < 0) paletteIndex = MAX_PALETTES - 1;
+ if (currentPalette >= MAX_PALETTES) currentPalette = 0;
+ else if (currentPalette < 0) currentPalette = 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;
- SetShaderValueV(shader, paletteLoc, palettes[currentPalette], UNIFORM_IVEC3, COLORS_PER_PALETTE);
- }
+ // NOTE: We are sending RGB triplets w/o the alpha channel
+ SetShaderValueV(shader, paletteLoc, palettes[currentPalette], UNIFORM_IVEC3, COLORS_PER_PALETTE);
//----------------------------------------------------------------------------------
// Draw
@@ -121,33 +115,19 @@ int main()
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)
+ for (int i = 0; i < COLORS_PER_PALETTE; i++)
{
- int height = linesPerRectangle;
-
- if (leftover > 0)
- {
- height += 1;
- leftover -= 1;
- }
-
- DrawRectangle(0, y, screenWidth, height, (Color){ i, i, i, 255 });
-
- y += height;
+ // Draw horizontal screen-wide rectangles with increasing "palette index"
+ // The used palette index is encoded in the RGB components of the pixel
+ DrawRectangle(0, lineHeight*i, GetScreenWidth(), lineHeight, (Color){ i, i, i, 255 });
}
EndShaderMode();
- DrawText("CURRENT PALETTE:", 10, 15, 20, RAYWHITE);
- DrawText(paletteText[currentPalette], 240, 15, 20, RED);
- DrawText("< >", 540, 10, 30, DARKBLUE);
-
+ DrawText("< >", 10, 10, 30, DARKBLUE);
+ DrawText("CURRENT PALETTE:", 60, 15, 20, RAYWHITE);
+ DrawText(paletteText[currentPalette], 300, 15, 20, RED);
+
DrawFPS(700, 15);
EndDrawing();
@@ -158,7 +138,7 @@ int main()
//--------------------------------------------------------------------------------------
UnloadShader(shader); // Unload shader
- CloseWindow(); // Close window and OpenGL context
+ CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;