Example review

This commit is contained in:
Ray 2019-05-06 09:41:54 +02:00
parent 3e0e91013f
commit 2ddc4bacba
4 changed files with 106 additions and 126 deletions

View file

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

View file

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

View file

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

View file

@ -29,7 +29,7 @@
#define VALUES_PER_COLOR 3 #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] = {
{ { // 3-BIT RGB
0, 0, 0, 0, 0, 0,
255, 0, 0, 255, 0, 0,
0, 255, 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, 0,
255, 255, 255, 255, 255, 255,
}, },
{ { // AMMO-8 (GameBoy-like)
4, 12, 6, 4, 12, 6,
17, 35, 24, 17, 35, 24,
30, 58, 41, 30, 58, 41,
@ -49,7 +49,7 @@ static const int palettes[MAX_PALETTES][COLORS_PER_PALETTE*VALUES_PER_COLOR] = {
190, 220, 127, 190, 220, 127,
238, 255, 204, 238, 255, 204,
}, },
{ { // RKBV (2-strip film)
21, 25, 26, 21, 25, 26,
138, 76, 88, 138, 76, 88,
217, 98, 117, 217, 98, 117,
@ -85,8 +85,8 @@ int main()
// NOTE: If uniform variable could not be found in the shader, function returns -1 // NOTE: If uniform variable could not be found in the shader, function returns -1
int paletteLoc = GetShaderLocation(shader, "palette"); int paletteLoc = GetShaderLocation(shader, "palette");
// Initial index not set, will be automatically bounded below. int currentPalette = 0;
int currentPalette = -1; int lineHeight = screenHeight/COLORS_PER_PALETTE;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
@ -96,21 +96,15 @@ int main()
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
int paletteIndex = currentPalette; if (IsKeyPressed(KEY_RIGHT)) currentPalette++;
if (IsKeyPressed(KEY_RIGHT)) paletteIndex++; else if (IsKeyPressed(KEY_LEFT)) currentPalette--;
else if (IsKeyPressed(KEY_LEFT)) paletteIndex--;
if (paletteIndex >= MAX_PALETTES) paletteIndex = 0; if (currentPalette >= MAX_PALETTES) currentPalette = 0;
else if (paletteIndex < 0) paletteIndex = MAX_PALETTES - 1; else if (currentPalette < 0) currentPalette = MAX_PALETTES - 1;
// Send new value to the shader to be used on drawing. // 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 // NOTE: We are sending RGB triplets w/o the alpha channel
// palette index has changed (in order to save performances). SetShaderValueV(shader, paletteLoc, palettes[currentPalette], UNIFORM_IVEC3, COLORS_PER_PALETTE);
if (currentPalette != paletteIndex)
{
currentPalette = paletteIndex;
SetShaderValueV(shader, paletteLoc, palettes[currentPalette], UNIFORM_IVEC3, COLORS_PER_PALETTE);
}
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw
@ -121,33 +115,19 @@ int main()
BeginShaderMode(shader); BeginShaderMode(shader);
// Draw horizontal screen-wide rectangles with increasing "palette index". for (int i = 0; i < COLORS_PER_PALETTE; i++)
// 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; // Draw horizontal screen-wide rectangles with increasing "palette index"
// The used palette index is encoded in the RGB components of the pixel
if (leftover > 0) DrawRectangle(0, lineHeight*i, GetScreenWidth(), lineHeight, (Color){ i, i, i, 255 });
{
height += 1;
leftover -= 1;
}
DrawRectangle(0, y, screenWidth, height, (Color){ i, i, i, 255 });
y += height;
} }
EndShaderMode(); EndShaderMode();
DrawText("CURRENT PALETTE:", 10, 15, 20, RAYWHITE); DrawText("< >", 10, 10, 30, DARKBLUE);
DrawText(paletteText[currentPalette], 240, 15, 20, RED); DrawText("CURRENT PALETTE:", 60, 15, 20, RAYWHITE);
DrawText("< >", 540, 10, 30, DARKBLUE); DrawText(paletteText[currentPalette], 300, 15, 20, RED);
DrawFPS(700, 15); DrawFPS(700, 15);
EndDrawing(); EndDrawing();
@ -158,7 +138,7 @@ int main()
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
UnloadShader(shader); // Unload shader UnloadShader(shader); // Unload shader
CloseWindow(); // Close window and OpenGL context CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;