chore: Initial commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
**.vscode/
|
||||
build/
|
||||
43
makefile
Normal file
43
makefile
Normal file
@@ -0,0 +1,43 @@
|
||||
CC=g++
|
||||
C_FLAGS=-Wall -Wextra -pedantic -ggdb -std=c++20
|
||||
GDB_FLAGS=-ex "b main"
|
||||
VAL_FLAGS=--leak-check=full --show-leak-kinds=all --track-origins=yes -s
|
||||
|
||||
DIR_SRC=src
|
||||
DIR_BUILD=build
|
||||
DEPS=m raylib
|
||||
|
||||
OUTBIN=$(DIR_BUILD)/bin/main
|
||||
|
||||
SRCS=$(shell find $(DIR_SRC) -type f -name '*.cpp')
|
||||
OBJS=$(patsubst $(DIR_SRC)/%.cpp,$(DIR_BUILD)/obj/%.o,$(SRCS))
|
||||
|
||||
DEPS_EXT=$(patsubst %,-l%,$(DEPS))
|
||||
|
||||
|
||||
.PHONY: all build rebuild run dbg val clean
|
||||
|
||||
|
||||
all: $(OUTBIN)
|
||||
build: $(OUTBIN)
|
||||
rebuild: clean build
|
||||
|
||||
run: $(OUTBIN)
|
||||
$(OUTBIN)
|
||||
|
||||
$(OUTBIN): $(OBJS)
|
||||
@mkdir -p $(@D)
|
||||
$(CC) $(OBJS) $(DEPS_EXT) -o $@
|
||||
|
||||
$(DIR_BUILD)/obj/%.o: $(DIR_SRC)/%.cpp
|
||||
@mkdir -p $(@D)
|
||||
$(CC) $(C_FLAGS) -c $< -o $@
|
||||
|
||||
dbg: $(OUTBIN)
|
||||
gdb $(GDB_FLAGS) $<
|
||||
|
||||
val: $(OUTBIN)
|
||||
valgrind $(VAL_FLAGS) $<
|
||||
|
||||
clean:
|
||||
@$(RM) -r $(DIR_BUILD)
|
||||
168
src/main.cpp
Normal file
168
src/main.cpp
Normal file
@@ -0,0 +1,168 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <raylib.h>
|
||||
|
||||
Mesh gen_mesh();
|
||||
void draw_custom();
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
(void)argc; (void)argv;
|
||||
|
||||
InitWindow(600, 400, "ecogame");
|
||||
|
||||
Camera3D camera = {
|
||||
.position = { 1, 2, 3 },
|
||||
.target = { 0, .5, 0 },
|
||||
.up = { 0, 1, 0 },
|
||||
.fovy = 80,
|
||||
.projection = CAMERA_PERSPECTIVE,
|
||||
};
|
||||
|
||||
Mesh mesh = gen_mesh();
|
||||
Model model = LoadModelFromMesh(mesh);
|
||||
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
BeginDrawing();
|
||||
ClearBackground({ 16, 16, 16, 255 });
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
DrawGrid(10, 2);
|
||||
//DrawSphere({}, .5, GREEN);
|
||||
//DrawModelWires(model, {}, 1, RED);
|
||||
draw_custom();
|
||||
|
||||
EndMode3D();
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
CloseWindow();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef struct vec2fp
|
||||
{
|
||||
float x, y;
|
||||
} __attribute__((packed)) vec2fp;
|
||||
|
||||
typedef struct vec3fp
|
||||
{
|
||||
float x, y, z;
|
||||
} __attribute__((packed)) vec3fp;
|
||||
|
||||
typedef struct triangle
|
||||
{
|
||||
uint16_t a, b, c;
|
||||
} __attribute__((packed)) triangle_t;
|
||||
|
||||
typedef struct body_def
|
||||
{
|
||||
int segcount;
|
||||
float spine_lengths[8];
|
||||
int branch_hcount[8];
|
||||
float branch_lenghts[8];
|
||||
} body_def_t;
|
||||
|
||||
void draw_custom()
|
||||
{
|
||||
body_def_t body = {};
|
||||
body.segcount = 4;
|
||||
body.spine_lengths[0] = .9;
|
||||
body.spine_lengths[1] = .7;
|
||||
body.spine_lengths[2] = .5;
|
||||
body.branch_hcount[0] = 1;
|
||||
body.branch_hcount[1] = 1;
|
||||
body.branch_lenghts[0] = 1.2;
|
||||
body.branch_lenghts[1] = .8;
|
||||
|
||||
//First point (head)
|
||||
Vector3 prev, cur, branch;
|
||||
std::vector<Vector3> prev_points, cur_points;
|
||||
prev_points = std::vector<Vector3>();
|
||||
cur_points = std::vector<Vector3>();
|
||||
cur = { 0, .5, 0 };
|
||||
|
||||
DrawSphere(cur, 0.05, GREEN);
|
||||
prev = cur;
|
||||
prev_points.push_back(cur);
|
||||
|
||||
for (int i = 0; i < body.segcount; ++i)
|
||||
{
|
||||
cur.z += body.spine_lengths[i];
|
||||
cur_points.push_back(cur);
|
||||
|
||||
DrawSphere(cur, 0.05, BLUE);
|
||||
for (Vector3 p : prev_points)
|
||||
DrawLine3D(cur, p, PINK);
|
||||
DrawLine3D(cur, prev, BLUE);
|
||||
|
||||
for (int b = 0; b < body.branch_hcount[i]; ++b)
|
||||
{
|
||||
branch = cur;
|
||||
branch.x += body.branch_lenghts[i];
|
||||
cur_points.push_back(branch);
|
||||
|
||||
DrawSphere(branch, 0.05, RED);
|
||||
for (Vector3 p : prev_points)
|
||||
DrawLine3D(branch, p, PINK);
|
||||
DrawLine3D(branch, cur, RED);
|
||||
|
||||
branch = cur;
|
||||
branch.x -= body.branch_lenghts[i];
|
||||
cur_points.push_back(branch);
|
||||
|
||||
DrawSphere(branch, 0.05, RED);
|
||||
for (Vector3 p : prev_points)
|
||||
DrawLine3D(branch, p, PINK);
|
||||
DrawLine3D(branch, cur, RED);
|
||||
}
|
||||
|
||||
prev = cur;
|
||||
|
||||
//Swap vectors
|
||||
std::vector<Vector3> tmp = prev_points;
|
||||
prev_points = cur_points;
|
||||
cur_points = tmp;
|
||||
cur_points.clear();
|
||||
}
|
||||
}
|
||||
|
||||
Mesh gen_mesh()
|
||||
{
|
||||
Mesh mesh = {};
|
||||
|
||||
mesh.vertexCount = 4;
|
||||
mesh.triangleCount = 2;
|
||||
|
||||
mesh.vertices = (float*)malloc(sizeof(float) * 3 * mesh.vertexCount);
|
||||
mesh.normals = (float*)malloc(sizeof(float) * 3 * mesh.vertexCount);
|
||||
mesh.indices = (uint16_t*)malloc(sizeof(uint16_t) * 3 * mesh.triangleCount);
|
||||
vec3fp *verts = (vec3fp*)mesh.vertices;
|
||||
vec3fp *normals = (vec3fp*)mesh.normals;
|
||||
triangle_t *tris = (triangle_t*)mesh.indices;
|
||||
|
||||
verts[0] = { -1, .5, -1 };
|
||||
verts[1] = { -1, .5, +1 };
|
||||
verts[2] = { +1, .5, -1 };
|
||||
verts[3] = { +1, .5, +1 };
|
||||
|
||||
normals[0] = { 0, 1, 0 };
|
||||
normals[1] = { 0, 1, 0 };
|
||||
normals[2] = { 0, 1, 0 };
|
||||
normals[3] = { 0, 1, 0 };
|
||||
|
||||
tris[0] = { 0, 1, 2 };
|
||||
tris[1] = { 2, 1, 3 };
|
||||
|
||||
UploadMesh(&mesh, false);
|
||||
|
||||
return mesh;
|
||||
}
|
||||
Reference in New Issue
Block a user