Hot reloading and somewhat working but SLOW shader

This commit is contained in:
Casey 2022-10-14 08:49:58 +03:00
parent e6f1a22fe1
commit 56fbc89983
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
2 changed files with 70 additions and 18 deletions

View File

@ -16,12 +16,42 @@ void main()
{ {
vec2 screenSize = fontSize * charactersXY; vec2 screenSize = fontSize * charactersXY;
vec2 pixelCoord = floor(fragTexCoord * screenSize); vec2 pixelCoord = floor(fragTexCoord * screenSize);
vec2 symbol = floor(pixelCoord / fontSize) / screenSize; vec2 symbol = floor(pixelCoord / fontSize);
vec2 inSymbol = mod(pixelCoord, fontSize) / fontSize; vec2 inSymbol = mod(pixelCoord, fontSize) / fontSize / vec2(16., 8.);
vec4 texelColor = vec4(0.0, 0.0, 0.0, 1.0); vec4 texelColor = vec4(0.0, 0.0, 0.0, 1.0);
texelColor.rgb = texture(texture0, symbol * fontSize).rgb; //texelColor.rgb = texture(texture0, symbol * fontSize).rgb;
//texelColor.rg = texture(fontAtlas, symbol + inSymbol / vec2(16., 8.)).rg;
//texelColor.rg = symbol + inSymbol / vec2(64., 32.);
vec3 smallest_diff = vec3(99999999999.), diff = vec3(0.);
ivec2 probeSym = ivec2(0), closestSymbol = ivec2(5, -3);
for (probeSym.y = 0; probeSym.y < 8; probeSym.y++) {
for (probeSym.x = 0; probeSym.x < 16; probeSym.x++) {
vec2 charPosition = probeSym / vec2(16., 8.), inCharPosition = vec2(0.);
diff = vec3(0.);
for (inCharPosition.y = 0.; inCharPosition.y < 1.0; inCharPosition.y += 1. / fontSize.y) {
for (inCharPosition.x = 0.; inCharPosition.x < 1.0; inCharPosition.x += 1. / fontSize.x) {
vec3 charRGB = texture(fontAtlas, (inCharPosition + probeSym) / vec2(16., 8.)).rgb,
srcRGB = texture(texture0, pixelCoord / screenSize).rgb;
diff = diff + abs(srcRGB - charRGB);
}
}
if (length(diff) < length(smallest_diff)) {
smallest_diff = diff;
closestSymbol = probeSym;
}
}
}
texelColor.rg = texture(fontAtlas, inSymbol + closestSymbol / vec2(16., 8.)).rg;
#if 0
if (fragTexCoord.x >= 0.5)
finalColor = texture(texture0, fragTexCoord);
else
finalColor = texelColor; finalColor = texelColor;
#else
finalColor = texelColor;
#endif
} }

View File

@ -32,9 +32,13 @@ int main(int argc, char **argv) {
InitWindow(320, 240, "Loading..."); InitWindow(320, 240, "Loading...");
SetTargetFPS(60); SetTargetFPS(60);
char *font_path = "unscii-16-full.ttf",
*shader_path = "./src/asciify.frag";
if (argc >= 2) font_path = argv[1];
int font_size = 16, chars_x = 160, chars_y = 50; int font_size = 16, chars_x = 160, chars_y = 50;
Font font = LoadFontEx("unscii-16-full.ttf", font_size, NULL, 256); Font font = LoadFontEx(font_path, font_size, NULL, 256);
Vector2 glyphSize = MeasureTextEx(font, "A", font_size, 0); Vector2 glyphSize = MeasureTextEx(font, "A", font_size, 0);
Image img_atlas = GenImageColor(glyphSize.x * 16, glyphSize.y * 8, BLACK); Image img_atlas = GenImageColor(glyphSize.x * 16, glyphSize.y * 8, BLACK);
@ -51,6 +55,8 @@ int main(int argc, char **argv) {
}, WHITE); }, WHITE);
} }
ImageFlipVertical(&img_atlas);
Texture2D tex_atlas = LoadTextureFromImage(img_atlas); Texture2D tex_atlas = LoadTextureFromImage(img_atlas);
int wsize_x = chars_x * glyphSize.x, wsize_y = chars_y * glyphSize.y; int wsize_x = chars_x * glyphSize.x, wsize_y = chars_y * glyphSize.y;
SetWindowMinSize(glyphSize.x * 40, glyphSize.y * 12); SetWindowMinSize(glyphSize.x * 40, glyphSize.y * 12);
@ -71,18 +77,8 @@ int main(int argc, char **argv) {
.position.z = 50.0, .position.z = 50.0,
}; };
Shader shd_asciify = LoadShader(NULL, "./src/asciify.frag"); Shader shd_asciify;
int locAtlas = GetShaderLocation(shd_asciify, "fontAtlas"); long shaderLastModified = 0, shaderModified = -1;
{
float tmp[2];
tmp[0] = glyphSize.x; tmp[1] = glyphSize.y;
printf("glyph size: %fx%f\n", tmp[0], tmp[1]);
SetShaderValue(shd_asciify, GetShaderLocation(shd_asciify, "fontSize"), tmp, SHADER_UNIFORM_VEC2);
tmp[0] = chars_x; tmp[1] = chars_y;
SetShaderValue(shd_asciify, GetShaderLocation(shd_asciify, "charactersXY"), tmp, SHADER_UNIFORM_VEC2);
}
SetCameraMode(cam, CAMERA_FREE); SetCameraMode(cam, CAMERA_FREE);
@ -102,6 +98,27 @@ int main(int argc, char **argv) {
SetShaderValue(shd_asciify, GetShaderLocation(shd_asciify, "charactersXY"), tmp, SHADER_UNIFORM_VEC2); SetShaderValue(shd_asciify, GetShaderLocation(shd_asciify, "charactersXY"), tmp, SHADER_UNIFORM_VEC2);
} }
if ((frame % 10) == 0) {
shaderModified = GetFileModTime(shader_path);
}
if (shaderModified != shaderLastModified)
{
printf("Recompiling...\n");
shd_asciify = LoadShader(NULL, shader_path);
float tmp[2];
tmp[0] = glyphSize.x; tmp[1] = glyphSize.y;
SetShaderValue(shd_asciify, GetShaderLocation(shd_asciify, "fontSize"), tmp, SHADER_UNIFORM_VEC2);
tmp[0] = chars_x; tmp[1] = chars_y;
SetShaderValue(shd_asciify, GetShaderLocation(shd_asciify, "charactersXY"), tmp, SHADER_UNIFORM_VEC2);
shaderLastModified = shaderModified;
}
if (IsKeyDown(KEY_LEFT_SHIFT) && IsKeyPressed(KEY_F)) { if (IsKeyDown(KEY_LEFT_SHIFT) && IsKeyPressed(KEY_F)) {
SetWindowSize(wsize_x, wsize_y); SetWindowSize(wsize_x, wsize_y);
} }
@ -137,8 +154,13 @@ int main(int argc, char **argv) {
ClearBackground(RED); ClearBackground(RED);
DrawRectangle(0, 0, wsize_x, wsize_y, BLACK); DrawRectangle(0, 0, wsize_x, wsize_y, BLACK);
if (shading) BeginShaderMode(shd_asciify);
if (shading) {
BeginShaderMode(shd_asciify);
int locAtlas = GetShaderLocation(shd_asciify, "fontAtlas");
SetShaderValueTexture(shd_asciify, locAtlas, tex_atlas); SetShaderValueTexture(shd_asciify, locAtlas, tex_atlas);
}
DrawTexturePro(framebuffer.texture, (Rectangle){ DrawTexturePro(framebuffer.texture, (Rectangle){
0, 0, wsize_x, -wsize_y 0, 0, wsize_x, -wsize_y
}, (Rectangle){ }, (Rectangle){