Skip to content

Commit

Permalink
Minor refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
arceryz committed Jun 12, 2024
1 parent f67cf47 commit 1115c68
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 74 deletions.
57 changes: 28 additions & 29 deletions Shaders/ray2.comp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ layout(location=5) uniform vec2 rayOrigin;

const float PI = 3.14159;
const float STEP_MIN = 0.001f;
const float ARC_FOCUS_INF = 1000.0f;
const float FOCUS_INF = 1000.0f;

struct HitInfo {
float t;
vec2 point;
vec2 normal;
};
HitInfo RayLineHit(vec2 o, vec2 d, vec2 u, vec2 v);
HitInfo RayCircleHit(vec2 o, vec2 d, vec2 cc, float rsq, bool convex);
HitInfo RayCircleHit(vec2 o, vec2 d, vec2 cc, float rsq, bool interior);
float DistanceSq(vec2 a, vec2 b);

void main()
Expand All @@ -42,8 +42,10 @@ void main()

// Ray originate from (0, 0).
vec2 ray;
ray.x = cos(2*PI*prog);
ray.y = sin(2*PI*prog);
//float rayAngle = 0.5*PI + 2*PI/10.0*prog;
float rayAngle = 2*PI*prog;
ray.x = cos(rayAngle);
ray.y = sin(rayAngle);

vec2 origin = rayOrigin;
//origin = vec2(cos(time*600), sin(time*600))*0.05;
Expand All @@ -59,13 +61,13 @@ void main()

// Evaluate every mirror plane.
for (int j = 0; j < numMirrors; j++) {
distances[index*numBounces+i] = vec2(1);
distances[index*numBounces+i] = vec2(0, 1);
vec2 u = mirrorPoints[2*j];
vec2 v = mirrorPoints[2*j+1];
vec2 c = (u+v)/2;

vec2 n = normalize(vec2(-v.y+u.y, v.x-u.x));
bool doCurved = abs(arcFocus) < ARC_FOCUS_INF-1;
bool doCurved = abs(arcFocus) < FOCUS_INF-1;

HitInfo hit;
if (doCurved) {
Expand Down Expand Up @@ -121,43 +123,40 @@ void main()
// This function collides a ray o+td with a line uv.
HitInfo RayLineHit(vec2 o, vec2 d, vec2 u, vec2 v)
{
HitInfo hit;
hit.t = -1;

// The normal is a left-rotated vector.
vec2 n = normalize(vec2(-v.y+u.y, v.x-u.x));
float nd = dot(n, d);

// We ensure that the dotproduct can't be zero
// by adding a small factor when its zero.
float t = dot(n, u-o) / (nd + float(nd==0)*0.001);

HitInfo hit;
hit.point = o + t*d;
if (nd == 0) return hit;

hit.t = dot(n, u-o) / nd;
hit.point = o + hit.t*d;
hit.normal = n;
hit.t = t;
return hit;
}

HitInfo RayCircleHit(vec2 o, vec2 d, vec2 cc, float rsq, bool convex)
HitInfo RayCircleHit(vec2 o, vec2 d, vec2 cc, float rsq, bool interior)
{
// Make the point relative to the circle.
vec2 rel = o-cc;
HitInfo hit;
hit.t = -1;

float a = 1;
float b = 2*dot(rel, d);
float c = dot(rel, rel) - rsq;
float D = b*b - 4*a*c;
// Make the point relative to the circle.
vec2 rel = cc-o;
float dp = dot(rel, d);
float D = dp*dp + rsq - dot(rel, rel);

// We take an absolute value here, but if the
// square root ends up negative we set t to zero.
float sqrtD = sqrt(abs(D));
float t1 = (-b - sqrtD) / (2*a);
float t2 = (-b + sqrtD) / (2*a);
if (D < 0) return hit;
float sqrtD = sqrt(D);
float t1 = dp - sqrtD;
float t2 = dp + sqrtD;

float t = float(convex)*max(t1, t2) + (1-float(convex))*min(t1, t2);

HitInfo hit;
hit.point = o + t*d;
hit.t = float(interior)*max(t1, t2) + (1-float(interior))*min(t1, t2);
hit.point = o + hit.t*d;
hit.normal = normalize(cc - hit.point);
hit.t = t * float(D >= 0);
return hit;
}

Expand Down
6 changes: 4 additions & 2 deletions Shaders/ray2.vert
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ void main()

// Ray originate from (0, 0).
vec2 ray;
ray.x = dist*cos(2*PI*prog);
ray.y = dist*sin(2*PI*prog);
//float rayAngle = 0.5*PI + 2*PI/10.0*prog;
float rayAngle = 2*PI*prog;
ray.x = dist*cos(rayAngle);
ray.y = dist*sin(rayAngle);

vec2 pos = vertexPosition;
pos *= 0.01 * pointSize;
Expand Down
8 changes: 3 additions & 5 deletions Shaders/ray3.frag
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ layout(location=9) uniform vec4 innerClearColor;
layout(location=10) uniform vec3 edgeColor;
uniform mat4 mvp;

const float FOCUS_INF = 1000.0f;
const float STEP_MIN = 0.001f;
const float OUTSIDE_MARGIN = 0.001f;

Expand Down Expand Up @@ -67,7 +68,7 @@ void main()
bool showEdgeMark = (showMarkFlags&2) == 2;
bool showEdges = (showMarkFlags&4) == 4;
bool hyperbolic = sphereFocus < 0;
bool straight = abs(sphereFocus) > 999.0f;
bool straight = abs(sphereFocus) > FOCUS_INF-1.0;
bool isInBox = true;

// We iterate every reflection.
Expand Down Expand Up @@ -105,10 +106,7 @@ void main()
correctSide = dot(mirror.normal.xyz, rayDir) < 0;
}
else {
vec3 cc = abs(sphereFocus) * mirror.normal.xyz;
if (hyperbolic) {
cc = mirror.center.xyz - 2 * mirror.normal.xyz * dot(cc-mirror.center.xyz, mirror.normal.xyz);
}
vec3 cc = sphereFocus * mirror.normal.xyz;
float rsq = DistanceSq(cornerPoint, cc);
hit = RaySphereHit(rayOrigin, rayDir, cc, rsq, hyperbolic);

Expand Down
65 changes: 32 additions & 33 deletions Source/Ray2/Ray2.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@
// This class contains the Ray2 running/UI code.
class Ray2
{
public:
private:
Ray2Scene scene;
Ray2Program program;
Camera2D camera = {};

float numRaysFact = 0.5;
int shape = 5;
int currentShape = 5;
Vector3 color = ColorToHSV({ 0, 255, 0, 255 });

float originAngle = 0.0f;
float originRadius = 0.0f;
bool drawInterface = true;
bool hideGui = false;
float arcFocusPercent = 0.0f;

public:
Ray2(): program(scene)
{
scene.GenerateRegularPolygon(5);
Expand All @@ -27,9 +38,9 @@ class Ray2
void RenderUpdate()
{
float delta = GetFrameTime();
program.arcFocus = (arcFocusPercent > 0 ? 1: -1) * powf(1-abs(arcFocusPercent), 8) * (FOCUS_INF);
program.numRays = (int)(pow(numRaysFact, 8) * MAX_RAYS);
program.color = ColorFromHSV(color.x, color.y, color.z);
program.arcFocus = (hyperbolic ? -1 : 1) * (pow(focusFact, 5) * (ARC_FOCUS_INF-1));

if (currentShape != shape) {
currentShape = shape;
Expand All @@ -51,26 +62,26 @@ class Ray2
}
void DrawGUI()
{
if (!hideGui)
{ // Gui Pass.
DrawText(TextFormat("N=%d", program.numRays), 10, 30, 20, DARKGRAY);
DrawFPS(10, 10);

float ox = 170;
float oy = 15;
GuiSlider({ ox, oy, 200, 10 }, "Rays%", TextFormat("%3.1f", 100*numRaysFact), &numRaysFact, 0, 1);
GuiSlider({ ox, oy+20, 200, 10 }, "Focus", TextFormat("%.2f (r=%.3f)", -program.arcFocus, program.GetArcRadius()), &focusFact, 0.001, 1);
GuiSlider({ ox+270, oy, 100, 10 }, "Zoom", TextFormat("%.1f", program.zoom), &program.zoom, 0.25, 4);
GuiCheckBox({ ox+315, oy+15, 20, 20 }, "Hyperbolic", &hyperbolic);
GuiCheckBox({ ox+315, oy+40, 20, 20 }, "Draw Lines", &drawInterface);
GuiSpinner({ ox, oy+40, 80, 15 }, "Num Bounces ", &program.numBounces, 1, MAX_BOUNCES, false);
GuiSpinner({ ox+130, oy+40, 80, 15 }, "Shape ", &shape, 3, 10, false);

ox = 660;
oy = 13;
GuiSlider({ ox, oy, 100, 10 }, "Falloff", TextFormat("%.2f", program.falloff), &program.falloff, 0.001, 1);
GuiSlider({ ox, oy+20, 100, 10 }, "Point Size", TextFormat("%.2f", program.pointSize), &program.pointSize, 0.3, 3);
if (hideGui) {
GuiSlider({ 200, 10, 400, 10 }, "Arc Focus", TextFormat("%.1f", program.arcFocus), &arcFocusPercent, -0.999, 1);
return;
}
DrawText(TextFormat("N=%d", program.numRays), 10, 30, 20, DARKGRAY);
DrawFPS(10, 10);

float ox = 170;
float oy = 15;
GuiSlider({ ox, oy, 200, 10 }, "Rays%", TextFormat("%3.1f", 100*numRaysFact), &numRaysFact, 0, 1);
GuiSlider({ ox, oy+20, 200, 10 }, "Focus", TextFormat("%.2f (r=%.3f)", program.arcFocus, program.GetArcRadius()), &arcFocusPercent, -0.999, 1.0);
GuiSlider({ ox+270, oy, 100, 10 }, "Zoom", TextFormat("%.1f", program.zoom), &program.zoom, 0.25, 4);
GuiCheckBox({ ox+315, oy+15, 20, 20 }, "Draw Lines", &drawInterface);
GuiSpinner({ ox, oy+40, 80, 15 }, "Num Bounces ", &program.numBounces, 1, MAX_BOUNCES, false);
GuiSpinner({ ox+130, oy+40, 80, 15 }, "Shape ", &shape, 3, 10, false);

ox = 660;
oy = 13;
GuiSlider({ ox, oy, 100, 10 }, "Falloff", TextFormat("%.2f", program.falloff), &program.falloff, 0.001, 1);
GuiSlider({ ox, oy+20, 100, 10 }, "Point Size", TextFormat("%.2f", program.pointSize), &program.pointSize, 0.3, 3);
}
void DrawContent()
{
Expand All @@ -79,18 +90,6 @@ class Ray2
if (drawInterface) program.InterfacePass();
EndMode2D();
}

private:
Ray2Scene scene;
Ray2Program program;
Camera2D camera = {};

float numRaysFact = 0.5;
float focusFact = 1.0;
bool hyperbolic = false;
int shape = 5;
int currentShape = 5;
Vector3 color = ColorToHSV({ 0, 255, 0, 255 });
};

#endif
4 changes: 2 additions & 2 deletions Source/Ray2/Ray2Program.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

#include <raylib.h>
#include "Ray2Scene.h"
#include "main.h"

#define WORKGROUP_SIZE 1024
#define MAX_MIRRORS 1000
#define MAX_BOUNCES 16
#define MAX_WORKGROUPS 500
#define MAX_RAYS (MAX_WORKGROUPS*WORKGROUP_SIZE)
#define ARC_FOCUS_INF 1000.0f

typedef int ShaderBuffer;
typedef int ComputeShader;
Expand All @@ -32,7 +32,7 @@ class Ray2Program
// If the focus is +-INF, then we have the straight edges.
// If the focus is positive, we enter spherical space.
// If the focus is negative, we enter hyperbolic space.
float arcFocus = ARC_FOCUS_INF;
float arcFocus = FOCUS_INF;

Ray2Program(Ray2Scene &_scene);
// Update the mirrors as a sequence of line segments.
Expand Down
2 changes: 1 addition & 1 deletion Source/Ray3/Ray3.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Ray3
void RenderUpdate()
{
float dt = GetFrameTime();
program.sphereFocus = (sphereFocusPercent > 0 ? 1: -1) * powf(1-abs(sphereFocusPercent), 8) * (SPHERE_FOCUS_INF);
program.sphereFocus = (sphereFocusPercent > 0 ? 1: -1) * powf(1-abs(sphereFocusPercent), 8) * (FOCUS_INF);
program.numBounces = (int)numBouncesFl;
Ray3Scene *activeScene = &scenes[sceneIndex];
if (program.GetScene() != activeScene) { program.SetScene(activeScene); };
Expand Down
4 changes: 2 additions & 2 deletions Source/Ray3/Ray3Program.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <raylib.h>
#include "Ray3Scene.h"
#include "main.h"

#define SPHERE_FOCUS_INF 1000.0f
typedef int ShaderBuffer;

// This class represents the 3D mirror raytracer.
Expand All @@ -18,7 +18,7 @@ class Ray3Program
int numMirrors = 0;
int numBounces = 5;
Color color = GREEN;
float sphereFocus = 1000.0f;
float sphereFocus = FOCUS_INF;
float edgeThickness = 1.0f;
float markSize = 1.0f;
float resolutionPercent = 0.5f;
Expand Down
2 changes: 2 additions & 0 deletions Source/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "raylib.h"

#define FOCUS_INF 1000.0

extern Color clearColor;
extern Color mainEdgeColor;

Expand Down

0 comments on commit 1115c68

Please sign in to comment.