summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2023-11-11 19:00:10 +0200
committerkartofen <mladenovnasko0@gmail.com>2023-11-11 19:00:10 +0200
commit1e436946514f3a0e9fe3d091394db6b86e06f033 (patch)
treecddf3bb618ade66fd5bd4eacd028a7d54a760e37 /src/main.c
parent2a733f3d513a7279c1df4efe88fc5386ced2ed14 (diff)
.obj loading
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c73
1 files changed, 57 insertions, 16 deletions
diff --git a/src/main.c b/src/main.c
index c4bf2c3..edaf8a3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -4,12 +4,15 @@
#include <cglm/cglm.h>
#include <cglm/struct.h>
+#include "objload.h"
+
+#define MONKEY
#include "graphics.h"
#include "window.h"
#include "common.h"
-#define SW 640
-#define SH 480
+u32 width = 640;
+u32 height = 480;
window_t window;
graphics_t graphics;
@@ -21,19 +24,24 @@ struct ubo {
mat4 model;
mat4 view;
mat4 proj;
-};
+};
+
+#ifndef MONKEY
vertex_t vertices[] = {
- {{-0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}},
- {{0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}},
- {{0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}},
- {{-0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}}
+ {{-0.5f, -0.5f, 0.0f}, {0.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}},
+ {{ 0.5f, -0.5f, -0.5f}, {0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}},
+ {{ 0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}},
+ {{-0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}},
};
+#endif
+
u32 indices[] = {
0, 1, 2, 2, 3, 0
};
+
int main(void)
{
int ret = 1;
@@ -46,9 +54,9 @@ int main(void)
// populate window info
struct window_info win_info = {0};
win_info.title = "Test App";
- // win_info.flags = SDL_WINDOW_RESIZABLE;
- win_info.w = SW;
- win_info.h = SH;
+ win_info.flags = SDL_WINDOW_RESIZABLE;
+ win_info.w = width;
+ win_info.h = height;
// create window
window = window_create(&win_info);
@@ -56,6 +64,33 @@ int main(void)
err("window_create: failed");
goto f2;
}
+
+#ifdef MONKEY
+ vertex_t *objvertices;
+ size_t nvertices = 0;
+
+ // Load obj
+ FILE *fp = fopen("../files/monkey.obj", "r");
+
+ objload_t obj = {0};
+ obj_load(fp, &obj);
+
+ struct obj_struct_metadata obj_metadata = {
+ sizeof(vertex_t),
+ offsetof(vertex_t, pos),
+ offsetof(vertex_t, color), 0,
+ OBJ_STRUCT_VERT_EXISTS |
+ OBJ_STRUCT_NORM_EXISTS
+ };
+
+ obj_transfer_raw(obj, obj_metadata, &nvertices, NULL);
+ // leaks memory
+ objvertices = xcalloc(nvertices, sizeof(*objvertices));
+ obj_transfer_raw(obj, obj_metadata, &nvertices, objvertices);
+
+ obj_free(obj);
+ fclose(fp);
+#endif
// get extensions
unsigned int ext_count = 0;
@@ -71,11 +106,17 @@ int main(void)
grph_info.ext_count = ext_count;
grph_info.extensions = (const char * const *)extensions;
-
+
+#ifndef MONKEY
grph_info.vertices = vertices;
grph_info.nvertices = ARR_SIZE(vertices);
+#else
+ grph_info.vertices = objvertices;
+ grph_info.nvertices = nvertices;
+#endif
+
grph_info.indices = indices;
- grph_info.nindices = ARR_SIZE(indices);
+ grph_info.nindices = ARR_SIZE(indices);
grph_info.ubo_size = sizeof(struct ubo);
grph_info.update_ubo = update_ubo;
@@ -120,12 +161,12 @@ int update_ubo(void *uniform_buffer)
time += 0.01f;
memcpy(ubo->model, glms_mat4_identity().raw, sizeof(ubo->model));
- glm_rotate(&ubo->model, time * glm_rad(90.0f),
+ glm_rotate(ubo->model, time * glm_rad(90.0f),
(vec3){0.0f, 0.0f, 1.0f});
- glm_lookat((vec3){1.0f, 1.0f, 2.0f}, (vec3){0.0f, 0.0f, 0.0f}, (vec3){0.0f, 0.0f, 1.0f}, ubo->view);
-
- glm_perspective(glm_rad(45.0f), (float)SW/(float)SH, 0.1f, 10.0f, ubo->proj);
+ glm_lookat((vec3){2.0f, 2.0f, 2.0f}, (vec3){0.0f, 0.0f, 0.0f}, (vec3){0.0f, 0.0f, 1.0f}, ubo->view);
+
+ glm_perspective(glm_rad(45.0f), (float)width/(float)height, 0.1f, 10.0f, ubo->proj);
ubo->proj[1][1] *= -1;
return 0;