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:
@@ -1,2 +1,3 @@
|
|||||||
#include "impl/arena.c"
|
#include "impl/arena.c"
|
||||||
#include "impl/string.c"
|
#include "impl/string.c"
|
||||||
|
#include "impl/math.c"
|
||||||
|
|||||||
@@ -6,5 +6,6 @@
|
|||||||
#include "macros.h"
|
#include "macros.h"
|
||||||
#include "arena.h"
|
#include "arena.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
#include "math.h"
|
||||||
|
|
||||||
#endif // LD_CORE_CORE_H_
|
#endif // LD_CORE_CORE_H_
|
||||||
|
|||||||
143
code/core/impl/math.c
Normal file
143
code/core/impl/math.c
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
V3f V3f_Neg(V3f x) {
|
||||||
|
V3f result = { -x.x, -x.y, -x.z };
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
V3f V3f_Scale(V3f x, F32 s) {
|
||||||
|
V3f result = { s * x.x, s * x.y, s * x.z };
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
F32 V3f_Dot(V3f a, V3f b) {
|
||||||
|
F32 result = (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
F32 V4f_Dot(V4f a, V4f b) {
|
||||||
|
F32 result = (a.x * b.x) + (a.y * b.y) + (a.z * b.z) + (a.w * b.w);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Mat4x4F M4x4F_Rows(V3f x, V3f y, V3f z) {
|
||||||
|
Mat4x4F result = {
|
||||||
|
x.x, x.y, x.z, 0,
|
||||||
|
y.x, y.y, y.z, 0,
|
||||||
|
z.x, z.y, z.z, 0,
|
||||||
|
0, 0, 0, 1
|
||||||
|
};
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Mat4x4F M4x4F_Columns(V3f x, V3f y, V3f z) {
|
||||||
|
Mat4x4F result = {
|
||||||
|
x.x, y.x, z.x, 0,
|
||||||
|
x.y, y.y, z.y, 0,
|
||||||
|
x.z, y.z, z.z, 0,
|
||||||
|
0, 0, 0, 1
|
||||||
|
};
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Mat4x4F M4x4F_Mul(Mat4x4F a, Mat4x4F b) {
|
||||||
|
Mat4x4F result;
|
||||||
|
|
||||||
|
for (U32 r = 0; r < 4; ++r) {
|
||||||
|
for (U32 c = 0; c < 4; ++c) {
|
||||||
|
result.m[r][c] =
|
||||||
|
(a.m[r][0] * b.m[0][c]) + (a.m[r][1] * b.m[1][c]) +
|
||||||
|
(a.m[r][2] * b.m[2][c]) + (a.m[r][3] * b.m[3][c]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
V4f M4x4F_VMul4(Mat4x4F m, V4f v) {
|
||||||
|
V4f result;
|
||||||
|
result.x = V4f_Dot(m.r[0], v);
|
||||||
|
result.y = V4f_Dot(m.r[1], v);
|
||||||
|
result.z = V4f_Dot(m.r[2], v);
|
||||||
|
result.w = V4f_Dot(m.r[3], v);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
V3f M4x4F_VMul3(Mat4x4F m, V3f v) {
|
||||||
|
V4f tx;
|
||||||
|
tx.xyz = v;
|
||||||
|
tx.w = 1.0f;
|
||||||
|
|
||||||
|
V3f result = M4x4F_VMul4(m, tx).xyz;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Mat4x4FInv M4x4F_Perspective(F32 fov, F32 aspect, F32 nearp, F32 farp) {
|
||||||
|
F32 focal_length = 1.0f / tanf(0.5f * (PI_F32 * (fov / 360.0f)));
|
||||||
|
|
||||||
|
F32 a = focal_length;
|
||||||
|
F32 b = focal_length * aspect;
|
||||||
|
|
||||||
|
F32 c = -(nearp + farp) / (farp - nearp);
|
||||||
|
F32 d = -(2.0f * nearp * farp) / (farp - nearp);
|
||||||
|
|
||||||
|
Mat4x4FInv result = {
|
||||||
|
// fwd
|
||||||
|
{
|
||||||
|
a, 0, 0, 0,
|
||||||
|
0, b, 0, 0,
|
||||||
|
0, 0, c, d,
|
||||||
|
0, 0, -1, 0
|
||||||
|
},
|
||||||
|
// inv
|
||||||
|
{
|
||||||
|
(1 / a), 0, 0, 0,
|
||||||
|
0, (1 / b), 0, 0,
|
||||||
|
0, 0, 0, -1,
|
||||||
|
0, 0, (1/ d), (c / d)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Mat4x4FInv M4x4F_CameraView(V3f x, V3f y, V3f z, V3f p) {
|
||||||
|
Mat4x4FInv result;
|
||||||
|
|
||||||
|
// Construct orthonomal basis from axes
|
||||||
|
//
|
||||||
|
result.fwd = M4x4F_Rows(x, y, z);
|
||||||
|
V3f txp = V3f_Neg(M4x4F_VMul3(result.fwd, p));
|
||||||
|
|
||||||
|
// Translate by txp
|
||||||
|
//
|
||||||
|
result.fwd.r[0].w += txp.x;
|
||||||
|
result.fwd.r[1].w += txp.y;
|
||||||
|
result.fwd.r[2].w += txp.z;
|
||||||
|
|
||||||
|
// Calculate inverse axes
|
||||||
|
//
|
||||||
|
V3f ix = V3f_Scale(x, 1.0f / V3f_Dot(x, x));
|
||||||
|
V3f iy = V3f_Scale(y, 1.0f / V3f_Dot(y, y));
|
||||||
|
V3f iz = V3f_Scale(z, 1.0f / V3f_Dot(z, y));
|
||||||
|
|
||||||
|
// Calculate inverse position
|
||||||
|
//
|
||||||
|
V3f ip;
|
||||||
|
ip.x = (txp.x * ix.x) + (txp.y * iy.x) + (txp.z * iz.x);
|
||||||
|
ip.y = (txp.x * ix.y) + (txp.y * iy.y) + (txp.z * iz.y);
|
||||||
|
ip.z = (txp.x * ix.z) + (txp.y * iy.z) + (txp.z * iz.z);
|
||||||
|
|
||||||
|
result.inv = M4x4F_Columns(ix, iy, iz);
|
||||||
|
|
||||||
|
// Translate by ip
|
||||||
|
//
|
||||||
|
result.inv.r[0].w -= ip.x;
|
||||||
|
result.inv.r[1].w -= ip.y;
|
||||||
|
result.inv.r[2].w -= ip.z;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
105
code/core/math.h
Normal file
105
code/core/math.h
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
#if !defined(LD_CORE_MATH_H_)
|
||||||
|
#define LD_CORE_MATH_H_
|
||||||
|
|
||||||
|
#define PI_F32 (3.14159265358979323846264338f)
|
||||||
|
#define TAU_F32 (2.0f * PI_F32)
|
||||||
|
|
||||||
|
typedef union V2f V2f;
|
||||||
|
union V2f {
|
||||||
|
struct {
|
||||||
|
F32 x, y;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct {
|
||||||
|
F32 u, v;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct {
|
||||||
|
F32 w, h;
|
||||||
|
};
|
||||||
|
|
||||||
|
F32 e[2];
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef union V2i V2i;
|
||||||
|
union V2i {
|
||||||
|
struct {
|
||||||
|
U32 x, y;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct {
|
||||||
|
U32 w, h;
|
||||||
|
};
|
||||||
|
|
||||||
|
U32 e[2];
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef union V3f V3f;
|
||||||
|
union V3f {
|
||||||
|
struct {
|
||||||
|
F32 x, y, z;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct {
|
||||||
|
F32 r, g, b;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct {
|
||||||
|
F32 w, h, d;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct {
|
||||||
|
V2f xy;
|
||||||
|
F32 _z;
|
||||||
|
};
|
||||||
|
|
||||||
|
F32 e[3];
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef union V4f V4f;
|
||||||
|
union V4f {
|
||||||
|
struct {
|
||||||
|
F32 x, y, z, w;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct {
|
||||||
|
F32 r, g, b, a;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct {
|
||||||
|
V3f xyz;
|
||||||
|
F32 _w;
|
||||||
|
};
|
||||||
|
|
||||||
|
F32 e[4];
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef union Mat4x4F Mat4x4F;
|
||||||
|
union Mat4x4F {
|
||||||
|
F32 m[4][4];
|
||||||
|
F32 e[16];
|
||||||
|
V4f r[4];
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct Mat4x4FInv Mat4x4FInv;
|
||||||
|
struct Mat4x4FInv {
|
||||||
|
Mat4x4F fwd;
|
||||||
|
Mat4x4F inv;
|
||||||
|
};
|
||||||
|
|
||||||
|
function V3f V3f_Neg(V3f x);
|
||||||
|
function V3f V3f_Scale(V3f x, F32 s);
|
||||||
|
|
||||||
|
function F32 V3f_Dot(V3f a, V3f b);
|
||||||
|
function F32 V4f_Dot(V4f a, V4f b);
|
||||||
|
|
||||||
|
function Mat4x4F M4x4F_Rows(V3f x, V3f y, V3f z);
|
||||||
|
function Mat4x4F M4x4F_Columns(V3f x, V3f y, V3f z);
|
||||||
|
|
||||||
|
function V4f M4x4F_VMul4(Mat4x4F m, V4f v);
|
||||||
|
function V3f M4x4F_VMul3(Mat4x4F m, V3f v);
|
||||||
|
|
||||||
|
function Mat4x4FInv M4x4F_Perspective(F32 fov, F32 aspect, F32 nearp, F32 farp);
|
||||||
|
function Mat4x4FInv M4x4F_CameraView(V3f x, V3f y, V3f z, V3f p);
|
||||||
|
|
||||||
|
#endif // LD_CORE_MATH_H_
|
||||||
@@ -29,18 +29,6 @@ struct Str8 {
|
|||||||
U8 *data;
|
U8 *data;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct V2f V2f;
|
|
||||||
struct V2f {
|
|
||||||
F32 x;
|
|
||||||
F32 y;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct V2i V2i;
|
|
||||||
struct V2i {
|
|
||||||
U32 x;
|
|
||||||
U32 y;
|
|
||||||
};
|
|
||||||
|
|
||||||
#define U8_MAX ((U8) -1)
|
#define U8_MAX ((U8) -1)
|
||||||
#define U16_MAX ((U16) -1)
|
#define U16_MAX ((U16) -1)
|
||||||
#define U32_MAX ((U32) -1)
|
#define U32_MAX ((U32) -1)
|
||||||
|
|||||||
42
code/first.c
42
code/first.c
@@ -37,7 +37,6 @@ int main(int argc, char **argv) {
|
|||||||
Vk_Setup(window);
|
Vk_Setup(window);
|
||||||
|
|
||||||
G_State *game = 0;
|
G_State *game = 0;
|
||||||
G_Image *img = 0;
|
|
||||||
{
|
{
|
||||||
M_Arena *arena = M_ArenaAlloc(GB(64), .initial = MB(4));
|
M_Arena *arena = M_ArenaAlloc(GB(64), .initial = MB(4));
|
||||||
game = M_ArenaPush(arena, G_State);
|
game = M_ArenaPush(arena, G_State);
|
||||||
@@ -47,14 +46,17 @@ int main(int argc, char **argv) {
|
|||||||
G_ImagesLoad(game);
|
G_ImagesLoad(game);
|
||||||
G_PipelinesLoad(game);
|
G_PipelinesLoad(game);
|
||||||
|
|
||||||
for (U32 it = 0; it < game->n_images; ++it) {
|
G_Camera *camera = &game->camera;
|
||||||
if (Str8_Equal(game->images[it].name, S("saloon_ext"), 0)) {
|
|
||||||
img = &game->images[it];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!img) { img = &game->images[0]; }
|
camera->x = (V3f) { 1, 0, 0 };
|
||||||
|
camera->y = (V3f) { 0, 1, 0 };
|
||||||
|
camera->z = (V3f) { 0, 0, 1 };
|
||||||
|
camera->p = (V3f) { 0, 0, 8 };
|
||||||
|
|
||||||
|
camera->fov = 60.0f;
|
||||||
|
|
||||||
|
camera->nearp = 0.01f;
|
||||||
|
camera->farp = 1000.0f;
|
||||||
|
|
||||||
Vk_Buffer *vbo = &game->vbo;
|
Vk_Buffer *vbo = &game->vbo;
|
||||||
vbo->size = KB(4096);
|
vbo->size = KB(4096);
|
||||||
@@ -65,13 +67,13 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
G_Vertex *vertices = cast(G_Vertex *) vbo->data;
|
G_Vertex *vertices = cast(G_Vertex *) vbo->data;
|
||||||
|
|
||||||
vertices[0] = (G_Vertex) { -0.25f, -0.25f, 1.0f, 1.0f, 0.0f, 0.0f, 0xFFFFFFFF, 1};
|
vertices[0] = (G_Vertex) { -0.25f, -0.625f, 1.0f, 1.0f, 0.0f, 0.0f, 0xFFFFFFFF, 1};
|
||||||
vertices[1] = (G_Vertex) { 0.25f, -0.25f, 1.0f, 1.0f, 1.0f, 0.0f, 0xFFFFFFFF, 1};
|
vertices[1] = (G_Vertex) { 0.25f, -0.625f, 1.0f, 1.0f, 1.0f, 0.0f, 0xFFFFFFFF, 1};
|
||||||
vertices[2] = (G_Vertex) { -0.25f, 0.25f, 1.0f, 1.0f, 0.0f, 1.0f, 0xFFFFFFFF, 1};
|
vertices[2] = (G_Vertex) { -0.25f, 0.625f, 1.0f, 1.0f, 0.0f, 1.0f, 0xFFFFFFFF, 1};
|
||||||
|
|
||||||
vertices[3] = (G_Vertex) { 0.25f, -0.25f, 1.0f, 1.0f, 1.0f, 0.0f, 0xFFFFFFFF, 1};
|
vertices[3] = (G_Vertex) { 0.25f, -0.625f, 1.0f, 1.0f, 1.0f, 0.0f, 0xFFFFFFFF, 1};
|
||||||
vertices[4] = (G_Vertex) { 0.25f, 0.25f, 1.0f, 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 1};
|
vertices[4] = (G_Vertex) { 0.25f, 0.625f, 1.0f, 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 1};
|
||||||
vertices[5] = (G_Vertex) { -0.25f, 0.25f, 1.0f, 1.0f, 0.0f, 1.0f, 0xFFFFFFFF, 1};
|
vertices[5] = (G_Vertex) { -0.25f, 0.625f, 1.0f, 1.0f, 0.0f, 1.0f, 0xFFFFFFFF, 1};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool running = true;
|
bool running = true;
|
||||||
@@ -97,10 +99,12 @@ int main(int argc, char **argv) {
|
|||||||
.currentNavNode = 0
|
.currentNavNode = 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
.navMesh = TestNavMesh,
|
.navMesh = &TestNavMesh,
|
||||||
.npcPOI = {100}
|
.npcPOI = {100}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
printf("%zu size in bytes\n", sizeof(TestNavMesh));
|
||||||
|
|
||||||
while (running)
|
while (running)
|
||||||
{
|
{
|
||||||
SDL_Event e;
|
SDL_Event e;
|
||||||
@@ -113,11 +117,13 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdateNPCs(1.0f / 60.0f, &world);
|
updateNPCs(1.0f / 60.0f, &world);
|
||||||
|
|
||||||
int w, h;
|
int w, h;
|
||||||
SDL_GetWindowSizeInPixels(window, &w, &h);
|
SDL_GetWindowSizeInPixels(window, &w, &h);
|
||||||
|
|
||||||
|
G_CalulateCamera(&game->camera, (F32) w / (F32) h);
|
||||||
|
|
||||||
Vk_Frame *frame = Vk_FrameBegin(window);
|
Vk_Frame *frame = Vk_FrameBegin(window);
|
||||||
VkCommandBuffer cmd = frame->cmd;
|
VkCommandBuffer cmd = frame->cmd;
|
||||||
|
|
||||||
@@ -137,7 +143,7 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
VkRenderingInfo rendering_info = {0};
|
VkRenderingInfo rendering_info = {0};
|
||||||
rendering_info.sType = VK_STRUCTURE_TYPE_RENDERING_INFO;
|
rendering_info.sType = VK_STRUCTURE_TYPE_RENDERING_INFO;
|
||||||
rendering_info.renderArea = (VkRect2D){0, 0, w, h};
|
rendering_info.renderArea = (VkRect2D) { 0, 0, w, h };
|
||||||
rendering_info.layerCount = 1;
|
rendering_info.layerCount = 1;
|
||||||
rendering_info.colorAttachmentCount = 1;
|
rendering_info.colorAttachmentCount = 1;
|
||||||
rendering_info.pColorAttachments = &colour_attachment;
|
rendering_info.pColorAttachments = &colour_attachment;
|
||||||
@@ -191,6 +197,7 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
vk.CmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, basic->handle);
|
vk.CmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, basic->handle);
|
||||||
vk.CmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, basic->layout.pipeline, 0, 1, &set, 0, 0);
|
vk.CmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, basic->layout.pipeline, 0, 1, &set, 0, 0);
|
||||||
|
vk.CmdPushConstants(cmd, basic->layout.pipeline, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(Mat4x4F), &game->camera.proj.fwd);
|
||||||
|
|
||||||
VkViewport viewport = { 0, 0, (F32) w, (F32) h, 0.0f, 1.0f };
|
VkViewport viewport = { 0, 0, (F32) w, (F32) h, 0.0f, 1.0f };
|
||||||
VkRect2D scissor = { 0, 0, w, h };
|
VkRect2D scissor = { 0, 0, w, h };
|
||||||
@@ -202,7 +209,6 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
vk.CmdEndRendering(cmd);
|
vk.CmdEndRendering(cmd);
|
||||||
|
|
||||||
|
|
||||||
Vk_FrameEnd();
|
Vk_FrameEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -171,10 +171,17 @@ void G_PipelinesLoad(G_State *game) {
|
|||||||
|
|
||||||
vk.CreateDescriptorSetLayout(vk.device, &set_info, 0, &basic->layout.set);
|
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 };
|
VkPipelineLayoutCreateInfo layout_create = { 0 };
|
||||||
layout_create.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
layout_create.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
||||||
layout_create.setLayoutCount = 1;
|
layout_create.setLayoutCount = 1;
|
||||||
layout_create.pSetLayouts = &basic->layout.set;
|
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);
|
vk.CreatePipelineLayout(vk.device, &layout_create, 0, &basic->layout.pipeline);
|
||||||
}
|
}
|
||||||
@@ -193,6 +200,14 @@ void G_PipelinesLoad(G_State *game) {
|
|||||||
Vk_PipelineCreate(basic);
|
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/aabb.c"
|
||||||
#include "impl/nav.c"
|
#include "impl/nav.c"
|
||||||
#include "impl/player.c"
|
#include "impl/player.c"
|
||||||
|
|||||||
@@ -15,6 +15,17 @@ struct G_Image {
|
|||||||
Vk_Image 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;
|
typedef struct G_State G_State;
|
||||||
struct G_State {
|
struct G_State {
|
||||||
M_Arena *arena;
|
M_Arena *arena;
|
||||||
@@ -26,11 +37,17 @@ struct G_State {
|
|||||||
Vk_Pipeline *pipelines;
|
Vk_Pipeline *pipelines;
|
||||||
|
|
||||||
Vk_Buffer vbo;
|
Vk_Buffer vbo;
|
||||||
|
|
||||||
|
G_Camera camera;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function void G_ImagesLoad(G_State *game);
|
function void G_ImagesLoad(G_State *game);
|
||||||
function void G_PipelinesLoad(G_State *game);
|
function void G_PipelinesLoad(G_State *game);
|
||||||
|
|
||||||
|
function void G_CalulateCamera(G_Camera *camera, F32 aspect);
|
||||||
|
|
||||||
#include "aabb.h"
|
#include "aabb.h"
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
#include "nav.h"
|
#include "nav.h"
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ navSearchState initState(U32 start, U32 meshSize) {
|
|||||||
for(U32 i = 0; i < meshSize; i++) {
|
for(U32 i = 0; i < meshSize; i++) {
|
||||||
state.nodeStates[i].visited = false;
|
state.nodeStates[i].visited = false;
|
||||||
state.nodeStates[i].addedToUnvisited = false;
|
state.nodeStates[i].addedToUnvisited = false;
|
||||||
// underflow to the max :)
|
|
||||||
state.nodeStates[i].distance = U64_MAX;
|
state.nodeStates[i].distance = U64_MAX;
|
||||||
state.nodeStates[i].shortest = 0;
|
state.nodeStates[i].shortest = 0;
|
||||||
}
|
}
|
||||||
@@ -53,8 +52,8 @@ U32 getLowestState(U32 unfinishedIndexes[128], U32 unfinishedCount, navSearchSta
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generate a path to follow between the start and end node.
|
// Generate a path to follow between the start and end node.
|
||||||
NavPath Nav_Path(NavMesh mesh, U32 start, U32 end) {
|
NavPath Nav_Path(NavMesh *mesh, U32 start, U32 end) {
|
||||||
navSearchState state = initState(start, mesh.nodeCount);
|
navSearchState state = initState(start, mesh->nodeCount);
|
||||||
U32 unfinishedCount = 1;
|
U32 unfinishedCount = 1;
|
||||||
U32 unfinishedIndexes[NAV_MAX_NODES] = {start};
|
U32 unfinishedIndexes[NAV_MAX_NODES] = {start};
|
||||||
// I don't want to spend time removing items from
|
// 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;
|
U32 lowestNodeIndex = start;
|
||||||
bool found = false;
|
bool found = false;
|
||||||
while(!found) {
|
while(!found) {
|
||||||
for(int connectionI = 0 ; connectionI < mesh.nodes[lowestNodeIndex].connectionCount; connectionI++) {
|
for(int connectionI = 0 ; connectionI < mesh->nodes[lowestNodeIndex].connectionCount; connectionI++) {
|
||||||
NavConnection connection = mesh.nodes[lowestNodeIndex].connections[connectionI];
|
NavConnection *connection = &mesh->nodes[lowestNodeIndex].connections[connectionI];
|
||||||
navSearchNodeState *testNode = &state.nodeStates[connection.NodeIndex];
|
navSearchNodeState *testNode = &state.nodeStates[connection->NodeIndex];
|
||||||
if(testNode->visited) {continue;}
|
if(testNode->visited) {continue;}
|
||||||
U32 distance = cast(U32) (state.nodeStates[lowestNodeIndex].distance + connection.Cost);
|
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.x - mesh->nodes[connection->NodeIndex].pos.x);
|
||||||
distance += cast(U32) (mesh.nodes[end].pos.y - mesh.nodes[connection.NodeIndex].pos.y);
|
distance += cast(U32) (mesh->nodes[end].pos.y - mesh->nodes[connection->NodeIndex].pos.y);
|
||||||
if(testNode->distance > distance) {
|
if(testNode->distance > distance) {
|
||||||
testNode->distance = distance;
|
testNode->distance = distance;
|
||||||
testNode->shortest = lowestNodeIndex;
|
testNode->shortest = lowestNodeIndex;
|
||||||
}
|
}
|
||||||
if(!testNode->addedToUnvisited) {
|
if(!testNode->addedToUnvisited) {
|
||||||
unfinishedIndexes[unfinishedCount] = connection.NodeIndex;
|
unfinishedIndexes[unfinishedCount] = connection->NodeIndex;
|
||||||
unfinishedCount++;
|
unfinishedCount++;
|
||||||
testNode->addedToUnvisited = true;
|
testNode->addedToUnvisited = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "../npc.h"
|
#include "game/npc.h"
|
||||||
#include "../world.h"
|
#include "game/world.h"
|
||||||
#include "../../core/types.h"
|
#include "core/types.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
void updateNPC(F32 delta, NPC *npc, World *world) {
|
void updateNPC(F32 delta, NPC *npc, World *world) {
|
||||||
@@ -33,15 +34,15 @@ void updateNPC(F32 delta, NPC *npc, World *world) {
|
|||||||
npc->pathIndex+=1;
|
npc->pathIndex+=1;
|
||||||
npc->currentNavNode = npc->path.indexes[npc->pathIndex];
|
npc->currentNavNode = npc->path.indexes[npc->pathIndex];
|
||||||
}
|
}
|
||||||
NavNode cNav = world->navMesh.nodes[npc->currentNavNode];
|
NavNode cNav = world->navMesh->nodes[npc->currentNavNode];
|
||||||
NavNode tNav = world->navMesh.nodes[npc->pathIndex];
|
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.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;
|
npc->collision.pos.y = cNav.pos.y * (1 - npc->walkTimer/NPC_SPEED) + tNav.pos.y * npc->walkTimer/NPC_SPEED;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpdateNPCs(F32 delta, World *world) {
|
void updateNPCs(F32 delta, World *world) {
|
||||||
for(U32 i = 0; i < world->npcCount; i++) {
|
for(U32 i = 0; i < world->npcCount; i++) {
|
||||||
updateNPC(delta, &world->npcs[i], world);
|
updateNPC(delta, &world->npcs[i], world);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ struct NavMesh{
|
|||||||
NavNode nodes[NAV_MAX_NODES];
|
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
|
#endif
|
||||||
|
|||||||
@@ -1,24 +1,28 @@
|
|||||||
#if !defined(LD_GAME_WORLD_H_)
|
#if !defined(LD_GAME_WORLD_H_)
|
||||||
#define LD_GAME_WORLD_H_
|
#define LD_GAME_WORLD_H_
|
||||||
|
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
#include "npc.h"
|
#include "npc.h"
|
||||||
|
|
||||||
// Areas are which
|
// Areas are which
|
||||||
enum AREA {
|
typedef U32 World_Area;
|
||||||
WORLD_AREA_OUTSIDE = 1,
|
enum World_Area {
|
||||||
WORLD_AREA_SALOON = 1 << 1,
|
WORLD_AREA_OUTSIDE = (1 << 0),
|
||||||
|
WORLD_AREA_SALOON = (1 << 1),
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct World World;
|
typedef struct World World;
|
||||||
struct World {
|
struct World {
|
||||||
U32 npcCount;
|
U32 npcCount;
|
||||||
NPC npcs[128];
|
NPC npcs[128];
|
||||||
NavMesh navMesh;
|
|
||||||
|
NavMesh *navMesh;
|
||||||
|
|
||||||
// NPC points of interest, places to walk to.
|
// NPC points of interest, places to walk to.
|
||||||
U32 npcPOI[256];
|
U32 npcPOI[256];
|
||||||
};
|
};
|
||||||
|
|
||||||
function void updateWorld(F32 delta, World *world);
|
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_
|
#endif // LD_GAME_WORLD_H_
|
||||||
|
|||||||
@@ -63,6 +63,7 @@
|
|||||||
VK_FUNC(CmdSetViewport);
|
VK_FUNC(CmdSetViewport);
|
||||||
VK_FUNC(CmdSetScissor);
|
VK_FUNC(CmdSetScissor);
|
||||||
|
|
||||||
|
VK_FUNC(CmdPushConstants);
|
||||||
VK_FUNC(CmdCopyBufferToImage);
|
VK_FUNC(CmdCopyBufferToImage);
|
||||||
VK_FUNC(CmdPipelineBarrier2);
|
VK_FUNC(CmdPipelineBarrier2);
|
||||||
VK_FUNC(CmdBeginRendering);
|
VK_FUNC(CmdBeginRendering);
|
||||||
|
|||||||
@@ -9,6 +9,11 @@ struct Vertex {
|
|||||||
uint pad;
|
uint pad;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
layout(push_constant, row_major)
|
||||||
|
uniform Global {
|
||||||
|
mat4 proj;
|
||||||
|
};
|
||||||
|
|
||||||
layout(binding = 0, scalar)
|
layout(binding = 0, scalar)
|
||||||
readonly buffer Vertices {
|
readonly buffer Vertices {
|
||||||
Vertex vtx[];
|
Vertex vtx[];
|
||||||
@@ -21,7 +26,7 @@ layout(location = 2) out flat uint idx;
|
|||||||
void main() {
|
void main() {
|
||||||
Vertex v = vtx[gl_VertexIndex];
|
Vertex v = vtx[gl_VertexIndex];
|
||||||
|
|
||||||
gl_Position = v.p;
|
gl_Position = proj * v.p;
|
||||||
|
|
||||||
frag_uv = v.uv;
|
frag_uv = v.uv;
|
||||||
frag_c = vec4((v.c >> 24) & 0xFF, (v.c >> 16) & 0xFF, (v.c >> 8) & 0xFF, (v.c >> 0) & 0xFF) / 255.0f;
|
frag_c = vec4((v.c >> 24) & 0xFF, (v.c >> 16) & 0xFF, (v.c >> 8) & 0xFF, (v.c >> 0) & 0xFF) / 255.0f;
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ glslangValidator -o "assets\shaders\basic.frag.spv" --target-env vulkan1.3 "..\c
|
|||||||
|
|
||||||
ECHO [Building source]
|
ECHO [Building source]
|
||||||
|
|
||||||
SET COMPILER_OPTS=-nologo /F8388608 -W4 -I"deps\SDL3\include" -I"deps\stb" -I"%VULKAN_SDK%\Include" -I"..\code"
|
SET COMPILER_OPTS=-nologo -W4 -I"deps\SDL3\include" -I"deps\stb" -I"%VULKAN_SDK%\Include" -I"..\code"
|
||||||
SET LINKER_OPTS=-LIBPATH:"deps\SDL3\lib" SDL3.lib Ole32.lib Shell32.lib
|
SET LINKER_OPTS=-LIBPATH:"deps\SDL3\lib" SDL3.lib Ole32.lib Shell32.lib
|
||||||
|
|
||||||
IF %release% equ 1 (
|
IF %release% equ 1 (
|
||||||
|
|||||||
Reference in New Issue
Block a user