From d52d63e310e380e419e5894632987a55fb401ca7 Mon Sep 17 00:00:00 2001 From: illyum Date: Wed, 18 Sep 2024 17:08:40 -0600 Subject: [PATCH] Working cube with imgui global cube scaling --- CMakeLists.txt | 39 +++++++++++++++++++++++++++++++-- main.cpp | 59 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 78 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d2c221..de21af4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,41 @@ set(BUILD_EXAMPLES OF CACHE BOOL "" FORCE) set(SUPPORT_EVENTS_WAITING ON CACHE BOOL "" FORCE) set(SUPPORT_BUSY_WAIT_LOOP OFF CACHE BOOL "" FORCE) +FetchContent_Declare( + imgui + GIT_REPOSITORY https://github.com/ocornut/imgui.git + GIT_SHALLOW TRUE + GIT_TAG v1.91.1 +) +FetchContent_MakeAvailable(imgui) + +add_library(ImGui SHARED + ${imgui_SOURCE_DIR}/imgui.cpp + ${imgui_SOURCE_DIR}/imgui_demo.cpp + ${imgui_SOURCE_DIR}/imgui_draw.cpp + ${imgui_SOURCE_DIR}/imgui_tables.cpp + ${imgui_SOURCE_DIR}/imgui_widgets.cpp +) +target_include_directories(ImGui PUBLIC ${imgui_SOURCE_DIR}) + +FetchContent_Declare( + rlimgui + GIT_REPOSITORY https://github.com/raylib-extras/rlImGui.git + GIT_SHALLOW TRUE + GIT_TAG main +) +FetchContent_MakeAvailable(rlimgui) + add_executable(Cuber main.cpp) -target_link_libraries(Cuber PRIVATE raylib) -target_include_directories(Cuber PRIVATE ${CMAKE_SOURCE_DIR}/raylib/include) + +target_include_directories(${PROJECT_NAME} PRIVATE + ${raylib_SOURCE_DIR} + ${imgui_SOURCE_DIR} + ${rlimgui_SOURCE_DIR} +) + +add_library(rlImGui STATIC ${rlimgui_SOURCE_DIR}/rlImGui.cpp) +target_include_directories(rlImGui PUBLIC ${rlimgui_SOURCE_DIR} ${imgui_SOURCE_DIR}) +target_link_libraries(rlImGui PRIVATE raylib ImGui) + +target_link_libraries(Cuber PRIVATE raylib ImGui rlImGui) \ No newline at end of file diff --git a/main.cpp b/main.cpp index 06f1d7e..0f77cef 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,8 @@ #include #include #include +#include "rlImGui.h" +#include "imgui.h" enum CubeFace { FRONT = 0, @@ -30,8 +32,6 @@ struct RubixCube { void DrawRubixCube(RubixCube* cube); void DrawPlane3D(Plane3D* plane); -Plane3D CreatePlane3D(float width, float height); -Plane3D CreatePlane3D(float width, float height, Vector3 position, Color color); Plane3D CreatePlane3D(float width, float height, Vector3 position, Color color, Vector3 normal); RubixCube CreateRubixCube(Vector3 position, float size); @@ -48,15 +48,38 @@ int main() { cam.fovy = 45.0f; cam.projection = CAMERA_PERSPECTIVE; - RubixCube cube = CreateRubixCube({0.0f, 0.0f, 0.0f}, 3.0f); + float cubeSize = 3.0f; + float previousCubeSize = cubeSize; + RubixCube cube = CreateRubixCube({0.0f, 0.0f, 0.0f}, cubeSize); // Disable backface culling rlDisableBackfaceCulling(); + // Setup ImGui + rlImGuiSetup(true); + + bool isControllingCamera = true; + while (!WindowShouldClose()) { - UpdateCamera(&cam, CAMERA_FREE); + if (IsKeyPressed(KEY_SPACE)) isControllingCamera = !isControllingCamera; + if (isControllingCamera) UpdateCamera(&cam, CAMERA_FREE); + if (!isControllingCamera) DrawText("Camera paused", 10, 10, 32, BLACK); + BeginDrawing(); ClearBackground(GRAY); + + rlImGuiBegin(); + ImGui::Begin("Controls"); + ImGui::SliderFloat("Cube Size", &cubeSize, 1.0f, 10.0f); + ImGui::End(); + rlImGuiEnd(); + + if (cubeSize != previousCubeSize) { + cube = CreateRubixCube({0.0f, 0.0f, 0.0f}, cubeSize); + previousCubeSize = cubeSize; + } + + BeginMode3D(cam); DrawRubixCube(&cube); @@ -86,35 +109,34 @@ RubixCube CreateRubixCube(Vector3 position, float size) { float planeSize = size / 3.0f; int faceIndex; - // Loop through each face and assign the 3x3 planes for (int face = 0; face < 6; face++) { - faceIndex = 0; // Reset faceIndex for each face + faceIndex = 0; for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { Vector3 planePos = { position.x + x * planeSize, position.y + y * planeSize, position.z }; switch (face) { - case FRONT: // FRONT face (Z positive) - RED + case FRONT: planePos = { position.x + x * planeSize, position.y + y * planeSize, position.z + size / 2 }; cube.faces[face][faceIndex++] = CreatePlane3D(planeSize, planeSize, planePos, RED, {0, 0, 1}); break; - case BACK: // BACK face (Z negative) - ORANGE + case BACK: planePos = { position.x + x * planeSize, position.y + y * planeSize, position.z - size / 2 }; cube.faces[face][faceIndex++] = CreatePlane3D(planeSize, planeSize, planePos, ORANGE, {0, 0, -1}); break; - case LEFT: // LEFT face (X negative) - BLUE + case LEFT: planePos = { position.x - size / 2, position.y + y * planeSize, position.z + x * planeSize }; cube.faces[face][faceIndex++] = CreatePlane3D(planeSize, planeSize, planePos, BLUE, {-1, 0, 0}); break; - case RIGHT: // RIGHT face (X positive) - GREEN + case RIGHT: planePos = { position.x + size / 2, position.y + y * planeSize, position.z + x * planeSize }; cube.faces[face][faceIndex++] = CreatePlane3D(planeSize, planeSize, planePos, GREEN, {1, 0, 0}); break; - case TOP: // TOP face (Y positive) - YELLOW + case TOP: planePos = { position.x + x * planeSize, position.y + size / 2, position.z + y * planeSize }; cube.faces[face][faceIndex++] = CreatePlane3D(planeSize, planeSize, planePos, YELLOW, {0, 1, 0}); break; - case BOTTOM: // BOTTOM face (Y negative) - WHITE + case BOTTOM: planePos = { position.x + x * planeSize, position.y - size / 2, position.z + y * planeSize }; cube.faces[face][faceIndex++] = CreatePlane3D(planeSize, planeSize, planePos, WHITE, {0, -1, 0}); break; @@ -165,19 +187,20 @@ Plane3D CreatePlane3D(float width, float height, Vector3 position, Color color, Plane3D plane3D; plane3D.color = color; - Vector3 right = {normal.z, 0, -normal.x}; // Calculate right vector from normal - Vector3 up = {0, 1, 0}; // Default up direction + Vector3 right, up; - if (normal.x != 0 || normal.z != 0) { - up = {0, 1, 0}; // Keep vertical for non-top/bottom faces + if (Vector3Equals(normal, {0, 1, 0}) || Vector3Equals(normal, {0, -1, 0})) { + right = {1, 0, 0}; + up = {0, 0, (normal.y > 0) ? -1 : 1}; + } else { + right = {normal.z, 0, -normal.x}; + up = {0, 1, 0}; } - // First triangle (bottom-left, bottom-right, top-left) plane3D.one.pointOne = Vector3Add(Vector3Add(position, Vector3Scale(right, -width / 2)), Vector3Scale(up, -height / 2)); plane3D.one.pointTwo = Vector3Add(Vector3Add(position, Vector3Scale(right, width / 2)), Vector3Scale(up, -height / 2)); plane3D.one.pointThree = Vector3Add(Vector3Add(position, Vector3Scale(right, -width / 2)), Vector3Scale(up, height / 2)); - // Second triangle (bottom-right, top-right, top-left) plane3D.two.pointOne = Vector3Add(Vector3Add(position, Vector3Scale(right, width / 2)), Vector3Scale(up, -height / 2)); plane3D.two.pointTwo = Vector3Add(Vector3Add(position, Vector3Scale(right, width / 2)), Vector3Scale(up, height / 2)); plane3D.two.pointThree = Vector3Add(Vector3Add(position, Vector3Scale(right, -width / 2)), Vector3Scale(up, height / 2));