summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/core.c9
-rw-r--r--src/core/core.h9
-rw-r--r--src/new_render/render.c60
-rw-r--r--src/new_render/render.h2
-rw-r--r--src/systems/terrain.c45
-rw-r--r--src/systems/terrain.h5
6 files changed, 94 insertions, 36 deletions
diff --git a/src/core/core.c b/src/core/core.c
index 38777ba..d508de8 100644
--- a/src/core/core.c
+++ b/src/core/core.c
@@ -20,7 +20,7 @@ Core g_core; /** @brief global `Core` that other files can use */
/** @brief Gets the global `Core` singleton */
inline Core* GetCore() { return &g_core; }
-void Core_Bringup() {
+void Core_Bringup(struct GLFWwindow* optional_window) {
INFO("Initiate Core bringup");
memset(&g_core, 0, sizeof(Core));
@@ -31,10 +31,15 @@ void Core_Bringup() {
g_core.renderer = malloc(Renderer_GetMemReqs());
// initialise all subsystems
- if (!Renderer_Init(conf, g_core.renderer, &g_core.window)) {
+ // renderer config, renderer ptr, ptr to store a window, and optional preexisting glfw window
+ if (!Renderer_Init(conf, g_core.renderer, &g_core.window, optional_window)) {
// FATAL("Failed to start renderer");
ERROR_EXIT("Failed to start renderer\n");
}
+ if (optional_window != NULL) {
+ g_core.window = optional_window;
+ }
+
if (!Input_Init(&g_core.input, g_core.window)) {
// the input system needs the glfw window which is created by the renderer
// hence the order here is important
diff --git a/src/core/core.h b/src/core/core.h
index ac07c50..17561f3 100644
--- a/src/core/core.h
+++ b/src/core/core.h
@@ -26,11 +26,16 @@ struct Renderer;
Core* get_global_core();
-/** @brief Throws error if the core cannot be instantiated */
-void Core_Bringup();
+/**
+ @brief Throws error if the core cannot be instantiated
+ @param [in] optional_window - Leave NULL if you want Celeritas to instantiate its own window with GLFW, if you
+ want to provide the glfw window then pass it in here.
+*/
+void Core_Bringup(GLFWwindow* optional_window);
void Core_Shutdown();
bool ShouldExit();
+GLFWwindow* Core_GetGlfwWindowPtr(Core* core);
struct Renderer* Core_GetRenderer(Core* core);
void Frame_Begin();
diff --git a/src/new_render/render.c b/src/new_render/render.c
index adf56b1..a5eae33 100644
--- a/src/new_render/render.c
+++ b/src/new_render/render.c
@@ -44,7 +44,7 @@ struct Renderer {
Renderer* get_renderer() { return g_core.renderer; }
-bool Renderer_Init(RendererConfig config, Renderer* ren, GLFWwindow** out_window) {
+bool Renderer_Init(RendererConfig config, Renderer* ren, GLFWwindow** out_window, GLFWwindow* optional_window) {
INFO("Renderer init");
ren->frame_arena = arena_create(malloc(FRAME_ARENA_SIZE), FRAME_ARENA_SIZE);
@@ -56,33 +56,49 @@ bool Renderer_Init(RendererConfig config, Renderer* ren, GLFWwindow** out_window
ResourcePools_Init(&pool_arena, ren->resource_pools);
// GLFW window creation
-
- // NOTE: all platforms use GLFW at the moment but thats subject to change
- glfwInit();
-
-#if defined(CEL_REND_BACKEND_OPENGL)
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
-#elif defined(CEL_REND_BACKEND_VULKAN)
- glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
-#endif
-
- GLFWwindow* window =
- glfwCreateWindow(config.scr_width, config.scr_height, config.window_name, NULL, NULL);
- if (window == NULL) {
- ERROR("Failed to create GLFW window\n");
- glfwTerminate();
- return false;
+ GLFWwindow* window;
+ if (optional_window != NULL) {
+ INFO("GLFWwindow pointer was provided!!!! Skipping generic glfw init..");
+ window = optional_window;
+ } else {
+ // NOTE: all platforms use GLFW at the moment but thats subject to change
+ glfwInit();
+
+ #if defined(CEL_REND_BACKEND_OPENGL)
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
+ glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
+ glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
+ #elif defined(CEL_REND_BACKEND_VULKAN)
+ glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
+ #endif
+
+ GLFWwindow* window =
+ glfwCreateWindow(config.scr_width, config.scr_height, config.window_name, NULL, NULL);
+ if (window == NULL) {
+ ERROR("Failed to create GLFW window\n");
+ glfwTerminate();
+ return false;
+ }
}
+
+ // #if defined(CEL_REND_BACKEND_OPENGL)
+ // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
+ // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
+ // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
+ // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
+ // #elif defined(CEL_REND_BACKEND_VULKAN)
+ // glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
+ // #endif
+
ren->window = window;
*out_window = window;
glfwMakeContextCurrent(ren->window);
- DEBUG("Set up GLFW window callbacks");
- glfwSetWindowSizeCallback(window, Render_WindowSizeChanged);
+ // FIXME
+ // DEBUG("Set up GLFW window callbacks");
+ // glfwSetWindowSizeCallback(window, Render_WindowSizeChanged);
// set the RAL backend up
if (!GPU_Backend_Init(config.window_name, window, ren->resource_pools)) {
diff --git a/src/new_render/render.h b/src/new_render/render.h
index 233c934..1f80290 100644
--- a/src/new_render/render.h
+++ b/src/new_render/render.h
@@ -28,7 +28,7 @@ typedef struct RenderCtx {
// --- Lifecycle
-PUB bool Renderer_Init(RendererConfig config, Renderer* renderer, GLFWwindow** out_window);
+PUB bool Renderer_Init(RendererConfig config, Renderer* renderer, GLFWwindow** out_window, GLFWwindow* optional_window);
PUB void Renderer_Shutdown(Renderer* renderer);
PUB size_t Renderer_GetMemReqs();
void Render_WindowSizeChanged(GLFWwindow* window, i32 new_width, i32 new_height);
diff --git a/src/systems/terrain.c b/src/systems/terrain.c
index c19ba33..3743a9b 100644
--- a/src/systems/terrain.c
+++ b/src/systems/terrain.c
@@ -8,6 +8,7 @@
#include "log.h"
#include "maths.h"
#include "mem.h"
+#include "ral_common.h"
#include "ral_impl.h"
#include "ral_types.h"
#include "render.h"
@@ -27,10 +28,6 @@ bool Terrain_Init(Terrain_Storage* storage) {
};
storage->hmap_renderpass = GPU_Renderpass_Create(rpass_desc);
- VertexDescription builder = { .debug_label = "pos only" };
- VertexDesc_AddAttr(&builder, "inPosition", ATTR_F32x3);
- builder.use_full_vertex_size = true;
-
arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024);
Str8 vert_path = str8("assets/shaders/terrain.vert");
@@ -42,12 +39,13 @@ bool Terrain_Init(Terrain_Storage* storage) {
}
ShaderData camera_data = { .get_layout = &Binding_Camera_GetLayout };
+ ShaderData terrain_data = { .get_layout = &TerrainUniforms_GetLayout};
GraphicsPipelineDesc pipeline_desc = {
.debug_name = "terrain rendering pipeline",
- .vertex_desc = builder,
- .data_layouts = { camera_data },
- .data_layouts_count = 1,
+ .vertex_desc = static_3d_vertex_description(),
+ .data_layouts = { camera_data, terrain_data },
+ .data_layouts_count = 2,
.vs = {
.debug_name = "terrain vertex shader",
.filepath = vert_path,
@@ -58,10 +56,12 @@ bool Terrain_Init(Terrain_Storage* storage) {
.filepath = frag_path,
.code = fragment_shader.contents,
},
- .wireframe = true,
+ .wireframe = false,
};
storage->hmap_pipeline = GPU_GraphicsPipeline_Create(pipeline_desc, storage->hmap_renderpass);
+ storage->texture = TextureLoadFromFile("assets/demo/textures/grass2.png");
+
return true;
}
@@ -94,7 +94,11 @@ void Terrain_LoadHeightmap(Terrain_Storage* storage, Heightmap hmap, f32 grid_sc
assert(index < num_vertices);
f32 height = Heightmap_HeightXZ(&hmap, i, j);
- Vertex v = { .pos_only.position = vec3_create(i * grid_scale, height, j * grid_scale) };
+ Vec3 v_pos = vec3_create(i * grid_scale, height, j * grid_scale);
+ Vec3 v_normal = VEC3_Y;
+ float tiling_factor = 505.0f;
+ Vec2 v_uv = vec2((f32)i / width * tiling_factor , (f32)j / height * tiling_factor);
+ Vertex v = { .static_3d = {.position = v_pos, .normal = v_normal, .tex_coords = v_uv} };
Vertex_darray_push(vertices, v);
index++;
}
@@ -165,6 +169,13 @@ void Terrain_Draw(Terrain_Storage* storage) {
GPU_EncodeBindShaderData(
enc, 0, (ShaderData){ .data = &camera_data, .get_layout = &Binding_Camera_GetLayout });
+ TerrainUniforms uniforms = {
+ .tex_slot_1 = storage->texture
+ };
+ ShaderData terrain_data = { .data = &uniforms, .get_layout = &TerrainUniforms_GetLayout};
+ GPU_EncodeBindShaderData(enc, 1, terrain_data);
+
+
GPU_EncodeSetVertexBuffer(enc, storage->vertex_buffer);
GPU_EncodeSetIndexBuffer(enc, storage->index_buffer);
@@ -179,4 +190,20 @@ f32 Heightmap_HeightXZ(const Heightmap* hmap, u32 x, u32 z) {
u8 channel = bytes[position];
float value = (float)channel / 2.0; /// 255.0;
return value;
+}
+
+ShaderDataLayout TerrainUniforms_GetLayout(void* data) {
+ TerrainUniforms* d = data;
+ bool has_data = data != NULL;
+
+ ShaderBinding b1 = {
+ .label = "TextureSlot1",
+ .kind = BINDING_TEXTURE,
+ .vis = VISIBILITY_FRAGMENT,
+ };
+
+ if (has_data) {
+ b1.data.texture.handle = d->tex_slot_1;
+ }
+ return (ShaderDataLayout){ .bindings = { b1 }, .binding_count = 1 };
} \ No newline at end of file
diff --git a/src/systems/terrain.h b/src/systems/terrain.h
index 2e05a14..664aa8e 100644
--- a/src/systems/terrain.h
+++ b/src/systems/terrain.h
@@ -35,6 +35,7 @@ typedef struct Heightmap {
Heightmap heightmap; // NULL = no heightmap
GPU_Renderpass* hmap_renderpass;
GPU_Pipeline* hmap_pipeline;
+ TextureHandle texture;
bool hmap_loaded;
BufferHandle vertex_buffer;
@@ -69,4 +70,8 @@ Vec3 Heightmap_NormalXZ(const Heightmap* hmap, f32 x, f32 z);
// /** @brief Generate the `geometry_data` for a heightmap ready to be uploaded to the GPU */
// Geometry geo_heightmap(arena* a, Heightmap heightmap);
+typedef struct TerrainUniforms {
+ TextureHandle tex_slot_1;
+} TerrainUniforms;
+
ShaderDataLayout TerrainUniforms_GetLayout(void* data); \ No newline at end of file