diff --git a/src/dawn/dawnlibs.hpp b/src/dawn/dawnlibs.hpp index 1248afc9..b3a31a2f 100644 --- a/src/dawn/dawnlibs.hpp +++ b/src/dawn/dawnlibs.hpp @@ -14,9 +14,11 @@ #include #include #include +#include #include #include #include #include +#include #include #include \ No newline at end of file diff --git a/src/dawn/display/mesh/CMakeLists.txt b/src/dawn/display/mesh/CMakeLists.txt index 7b31221a..51bc0ce3 100644 --- a/src/dawn/display/mesh/CMakeLists.txt +++ b/src/dawn/display/mesh/CMakeLists.txt @@ -1,12 +1,13 @@ -# Copyright (c) 2022 Dominic Masters -# -# This software is released under the MIT License. -# https://opensource.org/licenses/MIT - -# Sources -target_sources(${DAWN_TARGET_NAME} - PRIVATE - CubeMesh.cpp - TriangleMesh.cpp - QuadMesh.cpp +# Copyright (c) 2022 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DAWN_TARGET_NAME} + PRIVATE + CubeMesh.cpp + TriangleMesh.cpp + QuadMesh.cpp + SphereMesh.cpp ) \ No newline at end of file diff --git a/src/dawn/display/mesh/CubeMesh.cpp b/src/dawn/display/mesh/CubeMesh.cpp index 434951b9..5c2496d2 100644 --- a/src/dawn/display/mesh/CubeMesh.cpp +++ b/src/dawn/display/mesh/CubeMesh.cpp @@ -13,8 +13,8 @@ void CubeMesh::buffer( int32_t verticeStart, int32_t indiceStart ) { assertNotNull(mesh); - - mesh->bufferPositions(verticeStart, std::array{{ + + glm::vec3 positions[CUBE_VERTICE_COUNT] = { pos, glm::vec3(pos.x+size.x, pos.y, pos.z), glm::vec3(pos.x, pos.y+size.y, pos.z), @@ -24,9 +24,9 @@ void CubeMesh::buffer( glm::vec3(pos.x+size.x, pos.y, pos.z+size.z), glm::vec3(pos.x, pos.y+size.y, pos.z+size.z), pos + size - }}); + }; - mesh->bufferCoordinates(verticeStart,std::array{{ + glm::vec2 coordinates[CUBE_VERTICE_COUNT] = { glm::vec2(0, 0), glm::vec2(1, 0), glm::vec2(0, 1), @@ -36,9 +36,9 @@ void CubeMesh::buffer( glm::vec2(1, 0), glm::vec2(0, 1), glm::vec2(1, 1) - }}); + }; - mesh->bufferIndices(indiceStart, std::array{{ + meshindice_t indices[CUBE_INDICE_COUNT] = { // Back verticeStart, verticeStart + 1, verticeStart + 3, verticeStart, verticeStart + 2, verticeStart + 3, @@ -62,5 +62,9 @@ void CubeMesh::buffer( // Bottom verticeStart + 1, verticeStart, verticeStart + 4, verticeStart + 1, verticeStart + 4, verticeStart + 5 - }}); + }; + + mesh->bufferPositions(verticeStart, positions, CUBE_VERTICE_COUNT); + mesh->bufferCoordinates(verticeStart, coordinates, CUBE_VERTICE_COUNT); + mesh->bufferIndices(indiceStart, indices, CUBE_INDICE_COUNT); } \ No newline at end of file diff --git a/src/dawn/display/mesh/QuadMesh.cpp b/src/dawn/display/mesh/QuadMesh.cpp index f28a5f14..39c7fcc8 100644 --- a/src/dawn/display/mesh/QuadMesh.cpp +++ b/src/dawn/display/mesh/QuadMesh.cpp @@ -15,28 +15,24 @@ void QuadMesh::bufferQuadMeshWithZ( ) { assertNotNull(mesh); - mesh->bufferPositions( - verticeStart, std::array{{ - glm::vec3(xy0, z), - glm::vec3(xy1.x, xy0.y, z), - glm::vec3(xy0.x, xy1.y, z), - glm::vec3(xy1, z) - }} - ); + glm::vec3 positions[QUAD_VERTICE_COUNT] = { + glm::vec3(xy0, z), + glm::vec3(xy1.x, xy0.y, z), + glm::vec3(xy0.x, xy1.y, z), + glm::vec3(xy1, z) + }; + glm::vec2 coordinates[QUAD_VERTICE_COUNT] = { + uv0, glm::vec2(uv1.x, uv0.y), + glm::vec2(uv0.x, uv1.y), uv1 + }; + meshindice_t indices[QUAD_INDICE_COUNT] = { + verticeStart, verticeStart + 1, verticeStart + 2, + verticeStart + 1, verticeStart + 2, verticeStart + 3 + }; - mesh->bufferCoordinates( - verticeStart, std::array{{ - uv0, glm::vec2(uv1.x, uv0.y), - glm::vec2(uv0.x, uv1.y), uv1 - }} - ); - - mesh->bufferIndices( - indiceStart, std::array{{ - verticeStart, verticeStart + 1, verticeStart + 2, - verticeStart + 1, verticeStart + 2, verticeStart + 3 - }} - ); + mesh->bufferPositions(verticeStart, positions, QUAD_VERTICE_COUNT); + mesh->bufferCoordinates(verticeStart, coordinates, QUAD_VERTICE_COUNT); + mesh->bufferIndices(indiceStart, indices, QUAD_INDICE_COUNT); } void QuadMesh::bufferQuadMesh( @@ -56,10 +52,11 @@ void QuadMesh::bufferCoordinates( int32_t verticeStart ) { assertNotNull(mesh); - mesh->bufferCoordinates(verticeStart, std::array{{ + glm::vec2 coordinates[QUAD_VERTICE_COUNT] = { uv0, glm::vec2(uv1.x, uv0.y), glm::vec2(uv0.x, uv1.y), uv1 - }}); + }; + mesh->bufferCoordinates(verticeStart, coordinates, QUAD_VERTICE_COUNT); } void QuadMesh::bufferPositions( @@ -67,14 +64,14 @@ void QuadMesh::bufferPositions( glm::vec2 xy0, glm::vec2 xy1, int32_t verticeStart ) { - mesh->bufferPositions( - verticeStart, std::array{{ - glm::vec3(xy0, 0), - glm::vec3(xy1.x, xy0.y, 0), - glm::vec3(xy0.x, xy1.y, 0), - glm::vec3(xy1, 0) - }} - ); + assertNotNull(mesh); + glm::vec3 positions[QUAD_VERTICE_COUNT] = { + glm::vec3(xy0, 0), + glm::vec3(xy1.x, xy0.y, 0), + glm::vec3(xy0.x, xy1.y, 0), + glm::vec3(xy1, 0) + }; + mesh->bufferPositions(verticeStart, positions, QUAD_VERTICE_COUNT); } void QuadMesh::initQuadMesh( diff --git a/src/dawn/display/mesh/SphereMesh.cpp b/src/dawn/display/mesh/SphereMesh.cpp new file mode 100644 index 00000000..7737b6a3 --- /dev/null +++ b/src/dawn/display/mesh/SphereMesh.cpp @@ -0,0 +1,59 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "SphereMesh.hpp" + +using namespace Dawn; + +void SphereMesh::createSphere( + Mesh *mesh, + float_t radius, + int32_t slices, + int32_t stacks +) { + std::vector positions; + + // Create vertices + int32_t c = 0; + for (int32_t i = 0; i <= stacks; i++) { + float_t phi = M_PI * i / stacks; + float_t cosPhi = cos(phi); + float_t sinPhi = sin(phi); + + for (int32_t j = 0; j <= slices; j++) { + float_t theta = 2 * M_PI * j / slices; + float_t cosTheta = cos(theta); + float_t sinTheta = sin(theta); + + glm::vec3 v; + v.x = radius * sinPhi * cosTheta; + v.y = radius * sinPhi * sinTheta; + v.z = radius * cosPhi; + + positions.push_back(v); + } + } + + // Create indices + std::vector indices; + for (int32_t i = 0; i < stacks; i++) { + for (int32_t j = 0; j < slices; j++) { + meshindice_t p1 = i * (slices + 1) + j; + meshindice_t p2 = p1 + slices + 1; + + indices.push_back(p1); + indices.push_back(p2); + indices.push_back(p1 + 1); + + indices.push_back(p1 + 1); + indices.push_back(p2); + indices.push_back(p2 + 1); + } + } + + mesh->createBuffers(positions.size(), indices.size()); + mesh->bufferPositions(0, positions.data(), positions.size()); + mesh->bufferIndices(0, indices.data(), indices.size()); +} \ No newline at end of file diff --git a/src/dawn/display/mesh/SphereMesh.hpp b/src/dawn/display/mesh/SphereMesh.hpp new file mode 100644 index 00000000..04a8a557 --- /dev/null +++ b/src/dawn/display/mesh/SphereMesh.hpp @@ -0,0 +1,19 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "display/mesh/Mesh.hpp" + +namespace Dawn { + class SphereMesh { + public: + static void createSphere( + Mesh *mesh, + float_t radius, + int32_t slices, + int32_t stacks + ); + }; +} \ No newline at end of file diff --git a/src/dawn/display/mesh/TriangleMesh.cpp b/src/dawn/display/mesh/TriangleMesh.cpp index 8b12d6ad..e2d5ea7c 100644 --- a/src/dawn/display/mesh/TriangleMesh.cpp +++ b/src/dawn/display/mesh/TriangleMesh.cpp @@ -9,19 +9,25 @@ using namespace Dawn; void TriangleMesh::createTriangleMesh(Mesh *mesh) { assertNotNull(mesh); - - mesh->createBuffers(3, 3); - mesh->bufferPositions(0, std::array{{ + + glm::vec3 positions[3] = { glm::vec3(-0.5f, -0.5f, 0), glm::vec3(0.5f, -0.5f, 0), glm::vec3(0, 0.5f, 0) - }}); - mesh->bufferCoordinates(0, std::array{{ + }; + + glm::vec2 coordinates[3] = { glm::vec2(0, 0), glm::vec2(0, 1), glm::vec2(1, 0) - }}); - mesh->bufferIndices(0, std::array{{ + }; + + meshindice_t indices[3] = { 0, 1, 2 - }}); + }; + + mesh->createBuffers(3, 3); + mesh->bufferPositions(0, positions, 3); + mesh->bufferCoordinates(0, coordinates, 3); + mesh->bufferIndices(0, indices, 3); } \ No newline at end of file diff --git a/src/dawn/input/_InputManager.hpp b/src/dawn/input/_InputManager.hpp index 19d53b66..94be351c 100644 --- a/src/dawn/input/_InputManager.hpp +++ b/src/dawn/input/_InputManager.hpp @@ -109,6 +109,17 @@ namespace Dawn { return glm::vec2(getAxis(negativeX, positiveX), getAxis(negativeY, positiveY)); } + /** + * Returns the 2D Axis for the given binds. + * + * @param x X Axis bind. + * @param y Y Axis bind. + * @return 2D vector of the two given input binds. + */ + glm::vec2 getAxis2D(inputbind_t x, inputbind_t y) { + return glm::vec2(getValue(x), getValue(y)); + } + /** * Returns true if the given bind is currently being pressed (a non-zero * value). diff --git a/src/dawn/physics/3d/AABB3D.hpp b/src/dawn/physics/3d/AABB3D.hpp new file mode 100644 index 00000000..8b609d2d --- /dev/null +++ b/src/dawn/physics/3d/AABB3D.hpp @@ -0,0 +1,14 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "dawnlibs.hpp" + +namespace Dawn { + struct AABB3D { + glm::vec3 min; + glm::vec3 max; + }; +} \ No newline at end of file diff --git a/src/dawn/physics/3d/CMakeLists.txt b/src/dawn/physics/3d/CMakeLists.txt new file mode 100644 index 00000000..98296ba4 --- /dev/null +++ b/src/dawn/physics/3d/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright (c) 2023 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DAWN_TARGET_NAME} + PRIVATE + Ray3D.cpp +) \ No newline at end of file diff --git a/src/dawn/physics/3d/PhysicsSphere.hpp b/src/dawn/physics/3d/PhysicsSphere.hpp new file mode 100644 index 00000000..94bffabe --- /dev/null +++ b/src/dawn/physics/3d/PhysicsSphere.hpp @@ -0,0 +1,14 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "dawnlibs.hpp" + +namespace Dawn { + struct PhysicsSphere { + glm::vec3 center; + float_t radius; + }; +} \ No newline at end of file diff --git a/src/dawn/physics/3d/PhysicsTriangle.hpp b/src/dawn/physics/3d/PhysicsTriangle.hpp new file mode 100644 index 00000000..3e33ccaf --- /dev/null +++ b/src/dawn/physics/3d/PhysicsTriangle.hpp @@ -0,0 +1,15 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "dawnlibs.hpp" + +namespace Dawn { + struct PhysicsTriangle { + glm::vec3 v0; + glm::vec3 v1; + glm::vec3 v2; + }; +} \ No newline at end of file diff --git a/src/dawn/physics/3d/Ray3D.cpp b/src/dawn/physics/3d/Ray3D.cpp new file mode 100644 index 00000000..ed8eb541 --- /dev/null +++ b/src/dawn/physics/3d/Ray3D.cpp @@ -0,0 +1,106 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "Ray3D.hpp" + +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, + glm::vec3 *hit, + glm::vec3 *normal +) { + glm::vec3 h, n; + auto result = glm::intersectRaySphere( + ray.origin, ray.direction, + sphere.center, sphere.radius, + h, n + ); + + if(hit != nullptr) *hit = h; + if(normal != nullptr) *normal = n; + + return result; +} + +bool_t Dawn::raytestTriangle( + struct Ray3D ray, + struct PhysicsTriangle triangle, + glm::vec2 *hit, + float_t *distance +) { + glm::vec2 h; + float_t d; + auto result = glm::intersectRayTriangle( + ray.origin, ray.direction, + triangle.v0, triangle.v1, triangle.v2, + h, d + ); + if(hit != nullptr) *hit = h; + if(distance != nullptr) *distance = d; + return result; +} + +bool_t Dawn::raytestAABB( + struct Ray3D ray, + struct AABB3D box, + glm::vec3 *point, + glm::vec3 *normal, + float_t *distance +) { + // Compute the inverse direction of the ray, for numerical stability + glm::vec3 invDir(1.0f / ray.direction.x, 1.0f / ray.direction.y, 1.0f / ray.direction.z); + + // Compute the t-values for the two intersection candidates + glm::vec3 tMin = (box.min - ray.origin) * invDir; + glm::vec3 tMax = (box.max - ray.origin) * invDir; + + // Make sure tMin is less than or equal to tMax for all components + glm::vec3 t1 = glm::min(tMin, tMax); + glm::vec3 t2 = glm::max(tMin, tMax); + float tNear = glm::compMax(t1); + float tFar = glm::compMin(t2); + + // If tNear is greater than or equal to tFar, there is no intersection + if (tNear >= tFar) return false; + + // If tFar is negative, the ray is pointing away from the box + if(tFar < 0.0f) return false; + + // Compute the hit point and normal + glm::vec3 hitPoint = ray.origin + tNear * ray.direction; + + if(point != nullptr) *point = hitPoint; + if(distance != nullptr) *distance = tNear; + if(normal != nullptr) { + if (hitPoint.x == box.min.x) { + *normal = glm::vec3(-1, 0, 0); + } else if (hitPoint.x == box.max.x) { + *normal = glm::vec3(1, 0, 0); + } else if (hitPoint.y == box.min.y) { + *normal = glm::vec3(0, -1, 0); + } else if (hitPoint.y == box.max.y) { + *normal = glm::vec3(0, 1, 0); + } else if (hitPoint.z == box.min.z) { + *normal = glm::vec3(0, 0, -1); + } else if (hitPoint.z == box.max.z) { + *normal = glm::vec3(0, 0, 1); + } + } + + // The ray intersects the box + return true; +} \ No newline at end of file diff --git a/src/dawn/physics/3d/Ray3D.hpp b/src/dawn/physics/3d/Ray3D.hpp new file mode 100644 index 00000000..ea71dd95 --- /dev/null +++ b/src/dawn/physics/3d/Ray3D.hpp @@ -0,0 +1,40 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "dawnlibs.hpp" +#include "assert/assert.hpp" +#include "PhysicsTriangle.hpp" +#include "PhysicsSphere.hpp" +#include "AABB3D.hpp" + +namespace Dawn { + struct Ray3D { + glm::vec3 origin; + glm::vec3 direction; + }; + + bool_t raytestSphere( + struct Ray3D ray, + struct PhysicsSphere sphere, + glm::vec3 *hit, + glm::vec3 *normal + ); + + bool_t raytestTriangle( + struct Ray3D ray, + struct PhysicsTriangle triangle, + glm::vec2 *hit, + float_t *distance + ); + + bool_t raytestAABB( + struct Ray3D ray, + struct AABB3D box, + glm::vec3 *point, + glm::vec3 *normal, + float_t *distance + ); +} \ No newline at end of file diff --git a/src/dawn/physics/CMakeLists.txt b/src/dawn/physics/CMakeLists.txt index 539b9593..a4a8d532 100644 --- a/src/dawn/physics/CMakeLists.txt +++ b/src/dawn/physics/CMakeLists.txt @@ -1,10 +1,13 @@ -# Copyright (c) 2023 Dominic Masters -# -# This software is released under the MIT License. -# https://opensource.org/licenses/MIT - -# Sources -target_sources(${DAWN_TARGET_NAME} -PRIVATE - PhysicsManager.cpp -) \ No newline at end of file +# Copyright (c) 2023 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DAWN_TARGET_NAME} + PRIVATE + PhysicsManager.cpp +) + +# Subdirs +add_subdirectory(3d) \ No newline at end of file diff --git a/src/dawn/prefabs/SimpleSpinningCubePrefab.hpp b/src/dawn/prefabs/SimpleSpinningCubePrefab.hpp index 09ad7243..0c21eec3 100644 --- a/src/dawn/prefabs/SimpleSpinningCubePrefab.hpp +++ b/src/dawn/prefabs/SimpleSpinningCubePrefab.hpp @@ -16,6 +16,9 @@ namespace Dawn { public SceneItemPrefab { public: + MeshHost *meshHost; + SimpleTexturedMaterial *material; + static std::vector prefabAssets(AssetManager *man) { return std::vector(); } @@ -27,9 +30,9 @@ namespace Dawn { void prefabInit(AssetManager *man) override { auto meshRenderer = this->addComponent(); - auto meshHost = this->addComponent(); - auto spinning = this->addComponent(); - auto material = this->addComponent(); + meshHost = this->addComponent(); + // auto spinning = this->addComponent(); + material = this->addComponent(); meshHost->mesh.createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT); CubeMesh::buffer(&meshHost->mesh, glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(1, 1, 1), 0, 0); diff --git a/src/dawn/scene/components/display/Camera.hpp b/src/dawn/scene/components/display/Camera.hpp index 2b11837e..62680841 100644 --- a/src/dawn/scene/components/display/Camera.hpp +++ b/src/dawn/scene/components/display/Camera.hpp @@ -43,7 +43,7 @@ namespace Dawn { // Shared float_t clipNear = 0.001f; - float_t clipFar = 1000.0f; + float_t clipFar = 50.0f; /** * Create a new Camera Component. diff --git a/src/dawn/scene/components/physics/3d/CMakeLists.txt b/src/dawn/scene/components/physics/3d/CMakeLists.txt index f6a4e7a0..b77295db 100644 --- a/src/dawn/scene/components/physics/3d/CMakeLists.txt +++ b/src/dawn/scene/components/physics/3d/CMakeLists.txt @@ -1,4 +1,13 @@ -# Copyright (c) 2023 Dominic Masters -# -# This software is released under the MIT License. -# https://opensource.org/licenses/MIT \ No newline at end of file +# Copyright (c) 2023 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DAWN_TARGET_NAME} + PRIVATE + RayTester3D.cpp +) + +# Subdirs +add_subdirectory(collider) \ No newline at end of file diff --git a/src/dawn/scene/components/physics/3d/RayTester3D.cpp b/src/dawn/scene/components/physics/3d/RayTester3D.cpp new file mode 100644 index 00000000..6166811a --- /dev/null +++ b/src/dawn/scene/components/physics/3d/RayTester3D.cpp @@ -0,0 +1,42 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "RayTester3D.hpp" +#include "scene/Scene.hpp" + +using namespace Dawn; + +RayTester3D::RayTester3D(SceneItem *item) : SceneItemComponent(item) { + +} + +void RayTester3D::onStart() { + this->getScene()->eventSceneUpdate.addListener(this, &RayTester3D::onUpdate); +} + +void RayTester3D::onUpdate() { + prefab->transform.setLocalPosition(glm::vec3(0, 0, 0)); + box.max = glm::vec3( 0.5f, 0.5f, 0.5f); + box.min = glm::vec3(-0.5f, -0.5f, -0.5f); + + auto mouse = this->getGame()->inputManager.getAxis2D(INPUT_BIND_MOUSE_X, INPUT_BIND_MOUSE_Y); + mouse *= 2.0f; + mouse -= glm::vec2(1, 1); + + 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; + + // prefab->transform.setLocalPosition(pos); + // ray.origin = glm::vec3(0, 0, 5); + + bool_t x = raytestAABB(ray, box, &hit, &normal, &distance); + + if(x) { + prefab->material->color = COLOR_RED; + } else { + prefab->material->color = COLOR_WHITE; + } +} \ No newline at end of file diff --git a/src/dawn/scene/components/physics/3d/RayTester3D.hpp b/src/dawn/scene/components/physics/3d/RayTester3D.hpp new file mode 100644 index 00000000..390a1746 --- /dev/null +++ b/src/dawn/scene/components/physics/3d/RayTester3D.hpp @@ -0,0 +1,27 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "scene/SceneItemComponent.hpp" +#include "prefabs/SimpleSpinningCubePrefab.hpp" +#include "physics/3d/Ray3D.hpp" +#include "scene/components/display/Camera.hpp" + +namespace Dawn { + class RayTester3D : public SceneItemComponent { + public: + SimpleSpinningCubePrefab *prefab; + Camera *camera; + struct AABB3D box; + struct Ray3D ray; + glm::vec3 hit; + glm::vec3 normal; + float_t distance; + + RayTester3D(SceneItem *item); + void onStart() override; + void onUpdate(); + }; +} \ No newline at end of file diff --git a/src/dawn/scene/components/physics/3d/collider/CMakeLists.txt b/src/dawn/scene/components/physics/3d/collider/CMakeLists.txt new file mode 100644 index 00000000..d61702b9 --- /dev/null +++ b/src/dawn/scene/components/physics/3d/collider/CMakeLists.txt @@ -0,0 +1,11 @@ +# Copyright (c) 2023 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DAWN_TARGET_NAME} + PRIVATE + Collider3D.cpp + CubeCollider.cpp +) \ No newline at end of file diff --git a/src/dawn/scene/components/physics/3d/collider/Collider3D.cpp b/src/dawn/scene/components/physics/3d/collider/Collider3D.cpp new file mode 100644 index 00000000..a431828a --- /dev/null +++ b/src/dawn/scene/components/physics/3d/collider/Collider3D.cpp @@ -0,0 +1,12 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "Collider3D.hpp" + +using namespace Dawn; + +Collider3D::Collider3D(SceneItem *item) : SceneItemComponent(item) { + +} \ No newline at end of file diff --git a/src/dawn/scene/components/physics/3d/collider/Collider3D.hpp b/src/dawn/scene/components/physics/3d/collider/Collider3D.hpp new file mode 100644 index 00000000..aaac2607 --- /dev/null +++ b/src/dawn/scene/components/physics/3d/collider/Collider3D.hpp @@ -0,0 +1,22 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "scene/SceneItemComponent.hpp" +#include "physics/3d/Ray3D.hpp" + +namespace Dawn { + struct Collider3DRayResult { + glm::vec3 normal; + glm::vec3 point; + }; + + class Collider3D : public SceneItemComponent { + public: + Collider3D(SceneItem *item); + + virtual bool_t raycast(struct Ray3D ray, struct Collider3DRayResult *out) = 0; + }; +} \ No newline at end of file diff --git a/src/dawn/scene/components/physics/3d/collider/CubeCollider.cpp b/src/dawn/scene/components/physics/3d/collider/CubeCollider.cpp new file mode 100644 index 00000000..05a399cb --- /dev/null +++ b/src/dawn/scene/components/physics/3d/collider/CubeCollider.cpp @@ -0,0 +1,16 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "CubeCollider.hpp" + +using namespace Dawn; + +CubeCollider::CubeCollider(SceneItem *item) : Collider3D(item) { + +} + +bool_t CubeCollider::raycast(struct Ray3D ray, struct Collider3DRayResult *out){ + return false; +} \ No newline at end of file diff --git a/src/dawn/scene/components/physics/3d/collider/CubeCollider.hpp b/src/dawn/scene/components/physics/3d/collider/CubeCollider.hpp new file mode 100644 index 00000000..5971d941 --- /dev/null +++ b/src/dawn/scene/components/physics/3d/collider/CubeCollider.hpp @@ -0,0 +1,19 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "Collider3D.hpp" + +namespace Dawn { + class CubeCollider : public Collider3D { + public: + glm::vec3 center = glm::vec3(0, 0, 0); + glm::vec3 size = glm::vec3(1, 1, 1); + + CubeCollider(SceneItem *item); + + bool_t raycast(struct Ray3D ray, struct Collider3DRayResult *out)override; + }; +} \ No newline at end of file diff --git a/src/dawnglfw/host/DawnGLFWHost.cpp b/src/dawnglfw/host/DawnGLFWHost.cpp index 31fafe34..02898f0f 100644 --- a/src/dawnglfw/host/DawnGLFWHost.cpp +++ b/src/dawnglfw/host/DawnGLFWHost.cpp @@ -66,6 +66,9 @@ int32_t DawnHost::init(DawnGame *game) { game->inputManager.bind(INPUT_BIND_NEGATIVE_Y, GLFW_KEY_S); game->inputManager.bind(INPUT_BIND_POSITIVE_Y, GLFW_KEY_W); + game->inputManager.bind(INPUT_BIND_MOUSE_X, INPUT_MANAGER_AXIS_MOUSE_X); + game->inputManager.bind(INPUT_BIND_MOUSE_Y, INPUT_MANAGER_AXIS_MOUSE_Y); + // Initialize the game auto result = game->init(); if(result != DAWN_GAME_INIT_RESULT_SUCCESS) return result; @@ -81,6 +84,7 @@ int32_t DawnHost::init(DawnGame *game) { // Set up event listeners glfwSetFramebufferSizeCallback(this->data->window, &glfwOnResize); glfwSetKeyCallback(this->data->window, &glfwOnKey); + glfwSetCursorPosCallback(this->data->window, &glfwOnCursor); return DAWN_HOST_INIT_RESULT_SUCCESS; } @@ -178,4 +182,17 @@ void glfwOnKey( // Determine the input axis DAWN_HOST->game->inputManager.rawInputValues[key] = value; +} + +void glfwOnCursor(GLFWwindow* window, double xpos, double ypos) { + if(DAWN_HOST == nullptr) return; + + DAWN_HOST->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_X] = mathClamp( + (float_t)(xpos / DAWN_HOST->game->renderManager.backBuffer.getWidth()), + 0, 1 + ); + DAWN_HOST->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_Y] = mathClamp( + (float_t)(ypos / DAWN_HOST->game->renderManager.backBuffer.getHeight()), + 0, 1 + ); } \ No newline at end of file diff --git a/src/dawnglfw/host/DawnGLFWHost.hpp b/src/dawnglfw/host/DawnGLFWHost.hpp index 7d35fb8b..1b3ea616 100644 --- a/src/dawnglfw/host/DawnGLFWHost.hpp +++ b/src/dawnglfw/host/DawnGLFWHost.hpp @@ -1,33 +1,34 @@ -// Copyright (c) 2022 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once - -#include -#include -#include "host/DawnHost.hpp" - -#define DAWN_GLFW_WINDOW_WIDTH_DEFAULT 1280 -#define DAWN_GLFW_WINDOW_HEIGHT_DEFAULT 720 - -#define DAWN_GLFW_INIT_RESULT_INIT_FAILED -1 -#define DAWN_GLFW_INIT_RESULT_WINDOW_CREATE_FAILED -2 - -#define DAWN_GLFW_START_RESULT_UPDATE_FAILED -1 - -namespace Dawn { - class DawnHostData { - public: - GLFWwindow *window; - }; -} - -// GLFW Callbacks -void glfwOnError(int error, const char* description); -void glfwOnResize(GLFWwindow *window, int32_t width, int32_t height); -void glfwOnKey( - GLFWwindow *window, - int32_t key, int32_t scancode, int32_t action, int32_t mods -); \ No newline at end of file +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once + +#include +#include +#include "host/DawnHost.hpp" + +#define DAWN_GLFW_WINDOW_WIDTH_DEFAULT 1280 +#define DAWN_GLFW_WINDOW_HEIGHT_DEFAULT 720 + +#define DAWN_GLFW_INIT_RESULT_INIT_FAILED -1 +#define DAWN_GLFW_INIT_RESULT_WINDOW_CREATE_FAILED -2 + +#define DAWN_GLFW_START_RESULT_UPDATE_FAILED -1 + +namespace Dawn { + class DawnHostData { + public: + GLFWwindow *window; + }; +} + +// GLFW Callbacks +void glfwOnError(int error, const char* description); +void glfwOnResize(GLFWwindow *window, int32_t width, int32_t height); +void glfwOnKey( + GLFWwindow *window, + int32_t key, int32_t scancode, int32_t action, int32_t mods +); +void glfwOnCursor(GLFWwindow* window, double xpos, double ypos); \ No newline at end of file diff --git a/src/dawnglfw/input/InputManager.hpp b/src/dawnglfw/input/InputManager.hpp index cf755acf..784d5b4e 100644 --- a/src/dawnglfw/input/InputManager.hpp +++ b/src/dawnglfw/input/InputManager.hpp @@ -6,6 +6,9 @@ #pragma once #include "input/_InputManager.hpp" +#define INPUT_MANAGER_AXIS_MOUSE_X -580000 +#define INPUT_MANAGER_AXIS_MOUSE_Y -580001 + namespace Dawn { class InputManager : public IInputManager { protected: diff --git a/src/dawnopengl/display/mesh/Mesh.cpp b/src/dawnopengl/display/mesh/Mesh.cpp index 0ce9288e..e2fd41d9 100644 --- a/src/dawnopengl/display/mesh/Mesh.cpp +++ b/src/dawnopengl/display/mesh/Mesh.cpp @@ -1,118 +1,153 @@ -// Copyright (c) 2022 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#include "Mesh.hpp" - -using namespace Dawn; - -void Mesh::createBuffers( - int32_t verticeCount, - int32_t indiceCount -) { - if(verticeCount <= 0) throw "Vertice count must be greater than zero."; - if(indiceCount <= 0) throw "Indice count must be greater than zero."; - - this->disposeBuffers(); - - this->verticeCount = verticeCount; - this->indiceCount = indiceCount; - - auto sizePos = sizeof(glm::vec3) * verticeCount; - auto sizeInds = sizeof(meshindice_t) * indiceCount; - auto sizeCoords = sizeof(glm::vec2) * verticeCount; - - // Create some buffers, one for the vertex data, one for the indices - GLuint buffer[2]; - glGenBuffers(2, buffer); - this->vertexBuffer = buffer[0]; - if(this->vertexBuffer < 0) throw "Failed to create vertex buffer"; - this->indexBuffer = buffer[1]; - if(this->indexBuffer < 0) throw "Failed to create index buffer"; - - // Buffer an empty set of data then buffer each component - glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer); - glBufferData(GL_ARRAY_BUFFER, sizePos+sizeCoords, 0, GL_DYNAMIC_DRAW); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeInds, 0, GL_DYNAMIC_DRAW); - - // Setup the attrib pointers - size_t offset = 0; - glVertexAttribPointer( - 0, sizeof(glm::vec3) / sizeof(float_t), - GL_FLOAT, GL_FALSE, - 0, (void *)offset - ); - glEnableVertexAttribArray(0); - - offset += sizePos; - glVertexAttribPointer( - 1, sizeof(glm::vec2) / sizeof(float_t), - GL_FLOAT, GL_FALSE, - 0, (void *)offset - ); - glEnableVertexAttribArray(1); -} - -void Mesh::disposeBuffers() { - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - - if(this->vertexBuffer != -1) { - glDeleteBuffers(1, &this->vertexBuffer); - this->vertexBuffer = -1; - this->verticeCount = -1; - } - - if(this->indexBuffer != -1) { - glDeleteBuffers(1, &this->indexBuffer); - this->indexBuffer = -1; - this->indiceCount = -1; - } -} - -void Mesh::draw( - enum MeshDrawMode drawMode, - int32_t start, - int32_t count -) { - if( - count == 0 || - this->vertexBuffer == -1 || - this->indexBuffer == -1 - ) return; - - if(count == -1) count = this->indiceCount; - - - // Re-Bind the buffers - glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer); - - // Re-Calculate the attrib pointers. - size_t offset = 0; - glVertexAttribPointer( - 0, sizeof(glm::vec3) / sizeof(float_t), - GL_FLOAT, GL_FALSE, - 0, (void *)offset - ); - glEnableVertexAttribArray(0); - - offset += sizeof(glm::vec3) * this->verticeCount; - glVertexAttribPointer( - 1, sizeof(glm::vec2) / sizeof(float_t), - GL_FLOAT, GL_FALSE, - 0, (void *)offset - ); - glEnableVertexAttribArray(1); - - // Render the elements. - glDrawElements( - drawMode, count, GL_UNSIGNED_INT, (void *)(sizeof(meshindice_t) * start) - ); -} - -Mesh::~Mesh() { - this->disposeBuffers(); +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "Mesh.hpp" + +using namespace Dawn; + +void Mesh::createBuffers( + int32_t verticeCount, + int32_t indiceCount +) { + if(verticeCount <= 0) throw "Vertice count must be greater than zero."; + if(indiceCount <= 0) throw "Indice count must be greater than zero."; + + this->disposeBuffers(); + + this->verticeCount = verticeCount; + this->indiceCount = indiceCount; + + auto sizePos = sizeof(glm::vec3) * verticeCount; + auto sizeInds = sizeof(meshindice_t) * indiceCount; + auto sizeCoords = sizeof(glm::vec2) * verticeCount; + + // Create some buffers, one for the vertex data, one for the indices + GLuint buffer[2]; + glGenBuffers(2, buffer); + this->vertexBuffer = buffer[0]; + if(this->vertexBuffer < 0) throw "Failed to create vertex buffer"; + this->indexBuffer = buffer[1]; + if(this->indexBuffer < 0) throw "Failed to create index buffer"; + + // Buffer an empty set of data then buffer each component + glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer); + glBufferData(GL_ARRAY_BUFFER, sizePos+sizeCoords, 0, GL_DYNAMIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeInds, 0, GL_DYNAMIC_DRAW); + + // Setup the attrib pointers + size_t offset = 0; + glVertexAttribPointer( + 0, sizeof(glm::vec3) / sizeof(float_t), + GL_FLOAT, GL_FALSE, + 0, (void *)offset + ); + glEnableVertexAttribArray(0); + + offset += sizePos; + glVertexAttribPointer( + 1, sizeof(glm::vec2) / sizeof(float_t), + GL_FLOAT, GL_FALSE, + 0, (void *)offset + ); + glEnableVertexAttribArray(1); +} + +void Mesh::disposeBuffers() { + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + if(this->vertexBuffer != -1) { + glDeleteBuffers(1, &this->vertexBuffer); + this->vertexBuffer = -1; + this->verticeCount = -1; + } + + if(this->indexBuffer != -1) { + glDeleteBuffers(1, &this->indexBuffer); + this->indexBuffer = -1; + this->indiceCount = -1; + } +} + +void Mesh::bufferPositions(int32_t pos, glm::vec3 *positions, int32_t len) { + assertNotNull(positions); + glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer); + glBufferSubData( + GL_ARRAY_BUFFER, + sizeof(glm::vec3) * pos, + sizeof(glm::vec3) * len, + (void*)positions + ); +} + +void Mesh::bufferCoordinates(int32_t pos, glm::vec2 *coordinates, int32_t len) { + assertNotNull(coordinates); + + auto offsetCoordinates = ( + (sizeof(glm::vec3) * this->verticeCount) + + (sizeof(glm::vec2) * pos) + ); + + glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer); + glBufferSubData( + GL_ARRAY_BUFFER, + offsetCoordinates, + sizeof(glm::vec2) * len, + (void*)coordinates + ); +} + +void Mesh::bufferIndices(int32_t pos, meshindice_t *indices, int32_t len) { + assertNotNull(indices); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer); + glBufferSubData( + GL_ELEMENT_ARRAY_BUFFER, + sizeof(meshindice_t) * pos, + sizeof(meshindice_t) * len, + (void*)indices + ); +} + +void Mesh::draw(enum MeshDrawMode drawMode, int32_t start, int32_t count) { + if( + count == 0 || + this->vertexBuffer == -1 || + this->indexBuffer == -1 + ) return; + + if(count == -1) count = this->indiceCount; + + + // Re-Bind the buffers + glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer); + + // Re-Calculate the attrib pointers. + size_t offset = 0; + glVertexAttribPointer( + 0, sizeof(glm::vec3) / sizeof(float_t), + GL_FLOAT, GL_FALSE, + 0, (void *)offset + ); + glEnableVertexAttribArray(0); + + offset += sizeof(glm::vec3) * this->verticeCount; + glVertexAttribPointer( + 1, sizeof(glm::vec2) / sizeof(float_t), + GL_FLOAT, GL_FALSE, + 0, (void *)offset + ); + glEnableVertexAttribArray(1); + + // Render the elements. + glDrawElements( + drawMode, count, GL_UNSIGNED_INT, (void *)(sizeof(meshindice_t) * start) + ); +} + +Mesh::~Mesh() { + this->disposeBuffers(); } \ No newline at end of file diff --git a/src/dawnopengl/display/mesh/Mesh.hpp b/src/dawnopengl/display/mesh/Mesh.hpp index ef9250d0..f85e6668 100644 --- a/src/dawnopengl/display/mesh/Mesh.hpp +++ b/src/dawnopengl/display/mesh/Mesh.hpp @@ -51,70 +51,29 @@ namespace Dawn { /** * Write vertice positions to the mesh. * - * @tparam N Size of the array, in terms of number of elements. - * @param position Position, within the buffer, to write to. + * @param pos Position, within the buffer, to write to. * @param vertices Array of positions to write. + * @param len How many positions are in the array. */ - template - void bufferPositions( - int32_t position, - std::array positions - ) { - glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer); - glBufferSubData( - GL_ARRAY_BUFFER, - sizeof(glm::vec3) * position, - sizeof(positions), - (void *)positions.data() - ); - } + void bufferPositions(int32_t pos, glm::vec3 *positions, int32_t len); /** * Write vertice coordinates to the mesh. * - * @tparam N Size of the array, in terms of number of elements. - * @param position Position, within the buffer, to write to. + * @param pos Position, within the buffer, to write to. * @param coordinates Array of coordinates to write. + * @param len How many coordinates are in the array. */ - template - void bufferCoordinates( - int32_t position, - std::array coordinates - ) { - auto offsetCoordinates = ( - (sizeof(glm::vec3) * this->verticeCount) + - (sizeof(glm::vec2) * position) - ); - - glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer); - glBufferSubData( - GL_ARRAY_BUFFER, - offsetCoordinates, - sizeof(coordinates), - (void *)coordinates.data() - ); - } + void bufferCoordinates(int32_t pos, glm::vec2 *coordinates, int32_t len); /** * Write indices to the mesh. * - * @tparam N Size of the array, in terms of number of elements. - * @param position Position, within the buffer, to write to. + * @param pos Position, within the buffer, to write to. * @param indices Array of indices to write. + * @param len How many indices are in the array. */ - template - void bufferIndices( - int32_t position, - std::array indices - ) { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer); - glBufferSubData( - GL_ELEMENT_ARRAY_BUFFER, - sizeof(meshindice_t) * position, - sizeof(indices), - (void *)indices.data() - ); - } + void bufferIndices(int32_t pos, meshindice_t *indices, int32_t len); /** * Draw a primitive. Primitives are drawn by their indices. @@ -123,11 +82,7 @@ namespace Dawn { * @param start Start indice (index) to draw. * @param count Count of indices to draw. Use -1 to draw all. */ - void draw( - enum MeshDrawMode drawMode, - int32_t start, - int32_t count - ); + void draw(enum MeshDrawMode drawMode, int32_t start, int32_t count); /** * Cleanup a previously initiated mesh. diff --git a/src/dawntictactoe/input/InputBinds.hpp b/src/dawntictactoe/input/InputBinds.hpp index 89145de6..eb47ba8f 100644 --- a/src/dawntictactoe/input/InputBinds.hpp +++ b/src/dawntictactoe/input/InputBinds.hpp @@ -11,4 +11,6 @@ #define INPUT_BIND_NEGATIVE_X INPUT_BIND(2) #define INPUT_BIND_POSITIVE_X INPUT_BIND(3) #define INPUT_BIND_NEGATIVE_Y INPUT_BIND(4) -#define INPUT_BIND_POSITIVE_Y INPUT_BIND(5) \ No newline at end of file +#define INPUT_BIND_POSITIVE_Y INPUT_BIND(5) +#define INPUT_BIND_MOUSE_X INPUT_BIND(6) +#define INPUT_BIND_MOUSE_Y INPUT_BIND(7) \ No newline at end of file diff --git a/src/dawntictactoe/scenes/TicTacToeScene.hpp b/src/dawntictactoe/scenes/TicTacToeScene.hpp index 3bee8b7b..444ff876 100644 --- a/src/dawntictactoe/scenes/TicTacToeScene.hpp +++ b/src/dawntictactoe/scenes/TicTacToeScene.hpp @@ -7,6 +7,9 @@ #include "scene/Scene.hpp" #include "prefabs/SimpleSpinningCubePrefab.hpp" #include "prefabs/TicTacToeTilePrefab.hpp" +#include "display/mesh/SphereMesh.hpp" + +#include "scene/components/physics/3d/RayTester3D.hpp" namespace Dawn { class TicTacToeScene : public Scene { @@ -16,19 +19,34 @@ namespace Dawn { void stage() override { camera = Camera::create(this); - camera->transform->lookAt(glm::vec3(5, 5, 5), glm::vec3(0, 0, 0)); + camera->transform->lookAt(glm::vec3(0, 0, 5), glm::vec3(0, 0, 0)); + camera->type = CAMERA_TYPE_ORTHONOGRAPHIC; + + float_t s = 2.0f; + camera->orthoTop = -s; + camera->orthoBottom = s; + + float_t ratio = 1.0f / 9.0f * 16.0f; + camera->orthoLeft = -s * ratio; + camera->orthoRight = s * ratio; + + auto cube = SimpleSpinningCubePrefab::create(this); + // SphereMesh::createSphere(&cube->meshHost->mesh, 1.0f, 16, 16); + auto tester = cube->addComponent(); + tester->prefab = cube; + tester->camera = camera; + int32_t i = 0; for(int32_t x = -1; x <= 1; x++) { for(int32_t y = -1; y <= 1; y++) { - auto tile = TicTacToeTilePrefab::prefabCreate(this); + continue; + auto tile = TicTacToeTilePrefab::create(this); tile->transform.setLocalPosition(glm::vec3(x * 1.25f, y * 1.25f, 0)); - // tile->ticTacToe->setState(i % 2 == 0 ? TIC_TAC_TOE_CROSS : TIC_TAC_TOE_NOUGHT); + tile->ticTacToe->setState(i % 2 == 0 ? TIC_TAC_TOE_CROSS : TIC_TAC_TOE_NOUGHT); tiles[i++] = tile; } } - - } std::vector getRequiredAssets() override {