From ef5febdde3623fae2687f20e7d9c71e8681a6616 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sun, 29 Mar 2026 18:42:59 -0500 Subject: [PATCH] Fixed dolphin rendering. --- assets/scene/minesweeper.lua | 8 ++-- src/dusk/display/camera/camera.c | 2 +- src/dusk/display/display.c | 42 ++++++++++++++++++- src/dusk/script/module/display/moduleshader.c | 26 ++++++++++++ .../display/camera/cameradolphin.c | 4 ++ .../display/shader/shaderdolphin.c | 8 ++-- 6 files changed, 81 insertions(+), 9 deletions(-) diff --git a/assets/scene/minesweeper.lua b/assets/scene/minesweeper.lua index 3ece37a..a879422 100644 --- a/assets/scene/minesweeper.lua +++ b/assets/scene/minesweeper.lua @@ -195,13 +195,16 @@ function sceneRender() view = cameraGetViewMatrix(camera) shaderSetMatrix(SHADER_UNLIT, SHADER_UNLIT_VIEW, view) - -- textDraw(10, 10, "Hello World\nHow are you?") - + shaderSetTexture(SHADER_UNLIT, SHADER_UNLIT_TEXTURE, nil) spriteBatchPush( x, y, x + 32, y + 32, colorWhite() ) + spriteBatchFlush() + + textDraw(10, 10, "Hello World\nHow are you?", colorRed()) + spriteBatchFlush() -- Update mouse position -- if INPUT_POINTER then @@ -258,5 +261,4 @@ function sceneRender() -- ) -- end -- end - spriteBatchFlush() end \ No newline at end of file diff --git a/src/dusk/display/camera/camera.c b/src/dusk/display/camera/camera.c index d3ec349..a569284 100644 --- a/src/dusk/display/camera/camera.c +++ b/src/dusk/display/camera/camera.c @@ -37,7 +37,7 @@ void cameraInitOrthographic(camera_t *camera) { camera->orthographic.right = SCREEN.width; camera->orthographic.top = 0.0f; camera->orthographic.bottom = SCREEN.height; - camera->nearClip = -1.0f; + camera->nearClip = 0.1f; camera->farClip = 1.0f; camera->viewType = CAMERA_VIEW_TYPE_2D; diff --git a/src/dusk/display/display.c b/src/dusk/display/display.c index 45fb942..43b451c 100644 --- a/src/dusk/display/display.c +++ b/src/dusk/display/display.c @@ -23,6 +23,24 @@ #include "script/module/display/moduleshader.h" display_t DISPLAY = { 0 }; + mesh_t mesh; + meshvertex_t vertices[3] = { + { + .color = { 255, 0, 0, 255 }, + .uv = { 0.0f, 0.0f }, + .pos = { 0.0f, 0.5f, 0.0f } + }, + { + .color = { 0, 255, 0, 255 }, + .uv = { 0.5f, 1.0f }, + .pos = { -0.5f, -0.5f, 0.0f } + }, + { + .color = { 0, 0, 255, 255 }, + .uv = { 1.0f, 0.0f }, + .pos = { 0.5f, -0.5f, 0.0f } + } + }; errorret_t displayInit(void) { memoryZero(&DISPLAY, sizeof(DISPLAY)); @@ -50,6 +68,9 @@ errorret_t displayInit(void) { errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_MODEL, mat)); errorChain(shaderSetTexture(&SHADER_UNLIT, SHADER_UNLIT_TEXTURE, NULL)); + + errorChain(meshInit(&mesh, MESH_PRIMITIVE_TYPE_TRIANGLES, 3, vertices)); + errorOk(); } @@ -69,7 +90,26 @@ errorret_t displayUpdate(void) { SCREEN.background ); - errorChain(sceneRender()); + camera_t cam; + cameraInitOrthographic(&cam); + cam.orthographic.right = SCREEN.width; + cam.orthographic.top = SCREEN.height; + cam.orthographic.left = 0; + cam.orthographic.bottom = 0; + + mat4 mat; + cameraGetProjectionMatrix(&cam, mat); + errorChain(shaderBind(&SHADER_UNLIT)); + errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_PROJECTION, mat)); + cameraGetViewMatrix(&cam, mat); + errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_VIEW, mat)); + glm_mat4_identity(mat); + errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_MODEL, mat)); + + textDraw(32, 32, "Hello World", COLOR_WHITE, &DEFAULT_FONT_TILESET, &DEFAULT_FONT_TEXTURE); + spriteBatchFlush(); + + // errorChain(sceneRender()); // Render UI // uiRender(); diff --git a/src/dusk/script/module/display/moduleshader.c b/src/dusk/script/module/display/moduleshader.c index 57768b8..71644c5 100644 --- a/src/dusk/script/module/display/moduleshader.c +++ b/src/dusk/script/module/display/moduleshader.c @@ -90,5 +90,31 @@ int moduleShaderSetMatrix(lua_State *l) { int moduleShaderSetTexture(lua_State *l) { assertNotNull(l, "Lua state cannot be NULL."); + + shader_t *shader = (shader_t *)lua_touserdata(l, 1); + assertNotNull(shader, "Shader pointer cannot be NULL."); + + const char_t *uniformName = luaL_checkstring(l, 2); + assertStrLenMin(uniformName, 1, "Uniform name cannot be empty."); + + texture_t *texture; + // Texture can be Nil or a pointer, if not nil it must be a texture pointer. + if(lua_isnil(l, 3)) { + texture = NULL; + } else if(lua_isuserdata(l, 3)) { + texture = (texture_t *)lua_touserdata(l, 3); + assertNotNull(texture, "Texture pointer cannot be NULL."); + } else { + luaL_error(l, "Third argument must be a texture_mt userdata or nil."); + return 0; + } + + errorret_t ret = shaderSetTexture(shader, uniformName, texture); + if(ret.code != ERROR_OK) { + luaL_error(l, "Failed to set shader texture: %s", ret.state->message); + errorCatch(errorPrint(ret)); + return 0; + } + return 0; } \ No newline at end of file diff --git a/src/duskdolphin/display/camera/cameradolphin.c b/src/duskdolphin/display/camera/cameradolphin.c index 639561d..6af808f 100644 --- a/src/duskdolphin/display/camera/cameradolphin.c +++ b/src/duskdolphin/display/camera/cameradolphin.c @@ -11,6 +11,10 @@ void cameraPushMatrixDolphin(camera_t *camera) { assertNotNull(camera, "Camera cannot be null"); + assertTrue( + camera->nearClip > 0.0f, + "Camera near clip must be greater than 0 for Dolphin" + ); Mtx44 guProjection; Mtx guView; diff --git a/src/duskdolphin/display/shader/shaderdolphin.c b/src/duskdolphin/display/shader/shaderdolphin.c index 9b70c27..87c348a 100644 --- a/src/duskdolphin/display/shader/shaderdolphin.c +++ b/src/duskdolphin/display/shader/shaderdolphin.c @@ -60,15 +60,15 @@ errorret_t shaderSetMatrixDolphin( if(stringCompare(name, SHADER_UNLIT_PROJECTION) == 0) { shader->dirtyMatrix |= SHADER_DOLPHIN_DIRTY_PROJ; - glm_mat4_ucopy(mat, shader->proj); + glm_mat4_copy(mat, shader->proj); } else if(stringCompare(name, SHADER_UNLIT_VIEW) == 0) { shader->dirtyMatrix |= SHADER_DOLPHIN_DIRTY_VIEW; - glm_mat4_ucopy(mat, shader->view); + glm_mat4_copy(mat, shader->view); } else if(stringCompare(name, SHADER_UNLIT_MODEL) == 0) { shader->dirtyMatrix |= SHADER_DOLPHIN_DIRTY_MODEL; - glm_mat4_ucopy(mat, shader->model); + glm_mat4_copy(mat, shader->model); } else { assertUnreachable("Cannot use a custom matrix on dolphin."); @@ -107,7 +107,7 @@ errorret_t shaderSetTextureDolphin( GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORDNULL, GX_TEXMAP_NULL, GX_COLOR0A0); GX_SetTevOp(GX_TEVSTAGE0, GX_PASSCLR); - // GX_SetBlendMode(GX_BM_NONE, GX_BL_ONE, GX_BL_ZERO, GX_LO_CLEAR); + GX_SetBlendMode(GX_BM_NONE, GX_BL_ONE, GX_BL_ZERO, GX_LO_CLEAR); GX_SetAlphaCompare(GX_ALWAYS, 0, GX_AOP_AND, GX_ALWAYS, 0); errorOk();