diff options
author | nathansmithsmith <thenathansmithsmith@gmail.com> | 2023-12-23 17:04:05 -0700 |
---|---|---|
committer | nathansmithsmith <thenathansmithsmith@gmail.com> | 2023-12-23 17:04:05 -0700 |
commit | f439828dc068974ddc161cc3469ac489a93bbabc (patch) | |
tree | 1d74af0b59744a17ba73716d91196922d3843859 /src | |
parent | 2c046ef3dca9e128a801c7a92c3a3a63e01fdd98 (diff) |
Working on the web assembly openglES issue
Diffstat (limited to 'src')
-rw-r--r-- | src/entity.c | 9 | ||||
-rw-r--r-- | src/gameCommon.h | 7 | ||||
-rw-r--r-- | src/main.c | 8 | ||||
-rw-r--r-- | src/screens/infoScreen.c | 3 | ||||
-rw-r--r-- | src/util.c | 42 | ||||
-rw-r--r-- | src/util.h | 2 |
6 files changed, 54 insertions, 17 deletions
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 <rlgl.h> #include "raygui.h" +//#define PLATFORM_WEB + +#if defined(PLATFORM_WEB) + #include <emscripten/emscripten.h> +#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__) @@ -1,12 +1,6 @@ #include "gameCommon.h" #include "game.h" -//#define PLATFORM_WEB - -#if defined(PLATFORM_WEB) - #include <emscripten/emscripten.h> -#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(); } @@ -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 +} @@ -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 |