Added camera

Moved some math types
Added some more vector types
Did the camera matrix calulations
Updated shaders to take push constants
This commit is contained in:
2025-10-05 02:40:59 +01:00
parent 2c67896cf2
commit 3b8c50a361
15 changed files with 355 additions and 70 deletions

View File

@@ -171,10 +171,17 @@ void G_PipelinesLoad(G_State *game) {
vk.CreateDescriptorSetLayout(vk.device, &set_info, 0, &basic->layout.set);
VkPushConstantRange push_range = { 0 };
push_range.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
push_range.offset = 0;
push_range.size = 128;
VkPipelineLayoutCreateInfo layout_create = { 0 };
layout_create.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
layout_create.setLayoutCount = 1;
layout_create.pSetLayouts = &basic->layout.set;
layout_create.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
layout_create.setLayoutCount = 1;
layout_create.pSetLayouts = &basic->layout.set;
layout_create.pushConstantRangeCount = 1;
layout_create.pPushConstantRanges = &push_range;
vk.CreatePipelineLayout(vk.device, &layout_create, 0, &basic->layout.pipeline);
}
@@ -193,6 +200,14 @@ void G_PipelinesLoad(G_State *game) {
Vk_PipelineCreate(basic);
}
void G_CalulateCamera(G_Camera *camera, F32 aspect) {
Mat4x4FInv proj = M4x4F_Perspective(camera->fov, aspect, camera->nearp, camera->farp);
Mat4x4FInv view = M4x4F_CameraView(camera->x, camera->y, camera->z, camera->p);
camera->proj.fwd = M4x4F_Mul(proj.fwd, view.fwd);
camera->proj.inv = M4x4F_Mul(view.inv, proj.inv);
}
#include "impl/aabb.c"
#include "impl/nav.c"
#include "impl/player.c"

View File

@@ -15,6 +15,17 @@ struct G_Image {
Vk_Image image;
};
typedef struct G_Camera G_Camera;
struct G_Camera {
V3f x, y, z;
V3f p;
F32 fov;
F32 nearp, farp;
Mat4x4FInv proj;
};
typedef struct G_State G_State;
struct G_State {
M_Arena *arena;
@@ -26,11 +37,17 @@ struct G_State {
Vk_Pipeline *pipelines;
Vk_Buffer vbo;
G_Camera camera;
};
function void G_ImagesLoad(G_State *game);
function void G_PipelinesLoad(G_State *game);
function void G_CalulateCamera(G_Camera *camera, F32 aspect);
#include "aabb.h"
#include "player.h"
#include "nav.h"

View File

@@ -21,11 +21,10 @@ struct navSearchState{
navSearchState initState(U32 start, U32 meshSize) {
navSearchState state = {};
for(U32 i = 0; i < meshSize; i++) {
state.nodeStates[i].visited = false;
state.nodeStates[i].visited = false;
state.nodeStates[i].addedToUnvisited = false;
// underflow to the max :)
state.nodeStates[i].distance = U64_MAX;
state.nodeStates[i].shortest = 0;
state.nodeStates[i].distance = U64_MAX;
state.nodeStates[i].shortest = 0;
}
state.nodeStates[start].distance = 0;
return state;
@@ -53,8 +52,8 @@ U32 getLowestState(U32 unfinishedIndexes[128], U32 unfinishedCount, navSearchSta
}
// Generate a path to follow between the start and end node.
NavPath Nav_Path(NavMesh mesh, U32 start, U32 end) {
navSearchState state = initState(start, mesh.nodeCount);
NavPath Nav_Path(NavMesh *mesh, U32 start, U32 end) {
navSearchState state = initState(start, mesh->nodeCount);
U32 unfinishedCount = 1;
U32 unfinishedIndexes[NAV_MAX_NODES] = {start};
// I don't want to spend time removing items from
@@ -65,19 +64,19 @@ NavPath Nav_Path(NavMesh mesh, U32 start, U32 end) {
U32 lowestNodeIndex = start;
bool found = false;
while(!found) {
for(int connectionI = 0 ; connectionI < mesh.nodes[lowestNodeIndex].connectionCount; connectionI++) {
NavConnection connection = mesh.nodes[lowestNodeIndex].connections[connectionI];
navSearchNodeState *testNode = &state.nodeStates[connection.NodeIndex];
for(int connectionI = 0 ; connectionI < mesh->nodes[lowestNodeIndex].connectionCount; connectionI++) {
NavConnection *connection = &mesh->nodes[lowestNodeIndex].connections[connectionI];
navSearchNodeState *testNode = &state.nodeStates[connection->NodeIndex];
if(testNode->visited) {continue;}
U32 distance = cast(U32) (state.nodeStates[lowestNodeIndex].distance + connection.Cost);
distance += cast(U32) (mesh.nodes[end].pos.x - mesh.nodes[connection.NodeIndex].pos.x);
distance += cast(U32) (mesh.nodes[end].pos.y - mesh.nodes[connection.NodeIndex].pos.y);
U32 distance = cast(U32) (state.nodeStates[lowestNodeIndex].distance + connection->Cost);
distance += cast(U32) (mesh->nodes[end].pos.x - mesh->nodes[connection->NodeIndex].pos.x);
distance += cast(U32) (mesh->nodes[end].pos.y - mesh->nodes[connection->NodeIndex].pos.y);
if(testNode->distance > distance) {
testNode->distance = distance;
testNode->shortest = lowestNodeIndex;
}
if(!testNode->addedToUnvisited) {
unfinishedIndexes[unfinishedCount] = connection.NodeIndex;
unfinishedIndexes[unfinishedCount] = connection->NodeIndex;
unfinishedCount++;
testNode->addedToUnvisited = true;
}

View File

@@ -1,6 +1,7 @@
#include "../npc.h"
#include "../world.h"
#include "../../core/types.h"
#include "game/npc.h"
#include "game/world.h"
#include "core/types.h"
#include <stdio.h>
void updateNPC(F32 delta, NPC *npc, World *world) {
@@ -33,15 +34,15 @@ void updateNPC(F32 delta, NPC *npc, World *world) {
npc->pathIndex+=1;
npc->currentNavNode = npc->path.indexes[npc->pathIndex];
}
NavNode cNav = world->navMesh.nodes[npc->currentNavNode];
NavNode tNav = world->navMesh.nodes[npc->pathIndex];
NavNode cNav = world->navMesh->nodes[npc->currentNavNode];
NavNode tNav = world->navMesh->nodes[npc->pathIndex];
npc->collision.pos.x = cNav.pos.x * (1 - npc->walkTimer/NPC_SPEED) + tNav.pos.x * npc->walkTimer/NPC_SPEED;
npc->collision.pos.y = cNav.pos.y * (1 - npc->walkTimer/NPC_SPEED) + tNav.pos.y * npc->walkTimer/NPC_SPEED;
break;
}
}
void UpdateNPCs(F32 delta, World *world) {
void updateNPCs(F32 delta, World *world) {
for(U32 i = 0; i < world->npcCount; i++) {
updateNPC(delta, &world->npcs[i], world);
}

View File

@@ -34,7 +34,6 @@ struct NavMesh{
NavNode nodes[NAV_MAX_NODES];
};
function NavPath Nav_Path(NavMesh mesh, U32 start, U32 end);
function NavPath Nav_Path(NavMesh *mesh, U32 start, U32 end);
#endif

View File

@@ -1,24 +1,28 @@
#if !defined(LD_GAME_WORLD_H_)
#define LD_GAME_WORLD_H_
#include "player.h"
#include "npc.h"
// Areas are which
enum AREA {
WORLD_AREA_OUTSIDE = 1,
WORLD_AREA_SALOON = 1 << 1,
// Areas are which
typedef U32 World_Area;
enum World_Area {
WORLD_AREA_OUTSIDE = (1 << 0),
WORLD_AREA_SALOON = (1 << 1),
};
typedef struct World World;
struct World {
U32 npcCount;
NPC npcs[128];
NavMesh navMesh;
NavMesh *navMesh;
// NPC points of interest, places to walk to.
U32 npcPOI[256];
};
function void updateWorld(F32 delta, World *world);
function void UpdateNPCs(F32 delta, World *world);
function void updateNPCs(F32 delta, World *world);
#endif // LD_GAME_WORLD_H_