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
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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;