diff --git a/inc/canvas/BackGround.hpp b/inc/canvas/BackGround.hpp index e835f81..de3062c 100644 --- a/inc/canvas/BackGround.hpp +++ b/inc/canvas/BackGround.hpp @@ -1,6 +1,10 @@ #include -#include + #include "values/RandBuffer.hpp" +#include "canvas/Sun.hpp" + +#include + struct Moon { @@ -16,14 +20,17 @@ public: BackGround() = default; ~BackGround() = default; void init(int size); + void deinit(); void newGen(); void draw(); private: void drawStarts(); - void drawMoon(); + void drawSun(); void drawMounten(size_t mountenSegments, int min, int max, Color color); + Sun sun; + RandBuffer starBuff; RandBuffer mountenBuff; Moon m_moon; @@ -34,8 +41,8 @@ private: constexpr static size_t numOfStarts = 150; constexpr static float moonXOffset = 0.1f; - constexpr static float minSizeOfMoon = 0.05f; - constexpr static float maxSizeOfMoon = 0.075f; + constexpr static float minSizeOfMoon = 0.075f; + constexpr static float maxSizeOfMoon = 0.1f; constexpr static float maxYPosOfMoon = 0.80f; constexpr static float bigRingRatio = 0.5f; constexpr static float smallRingRatio = 0.25f; diff --git a/inc/canvas/Canvas.hpp b/inc/canvas/Canvas.hpp index ff0191f..b95e95a 100644 --- a/inc/canvas/Canvas.hpp +++ b/inc/canvas/Canvas.hpp @@ -9,8 +9,9 @@ public: Canvas() = default; ~Canvas() = default; void init(int size); + void deinit(); void newGen(RenderTexture2D& target); - + private: BackGround backGround; Tree tree; diff --git a/inc/canvas/Sun.hpp b/inc/canvas/Sun.hpp new file mode 100644 index 0000000..e1bb26b --- /dev/null +++ b/inc/canvas/Sun.hpp @@ -0,0 +1,25 @@ +#include + +class Sun +{ +public: + Sun() = default; + ~Sun() = default; + void init(); + void deinit(); + void draw(float x, float y, float size); +private: + const int sizeTexute = 100; + RenderTexture2D target = { 0 }; + Shader shader = { 0 }; + + float sun_radius = 0.60f; + float start_transperency = 0.40f; + float c[3] = { 240.0f / 255.0f, 240.0f / 255.0f, 190.0f / 255.0f }; + + int sun_radius_loc = 0; + int start_transperency_loc = 0; + int colorLoc = 0; +}; + + diff --git a/shaders/sun.fs b/shaders/sun.fs new file mode 100644 index 0000000..ba3b306 --- /dev/null +++ b/shaders/sun.fs @@ -0,0 +1,28 @@ +#version 330 + +in vec2 fragTexCoord; +in vec4 fragColor; +out vec4 finalColor; + +uniform vec3 color; +uniform float sun_radius; +uniform float start_transperency; +vec2 offset = vec2(1.0f, 1.0f); +float sun_end = 1.0f; + +void main() +{ + offset.x -= fragTexCoord.x * 2; + offset.y -= fragTexCoord.y * 2; + float radius = length(offset); + if(radius < sun_radius){ + finalColor = vec4(color, 1.0f); + }else if(radius < sun_end){ + float gradient = radius; + gradient -= sun_radius; + gradient = gradient / (sun_end - sun_radius) * start_transperency; + finalColor = vec4(color, start_transperency - gradient); + }else{ + finalColor = vec4(color, 0.0f); + } +} diff --git a/src/App.cpp b/src/App.cpp index 4cb96a7..5afa9f0 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -21,7 +21,7 @@ void App::init(int screenWidth, int screenHeight) canvas.newGen(canvasTexure[0]); canvas.newGen(canvasTexure[1]); - float posY = (screenHeight - screenWidth) / 2; + float posY = (screenHeight - screenWidth) / 2.0f; destA = {0, posY, (float)screenWidth, (float)screenWidth}; destB = destA; } @@ -69,4 +69,5 @@ void App::deinit() { UnloadRenderTexture(canvasTexure[i]); } + canvas.deinit(); } \ No newline at end of file diff --git a/src/canvas/BackGround.cpp b/src/canvas/BackGround.cpp index 60ac83d..21b5dc7 100644 --- a/src/canvas/BackGround.cpp +++ b/src/canvas/BackGround.cpp @@ -10,6 +10,12 @@ void BackGround::init(int size) { this->size = size; + sun.init(); +} + +void BackGround::deinit() +{ + sun.deinit(); } void BackGround::newGen() @@ -36,8 +42,8 @@ void BackGround::draw() { DrawRectangleGradientV(0, 0, size, size, ColorAdd(BackGroundColors::backGroundColor, 60), BackGroundColors::backGroundColor); } - - drawMoon(); + + drawSun(); drawMounten(20, (int)(mounten1min * size), (int)(mounten1max * size), BackGroundColors::MountenColor1); drawMounten(21, (int)(mounten2min * size), (int)(mounten2max * size), BackGroundColors::MountenColor2); drawMounten(23, (int)(mounten3min * size), (int)(mounten3max * size), BackGroundColors::MountenColor3); @@ -83,18 +89,13 @@ void BackGround::drawStarts() rlSetTexture(0); } -void BackGround::drawMoon() +void BackGround::drawSun() { int r = ((m_moon.size * (maxSizeOfMoon - minSizeOfMoon)) + minSizeOfMoon) * size; int xpos = Lerp(size * moonXOffset, size - size * moonXOffset, m_moon.x); int ypos = Lerp(size * moonXOffset, maxYPosOfMoon * size, m_moon.y); - int ring1 = (float)r * bigRingRatio; - int ring2 = (float)r * smallRingRatio; - - DrawCircle(xpos, ypos, r + ring1, ColorLerp(BackGroundColors::backGroundColor, BackGroundColors::moonColor, bigRingBlend)); - DrawCircle(xpos, ypos, r + ring2, ColorLerp(BackGroundColors::backGroundColor, BackGroundColors::moonColor, smallRingBlend)); - DrawCircle(xpos, ypos, r, BackGroundColors::moonColor); + sun.draw(xpos, ypos, r); } void BackGround::drawMounten(size_t mountenSegments, int min, int max, Color color) diff --git a/src/canvas/Canvas.cpp b/src/canvas/Canvas.cpp index c70041a..6c00f3d 100644 --- a/src/canvas/Canvas.cpp +++ b/src/canvas/Canvas.cpp @@ -16,3 +16,8 @@ void Canvas::newGen(RenderTexture2D& target) EndTextureMode(); } + +void Canvas::deinit() +{ + backGround.deinit(); +} diff --git a/src/canvas/Sun.cpp b/src/canvas/Sun.cpp new file mode 100644 index 0000000..91f0455 --- /dev/null +++ b/src/canvas/Sun.cpp @@ -0,0 +1,70 @@ +#include "canvas/Sun.hpp" + +#include "Math.hpp" + +#include +#include + +const char* sun_shader = "#version 330\n\ +in vec2 fragTexCoord;\ +in vec4 fragColor;\ +out vec4 finalColor;\ +uniform vec3 color;\ +uniform float sun_radius;\ +uniform float start_transperency;\ +vec2 offset = vec2(1.0f, 1.0f);\ +float sun_end = 1.0f;\ +void main()\ +{\ + offset.x -= fragTexCoord.x * 2;\ + offset.y -= fragTexCoord.y * 2;\ + float radius = length(offset);\ + if (radius < sun_radius) {\ + finalColor = vec4(color, 1.0f);\ + }\ + else if (radius < sun_end) {\ + float gradient = radius;\ + gradient -= sun_radius;\ + gradient = gradient / (sun_end - sun_radius) * start_transperency;\ + finalColor = vec4(color, start_transperency - gradient);\ + }\ + else {\ + finalColor = vec4(color, 0.0f);\ + }\ +}"; + +void Sun::init() +{ + shader = LoadShaderFromMemory(0, sun_shader); + target = LoadRenderTexture(sizeTexute, sizeTexute); + + sun_radius_loc = GetShaderLocation(shader, "sun_radius"); + SetShaderValue(shader, sun_radius_loc, &sun_radius, SHADER_UNIFORM_FLOAT); + + start_transperency_loc = GetShaderLocation(shader, "start_transperency"); + SetShaderValue(shader, start_transperency_loc, &start_transperency, SHADER_UNIFORM_FLOAT); + + 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 + // 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); +} + +void Sun::deinit() +{ + UnloadShader(shader); + UnloadRenderTexture(target); +} + +void Sun::draw(float x, float y, float size) +{ + 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(); + EndBlendMode(); +}