migrate to new c djstdlib

This commit is contained in:
Daniel Ledda
2025-11-14 10:37:18 +01:00
parent 344056744d
commit 7ec69b3067
21 changed files with 392 additions and 366 deletions

View File

@@ -1,5 +1,7 @@
CompileFlags: CompileFlags:
Add: Add:
- -std=c99
- -xc
# LINUX FLAGS # LINUX FLAGS
- -DOS_LINUX - -DOS_LINUX
# WINDOW FLAGS # WINDOW FLAGS

2
build
View File

@@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
LIB_INCLUDE="-lglfw -lGL -lm" LIB_INCLUDE="-lglfw -lGL -lm"
g++ -g -g3 -DOS_LINUX=1 -DENABLE_ASSERT=1 ./src/main.cpp -o ./target/somaesque $LIB_INCLUDE g++ -g -g3 -DOS_LINUX=1 -DENABLE_ASSERT=1 -xc -std=c99 ./src/main.cpp -o ./target/somaesque $LIB_INCLUDE
./target/somaesque ./target/somaesque

View File

@@ -1,5 +1,7 @@
#include <cstring> #include "string.h"
#include "VoxelSpace.h" #include "VoxelSpace.h"
#include "SomaSolve.h"
#include "math.h"
/* /*
void get_dims_input(int dims[3]) { void get_dims_input(int dims[3]) {
@@ -62,12 +64,12 @@ std::vector<uint64> get_reprs_input(int units_required) {
} }
*/ */
typedef list<uint64> SomaSolution; DefineList(size_t, Offset);
typedef struct Solver { typedef struct Solver {
list<uint64>* input; VoxelSpaceReprList *input;
list<size_t>* offsets; OffsetList *offsets;
list<SomaSolution>* solutions; SomaSolutionList *solutions;
} Solver; } Solver;
uint64 STD_SOMA[] = { 23ul, 30ul, 15ul, 1043ul, 24594ul, 12306ul, 11ul }; uint64 STD_SOMA[] = { 23ul, 30ul, 15ul, 1043ul, 24594ul, 12306ul, 11ul };
@@ -122,25 +124,25 @@ void backtrack_solve_iter(std::vector<uint64> *polycube_input, std::vector<int>
} }
*/ */
void backtrackSolve(Arena *arena, Solver *solver, uint64 working_solution = 0, size_t curr_piece = 0) { void backtrackSolve(Arena *arena, Solver *solver, uint64 working_solution, size_t curr_piece) {
list<uint64> *input = solver->input; VoxelSpaceReprList *input = solver->input;
list<size_t> *offsets = solver->offsets; OffsetList *offsets = solver->offsets;
list<SomaSolution> *solutions = solver->solutions; SomaSolutionList *solutions = solver->solutions;
size_t start = offsets->data[curr_piece]; size_t start = offsets->data[curr_piece];
size_t end = offsets->data[curr_piece + 1]; size_t end = offsets->data[curr_piece + 1];
size_t num_pieces = offsets->head - 1; size_t num_pieces = offsets->length - 1;
for (size_t i = start; i < end; i++) { for (size_t i = start; i < end; i++) {
bool successful_fuse = !collides(working_solution, input->data[i]); bool successful_fuse = !collides(working_solution, input->data[i]);
if (successful_fuse) { if (successful_fuse) {
uint64 new_working_solution = working_solution | input->data[i]; uint64 new_working_solution = working_solution | input->data[i];
solutions->data[solutions->head - 1].data[curr_piece] = input->data[i]; solutions->data[solutions->length - 1].data[curr_piece] = input->data[i];
if (curr_piece == num_pieces - 1) { if (curr_piece == num_pieces - 1) {
list<uint64> last_soln = solutions->data[solutions->head - 1]; VoxelSpaceReprList last_soln = solutions->data[solutions->length - 1];
list<uint64> last_soln_copy = PushList(arena, uint64, last_soln.head); VoxelSpaceReprList last_soln_copy = PushList(arena, VoxelSpaceReprList, last_soln.length);
last_soln_copy.length = last_soln.head; last_soln_copy.capacity = last_soln.length;
last_soln_copy.head = last_soln.head; last_soln_copy.length = last_soln.length;
memcpy(last_soln_copy.data, last_soln.data, last_soln.head * sizeof(uint64)); memcpy(last_soln_copy.data, last_soln.data, last_soln.length * sizeof(uint64));
appendList(solutions, last_soln_copy); AppendList(solutions, last_soln_copy);
return; return;
} else { } else {
backtrackSolve(arena, solver, new_working_solution, curr_piece + 1); backtrackSolve(arena, solver, new_working_solution, curr_piece + 1);
@@ -148,40 +150,40 @@ void backtrackSolve(Arena *arena, Solver *solver, uint64 working_solution = 0, s
} }
} }
if (curr_piece == 0) { if (curr_piece == 0) {
solutions->head -= 1; solutions->length -= 1;
} }
} }
list<SomaSolution> getSolutionRotations(Arena *arena, SomaSolution *solution, int dims[3]) { SomaSolutionList getSolutionRotations(Arena *arena, SomaSolution *solution, int dims[3]) {
list<SomaSolution> result = PushFullList(arena, SomaSolution, NUM_ROTS_3D); SomaSolutionList result = PushFullList(arena, SomaSolutionList, NUM_ROTS_3D);
for (EachIn(result, i)) { for (EachIn(result, i)) {
result.data[i] = PushList(arena, uint64, solution->head); result.data[i] = PushList(arena, SomaSolution, solution->length);
} }
for (int piece_i = 0; piece_i < solution->head; piece_i++) { for (int piece_i = 0; piece_i < solution->length; piece_i++) {
Space space = { Space space = {
solution->data[piece_i], solution->data[piece_i],
dims[0], dims[0],
dims[1], dims[1],
dims[2], dims[2],
}; };
list<Space> pieceRotations = getAllRotations(arena, &space); VoxelSpaceList pieceRotations = getAllRotations(arena, &space);
for (int rot_i = 0; rot_i < pieceRotations.head; rot_i++) { for (int rot_i = 0; rot_i < pieceRotations.length; rot_i++) {
appendList(&result.data[rot_i], pieceRotations.data[rot_i].space); AppendList(&result.data[rot_i], pieceRotations.data[rot_i].space);
} }
} }
return result; return result;
} }
list<SomaSolution> filterUnique(Arena *arena, list<SomaSolution> *solutions, int dims[3]) { SomaSolutionList filterUnique(Arena *arena, SomaSolutionList *solutions, int dims[3]) {
if (solutions->head == 0) { if (solutions->length == 0) {
return list<SomaSolution>{NULL,0,0}; return (SomaSolutionList)EmptyList();
} }
list<SomaSolution> uniqueSolns = PushList(arena, SomaSolution, solutions->head); SomaSolutionList uniqueSolns = PushList(arena, SomaSolutionList, solutions->length);
for (EachIn(*solutions, i)) { for (EachIn(*solutions, i)) {
SomaSolution solution = solutions->data[i]; SomaSolution solution = solutions->data[i];
bool foundMatch = false; bool foundMatch = false;
Scratch temp = scratchStart(&arena, 1); Scratch temp = scratchStart(&arena, 1);
list<SomaSolution> rots = getSolutionRotations(temp.arena, &solution, dims); SomaSolutionList rots = getSolutionRotations(temp.arena, &solution, dims);
for (EachIn(rots, j)) { for (EachIn(rots, j)) {
SomaSolution rotation = rots.data[j]; SomaSolution rotation = rots.data[j];
for (EachIn(uniqueSolns, k)) { for (EachIn(uniqueSolns, k)) {
@@ -204,11 +206,11 @@ list<SomaSolution> filterUnique(Arena *arena, list<SomaSolution> *solutions, int
} }
scratchEnd(temp); scratchEnd(temp);
if (!foundMatch) { if (!foundMatch) {
SomaSolution solutionCopy = PushList(arena, uint64, solution.head); SomaSolution solutionCopy = PushList(arena, SomaSolution, solution.length);
solutionCopy.length = solution.head; solutionCopy.capacity = solution.length;
solutionCopy.head = solution.head; solutionCopy.length = solution.length;
memcpy(solutionCopy.data, solution.data, solution.head * sizeof(uint64)); memcpy(solutionCopy.data, solution.data, solution.length * sizeof(SomaSolutionList_underlying));
appendList(&uniqueSolns, solutionCopy); AppendList(&uniqueSolns, solutionCopy);
} }
} }
return uniqueSolns; return uniqueSolns;
@@ -222,56 +224,56 @@ uint64 factorial(int n) {
return result; return result;
} }
list<SomaSolution> solve(uint64 *reprs_in, uint32 reprs_in_count, int dims[3]) { SomaSolutionList solve(uint64 *reprs_in, uint32 reprs_in_count, int dims[3]) {
Arena *arena = arenaAlloc(Megabytes(64)); Arena *arena = arenaAlloc(Megabytes(64));
Arena *permsArena = arenaAlloc(Megabytes(128)); Arena *permsArena = arenaAlloc(Megabytes(128));
list<size_t> offsets = PushList(arena, size_t, reprs_in_count + 1); OffsetList offsets = PushList(arena, OffsetList, reprs_in_count + 1);
list<uint64> polycubes = PushList(arena, uint64, 0); VoxelSpaceReprList polycubes = PushList(arena, VoxelSpaceReprList, 0);
Space empty_voxel_space = { Space empty_voxel_space = {
{}, 0,
dims[0], dims[0],
dims[1], dims[1],
dims[2], dims[2],
}; };
appendList(&offsets, (size_t)0); AppendList(&offsets, 0);
uint64 possibleCombos = 0; uint64 possibleCombos = 0;
{ {
list<uint64> positions = {}; VoxelSpaceReprList positions = {};
Space space = empty_voxel_space; Space space = empty_voxel_space;
space.space = reprs_in[0]; space.space = reprs_in[0];
cullEmptySpace(&space); cullEmptySpace(&space);
positions = getAllPositionsInPrism(permsArena, &space, dims); positions = getAllPositionsInPrism(permsArena, &space, dims);
possibleCombos += positions.head; possibleCombos += positions.length;
uint64 *insertion = PushArray(arena, uint64, positions.length); VoxelSpaceReprList_underlying *insertion = PushArray(arena, uint64, positions.capacity);
polycubes.capacity += positions.capacity;
polycubes.length += positions.length; polycubes.length += positions.length;
polycubes.head += positions.head; memcpy(insertion, positions.data, positions.capacity * sizeof(VoxelSpaceReprList_underlying));
memcpy(insertion, positions.data, positions.length * sizeof(uint64));
}; };
for (size_t i = 1; i < reprs_in_count; i++) { for (size_t i = 1; i < reprs_in_count; i++) {
appendList(&offsets, polycubes.length); AppendList(&offsets, polycubes.capacity);
Space space = empty_voxel_space; Space space = empty_voxel_space;
space.space = reprs_in[i]; space.space = reprs_in[i];
cullEmptySpace(&space); cullEmptySpace(&space);
list<uint64> perms = getAllPermutationsInPrism(permsArena, &space, dims); VoxelSpaceReprList perms = getAllPermutationsInPrism(permsArena, &space, dims);
possibleCombos *= perms.head; possibleCombos *= perms.length;
uint64 *insertion = PushArray(arena, uint64, perms.length); VoxelSpaceReprList_underlying *insertion = PushArray(arena, uint64, perms.capacity);
polycubes.capacity += perms.capacity;
polycubes.length += perms.length; polycubes.length += perms.length;
polycubes.head += perms.head; memcpy(insertion, perms.data, perms.capacity * sizeof(VoxelSpaceReprList_underlying));
memcpy(insertion, perms.data, perms.length * sizeof(uint64));
} }
appendList(&offsets, polycubes.head); AppendList(&offsets, polycubes.length);
list<SomaSolution> solutions = PushList(permsArena, SomaSolution, (size_t)floor(sqrt(possibleCombos))); SomaSolutionList solutions = PushList(permsArena, SomaSolutionList, (size_t)floor(sqrt(possibleCombos)));
SomaSolution initialSoln = PushFullList(permsArena, uint64, reprs_in_count); SomaSolution initialSoln = PushFullList(permsArena, SomaSolution, reprs_in_count);
appendList(&solutions, initialSoln); AppendList(&solutions, initialSoln);
Solver solver = { Solver solver = {
&polycubes, &polycubes,
@@ -279,7 +281,7 @@ list<SomaSolution> solve(uint64 *reprs_in, uint32 reprs_in_count, int dims[3]) {
&solutions, &solutions,
}; };
backtrackSolve(permsArena, &solver); backtrackSolve(permsArena, &solver, 0, 0);
return filterUnique(permsArena, solver.solutions, dims); return filterUnique(permsArena, solver.solutions, dims);
} }
@@ -291,6 +293,6 @@ void interactive_cmd_line_solve_soma() {
//std::cout << '\n'; //std::cout << '\n';
//std::vector<uint64> reprs = get_reprs_input(dims[0]*dims[1]*dims[2]); //std::vector<uint64> reprs = get_reprs_input(dims[0]*dims[1]*dims[2]);
print("Great. Calculating solutions...\n"); print("Great. Calculating solutions...\n");
list<SomaSolution> solutions = solve(STD_SOMA, ArrayCount(STD_SOMA), dims); SomaSolutionList solutions = solve(STD_SOMA, ArrayCount(STD_SOMA), dims);
print("%zu solutions found.\n", solutions.head); print("%zu solutions found.\n", solutions.length);
} }

View File

@@ -1,6 +1,9 @@
#include "VoxelSpace.h"
#include "lib/djstdlib/core.h" #include "lib/djstdlib/core.h"
extern uint64 STD_SOMA[]; extern uint64 STD_SOMA[];
typedef list<uint64> SomaSolution; typedef VoxelSpaceReprList SomaSolution;
list<SomaSolution> solve(list<uint64> *reprs_in, int dims[3]); DefineList(SomaSolution, SomaSolution);
SomaSolutionList solve(uint64 *reprs_in, uint32 reprs_in_length, int dims[3]);
void interactive_cmd_line_solve_soma(); void interactive_cmd_line_solve_soma();

View File

@@ -1,4 +1,4 @@
#include <cstring> #include "string.h"
#include "VoxelSpace.h" #include "VoxelSpace.h"
int index(int dim_y, int dim_z, int x, int y, int z) { int index(int dim_y, int dim_z, int x, int y, int z) {
@@ -32,7 +32,7 @@ int newIndexRotZ(Space *space, int x, int y, int z) {
return space->dim_x * space->dim_z * (space->dim_y - 1 - y) + space->dim_z * x + z; return space->dim_x * space->dim_z * (space->dim_y - 1 - y) + space->dim_z * x + z;
} }
uint64 toggle(uint64 space, int index) { inline uint64 toggle(uint64 space, int index) {
space ^= 1ull << index; space ^= 1ull << index;
return space; return space;
} }
@@ -46,13 +46,15 @@ uint64 set(uint64 space, int index, bool val) {
return space; return space;
} }
bool collides(uint64 a, uint64 b) { inline bool collides(uint64 a, uint64 b) {
return (a | b) != (a ^ b); return (a | b) != (a ^ b);
} }
/*
bool collides(Space *a, Space *b) { bool collides(Space *a, Space *b) {
return (a->space | b->space) != (a->space ^ b->space); return (a->space | b->space) != (a->space ^ b->space);
} }
*/
bool filledAt(Space *space, int x, int y, int z) { bool filledAt(Space *space, int x, int y, int z) {
uint64 mask = 1ull << (space->dim_y * space->dim_z * x + space->dim_z * y + z); uint64 mask = 1ull << (space->dim_y * space->dim_z * x + space->dim_z * y + z);
@@ -165,7 +167,7 @@ bool isMatch(Space *a, Space *b) {
&& a->dim_z == b->dim_z; && a->dim_z == b->dim_z;
} }
void pushNewUniqueSpins(list<Space> *existingSpaces, Space* spaceToSpin) { void pushNewUniqueSpins(VoxelSpaceList *existingSpaces, Space* spaceToSpin) {
Space spins[4] = {}; Space spins[4] = {};
spins[0] = *spaceToSpin; spins[0] = *spaceToSpin;
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
@@ -181,21 +183,21 @@ void pushNewUniqueSpins(list<Space> *existingSpaces, Space* spaceToSpin) {
} }
} }
if (!matchFound) { if (!matchFound) {
appendList(existingSpaces, spins[i]); AppendList(existingSpaces, spins[i]);
} }
} }
} }
void pushXAxisSpins(Arena *arena, list<Space> *existingSpaces, Space* spaceToSpin) { void pushXAxisSpins(Arena *arena, VoxelSpaceList *existingSpaces, Space* spaceToSpin) {
Space refSpace = *spaceToSpin; Space refSpace = *spaceToSpin;
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
rotate90X(&refSpace); rotate90X(&refSpace);
appendList(existingSpaces, refSpace); AppendList(existingSpaces, refSpace);
} }
} }
list<Space> getUniqueRotations(Arena *arena, Space *space) { VoxelSpaceList getUniqueRotations(Arena *arena, Space *space) {
list<Space> rotations = PushList(arena, Space, 24); VoxelSpaceList rotations = PushList(arena, VoxelSpaceList, 24);
Space refSpace = *space; Space refSpace = *space;
cullEmptySpace(&refSpace); cullEmptySpace(&refSpace);
pushNewUniqueSpins(&rotations, &refSpace); pushNewUniqueSpins(&rotations, &refSpace);
@@ -210,13 +212,13 @@ list<Space> getUniqueRotations(Arena *arena, Space *space) {
rotate90Z(&refSpace); rotate90Z(&refSpace);
rotate90Z(&refSpace); rotate90Z(&refSpace);
pushNewUniqueSpins(&rotations, &refSpace); pushNewUniqueSpins(&rotations, &refSpace);
rotations.length = rotations.head; rotations.capacity = rotations.length;
arenaPopTo(arena, rotations.data + rotations.head); arenaPopTo(arena, rotations.data + rotations.length);
return rotations; return rotations;
} }
list<Space> getAllRotations(Arena *arena, Space *space) { VoxelSpaceList getAllRotations(Arena *arena, Space *space) {
list<Space> rotations = PushList(arena, Space, 24); VoxelSpaceList rotations = PushList(arena, VoxelSpaceList, 24);
Space refSpace = *space; Space refSpace = *space;
pushXAxisSpins(arena, &rotations, &refSpace); pushXAxisSpins(arena, &rotations, &refSpace);
rotate90Y(&refSpace); rotate90Y(&refSpace);
@@ -233,9 +235,9 @@ list<Space> getAllRotations(Arena *arena, Space *space) {
return rotations; return rotations;
} }
list<uint64> getAllPositionsInPrism(Arena *arena, Space *space, int prism_dims[3]) { VoxelSpaceReprList getAllPositionsInPrism(Arena *arena, Space *space, int prism_dims[3]) {
if (space->dim_x > prism_dims[0] || space->dim_y > prism_dims[1] || space->dim_z > prism_dims[2]) { if (space->dim_x > prism_dims[0] || space->dim_y > prism_dims[1] || space->dim_z > prism_dims[2]) {
return list<uint64>{0}; return (VoxelSpaceReprList)EmptyList();
} }
int count = 0; int count = 0;
void *startList = 0; void *startList = 0;
@@ -263,24 +265,24 @@ list<uint64> getAllPositionsInPrism(Arena *arena, Space *space, int prism_dims[3
} }
} }
} }
list<uint64> result = {}; VoxelSpaceReprList result = {};
result.data = (uint64 *)startList; result.data = (uint64 *)startList;
result.capacity = count;
result.length = count; result.length = count;
result.head = count;
return result; return result;
} }
list<uint64> getAllPermutationsInPrism(Arena *arena, Space *space, int prism_dims[3]) { VoxelSpaceReprList getAllPermutationsInPrism(Arena *arena, Space *space, int prism_dims[3]) {
Scratch temp = scratchStart(&arena, 1); Scratch temp = scratchStart(&arena, 1);
list<Space> rotations = getUniqueRotations(temp.arena, space); VoxelSpaceList rotations = getUniqueRotations(temp.arena, space);
list<uint64> result = PushList(arena, uint64, 0); VoxelSpaceReprList result = PushList(arena, VoxelSpaceReprList, 0);
for (EachIn(rotations, i)) { for (EachIn(rotations, i)) {
list<uint64> positions = getAllPositionsInPrism(temp.arena, &rotations.data[i], prism_dims); VoxelSpaceReprList positions = getAllPositionsInPrism(temp.arena, &rotations.data[i], prism_dims);
uint64 *listAppend = PushArray(arena, uint64, positions.length); uint64 *listAppend = PushArray(arena, uint64, positions.capacity);
memcpy(listAppend, positions.data, positions.length * sizeof(uint64)); memcpy(listAppend, positions.data, positions.capacity * sizeof(uint64));
result.capacity += positions.capacity;
result.length += positions.length; result.length += positions.length;
result.head += positions.head;
} }
scratchEnd(temp); scratchEnd(temp);

View File

@@ -3,23 +3,26 @@
#include "lib/djstdlib/core.h" #include "lib/djstdlib/core.h"
constexpr int NUM_ROTS_3D = 24; #define NUM_ROTS_3D 24
struct Extrema { typedef struct Extrema {
int xMax; int xMax;
int xMin; int xMin;
int yMax; int yMax;
int yMin; int yMin;
int zMax; int zMax;
int zMin; int zMin;
}; } Extrema;
struct Space { typedef struct Space {
uint64 space; uint64 space;
int dim_x; int dim_x;
int dim_y; int dim_y;
int dim_z; int dim_z;
}; } Space;
DefineList(Space, VoxelSpace);
DefineList(uint64, VoxelSpaceRepr);
int newIndexRotX(Space *space, int x, int y, int z); int newIndexRotX(Space *space, int x, int y, int z);
@@ -31,7 +34,7 @@ uint64 toggle(uint64 space, int index);
uint64 set(uint64 space, int index, bool val); uint64 set(uint64 space, int index, bool val);
bool collides(Space *a, Space *b); //bool collides(Space *a, Space *b);
bool collides(uint64 a, uint64 b); bool collides(uint64 a, uint64 b);
Space add(Space *a, Space *b); Space add(Space *a, Space *b);
@@ -50,15 +53,15 @@ void rotate90Y(Space *space);
void rotate90Z(Space *space); void rotate90Z(Space *space);
void pushNewUniqueSpins(list<Space> *existingSpaces, Space* spaceToSpin); void pushNewUniqueSpins(VoxelSpaceList *existingSpaces, Space* spaceToSpin);
list<Space> getUniqueRotations(Arena *arena, Space *space); VoxelSpaceList getUniqueRotations(Arena *arena, Space *space);
list<Space> getAllRotations(Arena *arena, Space *space); VoxelSpaceList getAllRotations(Arena *arena, Space *space);
list<uint64> getAllPositionsInPrism(Arena *arena, Space *space, int prism_dims[3]); VoxelSpaceReprList getAllPositionsInPrism(Arena *arena, Space *space, int prism_dims[3]);
list<uint64> getAllPermutationsInPrism(Arena *arena, Space *space, int prism_dims[3]); VoxelSpaceReprList getAllPermutationsInPrism(Arena *arena, Space *space, int prism_dims[3]);
int size(uint64 space); int size(uint64 space);

View File

@@ -13,7 +13,7 @@ real32 hueToRGB(real32 p, real32 q, real32 t) {
return p; return p;
}; };
Vector4<real32> hslToHex(real32 h, real32 s, real32 l) { RLVector4 hslToHex(real32 h, real32 s, real32 l) {
h /= 360; h /= 360;
s /= 100; s /= 100;
l /= 100; l /= 100;
@@ -27,10 +27,10 @@ Vector4<real32> hslToHex(real32 h, real32 s, real32 l) {
g = hueToRGB(p, q, h); g = hueToRGB(p, q, h);
b = hueToRGB(p, q, h - 1.0f / 3); b = hueToRGB(p, q, h - 1.0f / 3);
} }
return vec4<real32>(r, g, b, 1); return (RLVector4){r, g, b, 1};
} }
Vector4<real32> colorFromIndex(int index) { RLVector4 colorFromIndex(int index) {
real32 color_wheel_cycle = floorf(index / 6.0f); real32 color_wheel_cycle = floorf(index / 6.0f);
real32 darkness_cycle = floorf(index / 12.0f); real32 darkness_cycle = floorf(index / 12.0f);
real32 spacing = (360.0f / 6.0f); real32 spacing = (360.0f / 6.0f);

View File

@@ -2,16 +2,17 @@
#define COLOR_H #define COLOR_H
#include "../lib/djstdlib/core.h" #include "../lib/djstdlib/core.h"
#include "../lib/raymath.h"
#define COLOR_BLACK vec4<real32>(0, 0, 0, 1) #define COLOR_BLACK (RLVector4){0, 0, 0, 1}
#define COLOR_RED vec4<real32>(1, 0, 0, 1) #define COLOR_RED (RLVector4){1, 0, 0, 1}
#define COLOR_GREEN vec4<real32>(0, 1, 0, 1) #define COLOR_GREEN (RLVector4){0, 1, 0, 1}
#define COLOR_BLUE vec4<real32>(0, 0, 1, 1) #define COLOR_BLUE (RLVector4){0, 0, 1, 1}
#define COLOR_MAGENTA vec4<real32>(1, 0, 1, 1) #define COLOR_MAGENTA (RLVector4){1, 0, 1, 1}
#define COLOR_YELLOW vec4<real32>(1, 1, 0, 1) #define COLOR_YELLOW (RLVector4){1, 1, 0, 1}
#define COLOR_CYAN vec4<real32>(0, 1, 1, 1) #define COLOR_CYAN (RLVector4){0, 1, 1, 1}
#define COLOR_WHITE vec4<real32>(1, 1, 1, 1) #define COLOR_WHITE (RLVector4){1, 1, 1, 1}
Vector4<real32> colorFromIndex(int index); RLVector4 colorFromIndex(int index);
#endif #endif

View File

@@ -3,11 +3,14 @@
#include "../lib/djstdlib/os.h" #include "../lib/djstdlib/os.h"
static void tinyobj_get_filedata(void* ctx, const char* filename, const int is_mtl, const char* obj_filename, char** data, size_t* len) { static void tinyobj_get_filedata(void* ctx, const char* filename, const int is_mtl, const char* obj_filename, char** data, size_t* len) {
string file = os_readEntireFile((Arena *)ctx, "./assets/models/cube.obj"_s); string file = os_readEntireFile((Arena *)ctx, s("./assets/models/cube.obj"));
*data = file.str; *data = file.str;
*len = file.length; *len = file.length;
} }
DefineList(uint32, MeshIndex);
DefineList(real32, MeshValue);
Mesh createMesh(const char* obj_file) { Mesh createMesh(const char* obj_file) {
Scratch temp = scratchStart(0, 0); Scratch temp = scratchStart(0, 0);
@@ -36,10 +39,10 @@ Mesh createMesh(const char* obj_file) {
return result; return result;
} }
list<uint32> indices = PushFullList(temp.arena, uint32, attrib.num_faces); MeshIndexList indices = PushFullList(temp.arena, MeshIndexList, attrib.num_faces);
list<real32> vertices = PushFullList(temp.arena, real32, 3*attrib.num_faces); MeshValueList vertices = PushFullList(temp.arena, MeshValueList, 3*attrib.num_faces);
list<real32> normals = PushFullList(temp.arena, real32, 3*attrib.num_faces); MeshValueList normals = PushFullList(temp.arena, MeshValueList, 3*attrib.num_faces);
list<real32> texcoords = PushFullList(temp.arena, real32, 2*attrib.num_faces); MeshValueList texcoords = PushFullList(temp.arena, MeshValueList, 2*attrib.num_faces);
for (int i = 0; i < attrib.num_faces; i++) { for (int i = 0; i < attrib.num_faces; i++) {
tinyobj_vertex_index_t vertex_data = attrib.faces[i]; tinyobj_vertex_index_t vertex_data = attrib.faces[i];
@@ -86,7 +89,7 @@ Mesh createMesh(const char* obj_file) {
return result; return result;
} }
Mesh createMesh(const Shape* shape) { Mesh createMeshFromShape(const Shape* shape) {
Mesh result = {0}; Mesh result = {0};
result.num_indices = shape->indices_size; result.num_indices = shape->indices_size;

View File

@@ -5,7 +5,7 @@
#include "../lib/djstdlib/core.h" #include "../lib/djstdlib/core.h"
#include "geometry.h" #include "geometry.h"
struct Mesh { typedef struct {
uint32 vao; uint32 vao;
union { union {
struct { struct {
@@ -17,9 +17,9 @@ struct Mesh {
uint32 vbos[4]; uint32 vbos[4];
}; };
uint64 num_indices; uint64 num_indices;
}; } Mesh;
Mesh createMesh(const char* obj_file); Mesh createMesh(const char* obj_file);
Mesh createMesh(const Shape* shape); Mesh createMeshFromShape(const Shape* shape);
#endif #endif

View File

@@ -4,10 +4,10 @@
#include "../lib/glad/glad.h" #include "../lib/glad/glad.h"
#include "../lib/raymath.h" #include "../lib/raymath.h"
enum ShaderType { typedef enum {
fragment=GL_FRAGMENT_SHADER, ShaderType_fragment=GL_FRAGMENT_SHADER,
vertex=GL_VERTEX_SHADER, ShaderType_vertex=GL_VERTEX_SHADER,
}; } ShaderType;
uint32 createGlShader(string file_path, ShaderType shader_type) { uint32 createGlShader(string file_path, ShaderType shader_type) {
Scratch temp = scratchStart(0, 0); Scratch temp = scratchStart(0, 0);
@@ -23,7 +23,7 @@ uint32 createGlShader(string file_path, ShaderType shader_type) {
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_log_length); glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_log_length);
string info_log = PushString(temp.arena, (size_t)info_log_length + 1); string info_log = PushString(temp.arena, (size_t)info_log_length + 1);
glGetShaderInfoLog(shader, info_log_length, NULL, info_log.str); glGetShaderInfoLog(shader, info_log_length, NULL, info_log.str);
const char* shader_type_name = shader_type == ShaderType::fragment ? "FRAGMENT" : "VERTEX"; const char* shader_type_name = shader_type == ShaderType_fragment ? "FRAGMENT" : "VERTEX";
print("%s shader compilation error (%S):\n%S", shader_type_name, file_path, info_log); print("%s shader compilation error (%S):\n%S", shader_type_name, file_path, info_log);
} }
scratchEnd(temp); scratchEnd(temp);
@@ -35,8 +35,8 @@ Shader createShader(string vertex_path, string fragment_path) {
Shader result = {0}; Shader result = {0};
uint32 vertex_shader = createGlShader(vertex_path, ShaderType::vertex); uint32 vertex_shader = createGlShader(vertex_path, ShaderType_vertex);
uint32 fragment_shader = createGlShader(fragment_path, ShaderType::fragment); uint32 fragment_shader = createGlShader(fragment_path, ShaderType_fragment);
result.progId = glCreateProgram(); result.progId = glCreateProgram();
glAttachShader(result.progId, vertex_shader); glAttachShader(result.progId, vertex_shader);
@@ -65,37 +65,28 @@ void setUniformMat4fv(Shader *s, const char *uniformName, Matrix *matrix) {
glGetUniformLocation(s->progId, uniformName), glGetUniformLocation(s->progId, uniformName),
1, GL_FALSE, MatrixToFloat(*matrix)); 1, GL_FALSE, MatrixToFloat(*matrix));
} }
void setUniformMat4fv(int uniformLocation, Matrix *matrix) { void setUniformMat4fvByLoc(int uniformLocation, Matrix *matrix) {
glUniformMatrix4fv(uniformLocation, 1, GL_FALSE, MatrixToFloat(*matrix)); glUniformMatrix4fv(uniformLocation, 1, GL_FALSE, MatrixToFloat(*matrix));
} }
void setUniform4fv(Shader *s, const char *uniformName, Vector4<real32> *vector) {
glUniform4fv(glGetUniformLocation(s->progId, uniformName), 1, vector->vec);
}
void setUniform4fv(Shader *s, const char *uniformName, RLVector4 *vector) { void setUniform4fv(Shader *s, const char *uniformName, RLVector4 *vector) {
glUniform4fv(glGetUniformLocation(s->progId, uniformName), 1, (const GLfloat *)vector); glUniform4fv(glGetUniformLocation(s->progId, uniformName), 1, (const GLfloat *)vector);
} }
void setUniform4fv(int uniformLocation, RLVector4 *vector) { void setUniform4fvByLoc(int uniformLocation, RLVector4 *vector) {
glUniform4fv(uniformLocation, 1, (const GLfloat *)vector); glUniform4fv(uniformLocation, 1, (const GLfloat *)vector);
} }
void setUniform3fv(Shader *s, const char *uniformName, Vector3<real32> *vector) {
glUniform3fv(glGetUniformLocation(s->progId, uniformName), 1, vector->vec);
}
void setUniform3fv(Shader *s, const char *uniformName, RLVector3 *vector) { void setUniform3fv(Shader *s, const char *uniformName, RLVector3 *vector) {
glUniform3fv(glGetUniformLocation(s->progId, uniformName), 1, Vector3ToFloat(*vector)); glUniform3fv(glGetUniformLocation(s->progId, uniformName), 1, Vector3ToFloat(*vector));
} }
void setUniform3fv(int uniformLocation, RLVector3 *vector) { void setUniform3fvByLoc(int uniformLocation, RLVector3 *vector) {
glUniform3fv(uniformLocation, 1, Vector3ToFloat(*vector)); glUniform3fv(uniformLocation, 1, Vector3ToFloat(*vector));
} }
void setUniform2fv(Shader *s, const char *uniformName, Vector2<real32> *vector) {
glUniform2fv(glGetUniformLocation(s->progId, uniformName), 1, vector->vec);
}
void setUniform2fv(Shader *s, const char *uniformName, RLVector2 *vector) { void setUniform2fv(Shader *s, const char *uniformName, RLVector2 *vector) {
glUniform2fv(glGetUniformLocation(s->progId, uniformName), 1, (const GLfloat *)vector); glUniform2fv(glGetUniformLocation(s->progId, uniformName), 1, (const GLfloat *)vector);
} }
void setUniform2fv(int uniformLocation, RLVector2 *vector) { void setUniform2fvByLoc(int uniformLocation, RLVector2 *vector) {
glUniform2fv(uniformLocation, 1, (const GLfloat *)vector); glUniform2fv(uniformLocation, 1, (const GLfloat *)vector);
} }

View File

@@ -4,23 +4,23 @@
#include "../lib/raymath.h" #include "../lib/raymath.h"
#include "../lib/djstdlib/core.h" #include "../lib/djstdlib/core.h"
struct Shader { typedef struct {
uint32 progId; uint32 progId;
}; } Shader;
Shader createShader(string vertex_path, string fragment_path); Shader createShader(string vertex_path, string fragment_path);
void setUniformMat4fv(Shader *s, const char *uniformName, Matrix *matrix); void setUniformMat4fv(Shader *s, const char *uniformName, Matrix *matrix);
void setUniformMat4fv(int uniformLocation, Matrix *matrix); void setUniformMat4fvByLoc(int uniformLocation, Matrix *matrix);
void setUniform4fv(Shader *s, const char *uniformName, RLVector4 *vector); void setUniform4fv(Shader *s, const char *uniformName, RLVector4 *vector);
void setUniform4fv(int uniformLocation, RLVector4 *vector); void setUniform4fvByLoc(int uniformLocation, RLVector4 *vector);
void setUniform3fv(Shader *s, const char *uniformName, RLVector3 *vector); void setUniform3fv(Shader *s, const char *uniformName, RLVector3 *vector);
void setUniform3fv(int uniformLocation, RLVector3 *vector); void setUniform3fvByLoc(int uniformLocation, RLVector3 *vector);
void setUniform2fv(Shader *s, const char *uniformName, RLVector2 *vector); void setUniform2fv(Shader *s, const char *uniformName, RLVector2 *vector);
void setUniform2fv(int uniformLocation, RLVector2 *vector); void setUniform2fvByLoc(int uniformLocation, RLVector2 *vector);
int getUniformLocation(Shader *s, const char *uniformName); int getUniformLocation(Shader *s, const char *uniformName);

View File

@@ -1,11 +1,11 @@
#ifndef LEDDA_TEXTURE_H #ifndef LEDDA_TEXTURE_H
#define LEDDA_TEXTURE_H #define LEDDA_TEXTURE_H
struct Texture { typedef struct {
unsigned int tex_id; unsigned int tex_id;
int width; int width;
int height; int height;
}; } Texture;
Texture createTexture(const char* source_path); Texture createTexture(const char* source_path);

View File

@@ -3,14 +3,14 @@
#include "stddef.h" #include "stddef.h"
struct Shape { typedef struct {
unsigned int* indices; unsigned int* indices;
size_t indices_size; size_t indices_size;
float* uv; float* uv;
size_t uv_size; size_t uv_size;
float* xyz; float* xyz;
size_t xyz_size; size_t xyz_size;
}; } Shape;
extern const Shape TRIANGLE; extern const Shape TRIANGLE;
extern const Shape SQUARE; extern const Shape SQUARE;

View File

@@ -117,7 +117,7 @@
typedef struct RLVector2 { typedef struct RLVector2 {
float x; float x;
float y; float y;
} RMVector2; } RLVector2;
#define RL_VECTOR2_TYPE #define RL_VECTOR2_TYPE
#endif #endif
@@ -233,55 +233,55 @@ RMAPI int FloatEquals(float x, float y)
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Vector with components value 0.0f // Vector with components value 0.0f
RMAPI RMVector2 Vector2Zero(void) RMAPI RLVector2 Vector2Zero(void)
{ {
RMVector2 result = { 0.0f, 0.0f }; RLVector2 result = { 0.0f, 0.0f };
return result; return result;
} }
// Vector with components value 1.0f // Vector with components value 1.0f
RMAPI RMVector2 Vector2One(void) RMAPI RLVector2 Vector2One(void)
{ {
RMVector2 result = { 1.0f, 1.0f }; RLVector2 result = { 1.0f, 1.0f };
return result; return result;
} }
// Add two vectors (v1 + v2) // Add two vectors (v1 + v2)
RMAPI RMVector2 Vector2Add(RMVector2 v1, RMVector2 v2) RMAPI RLVector2 Vector2Add(RLVector2 v1, RLVector2 v2)
{ {
RMVector2 result = { v1.x + v2.x, v1.y + v2.y }; RLVector2 result = { v1.x + v2.x, v1.y + v2.y };
return result; return result;
} }
// Add vector and float value // Add vector and float value
RMAPI RMVector2 Vector2AddValue(RMVector2 v, float add) RMAPI RLVector2 Vector2AddValue(RLVector2 v, float add)
{ {
RMVector2 result = { v.x + add, v.y + add }; RLVector2 result = { v.x + add, v.y + add };
return result; return result;
} }
// Subtract two vectors (v1 - v2) // Subtract two vectors (v1 - v2)
RMAPI RMVector2 Vector2Subtract(RMVector2 v1, RMVector2 v2) RMAPI RLVector2 Vector2Subtract(RLVector2 v1, RLVector2 v2)
{ {
RMVector2 result = { v1.x - v2.x, v1.y - v2.y }; RLVector2 result = { v1.x - v2.x, v1.y - v2.y };
return result; return result;
} }
// Subtract vector by float value // Subtract vector by float value
RMAPI RMVector2 Vector2SubtractValue(RMVector2 v, float sub) RMAPI RLVector2 Vector2SubtractValue(RLVector2 v, float sub)
{ {
RMVector2 result = { v.x - sub, v.y - sub }; RLVector2 result = { v.x - sub, v.y - sub };
return result; return result;
} }
// Calculate vector length // Calculate vector length
RMAPI float Vector2Length(RMVector2 v) RMAPI float Vector2Length(RLVector2 v)
{ {
float result = sqrtf((v.x*v.x) + (v.y*v.y)); float result = sqrtf((v.x*v.x) + (v.y*v.y));
@@ -289,7 +289,7 @@ RMAPI float Vector2Length(RMVector2 v)
} }
// Calculate vector square length // Calculate vector square length
RMAPI float Vector2LengthSqr(RMVector2 v) RMAPI float Vector2LengthSqr(RLVector2 v)
{ {
float result = (v.x*v.x) + (v.y*v.y); float result = (v.x*v.x) + (v.y*v.y);
@@ -297,7 +297,7 @@ RMAPI float Vector2LengthSqr(RMVector2 v)
} }
// Calculate two vectors dot product // Calculate two vectors dot product
RMAPI float Vector2DotProduct(RMVector2 v1, RMVector2 v2) RMAPI float Vector2DotProduct(RLVector2 v1, RLVector2 v2)
{ {
float result = (v1.x*v2.x + v1.y*v2.y); float result = (v1.x*v2.x + v1.y*v2.y);
@@ -305,7 +305,7 @@ RMAPI float Vector2DotProduct(RMVector2 v1, RMVector2 v2)
} }
// Calculate two vectors cross product // Calculate two vectors cross product
RMAPI float Vector2CrossProduct(RMVector2 v1, RMVector2 v2) RMAPI float Vector2CrossProduct(RLVector2 v1, RLVector2 v2)
{ {
float result = (v1.x*v2.y - v1.y*v2.x); float result = (v1.x*v2.y - v1.y*v2.x);
@@ -313,7 +313,7 @@ RMAPI float Vector2CrossProduct(RMVector2 v1, RMVector2 v2)
} }
// Calculate distance between two vectors // Calculate distance between two vectors
RMAPI float Vector2Distance(RMVector2 v1, RMVector2 v2) RMAPI float Vector2Distance(RLVector2 v1, RLVector2 v2)
{ {
float result = sqrtf((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y)); float result = sqrtf((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y));
@@ -321,7 +321,7 @@ RMAPI float Vector2Distance(RMVector2 v1, RMVector2 v2)
} }
// Calculate square distance between two vectors // Calculate square distance between two vectors
RMAPI float Vector2DistanceSqr(RMVector2 v1, RMVector2 v2) RMAPI float Vector2DistanceSqr(RLVector2 v1, RLVector2 v2)
{ {
float result = ((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y)); float result = ((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y));
@@ -331,7 +331,7 @@ RMAPI float Vector2DistanceSqr(RMVector2 v1, RMVector2 v2)
// Calculate the signed angle from v1 to v2, relative to the origin (0, 0) // Calculate the signed angle from v1 to v2, relative to the origin (0, 0)
// NOTE: Coordinate system convention: positive X right, positive Y down // NOTE: Coordinate system convention: positive X right, positive Y down
// positive angles appear clockwise, and negative angles appear counterclockwise // positive angles appear clockwise, and negative angles appear counterclockwise
RMAPI float Vector2Angle(RMVector2 v1, RMVector2 v2) RMAPI float Vector2Angle(RLVector2 v1, RLVector2 v2)
{ {
float result = 0.0f; float result = 0.0f;
@@ -346,7 +346,7 @@ RMAPI float Vector2Angle(RMVector2 v1, RMVector2 v2)
// Calculate angle defined by a two vectors line // Calculate angle defined by a two vectors line
// NOTE: Parameters need to be normalized // NOTE: Parameters need to be normalized
// Current implementation should be aligned with glm::angle // Current implementation should be aligned with glm::angle
RMAPI float Vector2LineAngle(RMVector2 start, RMVector2 end) RMAPI float Vector2LineAngle(RLVector2 start, RLVector2 end)
{ {
float result = 0.0f; float result = 0.0f;
@@ -357,41 +357,41 @@ RMAPI float Vector2LineAngle(RMVector2 start, RMVector2 end)
} }
// Scale vector (multiply by value) // Scale vector (multiply by value)
RMAPI RMVector2 Vector2Scale(RMVector2 v, float scale) RMAPI RLVector2 Vector2Scale(RLVector2 v, float scale)
{ {
RMVector2 result = { v.x*scale, v.y*scale }; RLVector2 result = { v.x*scale, v.y*scale };
return result; return result;
} }
// Multiply vector by vector // Multiply vector by vector
RMAPI RMVector2 Vector2Multiply(RMVector2 v1, RMVector2 v2) RMAPI RLVector2 Vector2Multiply(RLVector2 v1, RLVector2 v2)
{ {
RMVector2 result = { v1.x*v2.x, v1.y*v2.y }; RLVector2 result = { v1.x*v2.x, v1.y*v2.y };
return result; return result;
} }
// Negate vector // Negate vector
RMAPI RMVector2 Vector2Negate(RMVector2 v) RMAPI RLVector2 Vector2Negate(RLVector2 v)
{ {
RMVector2 result = { -v.x, -v.y }; RLVector2 result = { -v.x, -v.y };
return result; return result;
} }
// Divide vector by vector // Divide vector by vector
RMAPI RMVector2 Vector2Divide(RMVector2 v1, RMVector2 v2) RMAPI RLVector2 Vector2Divide(RLVector2 v1, RLVector2 v2)
{ {
RMVector2 result = { v1.x/v2.x, v1.y/v2.y }; RLVector2 result = { v1.x/v2.x, v1.y/v2.y };
return result; return result;
} }
// Normalize provided vector // Normalize provided vector
RMAPI RMVector2 Vector2Normalize(RMVector2 v) RMAPI RLVector2 Vector2Normalize(RLVector2 v)
{ {
RMVector2 result = { 0 }; RLVector2 result = { 0 };
float length = sqrtf((v.x*v.x) + (v.y*v.y)); float length = sqrtf((v.x*v.x) + (v.y*v.y));
if (length > 0) if (length > 0)
@@ -405,9 +405,9 @@ RMAPI RMVector2 Vector2Normalize(RMVector2 v)
} }
// Transforms a Vector2 by a given Matrix // Transforms a Vector2 by a given Matrix
RMAPI RMVector2 Vector2Transform(RMVector2 v, Matrix mat) RMAPI RLVector2 Vector2Transform(RLVector2 v, Matrix mat)
{ {
RMVector2 result = { 0 }; RLVector2 result = { 0 };
float x = v.x; float x = v.x;
float y = v.y; float y = v.y;
@@ -420,9 +420,9 @@ RMAPI RMVector2 Vector2Transform(RMVector2 v, Matrix mat)
} }
// Calculate linear interpolation between two vectors // Calculate linear interpolation between two vectors
RMAPI RMVector2 Vector2Lerp(RMVector2 v1, RMVector2 v2, float amount) RMAPI RLVector2 Vector2Lerp(RLVector2 v1, RLVector2 v2, float amount)
{ {
RMVector2 result = { 0 }; RLVector2 result = { 0 };
result.x = v1.x + amount*(v2.x - v1.x); result.x = v1.x + amount*(v2.x - v1.x);
result.y = v1.y + amount*(v2.y - v1.y); result.y = v1.y + amount*(v2.y - v1.y);
@@ -431,9 +431,9 @@ RMAPI RMVector2 Vector2Lerp(RMVector2 v1, RMVector2 v2, float amount)
} }
// Calculate reflected vector to normal // Calculate reflected vector to normal
RMAPI RMVector2 Vector2Reflect(RMVector2 v, RMVector2 normal) RMAPI RLVector2 Vector2Reflect(RLVector2 v, RLVector2 normal)
{ {
RMVector2 result = { 0 }; RLVector2 result = { 0 };
float dotProduct = (v.x*normal.x + v.y*normal.y); // Dot product float dotProduct = (v.x*normal.x + v.y*normal.y); // Dot product
@@ -444,9 +444,9 @@ RMAPI RMVector2 Vector2Reflect(RMVector2 v, RMVector2 normal)
} }
// Get min value for each pair of components // Get min value for each pair of components
RMAPI RMVector2 Vector2Min(RMVector2 v1, RMVector2 v2) RMAPI RLVector2 Vector2Min(RLVector2 v1, RLVector2 v2)
{ {
RMVector2 result = { 0 }; RLVector2 result = { 0 };
result.x = fminf(v1.x, v2.x); result.x = fminf(v1.x, v2.x);
result.y = fminf(v1.y, v2.y); result.y = fminf(v1.y, v2.y);
@@ -455,9 +455,9 @@ RMAPI RMVector2 Vector2Min(RMVector2 v1, RMVector2 v2)
} }
// Get max value for each pair of components // Get max value for each pair of components
RMAPI RMVector2 Vector2Max(RMVector2 v1, RMVector2 v2) RMAPI RLVector2 Vector2Max(RLVector2 v1, RLVector2 v2)
{ {
RMVector2 result = { 0 }; RLVector2 result = { 0 };
result.x = fmaxf(v1.x, v2.x); result.x = fmaxf(v1.x, v2.x);
result.y = fmaxf(v1.y, v2.y); result.y = fmaxf(v1.y, v2.y);
@@ -466,9 +466,9 @@ RMAPI RMVector2 Vector2Max(RMVector2 v1, RMVector2 v2)
} }
// Rotate vector by angle // Rotate vector by angle
RMAPI RMVector2 Vector2Rotate(RMVector2 v, float angle) RMAPI RLVector2 Vector2Rotate(RLVector2 v, float angle)
{ {
RMVector2 result = { 0 }; RLVector2 result = { 0 };
float cosres = cosf(angle); float cosres = cosf(angle);
float sinres = sinf(angle); float sinres = sinf(angle);
@@ -480,9 +480,9 @@ RMAPI RMVector2 Vector2Rotate(RMVector2 v, float angle)
} }
// Move Vector towards target // Move Vector towards target
RMAPI RMVector2 Vector2MoveTowards(RMVector2 v, RMVector2 target, float maxDistance) RMAPI RLVector2 Vector2MoveTowards(RLVector2 v, RLVector2 target, float maxDistance)
{ {
RMVector2 result = { 0 }; RLVector2 result = { 0 };
float dx = target.x - v.x; float dx = target.x - v.x;
float dy = target.y - v.y; float dy = target.y - v.y;
@@ -499,18 +499,18 @@ RMAPI RMVector2 Vector2MoveTowards(RMVector2 v, RMVector2 target, float maxDista
} }
// Invert the given vector // Invert the given vector
RMAPI RMVector2 Vector2Invert(RMVector2 v) RMAPI RLVector2 Vector2Invert(RLVector2 v)
{ {
RMVector2 result = { 1.0f/v.x, 1.0f/v.y }; RLVector2 result = { 1.0f/v.x, 1.0f/v.y };
return result; return result;
} }
// Clamp the components of the vector between // Clamp the components of the vector between
// min and max values specified by the given vectors // min and max values specified by the given vectors
RMAPI RMVector2 Vector2Clamp(RMVector2 v, RMVector2 min, RMVector2 max) RMAPI RLVector2 Vector2Clamp(RLVector2 v, RLVector2 min, RLVector2 max)
{ {
RMVector2 result = { 0 }; RLVector2 result = { 0 };
result.x = fminf(max.x, fmaxf(min.x, v.x)); result.x = fminf(max.x, fmaxf(min.x, v.x));
result.y = fminf(max.y, fmaxf(min.y, v.y)); result.y = fminf(max.y, fmaxf(min.y, v.y));
@@ -519,9 +519,9 @@ RMAPI RMVector2 Vector2Clamp(RMVector2 v, RMVector2 min, RMVector2 max)
} }
// Clamp the magnitude of the vector between two min and max values // Clamp the magnitude of the vector between two min and max values
RMAPI RMVector2 Vector2ClampValue(RMVector2 v, float min, float max) RMAPI RLVector2 Vector2ClampValue(RLVector2 v, float min, float max)
{ {
RMVector2 result = v; RLVector2 result = v;
float length = (v.x*v.x) + (v.y*v.y); float length = (v.x*v.x) + (v.y*v.y);
if (length > 0.0f) if (length > 0.0f)
@@ -546,7 +546,7 @@ RMAPI RMVector2 Vector2ClampValue(RMVector2 v, float min, float max)
} }
// Check whether two given vectors are almost equal // Check whether two given vectors are almost equal
RMAPI int Vector2Equals(RMVector2 p, RMVector2 q) RMAPI int Vector2Equals(RLVector2 p, RLVector2 q)
{ {
#if !defined(EPSILON) #if !defined(EPSILON)
#define EPSILON 0.000001f #define EPSILON 0.000001f
@@ -563,9 +563,9 @@ RMAPI int Vector2Equals(RMVector2 p, RMVector2 q)
// n: normalized normal vector of the interface of two optical media // n: normalized normal vector of the interface of two optical media
// r: ratio of the refractive index of the medium from where the ray comes // r: ratio of the refractive index of the medium from where the ray comes
// to the refractive index of the medium on the other side of the surface // to the refractive index of the medium on the other side of the surface
RMAPI RMVector2 Vector2Refract(RMVector2 v, RMVector2 n, float r) RMAPI RLVector2 Vector2Refract(RLVector2 v, RLVector2 n, float r)
{ {
RMVector2 result = { 0 }; RLVector2 result = { 0 };
float dot = v.x*n.x + v.y*n.y; float dot = v.x*n.x + v.y*n.y;
float d = 1.0f - r*r*(1.0f - dot*dot); float d = 1.0f - r*r*(1.0f - dot*dot);

View File

@@ -1,24 +1,26 @@
// Library initialisation
#define RAYMATH_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#define TINYOBJ_LOADER_C_IMPLEMENTATION
// Project // Project
#include "SomaSolve.cpp" #include "SomaSolve.cpp"
#include "gfx/gfx.cpp" #include "gfx/gfx.cpp"
#include "world/world.cpp" #include "world/world.cpp"
#include "VoxelSpace.cpp" #include "VoxelSpace.cpp"
#include "./tests.cpp" #include "./tests.cpp"
#include "lib/djstdlib/core.cpp" #include "lib/djstdlib/core.c"
// Graphics bindings and libs // Graphics bindings and libs
#include "lib/glad/glad.c"
#include <GLFW/glfw3.h>
// Library initialisation
#define STB_IMAGE_IMPLEMENTATION
#include "lib/loaders/stb_image.h"
#define TINYOBJ_LOADER_C_IMPLEMENTATION
#include "lib/loaders/tinyobj.h"
#define RAYMATH_IMPLEMENTATION
#include "lib/raymath.h" #include "lib/raymath.h"
#include "lib/glad/glad.c"
#include "GLFW/glfw3.h"
void print(RLVector3* vector) { #ifdef inline
#undef inline
#endif
void printRLVec3(RLVector3* vector) {
RLVector3 vec = *vector; RLVector3 vec = *vector;
print( print(
"┌ ┐\n" "┌ ┐\n"
@@ -27,7 +29,7 @@ void print(RLVector3* vector) {
vec.x, vec.y, vec.z); vec.x, vec.y, vec.z);
} }
void print(Matrix* matrix) { void printMatrix(Matrix* matrix) {
Matrix mat = *matrix; Matrix mat = *matrix;
print( print(
"┌ ┐\n" "┌ ┐\n"
@@ -42,15 +44,15 @@ void print(Matrix* matrix) {
mat.m12, mat.m13, mat.m14, mat.m15); mat.m12, mat.m13, mat.m14, mat.m15);
} }
struct Camera { typedef struct {
Matrix view; Matrix view;
Matrix proj; Matrix proj;
RLVector3 pos; RLVector3 pos;
RLVector3 up; RLVector3 up;
RLVector3 target; RLVector3 target;
}; } Camera;
Camera *createCamera(Arena *arena, real32 aspect_ratio = 800.0f / 600.0f) { Camera *createCamera(Arena *arena, real32 aspect_ratio) {
Camera *result = PushStruct(arena, Camera); Camera *result = PushStruct(arena, Camera);
result->view = (Matrix){0}; result->view = (Matrix){0};
result->proj = MatrixPerspective(DEG2RAD * 45.0f, aspect_ratio, 0.1f, 100.0f); result->proj = MatrixPerspective(DEG2RAD * 45.0f, aspect_ratio, 0.1f, 100.0f);
@@ -68,44 +70,49 @@ void cameraSetUp(Camera *c, real32 up_x, real32 up_y, real32 up_z) {
c->up = (RLVector3){up_x, up_y, up_z}; c->up = (RLVector3){up_x, up_y, up_z};
} }
struct Frame { typedef struct {
uint32 width; uint32 width;
uint32 height; uint32 height;
int32 x; int32 x;
int32 y; int32 y;
Camera* cam; Camera* cam;
}; } Frame;
struct Polycube { typedef struct {
uint32 entityHandle; uint32 entityHandle;
Space repr; Space repr;
Vector4<real32> color; RLVector4 color;
}; } Polycube;
DefineList(Polycube, Polycube);
struct RenderObjects_Rectangle { DefineList(RLVector2, RLVec2);
DefineList(RLVector4, RLVec4);
DefineList(real32, Float);
typedef struct {
uint32 vao; uint32 vao;
uint64 count; uint64 count;
list<Vector2<real32>> p0; RLVec2List p0;
uint32 p0BufferId; uint32 p0BufferId;
list<Vector2<real32>> p1; RLVec2List p1;
uint32 p1BufferId; uint32 p1BufferId;
list<Vector4<real32>> color; RLVec4List color;
uint32 colorBufferId; uint32 colorBufferId;
list<real32> borderRadius; FloatList borderRadius;
uint32 borderRadiusBufferId; uint32 borderRadiusBufferId;
list<real32> borderThickness; FloatList borderThickness;
uint32 borderThicknessBufferId; uint32 borderThicknessBufferId;
list<real32> edgeSoftness; FloatList edgeSoftness;
uint32 edgeSoftnessBufferId; uint32 edgeSoftnessBufferId;
}; } RenderObjects_Rectangle;
struct Input { typedef struct {
struct { struct {
bool escape; bool escape;
bool enter; bool enter;
@@ -121,38 +128,39 @@ struct Input {
real32 x; real32 x;
real32 y; real32 y;
}; };
Vector2<real32> point; RLVector2 point;
}; };
bool btnLeft; bool btnLeft;
bool btnRight; bool btnRight;
bool btnMiddle; bool btnMiddle;
} mouse; } mouse;
}; } Input;
struct Renderer { typedef struct {
Scene *scene; Scene *scene;
RenderObjects_Rectangle rects; RenderObjects_Rectangle rects;
}; } Renderer;
struct PolycubeInput { typedef struct {
Space repr; Space repr;
Vector4<real32> color; RLVector4 color;
}; } PolycubeInput;
DefineList(PolycubeInput, PolycubeInput);
struct SomaState { typedef struct {
bool wireframe; bool wireframe;
bool polycubeDirty; bool polycubeDirty;
uint32 currentPolycube; uint32 currentPolycube;
uint32 lastPolycubeVisible; uint32 lastPolycubeVisible;
uint32 light; uint32 light;
list<Polycube> polycubes; PolycubeList polycubes;
Camera* camera; Camera* camera;
RLVector3 rotAxisX; RLVector3 rotAxisX;
RLVector3 rotAxisY; RLVector3 rotAxisY;
list<PolycubeInput> polycubeInput; PolycubeInputList polycubeInput;
}; } SomaState;
struct Soma { typedef struct {
Scene *scene; Scene *scene;
Renderer *renderer; Renderer *renderer;
SomaState state; SomaState state;
@@ -163,7 +171,7 @@ struct Soma {
uint32 width; uint32 width;
uint32 height; uint32 height;
} window; } window;
}; } Soma;
void showEntity(Scene *scene, uint32 entityHandle) { void showEntity(Scene *scene, uint32 entityHandle) {
SceneGraphNode *node = getSceneGraphNodeForEntity(scene, entityHandle); SceneGraphNode *node = getSceneGraphNodeForEntity(scene, entityHandle);
@@ -189,12 +197,12 @@ void hideEntity(Scene *scene, uint32 entityHandle) {
RLVector3 centreFromPolycube(Scene *scene, Polycube *p) { RLVector3 centreFromPolycube(Scene *scene, Polycube *p) {
RLVector3 centre = (RLVector3){0,0,0}; RLVector3 centre = (RLVector3){0,0,0};
list<uint32> *children = &getSceneGraphNode(scene, p->entityHandle)->children; HandleList *children = &getSceneGraphNode(scene, p->entityHandle)->children;
for (EachIn(*children, i)) { for (EachIn(*children, i)) {
uint32 child = children->data[i]; uint32 child = children->data[i];
centre = Vector3Add(centre, getSceneGraphNode(scene, child)->translation); centre = Vector3Add(centre, getSceneGraphNode(scene, child)->translation);
} }
centre = Vector3Scale(centre, 1.0f/(getSceneGraphNodeForEntity(scene, p->entityHandle)->children.head)); centre = Vector3Scale(centre, 1.0f/(getSceneGraphNodeForEntity(scene, p->entityHandle)->children.length));
return centre; return centre;
} }
@@ -242,21 +250,21 @@ void updateViewportFromFrame(uint32 windowWidth, uint32 windowHeight, Frame* fra
glViewport(frame->x, windowHeight - frame->y - frame->height, frame->width, frame->height); glViewport(frame->x, windowHeight - frame->y - frame->height, frame->width, frame->height);
} }
struct UI_Context { typedef struct {
Renderer *renderer; Renderer *renderer;
Input *prevInput; Input *prevInput;
Input *input; Input *input;
bool cursorIsPointer; bool cursorIsPointer;
}; } UI_Context;
struct UI_Rect { typedef struct {
real32 x; real32 x;
real32 y; real32 y;
real32 width; real32 width;
real32 height; real32 height;
real32 borderRadius; real32 borderRadius;
Vector4<real32> color; RLVector4 color;
}; } UI_Rect;
Mesh cubeMesh = {0}; Mesh cubeMesh = {0};
Texture wallTex = {0}; Texture wallTex = {0};
@@ -264,7 +272,7 @@ Texture wallTex = {0};
Shader solidColorShader; Shader solidColorShader;
Shader phongShader; Shader phongShader;
inline bool glfwMouse(GLFWwindow *window, int mouseBtnCode) { bool glfwMouse(GLFWwindow *window, int mouseBtnCode) {
switch (glfwGetMouseButton(window, mouseBtnCode)) { switch (glfwGetMouseButton(window, mouseBtnCode)) {
case GLFW_RELEASE: return false; case GLFW_RELEASE: return false;
case GLFW_PRESS: return true; case GLFW_PRESS: return true;
@@ -272,7 +280,7 @@ inline bool glfwMouse(GLFWwindow *window, int mouseBtnCode) {
} }
} }
inline bool glfwKey(GLFWwindow *window, int keyCode) { bool glfwKey(GLFWwindow *window, int keyCode) {
switch (glfwGetKey(window, keyCode)) { switch (glfwGetKey(window, keyCode)) {
case GLFW_RELEASE: return false; case GLFW_RELEASE: return false;
case GLFW_PRESS: return true; case GLFW_PRESS: return true;
@@ -298,7 +306,7 @@ Input getCurrentInput(GLFWwindow *window) {
real64 mouseX; real64 mouseX;
real64 mouseY; real64 mouseY;
glfwGetCursorPos(window, &mouseX, &mouseY); glfwGetCursorPos(window, &mouseX, &mouseY);
input.mouse.point = vec2<real32>((real32)mouseX, (real32)mouseY); input.mouse.point = (RLVector2){(real32)mouseX, (real32)mouseY};
return input; return input;
} }
@@ -347,7 +355,7 @@ void processInput(Soma *soma) {
} }
} }
Polycube createPolycubeFromRepr(Arena *arena, Scene *s, Space *repr, Vector4<real32> color) { Polycube createPolycubeFromRepr(Arena *arena, Scene *s, Space *repr, RLVector4 color) {
uint32 polycubeMainEntityHandle = createEntity(arena, s); uint32 polycubeMainEntityHandle = createEntity(arena, s);
Entity *polycubeMainEntity = getEntity(s, polycubeMainEntityHandle); Entity *polycubeMainEntity = getEntity(s, polycubeMainEntityHandle);
for (int x = 0; x < repr->dim_x; x++) { for (int x = 0; x < repr->dim_x; x++) {
@@ -359,7 +367,7 @@ Polycube createPolycubeFromRepr(Arena *arena, Scene *s, Space *repr, Vector4<rea
polycubeSegment->mesh = &cubeMesh; polycubeSegment->mesh = &cubeMesh;
polycubeSegment->tex = &wallTex; polycubeSegment->tex = &wallTex;
SceneGraphNode *graphNode = getSceneGraphNode(s, polycubeSegment->graphNodeHandle); SceneGraphNode *graphNode = getSceneGraphNode(s, polycubeSegment->graphNodeHandle);
graphNode->translation = RLVector3{ graphNode->translation = (RLVector3){
-((repr->dim_z - 1)/2.0f) + z, -((repr->dim_z - 1)/2.0f) + z,
-((repr->dim_x - 1)/2.0f) + x, -((repr->dim_x - 1)/2.0f) + x,
((repr->dim_y - 1)/2.0f) - y ((repr->dim_y - 1)/2.0f) - y
@@ -383,12 +391,12 @@ RenderObjects_Rectangle createRectangleObjects(Arena *arena, size_t count) {
RenderObjects_Rectangle result = {0}; RenderObjects_Rectangle result = {0};
result.count = count; result.count = count;
result.p0 = PushFullList(arena, Vector2<real32>, count); result.p0 = PushFullList(arena, RLVec2List, count);
result.p1 = PushFullList(arena, Vector2<real32>, count); result.p1 = PushFullList(arena, RLVec2List, count);
result.color = PushFullList(arena, Vector4<real32>, count); result.color = PushFullList(arena, RLVec4List, count);
result.borderRadius = PushFullList(arena, real32, count); result.borderRadius = PushFullList(arena, FloatList, count);
result.borderThickness = PushFullList(arena, real32, count); result.borderThickness = PushFullList(arena, FloatList, count);
result.edgeSoftness = PushFullList(arena, real32, count); result.edgeSoftness = PushFullList(arena, FloatList, count);
glGenVertexArrays(1, &result.vao); glGenVertexArrays(1, &result.vao);
@@ -402,38 +410,38 @@ RenderObjects_Rectangle createRectangleObjects(Arena *arena, size_t count) {
glBindVertexArray(result.vao); glBindVertexArray(result.vao);
glBindBuffer(GL_ARRAY_BUFFER, result.p0BufferId); glBindBuffer(GL_ARRAY_BUFFER, result.p0BufferId);
glBufferData(GL_ARRAY_BUFFER, result.p0.length * sizeof(Vector2<real32>), 0, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, result.p0.length * sizeof(RLVector2), 0, GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2<real32>), (void*)0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(RLVector2), (void*)0);
glVertexAttribDivisor(0, 1); glVertexAttribDivisor(0, 1);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, result.p1BufferId); glBindBuffer(GL_ARRAY_BUFFER, result.p1BufferId);
glBufferData(GL_ARRAY_BUFFER, result.p1.length * sizeof(Vector2<real32>), 0, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, result.p1.length * sizeof(RLVector2), 0, GL_DYNAMIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2<real32>), (void*)0); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(RLVector2), (void*)0);
glVertexAttribDivisor(1, 1); glVertexAttribDivisor(1, 1);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, result.colorBufferId); glBindBuffer(GL_ARRAY_BUFFER, result.colorBufferId);
glBufferData(GL_ARRAY_BUFFER, result.color.length * sizeof(Vector4<real32>), 0, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, result.color.length * sizeof(RLVector4), 0, GL_DYNAMIC_DRAW);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Vector4<real32>), (void*)0); glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(RLVector4), (void*)0);
glVertexAttribDivisor(2, 1); glVertexAttribDivisor(2, 1);
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, result.borderRadiusBufferId); glBindBuffer(GL_ARRAY_BUFFER, result.borderRadiusBufferId);
glBufferData(GL_ARRAY_BUFFER, result.borderRadius.length * sizeof(real32), 0, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, result.borderRadius.length * sizeof(FloatList_underlying), 0, GL_DYNAMIC_DRAW);
glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, sizeof(real32), (void*)0); glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, sizeof(FloatList_underlying), (void*)0);
glVertexAttribDivisor(3, 1); glVertexAttribDivisor(3, 1);
glEnableVertexAttribArray(3); glEnableVertexAttribArray(3);
glBindBuffer(GL_ARRAY_BUFFER, result.borderThicknessBufferId); glBindBuffer(GL_ARRAY_BUFFER, result.borderThicknessBufferId);
glBufferData(GL_ARRAY_BUFFER, result.borderThickness.length * sizeof(real32), 0, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, result.borderThickness.length * sizeof(FloatList_underlying), 0, GL_DYNAMIC_DRAW);
glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, sizeof(real32), (void*)0); glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, sizeof(FloatList_underlying), (void*)0);
glVertexAttribDivisor(4, 1); glVertexAttribDivisor(4, 1);
glEnableVertexAttribArray(4); glEnableVertexAttribArray(4);
glBindBuffer(GL_ARRAY_BUFFER, result.edgeSoftnessBufferId); glBindBuffer(GL_ARRAY_BUFFER, result.edgeSoftnessBufferId);
glBufferData(GL_ARRAY_BUFFER, result.edgeSoftness.length * sizeof(real32), 0, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, result.edgeSoftness.length * sizeof(FloatList_underlying), 0, GL_DYNAMIC_DRAW);
glVertexAttribPointer(5, 1, GL_FLOAT, GL_FALSE, sizeof(real32), (void*)0); glVertexAttribPointer(5, 1, GL_FLOAT, GL_FALSE, sizeof(FloatList_underlying), (void*)0);
glVertexAttribDivisor(5, 1); glVertexAttribDivisor(5, 1);
glEnableVertexAttribArray(5); glEnableVertexAttribArray(5);
@@ -444,22 +452,22 @@ void reinitRectangleObjectBuffers(Renderer *r) {
glBindVertexArray(r->rects.vao); glBindVertexArray(r->rects.vao);
glBindBuffer(GL_ARRAY_BUFFER, r->rects.p0BufferId); glBindBuffer(GL_ARRAY_BUFFER, r->rects.p0BufferId);
glBufferSubData(GL_ARRAY_BUFFER, 0, r->rects.p0.head * sizeof(Vector2<real32>), r->rects.p0.data); glBufferSubData(GL_ARRAY_BUFFER, 0, r->rects.p0.length * sizeof(RLVec2List), r->rects.p0.data);
glBindBuffer(GL_ARRAY_BUFFER, r->rects.p1BufferId); glBindBuffer(GL_ARRAY_BUFFER, r->rects.p1BufferId);
glBufferSubData(GL_ARRAY_BUFFER, 0, r->rects.p1.head * sizeof(Vector2<real32>), r->rects.p1.data); glBufferSubData(GL_ARRAY_BUFFER, 0, r->rects.p1.length * sizeof(RLVec2List), r->rects.p1.data);
glBindBuffer(GL_ARRAY_BUFFER, r->rects.colorBufferId); glBindBuffer(GL_ARRAY_BUFFER, r->rects.colorBufferId);
glBufferSubData(GL_ARRAY_BUFFER, 0, r->rects.color.head * sizeof(Vector4<real32>), r->rects.color.data); glBufferSubData(GL_ARRAY_BUFFER, 0, r->rects.color.length * sizeof(RLVec4List), r->rects.color.data);
glBindBuffer(GL_ARRAY_BUFFER, r->rects.borderRadiusBufferId); glBindBuffer(GL_ARRAY_BUFFER, r->rects.borderRadiusBufferId);
glBufferSubData(GL_ARRAY_BUFFER, 0, r->rects.borderRadius.head * sizeof(real32), r->rects.borderRadius.data); glBufferSubData(GL_ARRAY_BUFFER, 0, r->rects.borderRadius.length * sizeof(FloatList), r->rects.borderRadius.data);
glBindBuffer(GL_ARRAY_BUFFER, r->rects.borderThicknessBufferId); glBindBuffer(GL_ARRAY_BUFFER, r->rects.borderThicknessBufferId);
glBufferSubData(GL_ARRAY_BUFFER, 0, r->rects.borderThickness.head * sizeof(real32), r->rects.borderThickness.data); glBufferSubData(GL_ARRAY_BUFFER, 0, r->rects.borderThickness.length * sizeof(FloatList), r->rects.borderThickness.data);
glBindBuffer(GL_ARRAY_BUFFER, r->rects.edgeSoftnessBufferId); glBindBuffer(GL_ARRAY_BUFFER, r->rects.edgeSoftnessBufferId);
glBufferSubData(GL_ARRAY_BUFFER, 0, r->rects.edgeSoftness.head * sizeof(real32), r->rects.edgeSoftness.data); glBufferSubData(GL_ARRAY_BUFFER, 0, r->rects.edgeSoftness.length * sizeof(FloatList), r->rects.edgeSoftness.data);
} }
Renderer createRenderer(Arena *arena, Scene *scene) { Renderer createRenderer(Arena *arena, Scene *scene) {
@@ -472,12 +480,12 @@ Renderer createRenderer(Arena *arena, Scene *scene) {
} }
void renderBegin(Renderer *r) { void renderBegin(Renderer *r) {
r->rects.p0.head = 0; r->rects.p0.length = 0;
r->rects.p1.head = 0; r->rects.p1.length = 0;
r->rects.color.head = 0; r->rects.color.length = 0;
r->rects.borderRadius.head = 0; r->rects.borderRadius.length = 0;
r->rects.borderThickness.head = 0; r->rects.borderThickness.length = 0;
r->rects.edgeSoftness.head = 0; r->rects.edgeSoftness.length = 0;
} }
void renderEnd(Soma *soma, Renderer *renderer) { void renderEnd(Soma *soma, Renderer *renderer) {
@@ -502,7 +510,7 @@ void renderEnd(Soma *soma, Renderer *renderer) {
for (EachIn(renderer->scene->entities, i)) { for (EachIn(renderer->scene->entities, i)) {
Entity *entity = &renderer->scene->entities.data[i]; Entity *entity = &renderer->scene->entities.data[i];
if (entity->flags & EntityFlags_Render && entity->flags & EntityFlags_Visible) { if (entity->flags & EntityFlags_Render && entity->flags & EntityFlags_Visible) {
setUniformMat4fv(model_uniform, &getSceneGraphNode(renderer->scene, entity->graphNodeHandle)->world); setUniformMat4fvByLoc(model_uniform, &getSceneGraphNode(renderer->scene, entity->graphNodeHandle)->world);
glBindTexture(GL_TEXTURE_2D, entity->tex->tex_id); glBindTexture(GL_TEXTURE_2D, entity->tex->tex_id);
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)entity->mesh->num_indices); glDrawArrays(GL_TRIANGLES, 0, (GLsizei)entity->mesh->num_indices);
entity->flags &= ~EntityFlags_Render; entity->flags &= ~EntityFlags_Render;
@@ -518,19 +526,21 @@ void renderEnd(Soma *soma, Renderer *renderer) {
setUniformMat4fv(&solidColorShader, "projection", &ortho); setUniformMat4fv(&solidColorShader, "projection", &ortho);
glBindVertexArray(renderer->rects.vao); glBindVertexArray(renderer->rects.vao);
glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, renderer->rects.p0.head); glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, renderer->rects.p0.length);
} }
void rendererPlaceRectangle(Renderer *r, real32 x, real32 y, real32 width, real32 height, Vector4<real32> color, real32 borderRadius, real32 borderThickness) { void rendererPlaceRectangle(Renderer *r, real32 x, real32 y, real32 width, real32 height, RLVector4 color, real32 borderRadius, real32 borderThickness) {
appendList(&r->rects.p0, vec2<real32>(x, y)); RLVector2 p0 = {x, y};
appendList(&r->rects.p1, vec2<real32>(x + width, y + height)); AppendList(&r->rects.p0, p0);
appendList(&r->rects.color, color); RLVector2 p1 = {x + width, y + height};
appendList(&r->rects.borderRadius, borderRadius); AppendList(&r->rects.p1, p1);
appendList(&r->rects.borderThickness, borderThickness); AppendList(&r->rects.color, color);
appendList(&r->rects.edgeSoftness, 0.0f); AppendList(&r->rects.borderRadius, borderRadius);
AppendList(&r->rects.borderThickness, borderThickness);
AppendList(&r->rects.edgeSoftness, 0.0f);
} }
inline bool pointInRect(Vector2<real32> point, UI_Rect rect) { bool pointInRect(RLVector2 point, UI_Rect rect) {
return point.x > rect.x && point.y > rect.y && point.x < (rect.x + rect.width) && point.y < (rect.y + rect.height); return point.x > rect.x && point.y > rect.y && point.x < (rect.x + rect.width) && point.y < (rect.y + rect.height);
} }
@@ -622,13 +632,13 @@ int mainGfx() {
soma.state.currentPolycube = 0; soma.state.currentPolycube = 0;
soma.state.lastPolycubeVisible = 6; soma.state.lastPolycubeVisible = 6;
soma.state.polycubeInput = PushListZero(arena, PolycubeInput, 64); soma.state.polycubeInput = PushListZero(arena, PolycubeInputList, 64);
soma.state.polycubes = PushListZero(arena, Polycube, 64); soma.state.polycubes = PushListZero(arena, PolycubeList, 64);
soma.state.light = createEntity(arena, &mainScene); soma.state.light = createEntity(arena, &mainScene);
soma.state.camera = mainFrame.cam; soma.state.camera = mainFrame.cam;
SceneGraphNode *light = getSceneGraphNode(&mainScene, getEntity(&mainScene, soma.state.light)->graphNodeHandle); SceneGraphNode *light = getSceneGraphNode(&mainScene, getEntity(&mainScene, soma.state.light)->graphNodeHandle);
light->translation = RLVector3{4.0f, 6.0f, 24.0f}; light->translation = (RLVector3){4.0f, 6.0f, 24.0f};
/* /*
Shader solid_texture_shader = createShader( Shader solid_texture_shader = createShader(
@@ -637,24 +647,25 @@ int mainGfx() {
*/ */
solidColorShader = createShader( solidColorShader = createShader(
"./assets/shaders/2d-solid.vertex.glsl"_s, s("./assets/shaders/2d-solid.vertex.glsl"),
"./assets/shaders/2d-solid.fragment.glsl"_s); s("./assets/shaders/2d-solid.fragment.glsl"));
phongShader = createShader( phongShader = createShader(
"./assets/shaders/phong-solid.vertex.glsl"_s, s("./assets/shaders/phong-solid.vertex.glsl"),
"./assets/shaders/phong-solid.fragment.glsl"_s); s("./assets/shaders/phong-solid.fragment.glsl"));
cubeMesh = createMesh("./assets/models/cube.obj"); cubeMesh = createMesh("./assets/models/cube.obj");
wallTex = createTexture("./assets/textures/brick-wall.jpg"); wallTex = createTexture("./assets/textures/brick-wall.jpg");
for (EachInArray(STD_SOMA, i)) { for (EachInArray(STD_SOMA, i)) {
Space voxelSpace = { STD_SOMA[i], 3, 3, 3 }; Space voxelSpace = { STD_SOMA[i], 3, 3, 3 };
Vector4<real32> color = colorFromIndex(i); RLVector4 color = colorFromIndex(i);
appendList(&soma.state.polycubeInput, PolycubeInput{ voxelSpace, color }); PolycubeInput input = (PolycubeInput){ voxelSpace, color };
AppendList(&soma.state.polycubeInput, input);
cullEmptySpace(&voxelSpace); cullEmptySpace(&voxelSpace);
Polycube polycube = createPolycubeFromRepr(arena, soma.scene, &voxelSpace, color); Polycube polycube = createPolycubeFromRepr(arena, soma.scene, &voxelSpace, color);
polycube.color = color; polycube.color = color;
appendList(&soma.state.polycubes, polycube); AppendList(&soma.state.polycubes, polycube);
sceneNodeAddEntity(renderer.scene, renderer.scene->sceneRoot, polycube.entityHandle); sceneNodeAddEntity(renderer.scene, renderer.scene->sceneRoot, polycube.entityHandle);
} }
@@ -663,7 +674,7 @@ int mainGfx() {
SceneGraphNode *reference_polycube_gn = getSceneGraphNodeForEntity(soma.scene, soma.state.polycubes.data[0].entityHandle); SceneGraphNode *reference_polycube_gn = getSceneGraphNodeForEntity(soma.scene, soma.state.polycubes.data[0].entityHandle);
Matrix worldInverse = MatrixInvert(reference_polycube_gn->world); Matrix worldInverse = MatrixInvert(reference_polycube_gn->world);
soma.state.rotAxisY = Vector3Normalize(RLVector3{worldInverse.m4, worldInverse.m5, worldInverse.m6}); soma.state.rotAxisY = Vector3Normalize((RLVector3){worldInverse.m4, worldInverse.m5, worldInverse.m6});
RLVector3 eyes = Vector3Normalize(Vector3Subtract(soma.state.camera->pos, reference_polycube_gn->translation)); RLVector3 eyes = Vector3Normalize(Vector3Subtract(soma.state.camera->pos, reference_polycube_gn->translation));
soma.state.rotAxisX = Vector3Normalize(Vector3CrossProduct(eyes, soma.state.rotAxisY)); soma.state.rotAxisX = Vector3Normalize(Vector3CrossProduct(eyes, soma.state.rotAxisY));
@@ -730,7 +741,8 @@ int mainGfx() {
} }
int main() { int main() {
initialiseCore(); initialiseDjStdCore();
return mainGfx(); test();
return mainCmd();
} }

View File

@@ -6,6 +6,7 @@ typedef struct MismatchData {
uint64 actual; uint64 actual;
uint64 expected; uint64 expected;
} MismatchData; } MismatchData;
DefineList(MismatchData, MismatchData);
void test() { void test() {
{ {
@@ -72,7 +73,7 @@ void test() {
space1.dim_y=3; space1.dim_y=3;
space1.dim_z=3; space1.dim_z=3;
list<Space> rotations = getUniqueRotations(arena, &space1); VoxelSpaceList rotations = getUniqueRotations(arena, &space1);
Space expected_rots[] = { Space expected_rots[] = {
{ 30ull, 1, 2, 3 }, { 30ull, 1, 2, 3 },
{ 45ull, 1, 3, 2 }, { 45ull, 1, 3, 2 },
@@ -109,7 +110,7 @@ void test() {
space1.dim_z=3; space1.dim_z=3;
int prism_dims[] = { 3, 3, 3 }; int prism_dims[] = { 3, 3, 3 };
list<uint64> perms = getAllPermutationsInPrism(arena, &space1, prism_dims); VoxelSpaceReprList perms = getAllPermutationsInPrism(arena, &space1, prism_dims);
uint64 expected_perms[] = { uint64 expected_perms[] = {
30ull, 30ull,
240ull, 240ull,
@@ -276,41 +277,44 @@ void test() {
31457280ull, 31457280ull,
}; };
list<uint64> positions1 = getAllPositionsInPrism(arena, &space1, dims); VoxelSpaceReprList positions1 = getAllPositionsInPrism(arena, &space1, dims);
list<uint64> positions2 = getAllPositionsInPrism(arena, &space2, dims); VoxelSpaceReprList positions2 = getAllPositionsInPrism(arena, &space2, dims);
list<uint64> positions3 = getAllPositionsInPrism(arena, &space3, dims); VoxelSpaceReprList positions3 = getAllPositionsInPrism(arena, &space3, dims);
list<MismatchData> mismatches1 = PushList(arena, MismatchData, 6); MismatchDataList mismatches1 = PushList(arena, MismatchDataList, 6);
list<MismatchData> mismatches2 = PushList(arena, MismatchData, 6); MismatchDataList mismatches2 = PushList(arena, MismatchDataList, 6);
list<MismatchData> mismatches3 = PushList(arena, MismatchData, 6); MismatchDataList mismatches3 = PushList(arena, MismatchDataList, 6);
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
if (positions1.data[i] != expected_results1[i]) { if (positions1.data[i] != expected_results1[i]) {
appendList(&mismatches1, { i, positions1.data[i], expected_results1[i] }); MismatchData data = { i, positions1.data[i], expected_results1[i] };
AppendList(&mismatches1, data);
} }
if (positions2.data[i] != expected_results2[i]) { if (positions2.data[i] != expected_results2[i]) {
appendList(&mismatches2, { i, positions2.data[i], expected_results2[i] }); MismatchData data = { i, positions2.data[i], expected_results2[i] };
AppendList(&mismatches2, data);
} }
if (positions3.data[i] != expected_results3[i]) { if (positions3.data[i] != expected_results3[i]) {
appendList(&mismatches3, { i, positions3.data[i], expected_results3[i] }); MismatchData data = { i, positions3.data[i], expected_results3[i] };
AppendList(&mismatches3, data);
} }
} }
Assert(mismatches1.head == 0); Assert(mismatches1.length == 0);
if (mismatches1.head > 0) { if (mismatches1.length > 0) {
print("Index - Actual - Expected\n"); print("Index - Actual - Expected\n");
for (EachIn(mismatches1, i)) { for (EachIn(mismatches1, i)) {
print("%zu - %zu - %zu\n", mismatches1.data[i].i, mismatches1.data[i].actual, mismatches1.data[i].expected); print("%zu - %zu - %zu\n", mismatches1.data[i].i, mismatches1.data[i].actual, mismatches1.data[i].expected);
} }
} }
Assert(mismatches2.head == 0); Assert(mismatches2.length == 0);
if (mismatches2.head > 0) { if (mismatches2.length > 0) {
print("Index - Actual - Expected\n"); print("Index - Actual - Expected\n");
for (EachIn(mismatches2, i)) { for (EachIn(mismatches2, i)) {
print("%zu - %zu - %zu\n", mismatches2.data[i].i, mismatches2.data[i].actual, mismatches2.data[i].expected); print("%zu - %zu - %zu\n", mismatches2.data[i].i, mismatches2.data[i].actual, mismatches2.data[i].expected);
} }
} }
Assert(mismatches3.head == 0); Assert(mismatches3.length == 0);
if (mismatches3.head > 0) { if (mismatches3.length > 0) {
print("Index - Actual - Expected\n"); print("Index - Actual - Expected\n");
for (EachIn(mismatches3, i)) { for (EachIn(mismatches3, i)) {
print("%zu - %zu - %zu\n", mismatches3.data[i].i, mismatches3.data[i].actual, mismatches3.data[i].expected); print("%zu - %zu - %zu\n", mismatches3.data[i].i, mismatches3.data[i].actual, mismatches3.data[i].expected);

View File

@@ -1,4 +1,4 @@
#include <cstring> #include "string.h"
#include "scene.h" #include "scene.h"
Entity *getEntity(Scene *s, uint32 id) { Entity *getEntity(Scene *s, uint32 id) {
@@ -14,26 +14,26 @@ SceneGraphNode *getSceneGraphNode(Scene *s, uint32 sceneGraphNodeHandle) {
} }
uint32 createSceneGraphNode(Arena *arena, Scene *s) { uint32 createSceneGraphNode(Arena *arena, Scene *s) {
appendList(&s->graphNodes, (SceneGraphNode){0}); AppendList(&s->graphNodes, (SceneGraphNode){0});
s->graphNodes.data[s->graphNodes.head - 1].children = PushList(arena, uint32, 64); s->graphNodes.data[s->graphNodes.length - 1].children = PushList(arena, HandleList, 64);
return (uint32)s->graphNodes.head; return (uint32)s->graphNodes.length;
} }
uint32 createEntity(Arena *arena, Scene *s) { uint32 createEntity(Arena *arena, Scene *s) {
appendList(&s->entities, (Entity){0}); AppendList(&s->entities, (Entity){0});
uint32 graphNodeId = createSceneGraphNode(arena, s); uint32 graphNodeId = createSceneGraphNode(arena, s);
s->entities.data[s->entities.head - 1].graphNodeHandle = graphNodeId; s->entities.data[s->entities.length - 1].graphNodeHandle = graphNodeId;
getSceneGraphNode(s, graphNodeId)->entityHandle = (uint32)s->entities.head; getSceneGraphNode(s, graphNodeId)->entityHandle = (uint32)s->entities.length;
uint32 handle = (uint32)s->entities.head; uint32 handle = (uint32)s->entities.length;
uint32 graphNodeHandle = (uint32)s->graphNodes.head; uint32 graphNodeHandle = (uint32)s->graphNodes.length;
initGraphNode(getSceneGraphNode(s, graphNodeHandle)); initGraphNode(getSceneGraphNode(s, graphNodeHandle));
return handle; return handle;
} }
void initGraphNode(SceneGraphNode *n) { void initGraphNode(SceneGraphNode *n) {
n->scale = RLVector3{1.0f, 1.0f, 1.0f}; n->scale = (RLVector3){1.0f, 1.0f, 1.0f};
n->translation = RLVector3{0.0f, 0.0f, 0.0f}; n->translation = (RLVector3){0.0f, 0.0f, 0.0f};
n->rotation = Quaternion{1.0f, 0.0f, 0.0f, 0.0f}; n->rotation = (Quaternion){1.0f, 0.0f, 0.0f, 0.0f};
n->local = MatrixIdentity(); n->local = MatrixIdentity();
n->world = n->local; n->world = n->local;
} }
@@ -44,8 +44,8 @@ void recalcGraphNode(SceneGraphNode *n) {
Scene createScene(Arena *arena) { Scene createScene(Arena *arena) {
Scene result = {}; Scene result = {};
result.entities = PushList(arena, Entity, 1024); result.entities = PushList(arena, EntityList, 1024);
result.graphNodes = PushList(arena, SceneGraphNode, 1024); result.graphNodes = PushList(arena, SceneGraphNodeList, 1024);
result.sceneRoot = createSceneGraphNode(arena, &result); result.sceneRoot = createSceneGraphNode(arena, &result);
return result; return result;
} }
@@ -75,10 +75,10 @@ void removeEntity(Scene *s, uint32 entityHandle) {
SceneGraphNode *graphNode = getSceneGraphNode(s, entity->graphNodeHandle); SceneGraphNode *graphNode = getSceneGraphNode(s, entity->graphNodeHandle);
if (graphNode->parentHandle) { if (graphNode->parentHandle) {
SceneGraphNode *parentNode = getSceneGraphNode(s, graphNode->parentHandle); SceneGraphNode *parentNode = getSceneGraphNode(s, graphNode->parentHandle);
for (int i = 0; i < parentNode->children.head; i++) { for (int i = 0; i < parentNode->children.length; i++) {
if (parentNode->children.data[i] == entity->graphNodeHandle) { if (parentNode->children.data[i] == entity->graphNodeHandle) {
memcpy(&parentNode->children.data[i], &parentNode->children.data[i + 1], parentNode->children.head - i); memcpy(&parentNode->children.data[i], &parentNode->children.data[i + 1], parentNode->children.length - i);
parentNode->children.head -= 1; parentNode->children.length -= 1;
graphNode->parentHandle = 0; graphNode->parentHandle = 0;
break; break;
} }
@@ -87,7 +87,6 @@ void removeEntity(Scene *s, uint32 entityHandle) {
} }
void sceneNodeAddEntity(Scene *s, uint32 graphNodeHandle, uint32 entityHandle) { void sceneNodeAddEntity(Scene *s, uint32 graphNodeHandle, uint32 entityHandle) {
list<uint32> *childList = &getSceneGraphNode(s, graphNodeHandle)->children; HandleList *childList = &getSceneGraphNode(s, graphNodeHandle)->children;
appendList(childList, getEntity(s, entityHandle)->graphNodeHandle); AppendList(childList, getEntity(s, entityHandle)->graphNodeHandle);
print("%zu HI\n", childList->length);
} }

View File

@@ -1,39 +1,43 @@
#include "../gfx/gfx.h" #include "../gfx/gfx.h"
#include "../lib/raymath.h" #include "../lib/raymath.h"
DefineList(uint32, Handle);
enum EntityFlags { enum EntityFlags {
EntityFlags_Visible=1<<0, EntityFlags_Visible=1<<0,
EntityFlags_Dead=1<<1, EntityFlags_Dead=1<<1,
EntityFlags_Render=1<<2, EntityFlags_Render=1<<2,
}; };
struct Entity { typedef struct {
Mesh *mesh; Mesh *mesh;
Texture *tex; Texture *tex;
uint32 graphNodeHandle; uint32 graphNodeHandle;
uint64 flags; uint64 flags;
}; } Entity;
DefineList(Entity, Entity);
struct SceneGraphNode { typedef struct {
Matrix local; Matrix local;
Matrix world; Matrix world;
RLVector3 translation; RLVector3 translation;
Quaternion rotation; Quaternion rotation;
RLVector3 scale; RLVector3 scale;
list<uint32> children; HandleList children;
uint32 entityHandle; uint32 entityHandle;
uint32 parentHandle; uint32 parentHandle;
}; } SceneGraphNode;
DefineList(SceneGraphNode, SceneGraphNode);
struct Scene { typedef struct {
uint32 sceneRoot; uint32 sceneRoot;
list<Entity> entities; EntityList entities;
list<SceneGraphNode> graphNodes; SceneGraphNodeList graphNodes;
}; } Scene;
uint32 createEntity(Arena *arena, Scene *s); uint32 createEntity(Arena *arena, Scene *s);
Entity *getEntity(Scene *s); Entity *getEntity(Scene *s, uint32 id);
SceneGraphNode *getSceneGraphNode(Scene *s, int id); SceneGraphNode *getSceneGraphNode(Scene *s, uint32 id);
uint32 createSceneGraphNode(Arena *arena, Scene *s); uint32 createSceneGraphNode(Arena *arena, Scene *s);
Scene createScene(Arena *arena); Scene createScene(Arena *arena);
void initGraphNode(SceneGraphNode *n); void initGraphNode(SceneGraphNode *n);