From f439828dc068974ddc161cc3469ac489a93bbabc Mon Sep 17 00:00:00 2001 From: nathansmithsmith Date: Sat, 23 Dec 2023 17:04:05 -0700 Subject: Working on the web assembly openglES issue --- CMakeLists.txt | 10 ++++++++-- src/entity.c | 9 +-------- src/gameCommon.h | 7 +++++++ src/main.c | 8 +------- src/screens/infoScreen.c | 3 +-- src/util.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/util.h | 2 ++ 7 files changed, 62 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b37f16b..c2f665e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,3 @@ - cmake_minimum_required(VERSION 3.11) project(KillaFacsista VERSION 1.0) @@ -17,8 +16,15 @@ file(GLOB SRC_FILES src/*.c src/entities/*.c src/levels/*.c src/screens/*.c) add_executable(${PROJECT_NAME} ${SRC_FILES}) +# Link the mother fucking assets. +if (${PLATFORM} MATCHES "Web") + set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "--preload-file assets.rres") +endif() + +set(RAYLIB raylib) + target_include_directories(${PROJECT_NAME} PUBLIC include src) -target_link_libraries(${PROJECT_NAME} raylib m glfw) +target_link_libraries(${PROJECT_NAME} ${RAYLIB} m glfw) file(COPY assets/assets.rres DESTINATION ${CMAKE_BINARY_DIR}) diff --git a/src/entity.c b/src/entity.c index a44220a..f36c941 100644 --- a/src/entity.c +++ b/src/entity.c @@ -368,14 +368,7 @@ RayCollision traceRayToEntityRadius(Entity entity, Ray ray, float scale) { // Basic wireframe drawing. void entityDraw(Entity * entity) { - entity->model->transform = QuaternionToMatrix(entity->rotation); - - DrawModelWires( - *entity->model, - entity->position, - 1.0, - GREEN - ); + drawModelWireframe(*entity->model, entity->position, entity->rotation, GREEN); } void entityUpdatePosition(Entity * entity) { diff --git a/src/gameCommon.h b/src/gameCommon.h index 2c655e3..fb0dfb4 100644 --- a/src/gameCommon.h +++ b/src/gameCommon.h @@ -14,6 +14,12 @@ #include #include "raygui.h" +//#define PLATFORM_WEB + +#if defined(PLATFORM_WEB) + #include +#endif + #ifndef GAME_COMMON_H #define GAME_COMMON_H @@ -33,6 +39,7 @@ typedef struct Entity Entity; #define KF_CALLOC(nmemb, size) calloc(nmemb, size) #define KF_REALLOC(ptr, size) realloc(ptr, size) #define KF_REALLOCARRAY(ptr, nmemb, size) reallocarray(ptr, nmemb, size) +//#define KF_REALLOCARRAY(ptr, nmemb, size) realloc(ptr, nmemb * size) #define KF_FREE(ptr) free(ptr) #define ALLOCATION_ERROR TraceLog(LOG_ERROR, "Allocation error in %s:%d", __FILE__, __LINE__) diff --git a/src/main.c b/src/main.c index d827561..6243100 100644 --- a/src/main.c +++ b/src/main.c @@ -1,12 +1,6 @@ #include "gameCommon.h" #include "game.h" -//#define PLATFORM_WEB - -#if defined(PLATFORM_WEB) - #include -#endif - Game game; void updateFrame() { @@ -16,7 +10,7 @@ void updateFrame() { int main(int argc, char ** argv) { initGame(&game); -#if defined(PLATFORM_WEB) +#ifdef PLATFORM_WEB emscripten_set_main_loop(updateFrame, 0, 1); #else while (!WindowShouldClose()) { diff --git a/src/screens/infoScreen.c b/src/screens/infoScreen.c index 9e7f8d5..73079de 100644 --- a/src/screens/infoScreen.c +++ b/src/screens/infoScreen.c @@ -242,8 +242,7 @@ void updateInfoScreen(Game * game) { // Draw shit. BeginMode3D(infoScreen->camera); - game->assets.models[info.assetId].transform = QuaternionToMatrix(infoScreen->rotations[currentEntity]); - DrawModelWires(game->assets.models[info.assetId], Vector3Zero(), 1.0, GREEN); + drawModelWireframe(game->assets.models[info.assetId], Vector3Zero(), infoScreen->rotations[currentEntity], GREEN); EndMode3D(); } diff --git a/src/util.c b/src/util.c index 8165e11..692ae4b 100644 --- a/src/util.c +++ b/src/util.c @@ -81,3 +81,45 @@ void copyTriangle3D(Triangle3D a, const Triangle3D b) { a[1] = b[1]; a[2] = b[2]; } + +Vector3 getVertexAt(Mesh mesh, int vertexNum) { + return (Vector3){ + mesh.vertices[vertexNum * 3], + mesh.vertices[vertexNum * 3 + 1], + mesh.vertices[vertexNum * 3 + 2] + }; +} + +void drawModelWireframe(Model model, Vector3 position, Quaternion rotation, Color color) { + // DrawModelWires doesn't work on the version of opengl that web assembly uses. + // Instead I use this stupid peice of shit. + +#if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) + model.transform = QuaternionToMatrix(rotation); + DrawModelWires(model, position, 1.0, color); +#else + for (int i = 0; i < model.meshCount; ++i) { + Mesh mesh = model.meshes[i]; + + // Loop through the triangles. + for (int triangleNum = 0; triangleNum < mesh.triangleCount; ++triangleNum) { + Triangle3D triangle = { + getVertexAt(mesh, triangleNum * 3), + getVertexAt(mesh, triangleNum * 3 + 1), + getVertexAt(mesh, triangleNum * 3 + 2) + }; + + // Rotation and transform. + for (int vertexNum = 0; vertexNum < 3; ++vertexNum) { + triangle[vertexNum] = Vector3RotateByQuaternion(triangle[vertexNum], rotation); + triangle[vertexNum] = Vector3Add(triangle[vertexNum], position); + } + + // Draw. + DrawLine3D(triangle[0], triangle[1], color); + DrawLine3D(triangle[1], triangle[2], color); + DrawLine3D(triangle[2], triangle[0], color); + } + } +#endif +} diff --git a/src/util.h b/src/util.h index e764393..baa7f96 100644 --- a/src/util.h +++ b/src/util.h @@ -29,4 +29,6 @@ bool checkTriangleCollision3D(const Triangle3D triangleA, const Triangle3D trian void copyTriangle3D(Triangle3D a, const Triangle3D b); +void drawModelWireframe(Model model, Vector3 position, Quaternion rotation, Color color); + #endif -- cgit v1.2.3