summaryrefslogtreecommitdiff
path: root/src/renderer
diff options
context:
space:
mode:
Diffstat (limited to 'src/renderer')
-rw-r--r--src/renderer/render.c78
-rw-r--r--src/renderer/render.h5
-rw-r--r--src/renderer/render_types.h74
3 files changed, 153 insertions, 4 deletions
diff --git a/src/renderer/render.c b/src/renderer/render.c
index 4e9ad89..f12954f 100644
--- a/src/renderer/render.c
+++ b/src/renderer/render.c
@@ -1,10 +1,19 @@
+#define STB_IMAGE_IMPLEMENTATION
+#include <stb_image.h>
+
+#define STB_TRUETYPE_IMPLEMENTATION
+#include <stb_truetype.h>
+
#include "render.h"
+#include <glad/glad.h>
#include <glfw3.h>
#include "log.h"
#include "render_backend.h"
+material DEFAULT_MATERIAL = { 0 };
+
bool renderer_init(renderer* ren) {
INFO("Renderer init");
@@ -39,4 +48,71 @@ void render_frame_end(renderer* ren) {
// present frame
glfwSwapBuffers(ren->window);
glfwPollEvents();
-} \ No newline at end of file
+}
+
+void default_material_init() {
+ INFO("Load default material")
+ DEFAULT_MATERIAL.ambient_colour = (vec3){ 0.5, 0.5, 0.5 };
+ DEFAULT_MATERIAL.diffuse = (vec3){ 0.8, 0.8, 0.8 };
+ DEFAULT_MATERIAL.specular = (vec3){ 1.0, 1.0, 1.0 };
+ DEFAULT_MATERIAL.diffuse_texture = texture_data_load("assets/textures/white1x1.png", false);
+ DEFAULT_MATERIAL.specular_texture = texture_data_load("assets/textures/black1x1.png", false);
+ DEFAULT_MATERIAL.spec_exponent = 32.0;
+ strcpy(DEFAULT_MATERIAL.name, "Default");
+ texture_data_upload(&DEFAULT_MATERIAL.diffuse_texture);
+ texture_data_upload(&DEFAULT_MATERIAL.specular_texture);
+}
+
+texture texture_data_load(const char* path, bool invert_y) {
+ TRACE("Load texture %s", path);
+
+ // load the file data
+ // texture loading
+ int width, height, num_channels;
+ stbi_set_flip_vertically_on_load(invert_y);
+
+#pragma GCC diagnostic ignored "-Wpointer-sign"
+ char* data = stbi_load(path, &width, &height, &num_channels, 0);
+ if (data) {
+ DEBUG("loaded texture: %s", path);
+ } else {
+ WARN("failed to load texture");
+ }
+
+ unsigned int channel_type;
+ if (num_channels == 4) {
+ channel_type = GL_RGBA;
+ } else {
+ channel_type = GL_RGB;
+ }
+
+ return (texture){ .texture_id = 0,
+ .width = width,
+ .height = height,
+ .channel_count = num_channels,
+ .channel_type = channel_type,
+ .name = "TODO: Texture names",
+ .image_data = data };
+}
+
+void texture_data_upload(texture* tex) {
+ TRACE("Upload texture data");
+ u32 texture_id;
+ glGenTextures(1, &texture_id);
+ glBindTexture(GL_TEXTURE_2D, texture_id);
+ tex->texture_id = texture_id;
+
+ // set the texture wrapping parameters
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
+ GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ // set texture filtering parameters
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex->width, tex->height, 0, tex->channel_type,
+ GL_UNSIGNED_BYTE, tex->image_data);
+ glGenerateMipmap(GL_TEXTURE_2D);
+ DEBUG("Freeing texture image data after uploading to GPU");
+ // stbi_image_free(tex->image_data); // data is on gpu now so we dont need it around
+}
diff --git a/src/renderer/render.h b/src/renderer/render.h
index c89c364..d1de515 100644
--- a/src/renderer/render.h
+++ b/src/renderer/render.h
@@ -1,6 +1,7 @@
#pragma once
#include "render_types.h"
+#include "loaders.h"
// --- Lifecycle
/** @brief initialise the render system frontend */
@@ -13,4 +14,6 @@ void renderer_shutdown(renderer* ren);
void render_frame_begin(renderer* ren);
void render_frame_end(renderer* ren);
-// --- \ No newline at end of file
+// ---
+texture texture_data_load(const char* path, bool invert_y); // #frontend
+void texture_data_upload(texture* tex); // #backend
diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h
index e24fc24..45851fe 100644
--- a/src/renderer/render_types.h
+++ b/src/renderer/render_types.h
@@ -13,7 +13,11 @@
struct GLFWwindow;
+#define MAX_MATERIAL_NAME_LEN 256
+#define MAX_TEXTURE_NAME_LEN 256
+
#ifndef RESOURCE_HANDLE_DEFS
+CORE_DEFINE_HANDLE(model_handle);
CORE_DEFINE_HANDLE(texture_handle);
#define RESOURCE_HANDLE_DEFS
#endif
@@ -36,6 +40,42 @@ typedef struct renderer {
renderer_config config;
} renderer;
+// --- Lighting & Materials
+
+typedef struct texture {
+ u32 texture_id;
+ char name[MAX_TEXTURE_NAME_LEN];
+ void *image_data;
+ u32 width;
+ u32 height;
+ u8 channel_count;
+ u32 channel_type;
+} texture;
+
+typedef struct blinn_phong_material {
+ char name[MAX_MATERIAL_NAME_LEN];
+ texture diffuse_texture;
+ char diffuse_tex_path[256];
+ texture specular_texture;
+ char specular_tex_path[256];
+ vec3 ambient_colour;
+ vec3 diffuse;
+ vec3 specular;
+ f32 spec_exponent;
+ bool is_loaded;
+ bool is_uploaded;
+} blinn_phong_material;
+typedef blinn_phong_material material; // when we start using PBR, this will no longer be the case
+
+// the default blinn-phong material. MUST be initialised with the function below
+extern material DEFAULT_MATERIAL;
+void default_material_init();
+
+#ifndef TYPED_MATERIAL_ARRAY
+KITC_DECL_TYPED_ARRAY(material) // creates "material_darray"
+#define TYPED_MATERIAL_ARRAY
+#endif
+
/** @brief Vertex format for a static mesh */
typedef struct vertex {
vec3 position;
@@ -57,7 +97,7 @@ typedef struct mesh {
u32 *indices;
u32 indices_len;
size_t material_index;
- u32 vbo, vao; /** OpenGL data */
+ u32 vbo, vao; /** OpenGL data. TODO: dont leak OpenGL details */
} mesh;
#ifndef TYPED_MESH_ARRAY
@@ -67,9 +107,39 @@ KITC_DECL_TYPED_ARRAY(mesh) // creates "mesh_darray"
typedef struct model {
str8 name;
+ mesh_darray meshes;
+ aabb_3d bbox;
+ material_darray *materials;
+ bool is_loaded;
+ bool is_uploaded;
} model;
#ifndef TYPED_MODEL_ARRAY
KITC_DECL_TYPED_ARRAY(model) // creates "model_darray"
#define TYPED_MODEL_ARRAY
-#endif \ No newline at end of file
+#endif
+
+// --- Graphics API related
+
+typedef enum cel_primitive_topology {
+ CEL_PRIMITIVE_TOPOLOGY_POINT,
+ CEL_PRIMITIVE_TOPOLOGY_LINE,
+ CEL_PRIMITIVE_TOPOLOGY_LINE_STRIP,
+ CEL_PRIMITIVE_TOPOLOGY_TRIANGLE,
+ CEL_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
+ CEL_PRIMITIVE_TOPOLOGY_COUNT
+} cel_primitive_topology;
+
+typedef enum gpu_texture_type {
+ TEXTURE_TYPE_2D,
+ TEXTURE_TYPE_3D,
+ TEXTURE_TYPE_2D_ARRAY,
+ TEXTURE_TYPE_CUBE_MAP,
+ TEXTURE_TYPE_COUNT
+} gpu_texture_type;
+
+typedef enum gpu_texture_format {
+ TEXTURE_FORMAT_8_8_8_8_RGBA_UNORM,
+ TEXTURE_FORMAT_DEPTH_DEFAULT,
+ TEXTURE_FORMAT_COUNT
+} gpu_texture_format;