Skip to content

Commit

Permalink
Merge branch 'webpage'
Browse files Browse the repository at this point in the history
  • Loading branch information
HuskyNator committed Dec 28, 2023
2 parents b0750b1 + 676f11f commit a52fffb
Show file tree
Hide file tree
Showing 21 changed files with 234 additions and 158 deletions.
4 changes: 2 additions & 2 deletions dub.selections.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"bindbc-glfw": "0.10.1",
"bindbc-loader": "0.3.2",
"bindbc-opengl": "0.13.0",
"gamut": "2.3.0",
"gamut": "2.3.1",
"imageformats": "7.0.2",
"intel-intrinsics": "1.11.15",
"intel-intrinsics": "1.11.17",
"vertexd": {"path":"../VertexD"}
}
}
38 changes: 38 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ray - A Dlang Raytracer</title>
<meta name="description" content="Ray is a cpu based raytracer with a focus on learning. It's built on top of Dlang and the VertexD (Opengl 4.6) framework." />
<meta name="keywords" content="dlang, raytracing, programming, opengl" />
<link rel="icon" href="logo/logo.ico">
<link rel="stylesheet" href="style.css">
</head>


<body>
<header class="h_flex">
<svg width="32" height="32">
<polygon class="svg_blue" points="3,0 10,31 31,10" />
<polygon class="svg_orange" points="0,15 3,21 30,15" />
<line class="svg_red" x2="15" y2="14" x1="0" y1="27" />
</svg>
<h1>Ray - A Dlang Raytracer</h1>
</header>
<main>
<img src="web/helmet.gif" alt="Damaged Helmet Rotating Gif">
<p>
Ray is a Dlang based CPU raytracer, built on top of my <a href="https://github.com/HuskyNator/VertexD">opengl tinkering framework</a>.
</p>
</main>
<footer class="h_flex">
<a href="https://github.com/HuskyNator/Ray" class="h_flex">
<img src="web/github-mark-white.svg" alt="Github Logo">
Github
</a>
</footer>
</body>

</html>
Binary file added logo/logo.ico
Binary file not shown.
Binary file added logo/logo128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added logo/logo16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added logo/logo256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added logo/logo32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added logo/logo64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added logs/b0750b1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
108 changes: 44 additions & 64 deletions source/app.d
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
import gamut;
import player;
import raycam;
import raytracer;
import screen;
import std.conv;
import std.datetime.stopwatch;
import std.file;
import std.math.constants : PI;
import std.path;
import std.process;
import std.stdio;
import std.string;
import vertexd.core;
import std.path;
import vertexd.input.gltf_reader;
import vertexd.mesh;
import vertexd.misc : degreesToRadians;
import vertexd.world : World;
import vertexd.world : Node, World;

void main(string[] args) {
uint width = 1920/3;
uint height = 1080/3;
uint width = 1920 / 3;
uint height = 1080 / 3;

const bool RENDER_IMAGE = (args.length > 1 && args[1] == "image");
const bool PROFILE = (args.length > 1 && args[1] == "profile");
const uint PROFILE_COUNT = (PROFILE && args.length > 2) ? args[2].to!uint : 8;
const bool GIF = (args.length > 1 && args[1] == "gif");
const int GIF_COUNT = (args.length > 2) ? args[2].to!uint : 16;

const bool INTERACTIVE = !(RENDER_IMAGE || PROFILE || GIF);

vdInit();
if (RENDER_IMAGE || PROFILE)
Expand Down Expand Up @@ -51,13 +57,19 @@ void main(string[] args) {

RayTracer rayTracer = RayTracer(screen);

Speler speler = new Speler();
world.addNode(speler);
window.setMouseType(MouseType.CAPTURED);
window.keyCallbacks ~= &speler.toetsinvoer;
window.mousepositionCallbacks ~= &speler.muisinvoer;
speler.location = Vec!3(0, 0, 2.5);
speler.addAttribute(camera);
Node root;
if (INTERACTIVE) {
Player player = new Player();
root = player;
window.setMouseType(MouseType.CAPTURED);
window.keyCallbacks ~= &player.keyInput;
window.mousepositionCallbacks ~= &player.mouseInput;
} else {
root = new Node();
}
world.addNode(root);
root.location = Vec!3(0, 0, 2.5);
root.addAttribute(camera);

vdStep(); // Needs to be done before render.

Expand Down Expand Up @@ -88,62 +100,30 @@ void main(string[] args) {
append(performancePath, commitName ~ ':' ~ performance.to!string ~ '\n');
writeln("Performance: ", performance, " sec");
}
} else
} else if (GIF) {
Vec!(4, ubyte)[] images;

float angle = -2 * PI / (GIF_COUNT);
Quat rotation = Quat.rotation(Vec!3(0, 1, 0), angle);
Mat!3 rotationM = rotation.toMat;
foreach (i; 0 .. GIF_COUNT) {
rayTracer.trace(scene, 1, useBVH);
images ~= screen.texture.pixels.dup;
root.location = rotationM ^ root.location;
root.rotation = rotation * root.rotation;
vdStep();
}
Image image;
image.createLayeredViewFromData(cast(void*) images.ptr, cast(int) width, cast(int) height,
GIF_COUNT, PixelType.rgba8, cast(int)(width * 4 * ubyte.sizeof),
cast(int)(width * height * Vec!(4, ubyte).sizeof));
image.flipVertical();
image.saveToFile("../logs/helmet.gif");
} else {
while (!vdShouldClose()) {
rayTracer.trace(scene, 1, useBVH);
vdStep();
// writeln("FPS: " ~ vdFps().to!string);
}
// import std.random : uniform;
// import vertexd.misc;

// enum RUNS = 300;
// Vec!3[3][RUNS] verts;
// Vec!3[RUNS] normal;
// Vec!3[RUNS] bary;
// Vec!3[RUNS] point;
// foreach (k; 0 .. RUNS) {
// foreach (i; 0 .. 3)
// foreach (j; 0 .. 3)
// verts[k][i][j] = uniform(-5.0, 5.0);
// normal[k] = (verts[k][1] - verts[k][0]).cross(verts[k][2] - verts[k][0]).normalize();

// bary[k][0] = uniform!"[]"(0.0, 1.0);
// bary[k][1] = uniform!"[]"(0.0, 1.0);
// bary[k][2] = 1.0 - bary[k][0] - bary[k][1];
// assertAlmostEqual(bary[k].sum(), 1.0);

// point[k] = Vec!3(0);
// static foreach (i; 0 .. 3)
// point[k] += verts[k][i] * bary[k][i];
// }

// Vec!3[RUNS] calculatedBary;
// Vec!3[RUNS] calculatedProjectedBary;

// import std.datetime.stopwatch;

// StopWatch firstWatch = StopWatch(AutoStart.no);
// StopWatch secondWatch = StopWatch(AutoStart.no);

// firstWatch.start();
// foreach (k; 0 .. RUNS)
// calculatedBary[k] = RayTracer.calcBarycentric(verts[k], normal[k], point[k]);
// firstWatch.stop();

// secondWatch.start();
// foreach (k; 0 .. RUNS)
// calculatedProjectedBary[k] = RayTracer.calcProjectedBarycentric(verts[k], point[k]);
// secondWatch.stop();

// foreach (k; 0 .. RUNS) {
// calculatedBary[k].assertAlmostEq(bary[k]);
// calculatedProjectedBary[k].assertAlmostEq(bary[k]);
// }

// import std.stdio;
// import std.conv;

// writeln("First: " ~ std.conv.to!string(firstWatch.peek().total!"hnsecs"()));
// writeln("Second: " ~ std.conv.to!string(secondWatch.peek().total!"hnsecs"()));
}
}
6 changes: 4 additions & 2 deletions source/bvh.d
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ struct BVH {
Centroid[][3] allCentroids = [centroids, centroids.dup, centroids.dup];
buildTree(tree[0], allCentroids); // Build the tree

import std.stdio;
debug {
import std.stdio;

writeln(this.toString(true));
writeln(this.toString(true));
}

// Create resorted indeces array to match up with BoundingBox ID's
uint[3][] newIndices = [];
Expand Down
78 changes: 39 additions & 39 deletions source/player.d
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
module player;
import vertexd;

class Speler : Node {
private Quat xdraai;
private Quat ydraai;
class Player : Node {
private Quat xRot;
private Quat yRot;

private Vec!3 _verplaatsing;
private Vec!2 _draai;
private Vec!2 _draaiDelta;
private Vec!2 _oude_draai;
precision snelheid = 1;
precision draaiSnelheid = 0.2;
private Vec!3 _displacement;
private Vec!2 _rotation;
private Vec!2 _rotationDelta;
private Vec!2 _old_rotation;
precision speed = 1;
precision rotationSpeed = 0.2;

this() {
super();
}

void toetsinvoer(KeyInput input) nothrow {
void keyInput(KeyInput input) nothrow {
try {
import bindbc.glfw;
import bindbc.opengl;
Expand All @@ -27,25 +27,25 @@ class Speler : Node {
int delta = (input.event == GLFW_PRESS) ? 1 : -1;
switch (input.key) {
case GLFW_KEY_A:
_verplaatsing.x -= delta;
_displacement.x -= delta;
break;
case GLFW_KEY_D:
_verplaatsing.x += delta;
_displacement.x += delta;
break;
case GLFW_KEY_SPACE:
_verplaatsing.y += delta;
_displacement.y += delta;
break;
case GLFW_KEY_LEFT_SHIFT:
_verplaatsing.y -= delta;
_displacement.y -= delta;
break;
case GLFW_KEY_S:
_verplaatsing.z -= delta;
_displacement.z -= delta;
break;
case GLFW_KEY_W:
_verplaatsing.z += delta;
_displacement.z += delta;
break;
case GLFW_KEY_LEFT_CONTROL:
_draai = Vec!2(0);
_rotation = Vec!2(0);
// location = Vec!3(0);
break;
default:
Expand All @@ -54,11 +54,11 @@ class Speler : Node {
}
}

void muisinvoer(MousepositionInput input) nothrow {
_draaiDelta.y -= input.x - _oude_draai.x;
_draaiDelta.x -= input.y - _oude_draai.y;
_oude_draai.x = input.x;
_oude_draai.y = input.y;
void mouseInput(MousepositionInput input) nothrow {
_rotationDelta.y -= input.x - _old_rotation.x;
_rotationDelta.x -= input.y - _old_rotation.y;
_old_rotation.x = input.x;
_old_rotation.y = input.y;
}

import std.datetime;
Expand All @@ -68,28 +68,28 @@ class Speler : Node {

double deltaSec = deltaT.total!"hnsecs"() / 10_000_000.0;

Vec!3 vooruit = ydraai * Vec!3([0, 0, -1]);
Vec!3 rechts = ydraai * Vec!3([1, 0, 0]);
Vec!3 forward = yRot * Vec!3([0, 0, -1]);
Vec!3 right = yRot * Vec!3([1, 0, 0]);

Mat!3 verplaatsMat;
verplaatsMat.setCol(0, rechts);
verplaatsMat.setCol(1, Vec!3([0, 1, 0]));
verplaatsMat.setCol(2, vooruit);
Mat!3 displaceMat;
displaceMat.setCol(0, right);
displaceMat.setCol(1, Vec!3([0, 1, 0]));
displaceMat.setCol(2, forward);

this.location = this.location + cast(Vec!3)(verplaatsMat ^ (_verplaatsing * cast(prec)(snelheid * deltaSec)));
this.location = this.location + cast(Vec!3)(displaceMat ^ (_displacement * cast(prec)(speed * deltaSec)));

_draaiDelta = _draaiDelta * cast(prec)(draaiSnelheid * deltaSec);
_draai = _draai + _draaiDelta;
if (abs(_draai.x) > PI_2)
_draai.x = sgn(_draai.x) * PI_2;
if (abs(_draai.y) > PI)
_draai.y -= sgn(_draai.y) * 2 * PI;
_rotationDelta = _rotationDelta * cast(prec)(rotationSpeed * deltaSec);
_rotation = _rotation + _rotationDelta;
if (abs(_rotation.x) > PI_2)
_rotation.x = sgn(_rotation.x) * PI_2;
if (abs(_rotation.y) > PI)
_rotation.y -= sgn(_rotation.y) * 2 * PI;

xdraai = Quat.rotation(Vec!3([1, 0, 0]), _draai.x);
ydraai = Quat.rotation(Vec!3([0, 1, 0]), _draai.y);
this.rotation = ydraai * xdraai;
xRot = Quat.rotation(Vec!3([1, 0, 0]), _rotation.x);
yRot = Quat.rotation(Vec!3([0, 1, 0]), _rotation.y);
this.rotation = yRot * xRot;

_draaiDelta = Vec!2(0);
_rotationDelta = Vec!2(0);

super.logicStep(deltaT);
}
Expand Down
Loading

0 comments on commit a52fffb

Please sign in to comment.