diff --git a/src/dawn/game/_DawnGame.hpp b/src/dawn/game/_DawnGame.hpp index 314f46ac..d13da5ae 100644 --- a/src/dawn/game/_DawnGame.hpp +++ b/src/dawn/game/_DawnGame.hpp @@ -14,7 +14,7 @@ #include "locale/LocaleManager.hpp" #include "save/SaveManager.hpp" #include "audio/AudioManager.hpp" -#include "scene/SceneItemComponentList.hpp" +// #include "scene/SceneItemComponentList.hpp" #define DAWN_GAME_INIT_RESULT_SUCCESS 0 #define DAWN_GAME_UPDATE_RESULT_SUCCESS 0 @@ -25,7 +25,7 @@ namespace Dawn { class IDawnGame { protected: - SceneItemComponentList componentList; + // SceneItemComponentList componentList; public: Scene *scene = nullptr; diff --git a/src/dawn/physics/3d/Ray3D.cpp b/src/dawn/physics/3d/Ray3D.cpp index f83336ec..99c5bed8 100644 --- a/src/dawn/physics/3d/Ray3D.cpp +++ b/src/dawn/physics/3d/Ray3D.cpp @@ -7,16 +7,6 @@ using namespace Dawn; -struct Ray3D { - glm::vec3 origin; - glm::vec3 direction; -}; - -struct PhysicsSphere { - glm::vec3 center; - float_t radius; -}; - bool_t Dawn::raytestSphere( struct Ray3D ray, struct PhysicsSphere sphere, @@ -140,5 +130,32 @@ bool_t Dawn::raytestAABB( } // The ray intersects the box + return true; +} + +bool_t Dawn::raytestCube( + struct Ray3D ray, + struct AABB3D box, + glm::mat4 transform, + glm::vec3 *point, + glm::vec3 *normal, + float_t *distance +) { + // Compute the inverse transformation matrix + glm::mat4 inverseTransform = glm::inverse(transform); + + // Transform the ray into model space + struct Ray3D localRay; + localRay.origin = glm::vec3(inverseTransform * glm::vec4(ray.origin, 1.0f)); + localRay.direction = glm::normalize(glm::vec3(inverseTransform * glm::vec4(ray.direction, 0.0f))); + + // Call raytestAABB with the transformed ray and cube + bool_t hit = raytestAABB(localRay, box, point, normal, distance); + if(!hit) return false; + + // Transform the hit point and normal back into world space + *point = glm::vec3(transform * glm::vec4(*point, 1.0f)); + *normal = glm::normalize(glm::vec3(glm::transpose(inverseTransform) * glm::vec4(*normal, 0.0f))); + return true; } \ No newline at end of file diff --git a/src/dawn/physics/3d/Ray3D.hpp b/src/dawn/physics/3d/Ray3D.hpp index 022b9b6c..b86ed017 100644 --- a/src/dawn/physics/3d/Ray3D.hpp +++ b/src/dawn/physics/3d/Ray3D.hpp @@ -38,4 +38,13 @@ namespace Dawn { glm::vec3 *normal, float_t *distance ); -} \ No newline at end of file + + bool_t raytestCube( + struct Ray3D ray, + struct AABB3D box, + glm::mat4 transform, + glm::vec3 *point, + glm::vec3 *normal, + float_t *distance + ); +} diff --git a/src/dawn/scene/components/display/Camera.cpp b/src/dawn/scene/components/display/Camera.cpp index 84b44e92..3985899f 100644 --- a/src/dawn/scene/components/display/Camera.cpp +++ b/src/dawn/scene/components/display/Camera.cpp @@ -67,6 +67,18 @@ float_t Camera::getAspect() { return target->getWidth() / target->getHeight(); } +glm::vec3 Camera::getRayDirectionFromScreenSpace(glm::vec2 screenSpace) { + glm::vec4 clipCoords(screenSpace.x, -screenSpace.y, -1.0f, 1.0f); + glm::mat4 inverseProjectionMatrix = glm::inverse(this->projection); + glm::vec4 eyeCoords = inverseProjectionMatrix * clipCoords; + eyeCoords = glm::vec4(eyeCoords.x, eyeCoords.y, -1.0f, 0.0f); + + glm::mat4 inverseViewMatrix = glm::inverse(transform->getWorldTransform()); + glm::vec4 t = inverseViewMatrix * eyeCoords; + + return glm::normalize(glm::vec3(t.x, t.y, t.z)); +} + void Camera::onStart() { this->updateProjection(); } diff --git a/src/dawn/scene/components/display/Camera.hpp b/src/dawn/scene/components/display/Camera.hpp index 62680841..61a01a2a 100644 --- a/src/dawn/scene/components/display/Camera.hpp +++ b/src/dawn/scene/components/display/Camera.hpp @@ -7,6 +7,7 @@ #include "scene/SceneItemComponent.hpp" #include "display/RenderTarget.hpp" #include "scene/Scene.hpp" +#include "physics/3d/Ray3D.hpp" namespace Dawn { enum CameraType { @@ -79,6 +80,18 @@ namespace Dawn { */ float_t getAspect(); + /** + * Creates the directional vector for a given point on the screen space + * coordinates provided. This is useful if you want to, say, cast a ray + * based on a position on the screen. The directional vector is normalized + * between -1 and 1 where -1 is the near clipping plane, and 1 is the + * far clipping plane. + * + * @param screenSpace Screen space vector (-1,-1 to 1,1) on the screen. + * @return The vector for the direction to cast from. + */ + glm::vec3 getRayDirectionFromScreenSpace(glm::vec2 screenSpace); + /** * Event triggered by the scene item when the item is added to the scene. */ diff --git a/src/dawn/scene/debug/SceneDebugLine.cpp b/src/dawn/scene/debug/SceneDebugLine.cpp index ec27e4e1..e8782837 100644 --- a/src/dawn/scene/debug/SceneDebugLine.cpp +++ b/src/dawn/scene/debug/SceneDebugLine.cpp @@ -29,7 +29,7 @@ struct ShaderPassItem SceneDebugLine::createShaderItem( auto i = *lineIndex; item.mesh = mesh; item.drawMode = MESH_DRAW_MODE_LINES; - item.renderFlags = RENDER_MANAGER_RENDER_FLAG_BLEND | RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST; + item.renderFlags = 0x00; item.start = i * SCENE_DEBUG_LINE_INDICE_COUNT; item.count = SCENE_DEBUG_LINE_INDICE_COUNT; diff --git a/src/dawn/scene/debug/SceneDebugLine.hpp b/src/dawn/scene/debug/SceneDebugLine.hpp index f384862d..fe874437 100644 --- a/src/dawn/scene/debug/SceneDebugLine.hpp +++ b/src/dawn/scene/debug/SceneDebugLine.hpp @@ -10,7 +10,7 @@ #define SCENE_DEBUG_LINE_VERTICE_COUNT 2 #define SCENE_DEBUG_LINE_INDICE_COUNT 2 -#define SCENE_DEBUG_LINE_PRIORITY 1 +#define SCENE_DEBUG_LINE_PRIORITY 100 namespace Dawn { class SimpleTexturedShader; diff --git a/src/dawntictactoe/components/TicTacToeGame.cpp b/src/dawntictactoe/components/TicTacToeGame.cpp index b36da585..26b51734 100644 --- a/src/dawntictactoe/components/TicTacToeGame.cpp +++ b/src/dawntictactoe/components/TicTacToeGame.cpp @@ -5,6 +5,8 @@ #include "TicTacToeGame.hpp" #include "game/DawnGame.hpp" +#include "scene/components/example/ExampleSpin.hpp" +#include "physics/3d/Ray3D.hpp" using namespace Dawn; @@ -26,9 +28,36 @@ void TicTacToeGame::onSceneUpdate() { if(camera == nullptr) return; struct Ray3D ray; - glm::vec3 pos = glm::vec3(mouse.x * camera->orthoRight, mouse.y * camera->orthoBottom, 0.0f); - ray.direction = glm::vec3(0, 0, -15); - ray.origin = pos; + ray.origin = glm::vec3(1,2,3); + ray.direction = camera->getRayDirectionFromScreenSpace(mouse); - getScene()->debugRay((struct SceneDebugRay){ .start = ray.origin, .direction = ray.direction }); + struct AABB3D box; + box.min = glm::vec3(-0.5f, -0.5f, -0.5f); + box.max = glm::vec3(0.5f, 0.5f, 0.5f); + + struct SceneDebugCube cube = { + .min = box.min, + .max = box.max, + .color COLOR_GREEN, + .transform = getScene()->findComponent()->transform->getWorldTransform() + }; + getScene()->debugCube(cube); + + glm::vec3 point, normal; + float_t distance; + + auto test = raytestCube( + ray, + box, + cube.transform, + &point, + &normal, + &distance + ); + + if(test) { + getScene()->debugRay((struct SceneDebugRay){ .start = point, .direction = normal, .color = COLOR_BLUE }); + } else { + getScene()->debugRay((struct SceneDebugRay){ .start = ray.origin, .direction = ray.direction }); + } } \ No newline at end of file diff --git a/src/dawntictactoe/scenes/TicTacToeScene.hpp b/src/dawntictactoe/scenes/TicTacToeScene.hpp index 539d69d7..6c6f20d6 100644 --- a/src/dawntictactoe/scenes/TicTacToeScene.hpp +++ b/src/dawntictactoe/scenes/TicTacToeScene.hpp @@ -22,8 +22,8 @@ namespace Dawn { camera->transform->lookAt(glm::vec3(1, 2, 3), glm::vec3(0, 0, 0)); float_t s = 2.0f; - camera->orthoTop = -s; - camera->orthoBottom = s; + camera->orthoTop = s; + camera->orthoBottom = -s; float_t ratio = 1.0f / 9.0f * 16.0f; camera->orthoLeft = -s * ratio; diff --git a/src/dawntools/tools/CMakeLists.txt b/src/dawntools/tools/CMakeLists.txt index 5a70c87b..8d671409 100644 --- a/src/dawntools/tools/CMakeLists.txt +++ b/src/dawntools/tools/CMakeLists.txt @@ -100,39 +100,37 @@ endfunction() # Scene Item Component Tool function(tool_scenecomponent clazz hfile) - add_custom_target(${clazz}_scenecomponent - COMMENT "Registering component ${hfile}::${clazz}" - DEPENDS ${ARGN} - ) - set( - DAWN_SCENE_ITEM_COMPONENT_LIST - ${DAWN_SCENE_ITEM_COMPONENT_LIST} - ${clazz} - ${hfile} - CACHE INTERNAL ${DAWN_CACHE_TARGET} - ) - file(CONFIGURE OUTPUT - "${DAWN_TEMP_DIR}/SceneItemComponents.txt" - CONTENT "${DAWN_SCENE_ITEM_COMPONENT_LIST}" - ) - if(NOT TARGET sceneitemcomponentgen_cmd) - add_custom_target(sceneitemcomponentgen_cmd - COMMAND sceneitemcomponentgen --input="${DAWN_TEMP_DIR}/SceneItemComponents.txt" --output="${DAWN_GENERATED_DIR}/scene/SceneItemComponentListItems.cpp" - COMMENT "Generating scene item component ${hfile}::${clazz}" - DEPENDS sceneitemcomponentgen - ) - target_sources(${DAWN_TARGET_NAME} - PRIVATE - ${DAWN_GENERATED_DIR}/scene/SceneItemComponentListItems.cpp - ) - endif() - add_dependencies(sceneitemcomponentgen ${clazz}_scenecomponent) - add_dependencies(${DAWN_TARGET_NAME} sceneitemcomponentgen_cmd) + # add_custom_target(${clazz}_scenecomponent + # COMMENT "Registering component ${hfile}::${clazz}" + # DEPENDS ${ARGN} + # ) + # set( + # DAWN_SCENE_ITEM_COMPONENT_LIST + # ${DAWN_SCENE_ITEM_COMPONENT_LIST} + # ${clazz} + # ${hfile} + # CACHE INTERNAL ${DAWN_CACHE_TARGET} + # ) + # file(CONFIGURE OUTPUT + # "${DAWN_TEMP_DIR}/SceneItemComponents.txt" + # CONTENT "${DAWN_SCENE_ITEM_COMPONENT_LIST}" + # ) + # if(NOT TARGET sceneitemcomponentgen_cmd) + # add_custom_target(sceneitemcomponentgen_cmd + # COMMAND sceneitemcomponentgen --input="${DAWN_TEMP_DIR}/SceneItemComponents.txt" --output="${DAWN_GENERATED_DIR}/scene/SceneItemComponentListItems.cpp" + # COMMENT "Generating scene item component ${hfile}::${clazz}" + # DEPENDS sceneitemcomponentgen + # ) + # # target_sources(${DAWN_TARGET_NAME} + # # PRIVATE + # # ${DAWN_GENERATED_DIR}/scene/SceneItemComponentListItems.cpp + # # ) + # endif() + # add_dependencies(sceneitemcomponentgen ${clazz}_scenecomponent) + # add_dependencies(${DAWN_TARGET_NAME} ${clazz}_scenecomponent sceneitemcomponentgen sceneitemcomponentgen_cmd) endfunction() - - if(DAWN_VISUAL_NOVEL) add_subdirectory(vnscenegen) diff --git a/src/dawntools/tools/generatedlanguages/GeneratedLanguages.cpp b/src/dawntools/tools/generatedlanguages/GeneratedLanguages.cpp index ded35f47..45d6b0b2 100644 --- a/src/dawntools/tools/generatedlanguages/GeneratedLanguages.cpp +++ b/src/dawntools/tools/generatedlanguages/GeneratedLanguages.cpp @@ -117,10 +117,11 @@ int32_t GeneratedLanguages::scanDir( std::vector *files ) { DIR* handle = opendir(dir.c_str()); - if(ENOENT == errno || !handle) { + if(ENOENT == errno) { *error = "Input directory \"" + dir + "\" does not exist"; - return 1; + return 0; } + if(!handle) return 0; struct dirent *entry; while((entry=readdir(handle))) {