diff --git a/Readme.md b/Readme.md index 23e193f..0cd043a 100644 --- a/Readme.md +++ b/Readme.md @@ -30,4 +30,11 @@ or ## Include order - Std - local ( form inc dir ) - - external raylib \ No newline at end of file + - external raylib + + +## Generate shader .h files +``` +xxd -i shaders/sun_100.fs > inc/sunShader.hpp +xxd -i shaders/sun_330.fs >> inc/sunShader.hpp +``` \ No newline at end of file diff --git a/inc/sunShader.hpp b/inc/sunShader.hpp new file mode 100644 index 0000000..df314bc --- /dev/null +++ b/inc/sunShader.hpp @@ -0,0 +1,124 @@ +unsigned char shaders_sun_100_fs[] = { + 0x23, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x31, 0x30, 0x30, + 0x0a, 0x0a, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x69, 0x6f, 0x6e, 0x20, + 0x6d, 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x66, 0x6c, 0x6f, 0x61, + 0x74, 0x3b, 0x0a, 0x0a, 0x76, 0x61, 0x72, 0x79, 0x69, 0x6e, 0x67, 0x20, + 0x76, 0x65, 0x63, 0x32, 0x20, 0x66, 0x72, 0x61, 0x67, 0x54, 0x65, 0x78, + 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x3b, 0x0a, 0x76, 0x61, 0x72, 0x79, 0x69, + 0x6e, 0x67, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x66, 0x72, 0x61, 0x67, + 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0a, 0x0a, 0x0a, 0x75, 0x6e, 0x69, + 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x76, 0x65, 0x63, 0x33, 0x20, 0x63, 0x6f, + 0x6c, 0x6f, 0x72, 0x3b, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, + 0x20, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x73, 0x75, 0x6e, 0x5f, 0x72, + 0x61, 0x64, 0x69, 0x75, 0x73, 0x3b, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, + 0x72, 0x6d, 0x20, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x73, 0x74, 0x61, + 0x72, 0x74, 0x5f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x65, 0x72, 0x65, + 0x6e, 0x63, 0x79, 0x3b, 0x0a, 0x0a, 0x76, 0x65, 0x63, 0x32, 0x20, 0x6f, + 0x66, 0x66, 0x73, 0x65, 0x74, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x32, + 0x28, 0x31, 0x2e, 0x30, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x29, 0x3b, 0x0a, + 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x73, 0x75, 0x6e, 0x5f, 0x65, 0x6e, + 0x64, 0x20, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x3b, 0x0a, 0x0a, 0x76, 0x6f, + 0x69, 0x64, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x29, 0x0a, 0x7b, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x2e, 0x78, + 0x20, 0x2d, 0x3d, 0x20, 0x66, 0x72, 0x61, 0x67, 0x54, 0x65, 0x78, 0x43, + 0x6f, 0x6f, 0x72, 0x64, 0x2e, 0x78, 0x20, 0x2a, 0x20, 0x32, 0x2e, 0x30, + 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, + 0x2e, 0x79, 0x20, 0x2d, 0x3d, 0x20, 0x66, 0x72, 0x61, 0x67, 0x54, 0x65, + 0x78, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x2e, 0x79, 0x20, 0x2a, 0x20, 0x32, + 0x2e, 0x30, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x66, 0x6c, 0x6f, 0x61, + 0x74, 0x20, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x20, 0x3d, 0x20, 0x6c, + 0x65, 0x6e, 0x67, 0x74, 0x68, 0x28, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, + 0x29, 0x3b, 0x0a, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x28, 0x72, + 0x61, 0x64, 0x69, 0x75, 0x73, 0x20, 0x3c, 0x20, 0x73, 0x75, 0x6e, 0x5f, + 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x29, 0x7b, 0x0a, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x67, 0x6c, 0x5f, 0x46, 0x72, 0x61, + 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, + 0x34, 0x28, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, 0x31, 0x2e, 0x30, + 0x29, 0x3b, 0x0a, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x65, 0x6c, 0x73, + 0x65, 0x20, 0x69, 0x66, 0x28, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x20, + 0x3c, 0x20, 0x73, 0x75, 0x6e, 0x5f, 0x65, 0x6e, 0x64, 0x29, 0x7b, 0x0a, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x6c, 0x6f, + 0x61, 0x74, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, 0x65, 0x6e, 0x74, 0x20, + 0x3d, 0x20, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x3b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, 0x65, + 0x6e, 0x74, 0x20, 0x2d, 0x3d, 0x20, 0x73, 0x75, 0x6e, 0x5f, 0x72, 0x61, + 0x64, 0x69, 0x75, 0x73, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, 0x65, 0x6e, 0x74, 0x20, 0x3d, + 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, 0x65, 0x6e, 0x74, 0x20, 0x2f, 0x20, + 0x28, 0x73, 0x75, 0x6e, 0x5f, 0x65, 0x6e, 0x64, 0x20, 0x2d, 0x20, 0x73, + 0x75, 0x6e, 0x5f, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x29, 0x20, 0x2a, + 0x20, 0x73, 0x74, 0x61, 0x72, 0x74, 0x5f, 0x74, 0x72, 0x61, 0x6e, 0x73, + 0x70, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x67, 0x6c, 0x5f, 0x46, 0x72, 0x61, 0x67, + 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x34, + 0x28, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, 0x73, 0x74, 0x61, 0x72, + 0x74, 0x5f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x65, 0x72, 0x65, 0x6e, + 0x63, 0x79, 0x20, 0x2d, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, 0x65, 0x6e, + 0x74, 0x29, 0x3b, 0x0a, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x65, 0x6c, + 0x73, 0x65, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x67, 0x6c, 0x5f, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, + 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x34, 0x28, 0x63, 0x6f, 0x6c, 0x6f, + 0x72, 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x7d, 0x0a, 0x7d}; +unsigned int shaders_sun_100_fs_len = 724; +unsigned char shaders_sun_330_fs[] = { + 0x23, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x33, 0x33, 0x30, + 0x0a, 0x0a, 0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x66, 0x72, + 0x61, 0x67, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x3b, 0x0a, + 0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x66, 0x72, 0x61, 0x67, + 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0a, 0x6f, 0x75, 0x74, 0x20, 0x76, + 0x65, 0x63, 0x34, 0x20, 0x66, 0x69, 0x6e, 0x61, 0x6c, 0x43, 0x6f, 0x6c, + 0x6f, 0x72, 0x3b, 0x0a, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, + 0x20, 0x76, 0x65, 0x63, 0x33, 0x20, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, + 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x66, 0x6c, 0x6f, + 0x61, 0x74, 0x20, 0x73, 0x75, 0x6e, 0x5f, 0x72, 0x61, 0x64, 0x69, 0x75, + 0x73, 0x3b, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x66, + 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x73, 0x74, 0x61, 0x72, 0x74, 0x5f, 0x74, + 0x72, 0x61, 0x6e, 0x73, 0x70, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x3b, + 0x0a, 0x76, 0x65, 0x63, 0x32, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, + 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x32, 0x28, 0x31, 0x2e, 0x30, 0x66, + 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x66, 0x6c, 0x6f, + 0x61, 0x74, 0x20, 0x73, 0x75, 0x6e, 0x5f, 0x65, 0x6e, 0x64, 0x20, 0x3d, + 0x20, 0x31, 0x2e, 0x30, 0x66, 0x3b, 0x0a, 0x0a, 0x76, 0x6f, 0x69, 0x64, + 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x29, 0x0a, 0x7b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x2e, 0x78, 0x20, 0x2d, + 0x3d, 0x20, 0x66, 0x72, 0x61, 0x67, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f, + 0x72, 0x64, 0x2e, 0x78, 0x20, 0x2a, 0x20, 0x32, 0x3b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x2e, 0x79, 0x20, 0x2d, + 0x3d, 0x20, 0x66, 0x72, 0x61, 0x67, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f, + 0x72, 0x64, 0x2e, 0x79, 0x20, 0x2a, 0x20, 0x32, 0x3b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x72, 0x61, 0x64, 0x69, + 0x75, 0x73, 0x20, 0x3d, 0x20, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x28, + 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x69, 0x66, 0x28, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x20, 0x3c, + 0x20, 0x73, 0x75, 0x6e, 0x5f, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x29, + 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x69, + 0x6e, 0x61, 0x6c, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x76, + 0x65, 0x63, 0x34, 0x28, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, 0x31, + 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x65, + 0x6c, 0x73, 0x65, 0x20, 0x69, 0x66, 0x28, 0x72, 0x61, 0x64, 0x69, 0x75, + 0x73, 0x20, 0x3c, 0x20, 0x73, 0x75, 0x6e, 0x5f, 0x65, 0x6e, 0x64, 0x29, + 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x6c, + 0x6f, 0x61, 0x74, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, 0x65, 0x6e, 0x74, + 0x20, 0x3d, 0x20, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x3b, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, + 0x65, 0x6e, 0x74, 0x20, 0x2d, 0x3d, 0x20, 0x73, 0x75, 0x6e, 0x5f, 0x72, + 0x61, 0x64, 0x69, 0x75, 0x73, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, 0x65, 0x6e, 0x74, 0x20, + 0x3d, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, 0x65, 0x6e, 0x74, 0x20, 0x2f, + 0x20, 0x28, 0x73, 0x75, 0x6e, 0x5f, 0x65, 0x6e, 0x64, 0x20, 0x2d, 0x20, + 0x73, 0x75, 0x6e, 0x5f, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x29, 0x20, + 0x2a, 0x20, 0x73, 0x74, 0x61, 0x72, 0x74, 0x5f, 0x74, 0x72, 0x61, 0x6e, + 0x73, 0x70, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x3b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x69, 0x6e, 0x61, 0x6c, 0x43, + 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x34, 0x28, + 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, 0x73, 0x74, 0x61, 0x72, 0x74, + 0x5f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x65, 0x72, 0x65, 0x6e, 0x63, + 0x79, 0x20, 0x2d, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, 0x65, 0x6e, 0x74, + 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x65, 0x6c, 0x73, 0x65, + 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x69, + 0x6e, 0x61, 0x6c, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x76, + 0x65, 0x63, 0x34, 0x28, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, 0x30, + 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, + 0x7d, 0x0a}; +unsigned int shaders_sun_330_fs_len = 698; diff --git a/src/canvas/BackGround.cpp b/src/canvas/BackGround.cpp index 0eae445..709a4d7 100644 --- a/src/canvas/BackGround.cpp +++ b/src/canvas/BackGround.cpp @@ -44,7 +44,7 @@ void BackGround::draw() { DrawRectangleGradientV(0, 0, size, size, ColorAdd(BackGroundColors::backGroundColor, 60), BackGroundColors::backGroundColor); } - + drawSun(); drawMounten(20, (int)(mounten1min * size), (int)(mounten1max * size), BackGroundColors::MountenColor1); drawMounten(21, (int)(mounten2min * size), (int)(mounten2max * size), BackGroundColors::MountenColor2); @@ -97,11 +97,13 @@ void BackGround::drawSun() int xpos = Lerp(size * moonXOffset, size - size * moonXOffset, m_moon.x); int ypos = Lerp(size * moonXOffset, maxYPosOfMoon * size, m_moon.y); - if (colorSet == 3) { + if (colorSet == 3) + { sun.setColor(BackGroundColors::moonColor); r = ((m_moon.size * (maxSizeOfMoon - minSizeOfMoon)) + minSizeOfMoon) * size; } - else { + else + { Color color = {0}; color.r = 255; color.g = std::lerp(200, 50, m_moon.y); diff --git a/src/canvas/Sun.cpp b/src/canvas/Sun.cpp index 0e0ef38..b299168 100644 --- a/src/canvas/Sun.cpp +++ b/src/canvas/Sun.cpp @@ -1,41 +1,18 @@ #include "canvas/Sun.hpp" +#include "sunShader.hpp" #include "Math.hpp" #include #include -const char* sun_shader_100 = "#version 100\n\ -precision mediump float;\ -varying vec2 fragTexCoord;\ -varying vec4 fragColor;\ -uniform vec3 color;\ -uniform float sun_radius;\ -uniform float start_transperency;\ -vec2 offset = vec2(1.0, 1.0);\ -float sun_end = 1.0;\ -void main()\ -{\ - offset.x -= fragTexCoord.x * 2.0;\ - offset.y -= fragTexCoord.y * 2.0;\ - float radius = length(offset);\ - if (radius < sun_radius) {\ - gl_FragColor = vec4(color, 1.0);\ - }\ - else if (radius < sun_end) {\ - float gradient = radius;\ - gradient -= sun_radius;\ - gradient = gradient / (sun_end - sun_radius) * start_transperency;\ - gl_FragColor = vec4(color, start_transperency - gradient);\ - }\ - else {\ - gl_FragColor = vec4(color, 0.0);\ - }\ -}"; - void Sun::init() { - shader = LoadShaderFromMemory(0, sun_shader_100); +#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB) + shader = LoadShaderFromMemory(0, (const char *)shaders_sun_100_fs); +#else + shader = LoadShaderFromMemory(0, (const char *)shaders_sun_330_fs); +#endif target = LoadRenderTexture(sizeTexute, sizeTexute); sun_radius_loc = GetShaderLocation(shader, "sun_radius"); @@ -47,7 +24,7 @@ void Sun::init() colorLoc = GetShaderLocation(shader, "color"); SetShaderValue(shader, colorLoc, c, SHADER_UNIFORM_VEC3); - // resitev da ne postane tekstura okoli sonca transparentna in da se ne vidi nasledna slika + // resitev da ne postane tekstura okoli sonca transparentna in da se ne vidi nasledna slika // https://github.com/raysan5/raylib/issues/3820 rlSetBlendFactorsSeparate(RL_SRC_ALPHA, RL_ONE_MINUS_SRC_ALPHA, RL_ONE, RL_ONE, RL_FUNC_ADD, RL_MAX); } @@ -69,11 +46,11 @@ void Sun::setColor(Color color) void Sun::draw(float x, float y, float size) { - Rectangle dest = { x-size, y - size, size * 2, size * 2}; + Rectangle dest = {x - size, y - size, size * 2, size * 2}; // zgorni komentar da se mesanje barv oklopi pravilno BeginBlendMode(RL_BLEND_CUSTOM_SEPARATE); - BeginShaderMode(shader); - drawTexureWithRotation(target, dest, 0.0f); - EndShaderMode(); + BeginShaderMode(shader); + drawTexureWithRotation(target, dest, 0.0f); + EndShaderMode(); EndBlendMode(); }