Fix sun making everything transperent

and sun has beautiful gradient
This commit is contained in:
Nikola Petrov 2024-08-13 01:08:23 +02:00
parent d7afa1afc9
commit 53444955ad
8 changed files with 153 additions and 15 deletions

View File

@ -1,6 +1,10 @@
#include <vector>
#include <raylib.h>
#include "values/RandBuffer.hpp"
#include "canvas/Sun.hpp"
#include <raylib.h>
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;

View File

@ -9,8 +9,9 @@ public:
Canvas() = default;
~Canvas() = default;
void init(int size);
void deinit();
void newGen(RenderTexture2D& target);
private:
BackGround backGround;
Tree tree;

25
inc/canvas/Sun.hpp Normal file
View File

@ -0,0 +1,25 @@
#include <raylib.h>
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;
};

28
shaders/sun.fs Normal file
View File

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

View File

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

View File

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

View File

@ -16,3 +16,8 @@ void Canvas::newGen(RenderTexture2D& target)
EndTextureMode();
}
void Canvas::deinit()
{
backGround.deinit();
}

70
src/canvas/Sun.cpp Normal file
View File

@ -0,0 +1,70 @@
#include "canvas/Sun.hpp"
#include "Math.hpp"
#include <raylib.h>
#include <rlgl.h>
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();
}