Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Commit

Permalink
3d fly cam in example_current
Browse files Browse the repository at this point in the history
  • Loading branch information
krisvers committed Feb 25, 2024
1 parent d19d25b commit cc11bbd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
Binary file modified kgfx-examples
Binary file not shown.
47 changes: 38 additions & 9 deletions src/example_current.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ int example_current() {
return 1;
}

const struct aiScene* scene = aiImportFile("assets/mesh.obj", aiProcess_Triangulate | aiProcess_FlipUVs);
const struct aiScene* scene = aiImportFile("assets/mesh.obj", aiProcess_Triangulate);
if (scene == NULL) {
printf("Failed to import GLTF scene\n");
return 1;
Expand Down Expand Up @@ -201,12 +201,12 @@ int example_current() {
kgfxDestroyShader(ctx, fshader);

const struct aiMesh* gltf_mesh = scene->mMeshes[0];
/* create buffer (vertex buffer) based on type vertex_t, and indexBuffer using gltf_mesh */

KGFXbufferdesc bufferDesc;
bufferDesc.location = KGFX_BUFFER_LOCATION_GPU;
bufferDesc.usage = KGFX_BUFFER_USAGE_VERTEX_BUFFER;
bufferDesc.size = sizeof(vertex_t) * gltf_mesh->mNumVertices;
/* vertex_t has 3 float pos, 2 float uv */

vertex_t* vertices = malloc(sizeof(vertex_t) * gltf_mesh->mNumVertices);
for (unsigned int i = 0; i < gltf_mesh->mNumVertices; ++i) {
vertices[i].pos[0] = gltf_mesh->mVertices[i].x;
Expand Down Expand Up @@ -349,6 +349,7 @@ int example_current() {
}

camera_t camera = {
.position = { 0, 0, 5 },
.fov = 60.0f,
.aspect = 800.0f / 600.0f,
.near = 0.1f,
Expand All @@ -357,22 +358,47 @@ int example_current() {
mat4x4 m, v, p;
mat4x4_identity(m);

vec3 forward;
vec3 up = { 0, 1, 0 };
vec3 right;

float delta = 0;
float startTime = 0;
const float moveSpeed = 5.0f;
while (!glfwWindowShouldClose(window)) {
if (key(GLFW_KEY_ESCAPE)) {
break;
}

startTime = glfwGetTime();

up[1] = 1;
forward[0] = sinf(camera.rotation[1] * (M_PI / 180.0));
forward[1] = -sinf(camera.rotation[0] * (M_PI / 180.0));
forward[2] = -cosf(camera.rotation[1] * (M_PI / 180.0));
vec3_mul_cross(right, forward, up);

vec3_scale(forward, forward, delta * moveSpeed);
vec3_scale(up, up, delta * moveSpeed);
vec3_scale(right, right, delta * moveSpeed);

if (key(GLFW_KEY_W)) {
camera.position[2] -= 0.1f;
vec3_add(camera.position, camera.position, forward);
}
if (key(GLFW_KEY_S)) {
camera.position[2] += 0.1f;
vec3_sub(camera.position, camera.position, forward);
}
if (key(GLFW_KEY_A)) {
camera.position[0] -= 0.1f;
vec3_sub(camera.position, camera.position, right);
}
if (key(GLFW_KEY_D)) {
camera.position[0] += 0.1f;
vec3_add(camera.position, camera.position, right);
}
if (key(GLFW_KEY_E)) {
vec3_add(camera.position, camera.position, up);
}
if (key(GLFW_KEY_Q)) {
vec3_sub(camera.position, camera.position, up);
}

if (key(GLFW_KEY_LEFT)) {
Expand All @@ -388,19 +414,22 @@ int example_current() {
camera.rotation[0] += 1.0f;
}

mat4x4_translate(v, -camera.position[0], -camera.position[1], -camera.position[2]);
mat4x4_translate(v, -camera.position[0], camera.position[1], -camera.position[2]);
mat4x4_perspective(p, camera.fov * (M_PI / 180.0), camera.aspect, camera.near, camera.far);
mat4x4_rotate_X(p, p, -camera.rotation[0] * (M_PI / 180.0));
mat4x4_rotate_Y(p, p, camera.rotation[1] * (M_PI / 180.0));
mat4x4_rotate_Z(p, p, -camera.rotation[2] * (M_PI / 180.0));

v[1][1] = -1;
mat4x4_mul(mapped, p, v);
mat4x4_mul(matrixData, p, v);
memcpy(mapped, matrixData, sizeof(f32) * 16);
kgfxRender(ctx, pipeline);
glfwSwapBuffers(window);

memcpy(prev_keys, keys, sizeof(keys));
glfwPollEvents();

delta = glfwGetTime() - startTime;
}
kgfxBufferUnmap(ctx, uBuffer);

Expand Down

0 comments on commit cc11bbd

Please sign in to comment.