fixed camera, some cleanup
This commit is contained in:
parent
09b57ae114
commit
282f78baae
12
build.sh
12
build.sh
@ -83,3 +83,15 @@ if ! [ -f build/libm3d.a ]; then
|
|||||||
rm $obj
|
rm $obj
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# CGLM
|
||||||
|
src="external/cglm/cglm.c"
|
||||||
|
flags="-std=c99 -Wno-everything -Iexternal/cglm -c -static"
|
||||||
|
obj="build/cglm.o"
|
||||||
|
lib="build/libcglm.a"
|
||||||
|
|
||||||
|
if ! [ -f build/libcglm.a ]; then
|
||||||
|
$c_compiler $flags $src $out $obj
|
||||||
|
ar rcs $lib $obj
|
||||||
|
rm $obj
|
||||||
|
fi
|
||||||
|
|
||||||
|
|||||||
5
dub.json
5
dub.json
@ -7,7 +7,7 @@
|
|||||||
"targetType": "executable",
|
"targetType": "executable",
|
||||||
"targetName": "Gears",
|
"targetName": "Gears",
|
||||||
"targetPath": "build",
|
"targetPath": "build",
|
||||||
"sourceFiles-linux": ["build/libvma.a", "build/libstb_image.a", "build/libm3d.a"],
|
"sourceFiles-linux": ["build/libvma.a", "build/libstb_image.a", "build/libm3d.a", "build/libcglm.a"],
|
||||||
"sourceFiles-windows": [],
|
"sourceFiles-windows": [],
|
||||||
"importPaths": ["src/gears", "src/shared", "src/generated", "external/xxhash", "external/inteli"],
|
"importPaths": ["src/gears", "src/shared", "src/generated", "external/xxhash", "external/inteli"],
|
||||||
"sourcePaths": ["src/gears", "src/shared", "src/generated", "external/xxhash", "external/inteli"],
|
"sourcePaths": ["src/gears", "src/shared", "src/generated", "external/xxhash", "external/inteli"],
|
||||||
@ -15,6 +15,7 @@
|
|||||||
"libs-windows": [],
|
"libs-windows": [],
|
||||||
"preGenerateCommands-linux": ["./build.sh", "build/Packer"],
|
"preGenerateCommands-linux": ["./build.sh", "build/Packer"],
|
||||||
"preGenerateCommands-windows": [],
|
"preGenerateCommands-windows": [],
|
||||||
|
"dflags": ["-Xcc=-mno-sse"],
|
||||||
"dflags-dmd": ["-P=-DSTBI_NO_SIMD"]
|
"dflags-dmd": ["-P=-DSTBI_NO_SIMD"]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -24,7 +25,7 @@
|
|||||||
"targetName": "Packer",
|
"targetName": "Packer",
|
||||||
"importPaths": ["src/packer", "src/shared", "src/generated", "external/xxhash", "external/inteli"],
|
"importPaths": ["src/packer", "src/shared", "src/generated", "external/xxhash", "external/inteli"],
|
||||||
"sourcePaths": ["src/packer", "src/shared", "src/generated", "external/xxhash", "external/inteli"],
|
"sourcePaths": ["src/packer", "src/shared", "src/generated", "external/xxhash", "external/inteli"],
|
||||||
"sourceFiles-linux": ["build/libstb_image.a", "build/libm3d.a"],
|
"sourceFiles-linux": ["build/libstb_image.a", "build/libm3d.a", "build/libcglm"],
|
||||||
"preGenerateCommands-linux": ["./build.sh"],
|
"preGenerateCommands-linux": ["./build.sh"],
|
||||||
"postGenerateCommands-linux": [],
|
"postGenerateCommands-linux": [],
|
||||||
"preGenerateCommands-windows": [],
|
"preGenerateCommands-windows": [],
|
||||||
|
|||||||
3
external/cglm/cglm.c
vendored
Normal file
3
external/cglm/cglm.c
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
#define CGLM_FORCE_DEPTH_ZERO_TO_ONE
|
||||||
|
|
||||||
|
#include "cglm.h"
|
||||||
107
src/gears/game.d
107
src/gears/game.d
@ -98,48 +98,6 @@ ProcessInputs(Game* g, Camera* cam)
|
|||||||
HandleInputs(cam, &g.window.inputs);
|
HandleInputs(cam, &g.window.inputs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
void
|
|
||||||
Update(Game* g, Camera* cam)
|
|
||||||
{
|
|
||||||
Mat4 rotation = RotationMatrix(cam);
|
|
||||||
Vec3 v = cam.velocity;
|
|
||||||
cam.pos += (rotation * Vec4(v.x, v.y, v.z, 0.0)).xyz * g.delta;
|
|
||||||
Logf("%s", rotation.v);
|
|
||||||
|
|
||||||
Extent ext = GetExtent(&g.rd);
|
|
||||||
|
|
||||||
g.globals.view_matrix = ViewMatrix(cam);
|
|
||||||
Perspective(&g.globals.projection_matrix, Radians(90.0), cast(f32)(ext.x), cast(f32)(ext.y), 0.1, 10000.0);
|
|
||||||
g.globals.projection_matrix[1, 1] *= -1.0;
|
|
||||||
//g.globals.projection_matrix[2, 2] *= -1.0;
|
|
||||||
|
|
||||||
g.globals.view_projection = g.globals.projection_matrix * g.globals.view_matrix;
|
|
||||||
|
|
||||||
g.globals.res.x = cast(f32)ext.x;
|
|
||||||
g.globals.res.y = cast(f32)ext.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
pragma(inline): Mat4
|
|
||||||
RotationMatrix(Camera* cam)
|
|
||||||
{
|
|
||||||
Quat pitch_rotation, yaw_rotation;
|
|
||||||
QuatFromAxis(&pitch_rotation, cam.pitch, Vec3(1.0, 0.0, 0.0));
|
|
||||||
QuatFromAxis(&yaw_rotation, cam.yaw, Vec3(0.0, -1.0, 0.0));
|
|
||||||
Mat4 pitch_mat = cast(Mat4)(yaw_rotation) * cast(Mat4)(pitch_rotation);
|
|
||||||
return cast(Mat4)(yaw_rotation) * cast(Mat4)(pitch_rotation);
|
|
||||||
}
|
|
||||||
|
|
||||||
pragma(inline): Mat4
|
|
||||||
ViewMatrix(Camera* cam)
|
|
||||||
{
|
|
||||||
Mat4 translation;
|
|
||||||
Translate(&translation, cam.pos);
|
|
||||||
Mat4 rotation = RotationMatrix(cam);
|
|
||||||
return Inverse(translation * rotation);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Cycle(Game* g)
|
Cycle(Game* g)
|
||||||
{
|
{
|
||||||
@ -157,11 +115,10 @@ Cycle(Game* g)
|
|||||||
|
|
||||||
Extent ext = GetExtent(&g.rd);
|
Extent ext = GetExtent(&g.rd);
|
||||||
f32 aspect = (cast(f32)ext.x) / (cast(f32)ext.y);
|
f32 aspect = (cast(f32)ext.x) / (cast(f32)ext.y);
|
||||||
Mat4 projection = Perspective(90.0, aspect, 0.1, 100.0);
|
Mat4 projection = Perspective(90.0, aspect, 10000.0, 0.1);
|
||||||
projection[1, 1] *= -1.0;
|
|
||||||
|
|
||||||
g.globals.projection_matrix = projection;
|
g.globals.projection_matrix = projection;
|
||||||
g.globals.view_matrix = g.camera.view;
|
g.globals.view_matrix = ViewMatrix(&g.camera);
|
||||||
g.globals.res.x = ext.x;
|
g.globals.res.x = ext.x;
|
||||||
g.globals.res.y = ext.y;
|
g.globals.res.y = ext.y;
|
||||||
SetUniform(&g.rd, &g.globals);
|
SetUniform(&g.rd, &g.globals);
|
||||||
@ -217,21 +174,17 @@ Destroy(Game* g)
|
|||||||
|
|
||||||
struct Camera
|
struct Camera
|
||||||
{
|
{
|
||||||
Mat4 view = Mat4Identity();
|
|
||||||
Vec3 pos = Vec3(0.0, 0.0, 2.0);
|
|
||||||
Vec3 velocity = Vec3(0.0);
|
Vec3 velocity = Vec3(0.0);
|
||||||
Vec3 front = Vec3(0.0, 0.0, -1.0);
|
Vec3 pos = Vec3(0.0, 0.0, 5.0);
|
||||||
Vec3 up = Vec3(0.0, 1.0, 0.0);
|
|
||||||
Vec3 right = Vec3(0.0);
|
|
||||||
Vec3 world_up = Vec3(0.0, 1.0, 0.0);
|
|
||||||
f32 speed = 5000.0;
|
|
||||||
f32 yaw = -90.0;
|
|
||||||
f32 pitch = 0.0;
|
f32 pitch = 0.0;
|
||||||
|
f32 yaw = 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
pragma(inline): void
|
pragma(inline): void
|
||||||
HandleInputs(Camera* cam, Inputs* inputs)
|
HandleInputs(Camera* cam, Inputs* inputs)
|
||||||
{
|
{
|
||||||
|
Logf("%.04f %.04f", cam.pitch, cam.yaw);
|
||||||
|
|
||||||
foreach(i; 0 .. inputs.count)
|
foreach(i; 0 .. inputs.count)
|
||||||
{
|
{
|
||||||
InputEvent event = inputs.events[i];
|
InputEvent event = inputs.events[i];
|
||||||
@ -246,37 +199,35 @@ HandleInputs(Camera* cam, Inputs* inputs)
|
|||||||
case Input.MouseMotion:
|
case Input.MouseMotion:
|
||||||
{
|
{
|
||||||
// (0, 0) top left
|
// (0, 0) top left
|
||||||
cam.yaw -= cast(f32)(event.rel_x) / 20.0;
|
cam.yaw += cast(f32)(event.rel_x) / 200.0;
|
||||||
cam.pitch += cast(f32)(event.rel_y) / 20.0;
|
cam.pitch += cast(f32)(event.rel_y) / 200.0;
|
||||||
|
|
||||||
if (cam.pitch > 90.0)
|
|
||||||
{
|
|
||||||
cam.pitch = 90.0;
|
|
||||||
}
|
|
||||||
if (cam.pitch < -90.0)
|
|
||||||
{
|
|
||||||
cam.pitch = -90.0;
|
|
||||||
}
|
|
||||||
} break;
|
} break;
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pragma(inline): void
|
Mat4
|
||||||
|
RotationMatrix(Camera* cam)
|
||||||
|
{
|
||||||
|
Quat pitch = QuatFromAxis(cam.pitch, Vec3(1.0, 0.0, 0.0));
|
||||||
|
Quat yaw = QuatFromAxis(cam.yaw, Vec3(0.0, 1.0, 0.0));
|
||||||
|
return cast(Mat4)(yaw) * cast(Mat4)(pitch);
|
||||||
|
}
|
||||||
|
|
||||||
|
Mat4
|
||||||
|
ViewMatrix(Camera* cam)
|
||||||
|
{
|
||||||
|
Mat4 translation = Mat4Identity();
|
||||||
|
Translate(&translation, cam.pos);
|
||||||
|
Mat4 rotation = RotationMatrix(cam);
|
||||||
|
return Inverse(translation * rotation);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
Update(Camera* cam)
|
Update(Camera* cam)
|
||||||
{
|
{
|
||||||
Vec3 front = Vec3(0.0);
|
Mat4 rotation = RotationMatrix(cam);
|
||||||
|
Vec4 pos = rotation * Vec4(cam.velocity * 0.5 * g_DELTA, 0.0);
|
||||||
cam.pos += cam.velocity * g_DELTA;
|
cam.pos += pos.xyz;
|
||||||
|
|
||||||
front.x = cosf(Radians(cam.yaw)) * cosf(Radians(cam.pitch));
|
|
||||||
front.y = sinf(Radians(cam.pitch));
|
|
||||||
front.z = sinf(Radians(cam.yaw)) * cosf(Radians(cam.pitch));
|
|
||||||
cam.front = Normalize(front);
|
|
||||||
|
|
||||||
cam.right = Normalize(Cross(cam.front, cam.world_up));
|
|
||||||
cam.up = Normalize(Cross(cam.right, cam.front));
|
|
||||||
|
|
||||||
cam.view = LookAt(cam.pos, cam.pos + cam.front, cam.up);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -152,7 +152,7 @@ struct MeshPart
|
|||||||
|
|
||||||
struct Model
|
struct Model
|
||||||
{
|
{
|
||||||
Vec3 pos = Vec3(0.0, 0.0, 0.0);
|
Vec3 pos = Vec3(0.0);
|
||||||
Buffer vertex_buffer;
|
Buffer vertex_buffer;
|
||||||
Buffer index_buffer;
|
Buffer index_buffer;
|
||||||
MeshPart[] parts;
|
MeshPart[] parts;
|
||||||
|
|||||||
@ -1187,9 +1187,12 @@ Bind(Vulkan* vk, PipelineHandle* pipeline)
|
|||||||
);
|
);
|
||||||
|
|
||||||
VkViewport viewport = {
|
VkViewport viewport = {
|
||||||
|
x: 0.0,
|
||||||
|
y: 0.0,
|
||||||
width: cast(f32)vk.swapchain_extent.width,
|
width: cast(f32)vk.swapchain_extent.width,
|
||||||
height: cast(f32)vk.swapchain_extent.height,
|
height: cast(f32)vk.swapchain_extent.height,
|
||||||
maxDepth: 1.0F,
|
minDepth: 0.0,
|
||||||
|
maxDepth: 1.0,
|
||||||
};
|
};
|
||||||
|
|
||||||
vkCmdSetViewport(vk.cmds[vk.frame_index], 0, 1, &viewport);
|
vkCmdSetViewport(vk.cmds[vk.frame_index], 0, 1, &viewport);
|
||||||
@ -1303,7 +1306,7 @@ CreateGraphicsPipeline(Vulkan* vk, GfxPipelineInfo* build_info)
|
|||||||
depthTestEnable: VK_TRUE,
|
depthTestEnable: VK_TRUE,
|
||||||
depthWriteEnable: VK_TRUE,
|
depthWriteEnable: VK_TRUE,
|
||||||
depthCompareOp: VK_COMPARE_OP_GREATER_OR_EQUAL,
|
depthCompareOp: VK_COMPARE_OP_GREATER_OR_EQUAL,
|
||||||
depthBoundsTestEnable: VK_TRUE,
|
depthBoundsTestEnable: VK_FALSE,
|
||||||
stencilTestEnable: VK_FALSE,
|
stencilTestEnable: VK_FALSE,
|
||||||
minDepthBounds: 0.0,
|
minDepthBounds: 0.0,
|
||||||
maxDepthBounds: 1.0,
|
maxDepthBounds: 1.0,
|
||||||
|
|||||||
@ -8,6 +8,8 @@
|
|||||||
# include <X11/extensions/Xfixes.h>
|
# include <X11/extensions/Xfixes.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <xmmintrin.h>
|
||||||
|
|
||||||
#if __linux__
|
#if __linux__
|
||||||
# define VK_USE_PLATFORM_XCB_KHR
|
# define VK_USE_PLATFORM_XCB_KHR
|
||||||
#elif _WIN32
|
#elif _WIN32
|
||||||
|
|||||||
@ -22,7 +22,7 @@ f32 Radians(f32 deg)
|
|||||||
|
|
||||||
struct Vector(T, int N)
|
struct Vector(T, int N)
|
||||||
{
|
{
|
||||||
static assert(N > 0 && N <= 4);
|
static assert(N > 1 && N <= 4);
|
||||||
|
|
||||||
enum _N = N;
|
enum _N = N;
|
||||||
alias T _T;
|
alias T _T;
|
||||||
@ -35,11 +35,8 @@ struct Vector(T, int N)
|
|||||||
T x;
|
T x;
|
||||||
alias x r;
|
alias x r;
|
||||||
|
|
||||||
static if (N > 1)
|
|
||||||
{
|
|
||||||
T y;
|
T y;
|
||||||
alias y g;
|
alias y g;
|
||||||
}
|
|
||||||
|
|
||||||
static if (N > 2)
|
static if (N > 2)
|
||||||
{
|
{
|
||||||
@ -52,7 +49,7 @@ struct Vector(T, int N)
|
|||||||
T w;
|
T w;
|
||||||
alias w a;
|
alias w a;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
this(Vec3, f32)(Vec3 v3, f32 f) if (N == 4 && is(T: f32))
|
this(Vec3, f32)(Vec3 v3, f32 f) if (N == 4 && is(T: f32))
|
||||||
@ -475,13 +472,6 @@ align(16) struct Matrix(T, int D)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
MatrixVec opBinary(string op)(T factor) if (op == "*")
|
|
||||||
{
|
|
||||||
Matrix result;
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
static if (D == 4)
|
static if (D == 4)
|
||||||
{
|
{
|
||||||
Vec4 opBinary(string op, U)(U x) if (is(U: Vec4) && is(T: f32) && (op == "*"))
|
Vec4 opBinary(string op, U)(U x) if (is(U: Vec4) && is(T: f32) && (op == "*"))
|
||||||
@ -540,41 +530,9 @@ struct Quat
|
|||||||
|
|
||||||
U opCast(U)() if (is(U: Mat4))
|
U opCast(U)() if (is(U: Mat4))
|
||||||
{
|
{
|
||||||
f32 norm = Norm(&vec);
|
Mat4 result;
|
||||||
f32 s = norm > 0.0 ? 2.0 / norm : 0.0;
|
glm_quat_mat4(vec.ptr, result.glm_mat.ptr);
|
||||||
|
return result;
|
||||||
f32 _x = x;
|
|
||||||
f32 _y = y;
|
|
||||||
f32 _z = z;
|
|
||||||
f32 _w = w;
|
|
||||||
|
|
||||||
f32 xx = s * x * x; f32 xy = s * x * y; f32 wx = s * w * x;
|
|
||||||
f32 yy = s * y * y; f32 yz = s * y * z; f32 wy = s * w * y;
|
|
||||||
f32 zz = s * z * z; f32 xz = s * x * z; f32 wz = s * w * z;
|
|
||||||
|
|
||||||
U mat;
|
|
||||||
|
|
||||||
mat[0, 0] = 1.0 - yy - zz;
|
|
||||||
mat[1, 1] = 1.0 - xx - zz;
|
|
||||||
mat[2, 2] = 1.0 - xx - yy;
|
|
||||||
|
|
||||||
mat[0, 1] = xy + wz;
|
|
||||||
mat[1, 2] = yz + wx;
|
|
||||||
mat[2, 0] = xz + wy;
|
|
||||||
|
|
||||||
mat[1, 0] = xy - wz;
|
|
||||||
mat[2, 1] = yz - wx;
|
|
||||||
mat[0, 2] = xz - wy;
|
|
||||||
|
|
||||||
mat[0, 3] = 0.0;
|
|
||||||
mat[1, 3] = 0.0;
|
|
||||||
mat[2, 3] = 0.0;
|
|
||||||
mat[3, 0] = 0.0;
|
|
||||||
mat[3, 1] = 0.0;
|
|
||||||
mat[3, 2] = 0.0;
|
|
||||||
mat[3, 3] = 1.0;
|
|
||||||
|
|
||||||
return mat;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Quat opBinary(string op, U)(U r) if (op == "*" && is(U: Quat))
|
Quat opBinary(string op, U)(U r) if (op == "*" && is(U: Quat))
|
||||||
@ -744,21 +702,12 @@ Mat2Identity()
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pragma(inline): void
|
pragma(inline): Quat
|
||||||
QuatFromAxis(Quat* q, f32 angle, Vec3 axis)
|
QuatFromAxis(f32 angle, Vec3 axis)
|
||||||
{
|
{
|
||||||
Vec3 k;
|
Quat q;
|
||||||
|
glm_quatv(q.vec.ptr, angle, axis.v.ptr);
|
||||||
f32 a = angle * 0.5f;
|
return q;
|
||||||
f32 c = cosf(a);
|
|
||||||
f32 s = sinf(a);
|
|
||||||
|
|
||||||
k = Normalize(axis);
|
|
||||||
|
|
||||||
q.x = s * k.x;
|
|
||||||
q.y = s * k.y;
|
|
||||||
q.z = s * k.z;
|
|
||||||
q.w = c;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pragma(inline): f32
|
pragma(inline): f32
|
||||||
@ -836,6 +785,7 @@ Perspective(f32 fov, f32 aspect, f32 near, f32 far)
|
|||||||
Mat4 res;
|
Mat4 res;
|
||||||
MatZero(&res);
|
MatZero(&res);
|
||||||
glm_perspective(fov, aspect, near, far, res.glm_mat.ptr);
|
glm_perspective(fov, aspect, near, far, res.glm_mat.ptr);
|
||||||
|
res[1, 1] *= -1.0;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user