fragment shader experiment

This commit is contained in:
Kaehvaman 2024-12-26 17:18:09 +04:00
parent 6c3f50ca92
commit 5393ca2a5b
5 changed files with 131 additions and 10 deletions

Binary file not shown.

View File

@ -0,0 +1,40 @@
#version 330
// Input vertex attributes (from vertex shader)
in vec2 fragTexCoord;
in vec4 fragColor;
// Input uniform values
uniform sampler2D texture0;
uniform vec4 colDiffuse;
// Output fragment color
out vec4 finalColor;
// NOTE: Add here your custom variables
const vec2 size = vec2(800, 450); // Framebuffer size
const float samples = 5.0; // Pixels per axis; higher = bigger glow, worse performance
const float quality = 2.5; // Defines size factor: Lower = smaller glow, better quality
void main()
{
vec4 sum = vec4(0);
vec2 sizeFactor = vec2(1)/size*quality;
// Texel color fetching from texture sampler
vec4 source = texture(texture0, fragTexCoord);
const int range = 2; // should be = (samples - 1)/2;
for (int x = -range; x <= range; x++)
{
for (int y = -range; y <= range; y++)
{
sum += texture(texture0, fragTexCoord + vec2(x, y)*sizeFactor);
}
}
// Calculate final fragment color
finalColor = ((sum/(samples*samples)) + source)*colDiffuse;
}

View File

@ -0,0 +1,44 @@
#version 330
// Input vertex attributes (from vertex shader)
in vec2 fragTexCoord;
in vec4 fragColor;
// Input uniform values
uniform sampler2D texture0;
uniform vec4 colDiffuse;
// Output fragment color
out vec4 finalColor;
// NOTE: Add here your custom variables
// NOTE: values must be passed from code
uniform float renderWidth;
uniform float renderHeight;
uniform float seconds;
float offset[3] = float[](0.0, 1.3846153846, 3.2307692308);
float weight[3] = float[](0.2270270270, 0.3162162162, 0.0702702703);
void main()
{
// Texel color fetching from texture sampler
vec4 texelColor = texture(texture0, fragTexCoord).rgba*weight[0];
for (int i = 1; i < 3; i++)
{
texelColor += texture(texture0, fragTexCoord + vec2(offset[i], offset[i]) / renderWidth).rgba * weight[i] / 2;
texelColor += texture(texture0, fragTexCoord - vec2(offset[i], offset[i]) / renderWidth).rgba * weight[i] / 2;
texelColor += texture(texture0, fragTexCoord + vec2(-offset[i], offset[i]) / renderWidth).rgba * weight[i] / 2;
texelColor += texture(texture0, fragTexCoord - vec2(-offset[i], offset[i]) / renderWidth).rgba * weight[i] / 2;
}
vec4 red = vec4(1.0, 0.5, 0.5, 1.0);
vec4 blue = vec4(0.0, 0.5, 0.5, 1.0);
finalColor = mix(red, blue, (sin(fragTexCoord.x * 24 + seconds * 4.0) + 1.0) / 2.0);
//finalColor = texelColor;
//finalColor = vec4((sin(fragTexCoord.x * 24 + seconds * 4.0) + 1.0) / 2.0, 0.5, 0.5, 1.0);
}

View File

@ -284,7 +284,7 @@ void load() {
u8"Невозможно создать файл\nПроверьте целостность сохранения", u8"Невозможно создать файл\nПроверьте целостность сохранения",
u8"ok", u8"ok",
"error", 1); "error", 1);
errorCode = loadError1; //errorCode = loadError1;
return; return;
} }
int m, n; int m, n;
@ -501,9 +501,14 @@ void callNKErrorBoxes(struct nk_context* ctx) {
int main() int main()
{ {
//SetConfigFlags(FLAG_WINDOW_HIGHDPI); //SetConfigFlags(FLAG_WINDOW_HIGHDPI);
SetConfigFlags(FLAG_WINDOW_RESIZABLE); //SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(N * WIDTH, M * HEIGHT + VOFFSET, "lab16 with raylib"); const int screenWidth = N * WIDTH;
const int screenHeight = M * HEIGHT + VOFFSET;
const float screenWidthF = (float)screenWidth;
const float screenHeightF = (float)screenHeight;
InitWindow(screenWidth, screenHeight, "lab16 with raylib");
SetTargetFPS(60); SetTargetFPS(60);
@ -522,6 +527,15 @@ int main()
//Font Arial = LoadFontEx("arial.ttf", 36, codepoints, CPSIZE); //Font Arial = LoadFontEx("arial.ttf", 36, codepoints, CPSIZE);
//SetTextureFilter(Arial.texture, TEXTURE_FILTER_BILINEAR); //SetTextureFilter(Arial.texture, TEXTURE_FILTER_BILINEAR);
RenderTexture2D canvas = LoadRenderTexture(screenWidth, screenHeight);
Shader blur = LoadShader(0, "blur.frag");
int renderWidthLoc = GetShaderLocation(blur, "renderWidth");
int renderHeightLoc = GetShaderLocation(blur, "renderHeight");
int secondsLoc = GetShaderLocation(blur, "seconds");
SetShaderValue(blur, renderWidthLoc, &screenWidthF, SHADER_UNIFORM_FLOAT);
SetShaderValue(blur, renderHeightLoc, &screenHeightF, SHADER_UNIFORM_FLOAT);
GuiSetFont(InconsolataBold); GuiSetFont(InconsolataBold);
GuiSetStyle(DEFAULT, TEXT_SIZE, 24); GuiSetStyle(DEFAULT, TEXT_SIZE, 24);
GuiSetStyle(DEFAULT, TEXT_SPACING, 0); GuiSetStyle(DEFAULT, TEXT_SPACING, 0);
@ -542,8 +556,6 @@ int main()
// Update game logic // Update game logic
//------------------------------------------------------------------ //------------------------------------------------------------------
printf("lol");
if (errorCode == OK) if (errorCode == OK)
{ {
handleKeys(); handleKeys();
@ -578,7 +590,9 @@ int main()
//------------------------------------------------------------------ //------------------------------------------------------------------
BeginDrawing(); BeginDrawing();
// Setup the back buffer for drawing (clear color and depth buffers) // Setup the back buffer for drawing (clear color and depth buffers)
ClearBackground(WHITE); //ClearBackground(WHITE);
//BeginTextureMode(canvas);
drawMap(); drawMap();
drawPlayer(); drawPlayer();
@ -610,12 +624,32 @@ int main()
// Render the Nuklear GUI // Render the Nuklear GUI
//DrawNuklear(ctx); //DrawNuklear(ctx);
drawRayguiErrorBoxes(); //drawRayguiErrorBoxes();
} }
Rectangle roundRect = { 100, 250, 185, 36 }; BeginTextureMode(canvas);
DrawRectangleRounded(roundRect, 0.5f, 6, BLACK); {
DrawRectangleRoundedLines(roundRect, 0.5f, 6, ORANGE); ClearBackground((Color) { 255, 255, 255, 0 });
//ClearBackground(BLANK);
Rectangle roundRect = { 100, 260, 185, 36 };
DrawRectangleRounded(roundRect, 0.5f, 6, BLACK);
DrawRectangleRoundedLines(roundRect, 0.5f, 6, ORANGE);
}
EndTextureMode();
float timeF = (float)GetTime();
SetShaderValue(blur, secondsLoc, &timeF, SHADER_UNIFORM_FLOAT);
BeginShaderMode(blur);
{
Rectangle rec = { 0, 0, canvas.texture.width, -canvas.texture.height };
DrawTextureRec(canvas.texture, rec, (Vector2) { 0.0f, 0.0f }, WHITE);
}
EndShaderMode();
//drawBottomBar(InconsolataBold, 24);
//DrawTextureEx(canvas.texture, (Vector2) { 0.0f, 0.0f }, 0.0f, 1.0f, WHITE);
//DrawTextEx(Consolas, u8"Файл не найден\nПопробуйте сначала сохранить игру", (Vector2) { 100, 100 }, 24, 0, BLACK); //DrawTextEx(Consolas, u8"Файл не найден\nПопробуйте сначала сохранить игру", (Vector2) { 100, 100 }, 24, 0, BLACK);
@ -636,6 +670,9 @@ int main()
//UnloadFont(Arial); //UnloadFont(Arial);
//UnloadFont(InconsolataBold); //UnloadFont(InconsolataBold);
UnloadShader(blur);
UnloadRenderTexture(canvas);
// De-initialize the Nuklear GUI // De-initialize the Nuklear GUI
UnloadNuklear(ctx); UnloadNuklear(ctx);

BIN
raylib files/raylib43.lib Normal file

Binary file not shown.