feat: ball following mouse and blending with static polygon
This commit is contained in:
50
resources/shaders/basic.fs
Normal file
50
resources/shaders/basic.fs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#version 330
|
||||||
|
|
||||||
|
in vec2 fragTexCoord;
|
||||||
|
in vec4 fragColor;
|
||||||
|
|
||||||
|
uniform vec2 resolution;
|
||||||
|
uniform vec2 mouse_pos;
|
||||||
|
uniform float ball_radius;
|
||||||
|
uniform vec2 polygon_points[6];
|
||||||
|
uniform float polygon_influence;
|
||||||
|
|
||||||
|
out vec4 finalColor;
|
||||||
|
|
||||||
|
float distanceToPolygon(vec2 point, vec2 polyPoints[6]) {
|
||||||
|
float minDist = 100000.0;
|
||||||
|
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
int j = (i + 1) % 6;
|
||||||
|
vec2 a = polyPoints[i];
|
||||||
|
vec2 b = polyPoints[j];
|
||||||
|
|
||||||
|
vec2 pa = point - a;
|
||||||
|
vec2 ba = b - a;
|
||||||
|
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
|
||||||
|
float dist = length(pa - ba * h);
|
||||||
|
minDist = min(minDist, dist);
|
||||||
|
}
|
||||||
|
|
||||||
|
return minDist;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec2 pixel_pos = fragTexCoord * resolution;
|
||||||
|
|
||||||
|
vec2 ball_pixel_pos = mouse_pos;
|
||||||
|
float ball_dist = distance(pixel_pos, ball_pixel_pos);
|
||||||
|
float ball_value = (ball_radius * ball_radius) / (ball_dist * ball_dist + 1.0);
|
||||||
|
|
||||||
|
float poly_dist = distanceToPolygon(pixel_pos, polygon_points);
|
||||||
|
float poly_value = polygon_influence / (poly_dist * poly_dist + 1.0);
|
||||||
|
|
||||||
|
float total_value = ball_value + poly_value;
|
||||||
|
|
||||||
|
if (total_value > 0.7) {
|
||||||
|
finalColor = vec4(1.0, 1.0, 1.0, 1.0); // White
|
||||||
|
} else {
|
||||||
|
finalColor = vec4(0.0, 0.0, 0.0, 1.0); // Black
|
||||||
|
}
|
||||||
|
}
|
||||||
18
resources/shaders/basic.vs
Normal file
18
resources/shaders/basic.vs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#version 330
|
||||||
|
|
||||||
|
in vec3 vertexPosition;
|
||||||
|
in vec2 vertexTexCoord;
|
||||||
|
in vec4 vertexColor;
|
||||||
|
|
||||||
|
uniform mat4 mvp;
|
||||||
|
|
||||||
|
out vec2 fragTexCoord;
|
||||||
|
out vec4 fragColor;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
fragTexCoord = vertexTexCoord;
|
||||||
|
fragColor = vertexColor;
|
||||||
|
|
||||||
|
gl_Position = mvp * vec4(vertexPosition, 1.0);
|
||||||
|
}
|
||||||
75
src/main.c
75
src/main.c
@@ -1,21 +1,92 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
|
|
||||||
#define WIDTH 800
|
#define WIDTH 800
|
||||||
#define HEIGHT 600
|
#define HEIGHT 600
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
Vector2 points[6];
|
||||||
|
int numPoints;
|
||||||
|
float influence;
|
||||||
|
} Polygon;
|
||||||
|
|
||||||
|
Polygon poly = {
|
||||||
|
{{WIDTH / 2 + 50, HEIGHT / 2 - 50},
|
||||||
|
{WIDTH / 2 + 80, HEIGHT / 2},
|
||||||
|
{WIDTH / 2 + 50, HEIGHT / 2 + 50},
|
||||||
|
{WIDTH / 2 - 50, HEIGHT / 2 + 50},
|
||||||
|
{WIDTH / 2 - 80, HEIGHT / 2},
|
||||||
|
{WIDTH / 2 - 50, HEIGHT / 2 - 50}},
|
||||||
|
6,
|
||||||
|
3000.0f};
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
InitWindow(WIDTH, HEIGHT, "Wallpaper");
|
InitWindow(WIDTH, HEIGHT, "Wallpaper");
|
||||||
|
SetWindowState(FLAG_WINDOW_RESIZABLE);
|
||||||
|
SetTargetFPS(60);
|
||||||
|
|
||||||
|
// initialize shader
|
||||||
|
Shader shader = LoadShader("resources/shaders/basic.vs", "resources/shaders/basic.fs");
|
||||||
|
|
||||||
|
int resolutionLoc = GetShaderLocation(shader, "resolution");
|
||||||
|
int mousePosLoc = GetShaderLocation(shader, "mouse_pos");
|
||||||
|
int ballRadiusLoc = GetShaderLocation(shader, "ball_radius");
|
||||||
|
int polygonPointsLoc = GetShaderLocation(shader, "polygon_points");
|
||||||
|
int polygonInfluenceLoc = GetShaderLocation(shader, "polygon_influence");
|
||||||
|
|
||||||
|
Vector2 resolution = {WIDTH, HEIGHT};
|
||||||
|
SetShaderValue(shader, resolutionLoc, &resolution, SHADER_UNIFORM_VEC2);
|
||||||
|
|
||||||
|
// send ball data
|
||||||
|
float ballRadius = 40.0f;
|
||||||
|
SetShaderValue(shader, ballRadiusLoc, &ballRadius, SHADER_UNIFORM_FLOAT);
|
||||||
|
|
||||||
|
// send polygon data
|
||||||
|
float polygonInfluence = poly.influence;
|
||||||
|
SetShaderValue(shader, polygonInfluenceLoc, &polygonInfluence, SHADER_UNIFORM_FLOAT);
|
||||||
|
|
||||||
|
float polygonPoints[12];
|
||||||
|
for (int i = 0; i < poly.numPoints; i++)
|
||||||
|
{
|
||||||
|
polygonPoints[i * 2] = poly.points[i].x;
|
||||||
|
polygonPoints[i * 2 + 1] = poly.points[i].y;
|
||||||
|
}
|
||||||
|
SetShaderValueV(shader, polygonPointsLoc, polygonPoints, SHADER_UNIFORM_VEC2, poly.numPoints);
|
||||||
|
|
||||||
|
// initializee render texture
|
||||||
|
RenderTexture2D target = LoadRenderTexture(WIDTH, HEIGHT);
|
||||||
|
|
||||||
while (!WindowShouldClose())
|
while (!WindowShouldClose())
|
||||||
{
|
{
|
||||||
|
int screenWidth = GetScreenWidth();
|
||||||
|
int screenHeight = GetScreenHeight();
|
||||||
|
|
||||||
|
if (IsWindowResized())
|
||||||
|
{
|
||||||
|
UnloadRenderTexture(target);
|
||||||
|
target = LoadRenderTexture(screenWidth, screenHeight);
|
||||||
|
|
||||||
|
Vector2 resolution = {screenWidth, screenHeight};
|
||||||
|
SetShaderValue(shader, resolutionLoc, &resolution, SHADER_UNIFORM_VEC2);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 mousePos = GetMousePosition();
|
||||||
|
mousePos.y = screenHeight - mousePos.y;
|
||||||
|
SetShaderValue(shader, mousePosLoc, &mousePos, SHADER_UNIFORM_VEC2);
|
||||||
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
|
|
||||||
ClearBackground(RED);
|
BeginShaderMode(shader);
|
||||||
|
DrawTextureRec(target.texture, (Rectangle){0, 0, screenWidth, -screenHeight}, (Vector2){0, 0}, WHITE);
|
||||||
|
EndShaderMode();
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UnloadRenderTexture(target);
|
||||||
|
UnloadShader(shader);
|
||||||
|
CloseWindow();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user