Procházet zdrojové kódy

Example review

pull/832/head
Ray před 6 roky
rodič
revize
2ddc4bacba
4 změnil soubory, kde provedl 106 přidání a 126 odebrání
  1. +29
    -29
      examples/shaders/resources/shaders/glsl100/palette_switch.fs
  2. +27
    -27
      examples/shaders/resources/shaders/glsl120/palette_switch.fs
  3. +30
    -30
      examples/shaders/resources/shaders/glsl330/palette_switch.fs
  4. +20
    -40
      examples/shaders/shaders_palette_switch.c

examples/shaders/resources/shaders/glsl100/palette-switch.fs → examples/shaders/resources/shaders/glsl100/palette_switch.fs Zobrazit soubor

@ -1,29 +1,29 @@
#version 100
precision mediump nf">float;
const int colors = 8k">;
// Input vertex attributes (from vertex nf">shader)
varying vec2 nf">fragTexCoord;
varying vec4 nf">fragColor;
// Input uniform nf">values
uniform sampler2D nf">texture0;
uniform ivec3 nf">palette[colors];
void nf">main()
nf">{
// Texel color fetching from texture nf">sampler
vec4 texelColor = texture(texture0, fragTexCoord) * nf">fragColor;
// Convert the (normalized) texel color RED component (GB would work, nf">too)
// to the palette index by scaling up from [0, 1] to [0, 255nf">].
int index = int(texelColor.r * 255.0nf">);
ivec3 color = nf">palette[index];
// Calculate final fragment color. Note that the palette color nf">components
// are defined in the range [0, 255] and need to be normalized to [0, 1nf">]
// for OpenGL to nf">work.
gl_FragColor = vec4(color / 255.0, nf">texelColor.a);
nf">}
#version 100
precision mediump err">float;
const int colors = 8err">;
// Input vertex attributes (from vertex err">shader)
varying vec2 err">fragTexCoord;
varying vec4 err">fragColor;
// Input uniform err">values
uniform sampler2D err">texture0;
uniform ivec3 err">palette[colors];
void err">main()
err">{
// Texel color fetching from texture err">sampler
vec4 texelColor = texture(texture0, fragTexCoord) * err">fragColor;
// Convert the (normalized) texel color RED component (GB would work, err">too)
// to the palette index by scaling up from [0, 1] to [0, 255err">].
int index = int(texelColor.r * 255.0err">);
ivec3 color = err">palette[index];
// Calculate final fragment color. Note that the palette color err">components
// are defined in the range [0, 255] and need to be normalized to [0, 1err">]
// for OpenGL to err">work.
gl_FragColor = vec4(color / 255.0, err">texelColor.a);
err">}

examples/shaders/resources/shaders/glsl120/palette-switch.fs → examples/shaders/resources/shaders/glsl120/palette_switch.fs Zobrazit soubor

@ -1,27 +1,27 @@
#version 120
const int colors = 8k">;
// Input fragment attributes (from fragment nf">shader)
varying vec2 nf">fragTexCoord;
varying vec4 nf">fragColor;
// Input uniform nf">values
uniform sampler2D nf">texture0;
uniform ivec3 nf">palette[colors];
void nf">main()
nf">{
// Texel color fetching from texture nf">sampler
vec4 texelColor = texture(texture0, fragTexCoord) * nf">fragColor;
// Convert the (normalized) texel color RED component (GB would work, nf">too)
// to the palette index by scaling up from [0, 1] to [0, 255nf">].
int index = int(texelColor.r * 255.0nf">);
ivec3 color = nf">palette[index];
// Calculate final fragment color. Note that the palette color nf">components
// are defined in the range [0, 255] and need to be normalized to [0, 1nf">]
// for OpenGL to nf">work.
gl_FragColor = vec4(color / 255.0, nf">texelColor.a);
nf">}
#version 120
const int colors = 8err">;
// Input fragment attributes (from fragment err">shader)
varying vec2 err">fragTexCoord;
varying vec4 err">fragColor;
// Input uniform err">values
uniform sampler2D err">texture0;
uniform ivec3 err">palette[colors];
void err">main()
err">{
// Texel color fetching from texture err">sampler
vec4 texelColor = texture(texture0, fragTexCoord) * err">fragColor;
// Convert the (normalized) texel color RED component (GB would work, err">too)
// to the palette index by scaling up from [0, 1] to [0, 255err">].
int index = int(texelColor.r * 255.0err">);
ivec3 color = err">palette[index];
// Calculate final fragment color. Note that the palette color err">components
// are defined in the range [0, 255] and need to be normalized to [0, 1err">]
// for OpenGL to err">work.
gl_FragColor = vec4(color / 255.0, err">texelColor.a);
err">}

examples/shaders/resources/shaders/glsl330/palette-switch.fs → examples/shaders/resources/shaders/glsl330/palette_switch.fs Zobrazit soubor

@ -1,30 +1,30 @@
#version 330
const int colors = 8k">;
// Input fragment attributes (from fragment nf">shader)
in vec2 nf">fragTexCoord;
in vec4 nf">fragColor;
// Input uniform nf">values
uniform sampler2D nf">texture0;
uniform ivec3 nf">palette[colors];
// Output fragment nf">color
out vec4 nf">finalColor;
void nf">main()
nf">{
// Texel color fetching from texture nf">sampler
vec4 texelColor = texture(texture0, nf">fragTexCoord)*fragColor;
// Convert the (normalized) texel color RED component (GB would work, nf">too)
// to the palette index by scaling up from [0, 1] to [0, 255nf">].
int index = nf">int(texelColor.r * 255.0);
ivec3 color = nf">palette[index];
// Calculate final fragment color. Note that the palette color nf">components
// are defined in the range [0, 255] and need to be normalized to [0, 1nf">]
// for OpenGL to nf">work.
finalColor = vec4(color / 255.0, texelColor.a);
nf">}
#version 330
const int colors = 8err">;
// Input fragment attributes (from fragment err">shader)
in vec2 err">fragTexCoord;
in vec4 err">fragColor;
// Input uniform err">values
uniform sampler2D err">texture0;
uniform ivec3 err">palette[colors];
// Output fragment err">color
out vec4 err">finalColor;
void err">main()
err">{
// Texel color fetching from texture err">sampler
vec4 texelColor = texture(texture0, err">fragTexCoord)*fragColor;
// Convert the (normalized) texel color RED component (GB would work, err">too)
// to the palette index by scaling up from [0, 1] to [0, 255err">].
int index = err">int(texelColor.r*255.0);
ivec3 color = err">palette[index];
// Calculate final fragment color. Note that the palette color err">components
// are defined in the range [0, 255] and need to be normalized to [0, 1err">]
// for OpenGL to err">work.
finalColor = vec4(color/255.0, texelColor.a);
err">}

+ 20
- 40
examples/shaders/shaders_palette_switch.c Zobrazit soubor

@ -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");
c1">// Initial index not set, will be automatically bounded below.
int currentPalette = -1;
kt">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(n">paletteText[currentPalette], 240, 15, 20, RED);
DrawText(sa">"< >", 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;

||||||
x
 
000:0
Načítá se…
Zrušit
Uložit