diff --git a/CMakeLists.txt b/CMakeLists.txt index 75a705f..13a4b06 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,19 +1,17 @@ cmake_minimum_required (VERSION 3.16) +# Dependencies set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") set(USE_SYSTEM_OCCT ON CACHE INTERNAL "") -include(${CMAKE_SOURCE_DIR}/cmake/glad.cmake) -include(${CMAKE_SOURCE_DIR}/cmake/stb.cmake) -include(${CMAKE_SOURCE_DIR}/cmake/imgui.cmake) -include(${CMAKE_SOURCE_DIR}/cmake/occt.cmake) +# Main project project( - hyporo - VERSION 0.20.0 + project-hyporo + VERSION 0.10.0 LANGUAGES CXX ) @@ -22,16 +20,12 @@ option(WITH_GTESTS "Enable GTest unit testing" ON) enable_testing() if(WITH_GTESTS) + option(INSTALL_GMOCK "" OFF) + option(INSTALL_GTEST "" OFF) include(${CMAKE_SOURCE_DIR}/cmake/googletest.cmake) include(GoogleTest) endif() - -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") -add_definitions(-DPRECISION_DOUBLE) - add_subdirectory(source) - -add_subdirectory(docs) \ No newline at end of file +#add_subdirectory(docs) \ No newline at end of file diff --git a/cmake/imgui.cmake b/cmake/imgui.cmake index 68c8302..f9426d8 100644 --- a/cmake/imgui.cmake +++ b/cmake/imgui.cmake @@ -9,7 +9,7 @@ CPMAddPackage( ) if(imgui_external_ADDED) - project(imgui) + set(EXTERNAL_PROJECT_NAME imgui) set(CMAKE_CXX_STANDARD 17) @@ -18,7 +18,7 @@ if(imgui_external_ADDED) find_package(OpenGL REQUIRED) pkg_search_module(GLFW REQUIRED glfw3) - add_library(${PROJECT_NAME} STATIC + add_library(${EXTERNAL_PROJECT_NAME} STATIC ${imgui_external_SOURCE_DIR}/imgui.cpp ${imgui_external_SOURCE_DIR}/imgui_demo.cpp ${imgui_external_SOURCE_DIR}/imgui_draw.cpp @@ -31,11 +31,11 @@ if(imgui_external_ADDED) ${imgui_external_SOURCE_DIR}/misc/freetype/imgui_freetype.cpp ${imgui_external_SOURCE_DIR}/misc/cpp/imgui_stdlib.cpp ) - add_library(imgui::imgui ALIAS ${PROJECT_NAME}) + add_library(imgui::imgui ALIAS ${EXTERNAL_PROJECT_NAME}) add_compile_definitions(IMGUI_IMPL_OPENGL_LOADER_GLAD) - target_include_directories(${PROJECT_NAME} + target_include_directories(${EXTERNAL_PROJECT_NAME} PUBLIC $ $ @@ -46,14 +46,14 @@ if(imgui_external_ADDED) ${GLFW_INCLUDE_DIRS} ) - target_link_libraries(${PROJECT_NAME} + target_link_libraries(${EXTERNAL_PROJECT_NAME} PUBLIC Freetype::Freetype glfw OpenGL::GL ) - set_target_properties(${PROJECT_NAME} + set_target_properties(${EXTERNAL_PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON OUTPUT_NAME imgui @@ -63,21 +63,21 @@ if(imgui_external_ADDED) include(GNUInstallDirs) install( - TARGETS ${PROJECT_NAME} - EXPORT ${PROJECT_NAME}Targets - DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME} + TARGETS ${EXTERNAL_PROJECT_NAME} + EXPORT ${EXTERNAL_PROJECT_NAME}Targets + DESTINATION ${CMAKE_INSTALL_LIBDIR}/${EXTERNAL_PROJECT_NAME} ) install( - EXPORT ${PROJECT_NAME}Targets - FILE ${PROJECT_NAME}Targets.cmake - NAMESPACE ${PROJECT_NAME}:: - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} + EXPORT ${EXTERNAL_PROJECT_NAME}Targets + FILE ${EXTERNAL_PROJECT_NAME}Targets.cmake + NAMESPACE ${EXTERNAL_PROJECT_NAME}:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${EXTERNAL_PROJECT_NAME} ) install( DIRECTORY ${imgui_external_SOURCE_DIR} - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME} + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${EXTERNAL_PROJECT_NAME} COMPONENT devel FILES_MATCHING PATTERN "*.h" diff --git a/cmake/macros.cmake b/cmake/macros.cmake new file mode 100644 index 0000000..aca3d9a --- /dev/null +++ b/cmake/macros.cmake @@ -0,0 +1,52 @@ + +macro(add_component_library COMPONENT LIBRARY_TYPE COMPONENT_SOURCES) + add_library(${COMPONENT} ${LIBRARY_TYPE} ${COMPONENT_SOURCES}) + add_library(${PROJECT_NAME}::${COMPONENT} ALIAS ${COMPONENT}) +endmacro() + +macro(install_component_source COMPONENT) + set_target_properties(${COMPONENT} + PROPERTIES + VERSION ${${PROJECT_NAME}_VERSION} + SOVERSION ${${PROJECT_NAME}_VERSION_MAJOR} + OUTPUT_NAME ${PROJECT_NAME}-${COMPONENT} + ) + + install( + TARGETS ${COMPONENT} + EXPORT ${COMPONENT}Targets + DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME} + ) + + install( + EXPORT ${COMPONENT}Targets + FILE ${COMPONENT}Targets.cmake + NAMESPACE ${PROJECT_NAME}:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} + ) +endmacro() + +macro(install_component_headers COMPONENT) + + #target_include_directories(${COMPONENT} + # PUBLIC + # $ + # $#/${CMAKE_PROJECT_NAME}>#/include> + # ) + + install( + DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}#/${COMPONENT} + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}#/${COMPONENT} + COMPONENT devel + FILES_MATCHING + PATTERN "*.h" + PATTERN "*.hpp" + ) + + install( + FILES ${CMAKE_CURRENT_SOURCE_DIR}/../${COMPONENT}.hpp + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}#/${COMPONENT} + COMPONENT devel + ) + +endmacro() \ No newline at end of file diff --git a/cmake/stb.cmake b/cmake/stb.cmake index 1308ff8..f1377c4 100644 --- a/cmake/stb.cmake +++ b/cmake/stb.cmake @@ -8,17 +8,17 @@ CPMAddPackage( ) if(stb_external_ADDED) - project(stb) + set(EXTERNAL_PROJECT_NAME stb) - add_library(${PROJECT_NAME} INTERFACE) - add_library(stb::stb ALIAS ${PROJECT_NAME}) + add_library(${EXTERNAL_PROJECT_NAME} INTERFACE) + add_library(stb::stb ALIAS ${EXTERNAL_PROJECT_NAME}) - target_include_directories(${PROJECT_NAME} + target_include_directories(${EXTERNAL_PROJECT_NAME} INTERFACE $ ) - set_target_properties(${PROJECT_NAME} + set_target_properties(${EXTERNAL_PROJECT_NAME} PROPERTIES OUTPUT_NAME stb ) @@ -26,21 +26,21 @@ if(stb_external_ADDED) include(GNUInstallDirs) install( - TARGETS ${PROJECT_NAME} - EXPORT ${PROJECT_NAME}Targets - DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME} + TARGETS ${EXTERNAL_PROJECT_NAME} + EXPORT ${EXTERNAL_PROJECT_NAME}Targets + DESTINATION ${CMAKE_INSTALL_LIBDIR}/${EXTERNAL_PROJECT_NAME} ) install( - EXPORT ${PROJECT_NAME}Targets - FILE ${PROJECT_NAME}Targets.cmake - NAMESPACE ${PROJECT_NAME}:: - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} + EXPORT ${EXTERNAL_PROJECT_NAME}Targets + FILE ${EXTERNAL_PROJECT_NAME}Targets.cmake + NAMESPACE ${EXTERNAL_PROJECT_NAME}:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${EXTERNAL_PROJECT_NAME} ) install( DIRECTORY ${stb_external_SOURCE_DIR} - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME} + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${EXTERNAL_PROJECT_NAME} COMPONENT devel FILES_MATCHING PATTERN "*.h" diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 4086f48..4a1aa49 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -1,3 +1,3 @@ -add_subdirectory(hyporo) +add_subdirectory(hpr) add_subdirectory(creator) -add_subdirectory(applications) \ No newline at end of file +#add_subdirectory(applications) \ No newline at end of file diff --git a/source/applications/periodic/lattice.hpp b/source/applications/periodic/lattice.hpp new file mode 100644 index 0000000..951f94d --- /dev/null +++ b/source/applications/periodic/lattice.hpp @@ -0,0 +1,208 @@ +#pragma once + +#include "../../hyporo/hyplib/scalar/scalar.hpp" +#include "../../hyporo/hyplib/vector/vector.hpp" +#include "../../hyporo/hyplib/array/array.hpp" +#include "../../hyporo/hyplib/matrix/matrix.hpp" +#include "../../hyporo/csg/csg.hpp" + + +namespace hpr::csg +{ +void prints(scalar point) +{ + std::cout << point << std::endl; +} +class Lattice : public csg::Shape +{ + +public: + + enum class System + { + Triclinic, + Monoclinic, + Orthorhombic, + Tetragonal, + Rhombohedral, + Hexagonal, + Cubic, + Unknown + }; + + enum class Type + { + Primitive, + BaseCentered, + BodyCentered, + FaceCentered, + Unknown + }; + +protected: + + vec3 p_lengths; + vec3 p_angles; + Type p_type; + scalar p_radius; + darray p_controlPoints; + +public: + + Lattice() = delete; + + Lattice(const vec3& lengths, const vec3& angles, scalar radius, Type type) : + csg::Shape {}, + p_lengths {lengths}, + p_angles {angles}, + p_radius {radius}, + p_type {type} + { + generateControlPoints(); + darray spheres; + for (const auto& point : controlPoints()) { + spheres.push(csg::sphere(point, p_radius)); + print(point); + } + + p_shape = csg::Compound(spheres).tshape();//csg::fuse({spheres.front()}, spheres.slice(spheres.begin() + 1, spheres.end())).tshape(); + } + + darray controlPoints() const + { + return p_controlPoints; + } + + vec3 lengths() const + { + return p_lengths; + } + + vec3 angles() const + { + return p_angles; + } + + void generateControlPoints() + { + if (p_type == Type::Unknown) + throw std::runtime_error("Unknown type of lattice"); + p_controlPoints.resize(14); + // + vec3 ox {1, 0, 0}; + vec3 oy {0, 1, 0}; + vec3 oz {0, 0, 1}; + vec3 ox1 = hpr::rotate(ox, oz, radians(-p_angles[2])); + p_controlPoints.push(vec3{0, 0, 0}); + p_controlPoints.push(vec3{0, p_lengths[0], 0}); + vec3 t1 = hpr::translate(p_controlPoints.back(), ox1 * p_lengths[1]); + p_controlPoints.push(t1); + p_controlPoints.push(hpr::translate(p_controlPoints.front(), ox1 * p_lengths[1])); + print(t1); + print(ox1); + scalar c1 = cos(radians(p_angles[2])), c2 = cos(radians(p_angles[1])), c3 = cos(radians(p_angles[0])); + scalar D1 = sqrt(mat3( + 1, cos(radians(p_angles[2])), cos(radians(p_angles[1])), + cos(radians(p_angles[2])), 1, cos(radians(p_angles[0])), + cos(radians(p_angles[1])), cos(radians(p_angles[0])), 1).det()); + scalar volume = 1. / 6. * p_lengths[0] * p_lengths[1] * p_lengths[2] * + D1; + scalar s1 = sqrt(std::pow(p_lengths[0], 2) + std::pow(p_lengths[1], 2) - 2 * + p_lengths[0] * p_lengths[1] * cos(radians(p_angles[2]))); + scalar s2 = sqrt(std::pow(p_lengths[1], 2) + std::pow(p_lengths[2], 2) - 2 * + p_lengths[1] * p_lengths[2] * cos(radians(p_angles[1]))); + scalar s3 = sqrt(std::pow(p_lengths[0], 2) + std::pow(p_lengths[2], 2) - 2 * + p_lengths[0] * p_lengths[2] * cos(radians(p_angles[0]))); + scalar area = 1. / 2. * p_lengths[0] * p_lengths[1] * + sqrt(mat2{1, cos(radians(p_angles[2])), cos(radians(p_angles[2])), 1}.det()); + scalar h1 = 3 * volume / area; + scalar a1 = asin(h1 / p_lengths[2]); + scalar sh1 = sqrt(std::pow(p_lengths[2], 2) - std::pow(h1, 2)); + scalar sh2 = p_lengths[2] * cos(radians(p_angles[0])); + scalar a2 = acos(sh2 / sh1); + + vec3 ox2 = hpr::rotate(ox, oy, a1); + if (!std::isnan(a2)) + ox2 = hpr::rotate(ox2, oz, a2); + print(ox2); + for (auto n = 0; n < 4; ++n) + p_controlPoints.push(hpr::translate(p_controlPoints[n], ox2 * p_lengths[2])); + + /*p_controlPoints.push(vec3{p_lengths[0], p_lengths[1], 0}); + p_controlPoints.push(vec3{p_lengths[0], 0, 0}); + + p_controlPoints.push(vec3{0, 0, p_lengths[2]}); + p_controlPoints.push(vec3{0, p_lengths[1], p_lengths[2]}); + p_controlPoints.push(vec3{p_lengths[0], p_lengths[1], p_lengths[2]}); + p_controlPoints.push(vec3{p_lengths[0], 0, p_lengths[2]}); + + // central points on base faces + if (p_type == Type::BaseCentered || p_type == Type::FaceCentered) + { + for (int n = 0; n < 2; ++n) + { + vec3 center; + for (int k = 0; k < 4; ++k) + center += p_controlPoints[k + 4 * n]; + p_controlPoints.push(center * 0.25); + } + } + + // central point (center of mass) + if (p_type == Type::BodyCentered) + { + vec3 center; + for (const auto& point : p_controlPoints) + center += point; + p_controlPoints.push(center / p_controlPoints.size()); + } + + // central points on side faces + if (p_type == Type::FaceCentered) + { + for (int n = 0; n < 3; ++n) + { + vec3 center; + for (int k = 0; k < 2; ++k) + { + center += p_controlPoints[n + k]; + center += p_controlPoints[n + k + 4]; + } + p_controlPoints.push(center * 0.25); + } + vec3 center; + for (int n = 0; n < 2; ++n) + { + center += p_controlPoints[n * 3]; + center += p_controlPoints[4 + n * 3]; + } + p_controlPoints.push(center * 0.25); + } + + mat4 trans = mat4::identity(); + vec3 ox {1, 0, 0}; + vec3 oy {0, 1, 0}; + vec3 oz {0, 0, 1}; + int n = 0; + for (auto& point : p_controlPoints) + { + if (n == 0 || n == 3) + { + ++n; + continue; + } + trans.row(3, vec4(point, 0)); + trans = hpr::rotate(trans, oz, -radians(90 - p_angles[2])); + if (n >= 4 && n <= 7) + { + trans = hpr::rotate(trans, ox, -radians(90 - p_angles[1])); + trans = hpr::rotate(trans, oy, -radians(90 - p_angles[0])); + } + point = vec3(trans.row(3)[0], trans.row(3)[1], trans.row(3)[2]); + ++n; + }*/ + + } +}; + +} \ No newline at end of file diff --git a/source/applications/periodic/periodic.cpp b/source/applications/periodic/periodic.cpp index 800d7b2..fc1437e 100644 --- a/source/applications/periodic/periodic.cpp +++ b/source/applications/periodic/periodic.cpp @@ -144,6 +144,7 @@ public: lattice = lattice.scale({0, 0, 0}, 1e-2); } + std::cout << (int)lattice.type() << std::endl; return lattice; } @@ -200,6 +201,7 @@ public: face.label("periodic-up"); } + std::cout << (int)cell.type() << std::endl; return cell; } @@ -228,12 +230,17 @@ public: }; +#include "lattice.hpp" int main(int argc, char** argv) { - Simple simple {0.01, {1., 0., 0.}}; + /*Simple simple {0.01, {1., 0., 0.}}; simple.build(); - //simple.dump("simpleTest.step", csg::Shape::Format::STEP); + std::cout << (int)simple.type() << std::endl; + std::cout << simple.volume() << std::endl; + */ + csg::Lattice lattice {{2, 2, 2}, {90, 90, 90}, 1, csg::Lattice::Type::Primitive}; + lattice.dump("latticeTest.step", csg::Shape::Format::STEP); return 0; } \ No newline at end of file diff --git a/source/creator/CMakeLists.txt b/source/creator/CMakeLists.txt index 8f80dda..5e5754a 100644 --- a/source/creator/CMakeLists.txt +++ b/source/creator/CMakeLists.txt @@ -1,22 +1,37 @@ -include_directories( - ../hyporo/hyplib - ../hyporo/window_system - ../hyporo/gpu - ../hyporo/hmesh +project( + hyporo + VERSION 0.1.0 + LANGUAGES CXX ) -add_executable(hyporo - # Source files +# Compiler options +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") + +# Project options +include(GNUInstallDirs) + + +add_executable(${PROJECT_NAME} creator.cpp + ../hpr/window_system/window_system.cpp + ../hpr/window_system/monitor.cpp + ../hpr/window_system/window.cpp + ../hpr/window_system/glfw/window_system.cpp + ../hpr/window_system/glfw/monitor.cpp + ../hpr/window_system/glfw/window.cpp ) -target_link_libraries(hyporo - PUBLIC - hyporo-hyplib - hyporo-window-system - hyporo-gpu - hyporo-mesh +include(${CMAKE_SOURCE_DIR}/cmake/imgui.cmake) +message(STATUS "project name: ${PROJECT_NAME}") +target_link_libraries(${PROJECT_NAME} + hpr::hpr imgui ) + +target_include_directories(${PROJECT_NAME} + PRIVATE + ../ +) diff --git a/source/creator/creator.cpp b/source/creator/creator.cpp index 7c1e5dd..88cc2ef 100644 --- a/source/creator/creator.cpp +++ b/source/creator/creator.cpp @@ -1,10 +1,10 @@ -#include "../window_system/window_system.hpp" -#include "../window_system/glfw/window.hpp" -#include "../gpu/device.hpp" -#include "../gpu/opengl/device.hpp" -#include "../hmesh/mesh.hpp" -#include "../hyplib/matrix/matrix.hpp" -#include "../hyplib/vector/vector.hpp" +#include "hpr/window_system/window_system.hpp" +#include "hpr/window_system/glfw/window_system.hpp" +#include "hpr/window_system/glfw/window.hpp" +#include "hpr/gpu.hpp" +#include "hpr/math.hpp" +#include "hpr/mesh.hpp" + #include #include #include diff --git a/source/hpr/CMakeLists.txt b/source/hpr/CMakeLists.txt new file mode 100644 index 0000000..a991008 --- /dev/null +++ b/source/hpr/CMakeLists.txt @@ -0,0 +1,131 @@ + +project( + hpr + VERSION 0.10.0 + LANGUAGES CXX +) + +# Compiler options +set(CMAKE_CXX_STANDARD 20) +#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") + +add_definitions(-DPRECISION_DOUBLE) + +# Project options +include(GNUInstallDirs) +include(${CMAKE_SOURCE_DIR}/cmake/macros.cmake) + +option(WITH_CSG "Build with CSG component" ON) +option(WITH_GPU "Build with graphics component" ON) +option(WITH_MESH "Build with mesh utils component" ON) +option(WITH_WS "Build with window system component" ON) + +# containers +file(GLOB containers_hpp "containers.hpp" "containers/*.hpp" "containers/array/*.hpp") +file(GLOB containers_cpp "containers/*.cpp" "containers/array/*.cpp") + +# math +file(GLOB math_hpp "math.hpp" "math/*.hpp" "math/scalar/*.hpp" "math/vector/*.hpp" "math/matrix/*.hpp") +file(GLOB math_cpp "math/*.cpp" "math/scalar/*.cpp" "math/vector/*.cpp" "math/matrix/*.cpp") + +# io +file(GLOB io_hpp "io.hpp" "io/*.hpp") +file(GLOB io_cpp "io/*.cpp") + +# csg +file(GLOB csg_hpp "csg.hpp" "csg/*.hpp") +file(GLOB csg_cpp "csg/*.cpp") + +# gpu +file(GLOB gpu_hpp "gpu.hpp" "gpu/*.hpp") +file(GLOB gpu_cpp "gpu/*.cpp") +file(GLOB gpu_opengl_hpp "gpu/opengl/*.hpp") +file(GLOB gpu_opengl_cpp "gpu/opengl/*.cpp") + +# mesh +file(GLOB mesh_hpp "mesh.hpp" "mesh/*.hpp") +file(GLOB mesh_cpp "mesh/*.cpp") + +# window system +file(GLOB window_system_hpp "window_system.hpp" "window_system/*.hpp") +file(GLOB window_system_cpp "window_system/*.cpp") +file(GLOB window_system_glfw_hpp "window_system/glfw/*.hpp") +file(GLOB window_system_glfw_cpp "window_system/glfw/*.cpp") + +# +list(APPEND hpr_INCLUDE + ${containers_hpp} + ${math_hpp} + ${io_hpp} +) +list(APPEND hpr_SOURCES + ${containers_cpp} + ${math_cpp} + ${io_cpp} +) +list(APPEND hpr_LIBRARIES "") +list(APPEND hpr_INCLUDE_DIRS "") + +if (WITH_CSG) + include(${CMAKE_SOURCE_DIR}/cmake/occt.cmake) + + list(APPEND hpr_INCLUDE ${csg_hpp}) + list(APPEND hpr_SOURCES ${csg_cpp}) + list(APPEND hpr_LIBRARIES ${OCCT_LIBRARIES}) + + list(APPEND hpr_INCLUDE_DIRS ${OCCT_INCLUDE_DIRS}) +endif() + +if (WITH_GPU) + include(${CMAKE_SOURCE_DIR}/cmake/glad.cmake) + include(${CMAKE_SOURCE_DIR}/cmake/stb.cmake) + + list(APPEND hpr_INCLUDE ${gpu_hpp} ${gpu_opengl_hpp}) + list(APPEND hpr_SOURCES ${gpu_cpp} ${gpu_opengl_cpp}) + list(APPEND hpr_LIBRARIES glad stb) +endif() + +if (WITH_WS) + list(APPEND hpr_INCLUDE ${window_system_hpp} ${window_system_glfw_hpp}) + list(APPEND hpr_SOURCES ${window_system_cpp} ${window_system_glfw_cpp}) + list(APPEND hpr_LIBRARIES glfw) +endif() + +add_library(${PROJECT_NAME} SHARED + hpr.cpp + ${hpr_SOURCES} +) +add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) + +target_link_libraries(${PROJECT_NAME} + ${hpr_LIBRARIES} +) + +target_include_directories(${PROJECT_NAME} + PUBLIC + ${hpr_INCLUDE_DIRS} +) + +# Install library + +install( + TARGETS ${PROJECT_NAME} + EXPORT ${PROJECT_NAME}Targets + DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME} +) + +install( + EXPORT ${PROJECT_NAME}Targets + FILE ${PROJECT_NAME}Targets.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} +) + +include(CPack) + +# +message(STATUS "Components:") +message(STATUS " Core: ON") +message(STATUS " CSG: ${WITH_CSG}") +message(STATUS " GPU: ${WITH_GPU}") +message(STATUS " Mesh: ${WITH_MESH}") +message(STATUS " WindowSystem: ${WITH_WS}") diff --git a/source/hpr/containers.hpp b/source/hpr/containers.hpp new file mode 100644 index 0000000..1ea4bf1 --- /dev/null +++ b/source/hpr/containers.hpp @@ -0,0 +1,3 @@ +#pragma once + +#include "containers/array.hpp" diff --git a/source/hpr/containers/CMakeLists.txt b/source/hpr/containers/CMakeLists.txt new file mode 100644 index 0000000..55e06c6 --- /dev/null +++ b/source/hpr/containers/CMakeLists.txt @@ -0,0 +1,28 @@ + +set(COMPONENT_NAME containers) + +add_component_library(${COMPONENT_NAME} INTERFACE "" +) + +#target_sources(${COMPONENT_NAME} PUBLIC +# array/dynamic_array.hpp +# array.hpp +# array/iterator.hpp +# array/static_array.hpp +#) + +install_component_headers(${COMPONENT_NAME}) +install_component_source(${COMPONENT_NAME}) + +if (WITH_GTEST) + add_executable(${COMPONENT_NAME}-test + tests/${COMPONENT_NAME}-test.cpp + ) + + target_link_libraries(${COMPONENT_NAME}-test + ${CMAKE_PROJECT_NAME}::${COMPONENT_NAME} + GTest::gtest_main + ) + + gtest_add_tests(TARGET ${COMPONENT_NAME}-test) +endif() diff --git a/source/hpr/containers/array.hpp b/source/hpr/containers/array.hpp new file mode 100644 index 0000000..39bc1c7 --- /dev/null +++ b/source/hpr/containers/array.hpp @@ -0,0 +1,4 @@ +#pragma once + +#include "array/dynamic_array.hpp" +#include "array/static_array.hpp" \ No newline at end of file diff --git a/source/hyporo/hyplib/array/dynamic_array.hpp b/source/hpr/containers/array/dynamic_array.hpp similarity index 99% rename from source/hyporo/hyplib/array/dynamic_array.hpp rename to source/hpr/containers/array/dynamic_array.hpp index 8a8eef0..5359c11 100644 --- a/source/hyporo/hyplib/array/dynamic_array.hpp +++ b/source/hpr/containers/array/dynamic_array.hpp @@ -358,6 +358,7 @@ public: p_size = newSize; p_end = p_start + p_size; } + /*virtual void remove(const value_type& value) { @@ -418,4 +419,9 @@ public: } }; +// Aliases + +template +using darray = DynamicArray; + } \ No newline at end of file diff --git a/source/hyporo/hyplib/array/iterator.hpp b/source/hpr/containers/array/iterator.hpp similarity index 100% rename from source/hyporo/hyplib/array/iterator.hpp rename to source/hpr/containers/array/iterator.hpp diff --git a/source/hyporo/hyplib/array/static_array.hpp b/source/hpr/containers/array/static_array.hpp similarity index 98% rename from source/hyporo/hyplib/array/static_array.hpp rename to source/hpr/containers/array/static_array.hpp index 65bc1fa..f7fe2ff 100644 --- a/source/hyporo/hyplib/array/static_array.hpp +++ b/source/hpr/containers/array/static_array.hpp @@ -248,4 +248,9 @@ public: } }; +// Aliases + +template +using sarray = StaticArray; + } diff --git a/source/hpr/containers/tests/containers-test.cpp b/source/hpr/containers/tests/containers-test.cpp new file mode 100644 index 0000000..837f23c --- /dev/null +++ b/source/hpr/containers/tests/containers-test.cpp @@ -0,0 +1,37 @@ +#include + +#include "../array.hpp" + + +TEST(containers, StaticArray) +{ +hpr::StaticArray arr {1, 3, 2}; +hpr::StaticArray sarr {arr, 5}; +hpr::StaticArray sarr2 {1, 3, 2, 5}; +EXPECT_EQ(sarr, sarr2); +} + +TEST(containers, DynamicArray) +{ +hpr::DynamicArray arr {1, 3, 2}; +hpr::DynamicArray arr2 {1, 3, 2}; +EXPECT_EQ(arr, arr2); +arr.remove(1); +EXPECT_EQ(arr, hpr::darray({1, 2})); +auto iter = arr2.begin(); +++iter; +arr2.remove(iter); +EXPECT_EQ(arr2, hpr::darray({1, 2})); + +hpr::DynamicArray arr3 {1, 3, 0, 2, 9, 0, 5}; +arr3.remove([](float num) { return num == 0; }); +EXPECT_EQ(arr3, hpr::darray({1, 3, 2, 9, 5})); +EXPECT_EQ(arr3.size(), 5); + +hpr::DynamicArray arr4; +arr4.push(new float(5)); +arr4.push(new float(7)); +arr4.push(new float(9)); +EXPECT_EQ(*arr4[0], 5.f); +EXPECT_EQ(*arr4[2], 9.f); +} diff --git a/source/hpr/csg.hpp b/source/hpr/csg.hpp new file mode 100644 index 0000000..089f115 --- /dev/null +++ b/source/hpr/csg.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "csg/shape.hpp" +#include "csg/vertex.hpp" +#include "csg/edge.hpp" +#include "csg/wire.hpp" +#include "csg/face.hpp" +#include "csg/shell.hpp" +#include "csg/solid.hpp" +#include "csg/compound.hpp" +#include "csg/geometry.hpp" +#include "csg/surface.hpp" \ No newline at end of file diff --git a/source/hpr/csg/CMakeLists.txt b/source/hpr/csg/CMakeLists.txt new file mode 100644 index 0000000..90769b3 --- /dev/null +++ b/source/hpr/csg/CMakeLists.txt @@ -0,0 +1,35 @@ + +set(COMPONENT_NAME csg) + +add_component_library(${COMPONENT_NAME} STATIC + shape.cpp +) + +#target_sources(${COMPONENT_NAME} PUBLIC + +#) + +target_link_libraries(${COMPONENT_NAME} + ${OCCT_LIBRARIES} +) + +target_include_directories(${COMPONENT_NAME} + PUBLIC + ${OCCT_INCLUDE_DIRS} +) + +install_component_headers(${COMPONENT_NAME}) +install_component_source(${COMPONENT_NAME}) + +if (0) + add_executable(${COMPONENT_NAME}-test + tests/${COMPONENT_NAME}-test.cpp + ) + + target_link_libraries(${COMPONENT_NAME}-test + ${PROJECT_NAME}::${COMPONENT_NAME} + GTest::gtest_main + ) + + gtest_add_tests(TARGET ${COMPONENT_NAME}-test) +endif() diff --git a/source/hyporo/csg/compound.hpp b/source/hpr/csg/compound.hpp similarity index 100% rename from source/hyporo/csg/compound.hpp rename to source/hpr/csg/compound.hpp diff --git a/source/hyporo/csg/edge.hpp b/source/hpr/csg/edge.hpp similarity index 100% rename from source/hyporo/csg/edge.hpp rename to source/hpr/csg/edge.hpp diff --git a/source/hyporo/csg/face.hpp b/source/hpr/csg/face.hpp similarity index 100% rename from source/hyporo/csg/face.hpp rename to source/hpr/csg/face.hpp diff --git a/source/hyporo/csg/geometry.hpp b/source/hpr/csg/geometry.hpp similarity index 100% rename from source/hyporo/csg/geometry.hpp rename to source/hpr/csg/geometry.hpp diff --git a/source/hyporo/csg/shape.cpp b/source/hpr/csg/shape.cpp similarity index 100% rename from source/hyporo/csg/shape.cpp rename to source/hpr/csg/shape.cpp diff --git a/source/hyporo/csg/shape.hpp b/source/hpr/csg/shape.hpp similarity index 99% rename from source/hyporo/csg/shape.hpp rename to source/hpr/csg/shape.hpp index 30ed396..6492b12 100644 --- a/source/hyporo/csg/shape.hpp +++ b/source/hpr/csg/shape.hpp @@ -1,7 +1,8 @@ #pragma once -#include "../hyplib/scalar/scalar.hpp" -#include "../hyplib/vector/vector.hpp" +#include "../containers/array.hpp" +#include "../math/scalar.hpp" +#include "../math/vector.hpp" #include #include diff --git a/source/hyporo/csg/shell.hpp b/source/hpr/csg/shell.hpp similarity index 100% rename from source/hyporo/csg/shell.hpp rename to source/hpr/csg/shell.hpp diff --git a/source/hyporo/csg/solid.hpp b/source/hpr/csg/solid.hpp similarity index 100% rename from source/hyporo/csg/solid.hpp rename to source/hpr/csg/solid.hpp diff --git a/source/hyporo/csg/surface.hpp b/source/hpr/csg/surface.hpp similarity index 100% rename from source/hyporo/csg/surface.hpp rename to source/hpr/csg/surface.hpp diff --git a/source/hyporo/csg/tests/csg-test.cpp b/source/hpr/csg/tests/csg-test.cpp similarity index 96% rename from source/hyporo/csg/tests/csg-test.cpp rename to source/hpr/csg/tests/csg-test.cpp index 289e12c..c45a728 100644 --- a/source/hyporo/csg/tests/csg-test.cpp +++ b/source/hpr/csg/tests/csg-test.cpp @@ -1,5 +1,5 @@ #include -#include "../csg.hpp" +#include "../../csg.hpp" TEST(csgTest, Shape) { diff --git a/source/hyporo/csg/vertex.hpp b/source/hpr/csg/vertex.hpp similarity index 100% rename from source/hyporo/csg/vertex.hpp rename to source/hpr/csg/vertex.hpp diff --git a/source/hyporo/csg/wire.hpp b/source/hpr/csg/wire.hpp similarity index 100% rename from source/hyporo/csg/wire.hpp rename to source/hpr/csg/wire.hpp diff --git a/source/hpr/gpu.hpp b/source/hpr/gpu.hpp new file mode 100644 index 0000000..877f184 --- /dev/null +++ b/source/hpr/gpu.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "gpu/context.hpp" +#include "gpu/shader.hpp" +#include "gpu/shader_program.hpp" +#include "gpu/buffer.hpp" +#include "gpu/device.hpp" +#include "gpu/texture.hpp" + +#include "gpu/opengl/context.hpp" +#include "gpu/opengl/shader.hpp" +#include "gpu/opengl/shader_program.hpp" +#include "gpu/opengl/buffer.hpp" +#include "gpu/opengl/device.hpp" +#include "gpu/opengl/texture.hpp" diff --git a/source/hpr/gpu/CMakeLists.txt b/source/hpr/gpu/CMakeLists.txt new file mode 100644 index 0000000..db472b5 --- /dev/null +++ b/source/hpr/gpu/CMakeLists.txt @@ -0,0 +1,42 @@ + +set(COMPONENT_NAME gpu) + +add_component_library(${COMPONENT_NAME} STATIC + buffer.cpp + context.cpp + device.cpp + shader.cpp + shader_program.cpp + texture.cpp + opengl/buffer.cpp + opengl/context.cpp + opengl/device.cpp + opengl/shader.cpp + opengl/shader_program.cpp + opengl/texture.cpp +) + +#target_sources(${COMPONENT_NAME} PUBLIC + +#) + +target_link_libraries(${COMPONENT_NAME} + glad + stb +) + +install_component_headers(${COMPONENT_NAME}) +install_component_source(${COMPONENT_NAME}) + +if (0) + add_executable(${COMPONENT_NAME}-test + tests/${COMPONENT_NAME}-test.cpp + ) + + target_link_libraries(${COMPONENT_NAME}-test + ${PROJECT_NAME}::${COMPONENT_NAME} + GTest::gtest_main + ) + + gtest_add_tests(TARGET ${COMPONENT_NAME}-test) +endif() \ No newline at end of file diff --git a/source/hyporo/gpu/buffer.cpp b/source/hpr/gpu/buffer.cpp similarity index 92% rename from source/hyporo/gpu/buffer.cpp rename to source/hpr/gpu/buffer.cpp index 933ff08..7d2183c 100644 --- a/source/hyporo/gpu/buffer.cpp +++ b/source/hpr/gpu/buffer.cpp @@ -1,36 +1,36 @@ - -#include "buffer.hpp" - - -namespace hpr::gpu -{ - -Buffer::Buffer() : - Context {DeviceAPI::Unknown}, - p_type {BufferType::Undefined}, - p_size {0}, - p_stride {0} -{} - -Buffer::Buffer(DeviceAPI api) : - Context {api}, - p_type {BufferType::Undefined}, - p_size {0}, - p_stride {0} -{} - -Buffer::~Buffer() = default; - -// Member functions - -int Buffer::size() const -{ - return p_size; -} - -Buffer::BufferType Buffer::type() const -{ - return p_type; -} - + +#include "buffer.hpp" + + +namespace hpr::gpu +{ + +Buffer::Buffer() : + Context {DeviceAPI::Unknown}, + p_type {BufferType::Undefined}, + p_size {0}, + p_stride {0} +{} + +Buffer::Buffer(DeviceAPI api) : + Context {api}, + p_type {BufferType::Undefined}, + p_size {0}, + p_stride {0} +{} + +Buffer::~Buffer() = default; + +// Member functions + +int Buffer::size() const +{ + return p_size; +} + +Buffer::BufferType Buffer::type() const +{ + return p_type; +} + } \ No newline at end of file diff --git a/source/hyporo/gpu/buffer.hpp b/source/hpr/gpu/buffer.hpp similarity index 92% rename from source/hyporo/gpu/buffer.hpp rename to source/hpr/gpu/buffer.hpp index fad4063..765ba4e 100644 --- a/source/hyporo/gpu/buffer.hpp +++ b/source/hpr/gpu/buffer.hpp @@ -1,52 +1,52 @@ -#pragma once - -#include "context.hpp" - -#include - - -namespace hpr::gpu -{ - -class Buffer : public Context -{ - friend class Device; - -public: - - enum class BufferType - { - Undefined, - Vertex, - Index, - Uniform, - BufferTypeCount - }; - -protected: - - BufferType p_type; - int p_size; - int p_stride; - -public: - - // Constructors - - Buffer(); - - explicit - Buffer(DeviceAPI api); - - ~Buffer() override; - - // Member functions - - [[nodiscard]] - int size() const; - - [[nodiscard]] - BufferType type() const; -}; - +#pragma once + +#include "context.hpp" + +#include + + +namespace hpr::gpu +{ + +class Buffer : public Context +{ + friend class Device; + +public: + + enum class BufferType + { + Undefined, + Vertex, + Index, + Uniform, + BufferTypeCount + }; + +protected: + + BufferType p_type; + int p_size; + int p_stride; + +public: + + // Constructors + + Buffer(); + + explicit + Buffer(DeviceAPI api); + + ~Buffer() override; + + // Member functions + + [[nodiscard]] + int size() const; + + [[nodiscard]] + BufferType type() const; +}; + } // end namespace hpr::gpu \ No newline at end of file diff --git a/source/hyporo/gpu/context.cpp b/source/hpr/gpu/context.cpp similarity index 100% rename from source/hyporo/gpu/context.cpp rename to source/hpr/gpu/context.cpp diff --git a/source/hyporo/gpu/context.hpp b/source/hpr/gpu/context.hpp similarity index 100% rename from source/hyporo/gpu/context.hpp rename to source/hpr/gpu/context.hpp diff --git a/source/hyporo/gpu/device.cpp b/source/hpr/gpu/device.cpp similarity index 96% rename from source/hyporo/gpu/device.cpp rename to source/hpr/gpu/device.cpp index 684a1df..3b5a077 100644 --- a/source/hyporo/gpu/device.cpp +++ b/source/hpr/gpu/device.cpp @@ -1,244 +1,244 @@ - -#include "device.hpp" -#include "opengl/device.hpp" - - -namespace hpr::gpu -{ - -Device::Device() : - Context {DeviceAPI::Unknown}, - p_currentVertexBuffer {}, - p_currentIndexBuffer {}, - p_currentUniformBuffer {}, - p_currentShaderProgram {} -{} - -Device::Device(DeviceAPI api) : - Context {api}, - p_currentVertexBuffer {}, - p_currentIndexBuffer {}, - p_currentUniformBuffer {}, - p_currentShaderProgram {} -{} - -Device::~Device() -{ - for (auto& shader : p_shaders) - delete shader; - for (auto& buffer : p_buffers) - delete buffer; - for (auto& shaderProgram : p_shaderPrograms) - delete shaderProgram; - for (auto& texture : p_textures) - delete texture; -} - -// Global functions - -void Device::create(Device** device, DeviceAPI api) -{ - if (device == nullptr) - throw std::invalid_argument("Invalid parameter 'nullptr'"); - if (api == DeviceAPI::Unknown) - throw std::invalid_argument("Cannot create device for 'Unknown'"); - - *device = nullptr; - - if (api == DeviceAPI::OpenGL) - *device = new opengl::Device; - else - throw std::invalid_argument("Unsupported device"); -} - -// Render targets - -void Device::moveRenderTarget(RenderTarget* target, int x, int y) -{ - if (target == nullptr) - throw std::invalid_argument("Invalid parameter"); - target->p_posX = x; - target->p_posY = y; -} - -void Device::scaleRenderTarget(RenderTarget* target, int width, int height) -{ - if (target == nullptr) - throw std::invalid_argument("Invalid parameter"); - target->p_width = width; - target->p_height = height; -} - -void Device::destroyRenderTarget(RenderTarget*& target) -{ - if (!target) - throw std::runtime_error("Invalid parameter"); - - for (auto iter = p_renderTargets.begin(); iter != p_renderTargets.end(); ++iter) - if (*iter == target) - { - delete target; - target = nullptr; - p_renderTargets.remove(iter); - break; - } -} - -// Buffers - -void Device::useVertexBuffer(Buffer* buffer, int stride, int offset) -{ - if (buffer) - { - if (buffer->p_type == Buffer::BufferType::Vertex) - { - p_currentVertexBuffer = buffer; - p_currentVertexBuffer->p_stride = stride; - } - else - throw std::runtime_error("Incompatible buffer"); - } - else - p_currentVertexBuffer = nullptr; -} - -void Device::useIndexBuffer(Buffer* buffer, int offset) -{ - if (buffer) - { - if (buffer->p_type == Buffer::BufferType::Index) - { - p_currentIndexBuffer = buffer; - } - else - throw std::runtime_error("Incompatible buffer"); - } - else - p_currentIndexBuffer = nullptr; -} - -void Device::useUniformBuffer(Buffer* buffer, int slot) -{ - if (buffer) - { - if (buffer->p_type == Buffer::BufferType::Uniform) - p_currentUniformBuffer = buffer; - else - throw std::runtime_error("Incompatible buffer"); - } - else - p_currentUniformBuffer = nullptr; -} - -void Device::destroyBuffer(Buffer*& buffer) -{ - if (!buffer) - throw std::runtime_error("Invalid parameter"); - - for (auto iter = p_buffers.begin(); iter != p_buffers.end(); ++iter) - if (*iter == buffer) - { - delete buffer; - buffer = nullptr; - p_buffers.remove(iter); - break; - } -} - -Buffer* Device::activeBuffer(Buffer::BufferType type) -{ - switch (type) - { - case Buffer::BufferType::Vertex: - return p_currentVertexBuffer; - case Buffer::BufferType::Index: - return p_currentIndexBuffer; - case Buffer::BufferType::Uniform: - return p_currentUniformBuffer; - default: - return nullptr; - } -} - -// Shaders - -void Device::destroyShader(Shader* shader) -{ - if (shader == nullptr) - throw std::runtime_error("Invalid parameter"); - - for (auto iter = p_shaders.begin(); iter != p_shaders.end(); ++iter) - if (*iter == shader) - { - delete shader; - shader = nullptr; - p_shaders.remove(iter); - break; - } -} - -// Shader programs - -void Device::attachShader(ShaderProgram *program, Shader *shader) -{ - if (program == nullptr || shader == nullptr) - throw std::runtime_error("Invalid parameter"); - if (program->p_isLinked) - throw std::runtime_error("Shader program already linked"); - - program->p_slots[(int)shader->p_type] = shader; -} - -void Device::linkProgram(ShaderProgram *program) -{ - if (program == nullptr) - throw std::runtime_error("Invalid parameter"); - if (program->p_isLinked) - throw std::runtime_error("Shader program already linked"); - - program->p_isLinked = true; -} - -void Device::useShaderProgram(ShaderProgram *program) -{ - if (program != nullptr) - if (!program->p_isLinked) - throw std::runtime_error("Shader program is not linked"); - - p_currentShaderProgram = program; -} - -void Device::destroyShaderProgram(ShaderProgram *&program, bool withShaders) -{ - if (program == p_currentShaderProgram) - useShaderProgram(nullptr); - - if (withShaders) - for (size_t n = 0; n < (size_t)Shader::ShaderType::ShaderTypeCount; n++) - destroyShader(program->p_slots[n]); - - for (auto iter = p_shaderPrograms.begin(); iter != p_shaderPrograms.end(); ++iter) - if (*iter == program) - { - delete program; - program = nullptr; - p_shaderPrograms.remove(iter); - break; - } -} - -// Textures - -void Device::destroyTexture(Texture *&texture) -{ - for (auto iter = p_textures.begin(); iter != p_textures.end(); ++iter) - if (*iter == texture) - { - delete texture; - texture = nullptr; - p_textures.remove(iter); - break; - } -} - + +#include "device.hpp" +#include "opengl/device.hpp" + + +namespace hpr::gpu +{ + +Device::Device() : + Context {DeviceAPI::Unknown}, + p_currentVertexBuffer {}, + p_currentIndexBuffer {}, + p_currentUniformBuffer {}, + p_currentShaderProgram {} +{} + +Device::Device(DeviceAPI api) : + Context {api}, + p_currentVertexBuffer {}, + p_currentIndexBuffer {}, + p_currentUniformBuffer {}, + p_currentShaderProgram {} +{} + +Device::~Device() +{ + for (auto& shader : p_shaders) + delete shader; + for (auto& buffer : p_buffers) + delete buffer; + for (auto& shaderProgram : p_shaderPrograms) + delete shaderProgram; + for (auto& texture : p_textures) + delete texture; +} + +// Global functions + +void Device::create(Device** device, DeviceAPI api) +{ + if (device == nullptr) + throw std::invalid_argument("Invalid parameter 'nullptr'"); + if (api == DeviceAPI::Unknown) + throw std::invalid_argument("Cannot create device for 'Unknown'"); + + *device = nullptr; + + if (api == DeviceAPI::OpenGL) + *device = new opengl::Device; + else + throw std::invalid_argument("Unsupported device"); +} + +// Render targets + +void Device::moveRenderTarget(RenderTarget* target, int x, int y) +{ + if (target == nullptr) + throw std::invalid_argument("Invalid parameter"); + target->p_posX = x; + target->p_posY = y; +} + +void Device::scaleRenderTarget(RenderTarget* target, int width, int height) +{ + if (target == nullptr) + throw std::invalid_argument("Invalid parameter"); + target->p_width = width; + target->p_height = height; +} + +void Device::destroyRenderTarget(RenderTarget*& target) +{ + if (!target) + throw std::runtime_error("Invalid parameter"); + + for (auto iter = p_renderTargets.begin(); iter != p_renderTargets.end(); ++iter) + if (*iter == target) + { + delete target; + target = nullptr; + p_renderTargets.remove(iter); + break; + } +} + +// Buffers + +void Device::useVertexBuffer(Buffer* buffer, int stride, int offset) +{ + if (buffer) + { + if (buffer->p_type == Buffer::BufferType::Vertex) + { + p_currentVertexBuffer = buffer; + p_currentVertexBuffer->p_stride = stride; + } + else + throw std::runtime_error("Incompatible buffer"); + } + else + p_currentVertexBuffer = nullptr; +} + +void Device::useIndexBuffer(Buffer* buffer, int offset) +{ + if (buffer) + { + if (buffer->p_type == Buffer::BufferType::Index) + { + p_currentIndexBuffer = buffer; + } + else + throw std::runtime_error("Incompatible buffer"); + } + else + p_currentIndexBuffer = nullptr; +} + +void Device::useUniformBuffer(Buffer* buffer, int slot) +{ + if (buffer) + { + if (buffer->p_type == Buffer::BufferType::Uniform) + p_currentUniformBuffer = buffer; + else + throw std::runtime_error("Incompatible buffer"); + } + else + p_currentUniformBuffer = nullptr; +} + +void Device::destroyBuffer(Buffer*& buffer) +{ + if (!buffer) + throw std::runtime_error("Invalid parameter"); + + for (auto iter = p_buffers.begin(); iter != p_buffers.end(); ++iter) + if (*iter == buffer) + { + delete buffer; + buffer = nullptr; + p_buffers.remove(iter); + break; + } +} + +Buffer* Device::activeBuffer(Buffer::BufferType type) +{ + switch (type) + { + case Buffer::BufferType::Vertex: + return p_currentVertexBuffer; + case Buffer::BufferType::Index: + return p_currentIndexBuffer; + case Buffer::BufferType::Uniform: + return p_currentUniformBuffer; + default: + return nullptr; + } +} + +// Shaders + +void Device::destroyShader(Shader* shader) +{ + if (shader == nullptr) + throw std::runtime_error("Invalid parameter"); + + for (auto iter = p_shaders.begin(); iter != p_shaders.end(); ++iter) + if (*iter == shader) + { + delete shader; + shader = nullptr; + p_shaders.remove(iter); + break; + } +} + +// Shader programs + +void Device::attachShader(ShaderProgram *program, Shader *shader) +{ + if (program == nullptr || shader == nullptr) + throw std::runtime_error("Invalid parameter"); + if (program->p_isLinked) + throw std::runtime_error("Shader program already linked"); + + program->p_slots[(int)shader->p_type] = shader; +} + +void Device::linkProgram(ShaderProgram *program) +{ + if (program == nullptr) + throw std::runtime_error("Invalid parameter"); + if (program->p_isLinked) + throw std::runtime_error("Shader program already linked"); + + program->p_isLinked = true; +} + +void Device::useShaderProgram(ShaderProgram *program) +{ + if (program != nullptr) + if (!program->p_isLinked) + throw std::runtime_error("Shader program is not linked"); + + p_currentShaderProgram = program; +} + +void Device::destroyShaderProgram(ShaderProgram *&program, bool withShaders) +{ + if (program == p_currentShaderProgram) + useShaderProgram(nullptr); + + if (withShaders) + for (size_t n = 0; n < (size_t)Shader::ShaderType::ShaderTypeCount; n++) + destroyShader(program->p_slots[n]); + + for (auto iter = p_shaderPrograms.begin(); iter != p_shaderPrograms.end(); ++iter) + if (*iter == program) + { + delete program; + program = nullptr; + p_shaderPrograms.remove(iter); + break; + } +} + +// Textures + +void Device::destroyTexture(Texture *&texture) +{ + for (auto iter = p_textures.begin(); iter != p_textures.end(); ++iter) + if (*iter == texture) + { + delete texture; + texture = nullptr; + p_textures.remove(iter); + break; + } +} + } \ No newline at end of file diff --git a/source/hyporo/gpu/device.hpp b/source/hpr/gpu/device.hpp similarity index 94% rename from source/hyporo/gpu/device.hpp rename to source/hpr/gpu/device.hpp index 8d3ed59..6c0fb1a 100644 --- a/source/hyporo/gpu/device.hpp +++ b/source/hpr/gpu/device.hpp @@ -1,148 +1,148 @@ -#pragma once - -#include "context.hpp" -#include "buffer.hpp" -#include "shader.hpp" -#include "shader_program.hpp" -#include "texture.hpp" -#include "render_target.hpp" - -#include "../hyplib/array/array.hpp" - -#include "../window_system/window_system.hpp" - - -namespace hpr::gpu -{ - -class Device : public Context -{ - -public: - - enum class CullMode - { - Front, - Back, - FrontAndBack, - None - }; - -protected: - - darray p_buffers; - darray p_shaders; - darray p_shaderPrograms; - darray p_textures; - - Buffer* p_currentVertexBuffer; - Buffer* p_currentIndexBuffer; - Buffer* p_currentUniformBuffer; - ShaderProgram* p_currentShaderProgram; - - darray p_renderTargets; -protected: - - // Constructors - - Device(); - - explicit - Device(DeviceAPI api); - - ~Device() override; - -public: - - // Global functions - - static - void create(Device** device, DeviceAPI api); - - // Member functions - - // Setup - - virtual - bool initialize() = 0; - virtual - bool destroy() = 0; - - // State - - virtual - void faceCulling(bool enableFaceCulling, CullMode faceCullingMode) = 0; - - // Render targets - - virtual - void createScreenRenderTarget(RenderTarget** target, Window* window) = 0; - virtual - void createFramebufferRenderTarget(RenderTarget** target, int width, int height) = 0; - virtual - void createSubRenderTarget(RenderTarget** target, RenderTarget* parent, int x, int y, int width, int height) = 0; - virtual - void moveRenderTarget(RenderTarget* target, int x, int y); - virtual - void scaleRenderTarget(RenderTarget* target, int width, int height); - virtual - void destroyRenderTarget(RenderTarget*& target); - - // Buffers - - virtual - void createVertexBuffer(Buffer **buffer, int size, char* data) = 0; - virtual - void createIndexBuffer(Buffer **buffer, int size, char* data) = 0; - virtual - void createUniformBuffer(Buffer **buffer, int size, char* data) = 0; - virtual - void useVertexBuffer(Buffer* buffer, int stride, int offset); - virtual - void useIndexBuffer(Buffer* buffer, int offset); - virtual - void useUniformBuffer(Buffer* buffer, int slot); - virtual - void editBuffer(Buffer* buffer, char* data, int size, int offset) = 0; - virtual - void editBuffer(Buffer* buffer, char* data) = 0; - virtual - void destroyBuffer(Buffer*& buffer); - Buffer* activeBuffer(Buffer::BufferType type); - - - // Shaders - - virtual - void createVertexShader(Shader** shader, const std::string& filename, const std::string& label) = 0; - virtual - void createFragmentShader(Shader** shader, const std::string& filename, const std::string& label) = 0; - virtual - void createGeometryShader(Shader** shader, const std::string& filename, const std::string& label) = 0; - virtual - void destroyShader(Shader* shader); - - // Shader programs - - virtual - void createShaderProgram(ShaderProgram** program) = 0; - virtual - void attachShader(ShaderProgram* program, Shader* shader); - virtual - void linkProgram(ShaderProgram* program); - virtual - void useShaderProgram(ShaderProgram* program); - virtual - void destroyShaderProgram(ShaderProgram*& program, bool withShaders); - - // Textures - - virtual - void createTexture(Texture** texture, const std::string& filename) = 0; - virtual - void useTexture(Texture* texture, int slot) = 0; - virtual - void destroyTexture(Texture*& texture); -}; - -} +#pragma once + +#include "context.hpp" +#include "buffer.hpp" +#include "shader.hpp" +#include "shader_program.hpp" +#include "texture.hpp" +#include "render_target.hpp" + +#include "../containers/array.hpp" + +#include "../window_system/window_system.hpp" + + +namespace hpr::gpu +{ + +class Device : public Context +{ + +public: + + enum class CullMode + { + Front, + Back, + FrontAndBack, + None + }; + +protected: + + darray p_buffers; + darray p_shaders; + darray p_shaderPrograms; + darray p_textures; + + Buffer* p_currentVertexBuffer; + Buffer* p_currentIndexBuffer; + Buffer* p_currentUniformBuffer; + ShaderProgram* p_currentShaderProgram; + + darray p_renderTargets; +protected: + + // Constructors + + Device(); + + explicit + Device(DeviceAPI api); + + ~Device() override; + +public: + + // Global functions + + static + void create(Device** device, DeviceAPI api); + + // Member functions + + // Setup + + virtual + bool initialize() = 0; + virtual + bool destroy() = 0; + + // State + + virtual + void faceCulling(bool enableFaceCulling, CullMode faceCullingMode) = 0; + + // Render targets + + virtual + void createScreenRenderTarget(RenderTarget** target, Window* window) = 0; + virtual + void createFramebufferRenderTarget(RenderTarget** target, int width, int height) = 0; + virtual + void createSubRenderTarget(RenderTarget** target, RenderTarget* parent, int x, int y, int width, int height) = 0; + virtual + void moveRenderTarget(RenderTarget* target, int x, int y); + virtual + void scaleRenderTarget(RenderTarget* target, int width, int height); + virtual + void destroyRenderTarget(RenderTarget*& target); + + // Buffers + + virtual + void createVertexBuffer(Buffer **buffer, int size, char* data) = 0; + virtual + void createIndexBuffer(Buffer **buffer, int size, char* data) = 0; + virtual + void createUniformBuffer(Buffer **buffer, int size, char* data) = 0; + virtual + void useVertexBuffer(Buffer* buffer, int stride, int offset); + virtual + void useIndexBuffer(Buffer* buffer, int offset); + virtual + void useUniformBuffer(Buffer* buffer, int slot); + virtual + void editBuffer(Buffer* buffer, char* data, int size, int offset) = 0; + virtual + void editBuffer(Buffer* buffer, char* data) = 0; + virtual + void destroyBuffer(Buffer*& buffer); + Buffer* activeBuffer(Buffer::BufferType type); + + + // Shaders + + virtual + void createVertexShader(Shader** shader, const std::string& filename, const std::string& label) = 0; + virtual + void createFragmentShader(Shader** shader, const std::string& filename, const std::string& label) = 0; + virtual + void createGeometryShader(Shader** shader, const std::string& filename, const std::string& label) = 0; + virtual + void destroyShader(Shader* shader); + + // Shader programs + + virtual + void createShaderProgram(ShaderProgram** program) = 0; + virtual + void attachShader(ShaderProgram* program, Shader* shader); + virtual + void linkProgram(ShaderProgram* program); + virtual + void useShaderProgram(ShaderProgram* program); + virtual + void destroyShaderProgram(ShaderProgram*& program, bool withShaders); + + // Textures + + virtual + void createTexture(Texture** texture, const std::string& filename) = 0; + virtual + void useTexture(Texture* texture, int slot) = 0; + virtual + void destroyTexture(Texture*& texture); +}; + +} diff --git a/source/hyporo/gpu/opengl/buffer.cpp b/source/hpr/gpu/opengl/buffer.cpp similarity index 94% rename from source/hyporo/gpu/opengl/buffer.cpp rename to source/hpr/gpu/opengl/buffer.cpp index 3167a30..72b0418 100644 --- a/source/hyporo/gpu/opengl/buffer.cpp +++ b/source/hpr/gpu/opengl/buffer.cpp @@ -1,31 +1,31 @@ - -#include "buffer.hpp" - -#include - - -namespace hpr::gpu::opengl -{ - -Buffer::Buffer() : - gpu::Buffer(DeviceAPI::OpenGL), - p_bufferIndex {0}, - p_vertexArrayIndex {0} -{} - -Buffer::~Buffer() = default; - -int Buffer::target() const -{ - switch (p_type) - { - case BufferType::Vertex: - return GL_ARRAY_BUFFER; - case BufferType::Index: - return GL_ELEMENT_ARRAY_BUFFER; - default: - return GL_NONE; - } -} - + +#include "buffer.hpp" + +#include + + +namespace hpr::gpu::opengl +{ + +Buffer::Buffer() : + gpu::Buffer(DeviceAPI::OpenGL), + p_bufferIndex {0}, + p_vertexArrayIndex {0} +{} + +Buffer::~Buffer() = default; + +int Buffer::target() const +{ + switch (p_type) + { + case BufferType::Vertex: + return GL_ARRAY_BUFFER; + case BufferType::Index: + return GL_ELEMENT_ARRAY_BUFFER; + default: + return GL_NONE; + } +} + } \ No newline at end of file diff --git a/source/hyporo/gpu/opengl/buffer.hpp b/source/hpr/gpu/opengl/buffer.hpp similarity index 91% rename from source/hyporo/gpu/opengl/buffer.hpp rename to source/hpr/gpu/opengl/buffer.hpp index 42d0393..ca7969e 100644 --- a/source/hyporo/gpu/opengl/buffer.hpp +++ b/source/hpr/gpu/opengl/buffer.hpp @@ -1,30 +1,30 @@ -#pragma once - -#include "../buffer.hpp" - - -namespace hpr::gpu::opengl -{ - -class Buffer : public gpu::Buffer -{ - friend class Device; - -protected: - unsigned int p_bufferIndex; - unsigned int p_vertexArrayIndex; - -public: - - // Constructors - - Buffer(); - - virtual ~Buffer(); - - // Member functions - - int target() const; -}; - -} +#pragma once + +#include "../buffer.hpp" + + +namespace hpr::gpu::opengl +{ + +class Buffer : public gpu::Buffer +{ + friend class Device; + +protected: + unsigned int p_bufferIndex; + unsigned int p_vertexArrayIndex; + +public: + + // Constructors + + Buffer(); + + virtual ~Buffer(); + + // Member functions + + int target() const; +}; + +} diff --git a/source/hyporo/gpu/opengl/context.cpp b/source/hpr/gpu/opengl/context.cpp similarity index 93% rename from source/hyporo/gpu/opengl/context.cpp rename to source/hpr/gpu/opengl/context.cpp index e244f32..d24eea0 100644 --- a/source/hyporo/gpu/opengl/context.cpp +++ b/source/hpr/gpu/opengl/context.cpp @@ -1,5 +1,5 @@ -// -// Created by L-Nafaryus on 10/3/2022. -// - -#include "context.hpp" +// +// Created by L-Nafaryus on 10/3/2022. +// + +#include "context.hpp" diff --git a/source/hyporo/gpu/opengl/context.hpp b/source/hpr/gpu/opengl/context.hpp similarity index 94% rename from source/hyporo/gpu/opengl/context.hpp rename to source/hpr/gpu/opengl/context.hpp index 9ceef65..962de0b 100644 --- a/source/hyporo/gpu/opengl/context.hpp +++ b/source/hpr/gpu/opengl/context.hpp @@ -1,8 +1,8 @@ -// -// Created by L-Nafaryus on 10/3/2022. -// - -#ifndef HYPORO_CONTEXT_HPP -#define HYPORO_CONTEXT_HPP - -#endif //HYPORO_CONTEXT_HPP +// +// Created by L-Nafaryus on 10/3/2022. +// + +#ifndef HYPORO_CONTEXT_HPP +#define HYPORO_CONTEXT_HPP + +#endif //HYPORO_CONTEXT_HPP diff --git a/source/hyporo/gpu/opengl/device.cpp b/source/hpr/gpu/opengl/device.cpp similarity index 95% rename from source/hyporo/gpu/opengl/device.cpp rename to source/hpr/gpu/opengl/device.cpp index 34f6829..d0b7121 100644 --- a/source/hyporo/gpu/opengl/device.cpp +++ b/source/hpr/gpu/opengl/device.cpp @@ -1,597 +1,597 @@ - -#include "device.hpp" -#include "buffer.hpp" -#include "shader.hpp" -#include "shader_program.hpp" -#include "texture.hpp" -#include "render_target.hpp" - -#include "io/io.hpp" - -#include -#define STB_IMAGE_IMPLEMENTATION -#include -#include - -#include - - -namespace hpr::gpu::opengl -{ - -Device::Device() : - gpu::Device {DeviceAPI::OpenGL}, - p_isInitialized {false} -{} - -Device::~Device() = default; - -// Setup - -bool Device::initialize() -{ - return p_isInitialized = true; -} - -bool Device::destroy() -{ - return p_isInitialized = false; -} - -bool Device::loadLoader() -{ - return !gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); -} - -// State - -void Device::faceCulling(bool enableFaceCulling, CullMode faceCullingMode) -{ - if (enableFaceCulling) - glEnable(GL_CULL_FACE); - else - glDisable(GL_CULL_FACE); - - switch (faceCullingMode) - { - case CullMode::Front: - glCullFace(GL_FRONT); - break; - case CullMode::Back: - glCullFace(GL_BACK); - break; - case CullMode::FrontAndBack: - glCullFace(GL_FRONT_AND_BACK); - break; - case CullMode::None: - glCullFace(GL_NONE); - break; - } -} - -// Render targets - -void Device::createScreenRenderTarget(gpu::RenderTarget** target, Window* window) -{ - if (target == nullptr) - throw std::invalid_argument("Invalid parameter"); - *target = nullptr; - p_renderTargets.push(new opengl::RenderTarget()); - auto* newTarget = dynamic_cast(p_renderTargets.back()); - - newTarget->p_type = RenderTarget::Type::Screen; - newTarget->p_posX = 0; - newTarget->p_posY = 0; - newTarget->p_width = window->width(); - newTarget->p_height = window->height(); - - *target = newTarget; -} - -void Device::createFramebufferRenderTarget(gpu::RenderTarget** target, int width, int height) -{ - if (target == nullptr) - throw std::invalid_argument("Invalid parameter"); - unsigned int texture; - glGenTextures(GL_TEXTURE_2D, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); - - unsigned int framebuffer; - glGenFramebuffers(1, &framebuffer); - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer); - - p_renderTargets.push(new opengl::RenderTarget()); - auto* newTarget = dynamic_cast(p_renderTargets.back()); - - newTarget->p_type = RenderTarget::Type::Framebuffer; - newTarget->p_posX = 0; - newTarget->p_posY = 0; - newTarget->p_width = width; - newTarget->p_height = height; - newTarget->p_frameBufferIndex = framebuffer; - newTarget->p_textureIndex = texture; - - *target = newTarget; -} - -void Device::createSubRenderTarget(gpu::RenderTarget** target, RenderTarget* parent, int x, int y, int width, int height) -{ - -} - -void Device::moveRenderTarget(gpu::RenderTarget* target, int x, int y) -{ - -} - -void Device::scaleRenderTarget(gpu::RenderTarget* target, int width, int height) -{ - -} - -void Device::destroyRenderTarget(gpu::RenderTarget*& target) -{ - -} - -// Buffers - - -void Device::createVertexBuffer(gpu::Buffer **buffer, int size, char *data) -{ - if (buffer == nullptr) - throw std::invalid_argument("Invalid parameter"); - - *buffer = nullptr; - p_buffers.push(new opengl::Buffer()); - auto* newBuffer = dynamic_cast(p_buffers.back()); - - newBuffer->p_type = Buffer::BufferType::Vertex; - newBuffer->p_size = size; - - glGenVertexArrays(1, &newBuffer->p_vertexArrayIndex); - glBindVertexArray(newBuffer->p_vertexArrayIndex); - - glGenBuffers(1, &newBuffer->p_bufferIndex); - glBindBuffer(GL_ARRAY_BUFFER, newBuffer->p_bufferIndex); - glBufferData(GL_ARRAY_BUFFER, size, (void*)data, GL_STATIC_DRAW); - - *buffer = static_cast(newBuffer); -} - -void Device::createIndexBuffer(gpu::Buffer **buffer, int size, char *data) -{ - if (buffer == nullptr) - throw std::invalid_argument("Invalid parameter"); - - *buffer = nullptr; - p_buffers.push(new opengl::Buffer()); - auto* newBuffer = dynamic_cast(p_buffers.back()); - - newBuffer->p_type = Buffer::BufferType::Index; - newBuffer->p_size = size; - - glGenVertexArrays(1, &newBuffer->p_vertexArrayIndex); - glBindVertexArray(newBuffer->p_vertexArrayIndex); - - glGenBuffers(1, &newBuffer->p_bufferIndex); - glBindBuffer(GL_UNIFORM_BUFFER, newBuffer->p_bufferIndex); - glBufferData(GL_UNIFORM_BUFFER, size, (void*)data, GL_STATIC_DRAW); - - *buffer = static_cast(newBuffer); -} - -void Device::createUniformBuffer(gpu::Buffer** buffer, int size, char* data) -{ - if (buffer == nullptr) - throw std::invalid_argument("Invalid parameter"); - - *buffer = nullptr; - p_buffers.push(new opengl::Buffer()); - auto* newBuffer = dynamic_cast(p_buffers.back()); - - newBuffer->p_type = Buffer::BufferType::Uniform; - newBuffer->p_size = size; - - glGenBuffers(1, &newBuffer->p_bufferIndex); - glBindBuffer(GL_UNIFORM_BUFFER, newBuffer->p_bufferIndex); - glBufferData(GL_UNIFORM_BUFFER, size, (void*)data, GL_DYNAMIC_DRAW); - - *buffer = static_cast(newBuffer); -} - -void Device::useVertexBuffer(gpu::Buffer *buffer, int stride, int offset) -{ - if (buffer == nullptr) - { - glBindVertexArray(0); - glBindBuffer(GL_ARRAY_BUFFER, 0); - } - else - { - auto* curBuffer = dynamic_cast(buffer); - - if (curBuffer->p_type == Buffer::BufferType::Vertex && p_currentVertexBuffer != buffer) - { - glBindVertexArray(curBuffer->p_vertexArrayIndex); - glBindBuffer(GL_ARRAY_BUFFER, curBuffer->p_bufferIndex); - - auto* curIndexBuffer = dynamic_cast(p_currentIndexBuffer); - - if (curIndexBuffer != nullptr) - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, curIndexBuffer->p_bufferIndex); - } - } - gpu::Device::useVertexBuffer(buffer, stride, offset); -} - -void Device::useIndexBuffer(gpu::Buffer *buffer, int offset) -{ - if (buffer == nullptr) - { - glBindVertexArray(0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - } - else - { - auto* curBuffer = dynamic_cast(buffer); - - if (curBuffer->p_type == Buffer::BufferType::Index && p_currentVertexBuffer != buffer) - { - glBindVertexArray(curBuffer->p_vertexArrayIndex); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, curBuffer->p_bufferIndex); - } - } - gpu::Device::useIndexBuffer(buffer, offset); -} - -void Device::useUniformBuffer(gpu::Buffer* buffer, int slot) -{ - if (buffer == nullptr) - { - glBindBuffer(GL_UNIFORM_BUFFER, 0); - } - else - { - auto* curBuffer = dynamic_cast(buffer); - glBindBufferRange(GL_UNIFORM_BUFFER, slot, curBuffer->p_bufferIndex, 0, buffer->size()); - } - gpu::Device::useUniformBuffer(buffer, slot); -} - -void Device::editBuffer(gpu::Buffer* buffer, char* data, int size, int offset) -{ - if (!checkCompability(buffer)) - throw std::runtime_error("Incompatible platform"); - if (buffer == nullptr || data == nullptr) - throw std::invalid_argument("Invalid argument"); - if (size + offset > buffer->size() || size < 0 || offset < 0) - throw std::out_of_range("Out of bounds"); - - auto* prevBuffer = dynamic_cast(activeBuffer(buffer->type())); - - auto* buffer_ = dynamic_cast(buffer); - glBindBuffer(buffer_->target(), buffer_->p_bufferIndex); - glBufferSubData(buffer_->target(), offset, size, data); - - if (prevBuffer != buffer_) - glBindBuffer(buffer_->target(), prevBuffer ? prevBuffer->p_bufferIndex : 0); - -} - -void Device::editBuffer(gpu::Buffer* buffer, char* data) -{ - editBuffer(buffer, data, buffer->size(), 0); -} - -void Device::destroyBuffer(gpu::Buffer *&buffer) -{ - if (buffer == nullptr) - throw std::invalid_argument("Invalid parameter"); - - auto* curBuffer = dynamic_cast(buffer); - glDeleteBuffers(1, &curBuffer->p_bufferIndex); - - if (curBuffer->p_type == Buffer::BufferType::Vertex) - glDeleteVertexArrays(1, &curBuffer->p_vertexArrayIndex); - - gpu::Device::destroyBuffer(buffer); -} - -// Shaders - -void Device::createVertexShader(gpu::Shader **shader, const std::string &filename, const std::string &label) -{ - if (shader == nullptr) - throw std::invalid_argument("Invalid parameter"); - - *shader = nullptr; - - unsigned int shaderIndex = glCreateShader(GL_VERTEX_SHADER); - if (shaderIndex == 0) - throw std::runtime_error("Could not create shader"); - - File file; - file.open(filename, File::Binary | File::Read); - std::string content = file.read().str(); - const char* shaderSource = content.c_str(); - - glShaderSource(shaderIndex, 1, &shaderSource, nullptr); - GLenum result = glGetError(); - glCompileShader(shaderIndex); - - int shaderStatus; - glGetShaderiv(shaderIndex, GL_COMPILE_STATUS, &shaderStatus); - if (!shaderStatus) - { - char error[2048 + 1]; - glGetShaderInfoLog(shaderIndex, 2048, nullptr, error); - - throw std::runtime_error(error); - } - - p_shaders.push(new opengl::Shader()); - auto* newShader = dynamic_cast(p_shaders.back()); - newShader->p_type = Shader::ShaderType::Vertex; - newShader->p_filename = filename; - newShader->p_shaderIndex = shaderIndex; - newShader->p_label = "VertexShader"; - - *shader = static_cast(newShader); -} - - -void Device::createFragmentShader(gpu::Shader **shader, const std::string &filename, const std::string &label) -{ - if (shader == nullptr) - throw std::invalid_argument("Invalid parameter"); - - *shader = nullptr; - - unsigned int shaderIndex = glCreateShader(GL_FRAGMENT_SHADER); - if (shaderIndex == 0) - throw std::runtime_error("Could not create shader"); - - File file; - file.open(filename, File::Binary | File::Read); - std::string content = file.read().str(); - const char* shaderSource = content.c_str(); - - glShaderSource(shaderIndex, 1, &shaderSource, nullptr); - GLenum result = glGetError(); - glCompileShader(shaderIndex); - - int shaderStatus; - glGetShaderiv(shaderIndex, GL_COMPILE_STATUS, &shaderStatus); - if (!shaderStatus) - { - char error[2048 + 1]; - glGetShaderInfoLog(shaderIndex, 2048, nullptr, error); - - throw std::runtime_error(error); - } - - p_shaders.push(new opengl::Shader()); - auto* newShader = dynamic_cast(p_shaders.back()); - newShader->p_type = Shader::ShaderType::Fragment; - newShader->p_filename = filename; - newShader->p_shaderIndex = shaderIndex; - newShader->p_label = "FragmentShader"; - - *shader = static_cast(newShader); -} - - -void Device::createGeometryShader(gpu::Shader **shader, const std::string &filename, const std::string &label) -{ - if (shader == nullptr) - throw std::invalid_argument("Invalid parameter"); - - *shader = nullptr; - - unsigned int shaderIndex = glCreateShader(GL_GEOMETRY_SHADER); - if (shaderIndex == 0) - throw std::runtime_error("Could not create shader"); - - File file; - file.open(filename, File::Binary | File::Read); - std::string content = file.read().str(); - const char* shaderSource = content.c_str(); - - glShaderSource(shaderIndex, 1, &shaderSource, nullptr); - GLenum result = glGetError(); - glCompileShader(shaderIndex); - - int shaderStatus; - glGetShaderiv(shaderIndex, GL_COMPILE_STATUS, &shaderStatus); - if (!shaderStatus) - { - char error[2048 + 1]; - glGetShaderInfoLog(shaderIndex, 2048, nullptr, error); - - throw std::runtime_error(error); - } - - p_shaders.push(new opengl::Shader()); - auto* newShader = dynamic_cast(p_shaders.back()); - newShader->p_type = Shader::ShaderType::Geometry; - newShader->p_filename = filename; - newShader->p_shaderIndex = shaderIndex; - newShader->p_label = "FragmentShader"; - - *shader = static_cast(newShader); -} - -void Device::destroyShader(gpu::Shader* shader) -{ - if (shader == nullptr) - throw std::invalid_argument("Invalid parameter"); - - auto* shader_ = dynamic_cast(shader); - glDeleteShader(shader_->p_shaderIndex); - - gpu::Device::destroyShader(shader); -} - -void Device::createShaderProgram(gpu::ShaderProgram** program) -{ - if (program == nullptr) - throw std::invalid_argument("Invalid parameter"); - - *program = nullptr; - - p_shaderPrograms.push(new opengl::ShaderProgram()); - auto* newProgram = dynamic_cast(p_shaderPrograms.back()); - newProgram->p_shaderProgramIndex = glCreateProgram(); - - *program = static_cast(newProgram); -} - -void Device::attachShader(gpu::ShaderProgram* program, gpu::Shader* shader) -{ - gpu::Device::attachShader(program, shader); - auto* program_ = dynamic_cast(program); - auto* shader_ = dynamic_cast(shader); - glAttachShader(program_->p_shaderProgramIndex, shader_->p_shaderIndex); -} - - -void Device::linkProgram(gpu::ShaderProgram* program) -{ - gpu::Device::linkProgram(program); - auto* program_ = dynamic_cast(program); - glLinkProgram(program_->p_shaderProgramIndex); - - GLint status; - glGetProgramiv(program_->p_shaderProgramIndex, GL_LINK_STATUS, &status); - - if (status == GL_FALSE) - throw std::runtime_error("Shader program link error"); -} - -void Device::useShaderProgram(gpu::ShaderProgram* program) -{ - gpu::ShaderProgram* currentProgram = p_currentShaderProgram; - gpu::Device::useShaderProgram(program); - - if (currentProgram == program) - return; - if (program == nullptr) - glUseProgram(0); - else - { - auto* program_ = dynamic_cast(program); - glUseProgram(program_->p_shaderProgramIndex); - } -} - -void Device::destroyShaderProgram(gpu::ShaderProgram*& program, bool withShaders) -{ - if (program == nullptr) - throw std::invalid_argument("Invalid parameter"); - - auto* program_ = dynamic_cast(program); - - for (int n = 0; n < static_cast(Shader::ShaderType::ShaderTypeCount); ++n) - { - auto* shader = dynamic_cast(program_->p_slots[n]); - if (shader != nullptr) - glDetachShader(program_->p_shaderProgramIndex, shader->p_shaderIndex); - } - glDeleteProgram(program_->p_shaderProgramIndex); - gpu::Device::destroyShaderProgram(program, withShaders); -} - -void Device::createTexture(gpu::Texture** texture, const std::string& filename) -{ - if (texture == nullptr) - throw std::invalid_argument("Invalid parameter"); - if (filename.empty()) - throw std::invalid_argument("Invalid parameter"); - - *texture = nullptr; - bool alphaChannel = true; - int internalFormat = GL_RGBA; - int imageFormat = GL_RGBA; - int width; - int height; - int channelsCount; - - stbi_set_flip_vertically_on_load(true); - unsigned char* source = stbi_load(filename.c_str(), &width, &height, &channelsCount, 0); - - if (!source) - throw std::runtime_error("Failed to load texture source"); - else - { - p_textures.push(new opengl::Texture()); - auto* texture_ = dynamic_cast(p_textures.back()); - texture_->p_filename = filename; - texture_->p_width = width; - texture_->p_height = height; - - glGenTextures(1, &texture_->p_textureIndex); - glBindTexture(GL_TEXTURE_2D, texture_->p_textureIndex); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - - if (alphaChannel) - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - else - glPixelStorei(GL_UNPACK_ALIGNMENT, 4); - - glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, imageFormat, GL_UNSIGNED_BYTE, source); - glGenerateMipmap(GL_TEXTURE_2D); - - glBindTexture(GL_TEXTURE_2D, 0); - - *texture = static_cast(texture_); - } - - stbi_image_free(source); -} - -void Device::useTexture(gpu::Texture* texture, int slot) -{ - //gpu::Device::useTexture(texture, slot); - - if (texture == nullptr) - { - glActiveTexture(GL_TEXTURE0 + slot); - glBindTexture(GL_TEXTURE_2D, 0); - } - else - { - auto* texture_ = dynamic_cast(texture); - glActiveTexture(GL_TEXTURE0 + slot); - glBindTexture(GL_TEXTURE_2D, texture_->p_textureIndex); - } -} - -void Device::destroyTexture(gpu::Texture*& texture) -{ - if (texture == nullptr) - throw std::invalid_argument("Invalid parameter"); - - auto* texture_ = dynamic_cast(texture); - - glDeleteTextures(1, &texture_->p_textureIndex); - gpu::Device::destroyTexture(texture); -} - - -void Device::Draw(int numFaces, int indexOffset, int vertexOffset) -{ - if (p_currentVertexBuffer != nullptr) - glDrawElementsBaseVertex(GL_TRIANGLES, numFaces * 3, GL_UNSIGNED_SHORT, (void*)(indexOffset * 2), vertexOffset); -} - + +#include "device.hpp" +#include "buffer.hpp" +#include "shader.hpp" +#include "shader_program.hpp" +#include "texture.hpp" +#include "render_target.hpp" + +#include "../../io/file.hpp" + +#include +#define STB_IMAGE_IMPLEMENTATION +#include +#include + +#include + + +namespace hpr::gpu::opengl +{ + +Device::Device() : + gpu::Device {DeviceAPI::OpenGL}, + p_isInitialized {false} +{} + +Device::~Device() = default; + +// Setup + +bool Device::initialize() +{ + return p_isInitialized = true; +} + +bool Device::destroy() +{ + return p_isInitialized = false; +} + +bool Device::loadLoader() +{ + return !gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); +} + +// State + +void Device::faceCulling(bool enableFaceCulling, CullMode faceCullingMode) +{ + if (enableFaceCulling) + glEnable(GL_CULL_FACE); + else + glDisable(GL_CULL_FACE); + + switch (faceCullingMode) + { + case CullMode::Front: + glCullFace(GL_FRONT); + break; + case CullMode::Back: + glCullFace(GL_BACK); + break; + case CullMode::FrontAndBack: + glCullFace(GL_FRONT_AND_BACK); + break; + case CullMode::None: + glCullFace(GL_NONE); + break; + } +} + +// Render targets + +void Device::createScreenRenderTarget(gpu::RenderTarget** target, Window* window) +{ + if (target == nullptr) + throw std::invalid_argument("Invalid parameter"); + *target = nullptr; + p_renderTargets.push(new opengl::RenderTarget()); + auto* newTarget = dynamic_cast(p_renderTargets.back()); + + newTarget->p_type = RenderTarget::Type::Screen; + newTarget->p_posX = 0; + newTarget->p_posY = 0; + newTarget->p_width = window->width(); + newTarget->p_height = window->height(); + + *target = newTarget; +} + +void Device::createFramebufferRenderTarget(gpu::RenderTarget** target, int width, int height) +{ + if (target == nullptr) + throw std::invalid_argument("Invalid parameter"); + unsigned int texture; + glGenTextures(GL_TEXTURE_2D, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); + + unsigned int framebuffer; + glGenFramebuffers(1, &framebuffer); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer); + + p_renderTargets.push(new opengl::RenderTarget()); + auto* newTarget = dynamic_cast(p_renderTargets.back()); + + newTarget->p_type = RenderTarget::Type::Framebuffer; + newTarget->p_posX = 0; + newTarget->p_posY = 0; + newTarget->p_width = width; + newTarget->p_height = height; + newTarget->p_frameBufferIndex = framebuffer; + newTarget->p_textureIndex = texture; + + *target = newTarget; +} + +void Device::createSubRenderTarget(gpu::RenderTarget** target, gpu::RenderTarget* parent, int x, int y, int width, int height) +{ + +} + +void Device::moveRenderTarget(gpu::RenderTarget* target, int x, int y) +{ + +} + +void Device::scaleRenderTarget(gpu::RenderTarget* target, int width, int height) +{ + +} + +void Device::destroyRenderTarget(gpu::RenderTarget*& target) +{ + +} + +// Buffers + + +void Device::createVertexBuffer(gpu::Buffer **buffer, int size, char *data) +{ + if (buffer == nullptr) + throw std::invalid_argument("Invalid parameter"); + + *buffer = nullptr; + p_buffers.push(new opengl::Buffer()); + auto* newBuffer = dynamic_cast(p_buffers.back()); + + newBuffer->p_type = Buffer::BufferType::Vertex; + newBuffer->p_size = size; + + glGenVertexArrays(1, &newBuffer->p_vertexArrayIndex); + glBindVertexArray(newBuffer->p_vertexArrayIndex); + + glGenBuffers(1, &newBuffer->p_bufferIndex); + glBindBuffer(GL_ARRAY_BUFFER, newBuffer->p_bufferIndex); + glBufferData(GL_ARRAY_BUFFER, size, (void*)data, GL_STATIC_DRAW); + + *buffer = static_cast(newBuffer); +} + +void Device::createIndexBuffer(gpu::Buffer **buffer, int size, char *data) +{ + if (buffer == nullptr) + throw std::invalid_argument("Invalid parameter"); + + *buffer = nullptr; + p_buffers.push(new opengl::Buffer()); + auto* newBuffer = dynamic_cast(p_buffers.back()); + + newBuffer->p_type = Buffer::BufferType::Index; + newBuffer->p_size = size; + + glGenVertexArrays(1, &newBuffer->p_vertexArrayIndex); + glBindVertexArray(newBuffer->p_vertexArrayIndex); + + glGenBuffers(1, &newBuffer->p_bufferIndex); + glBindBuffer(GL_UNIFORM_BUFFER, newBuffer->p_bufferIndex); + glBufferData(GL_UNIFORM_BUFFER, size, (void*)data, GL_STATIC_DRAW); + + *buffer = static_cast(newBuffer); +} + +void Device::createUniformBuffer(gpu::Buffer** buffer, int size, char* data) +{ + if (buffer == nullptr) + throw std::invalid_argument("Invalid parameter"); + + *buffer = nullptr; + p_buffers.push(new opengl::Buffer()); + auto* newBuffer = dynamic_cast(p_buffers.back()); + + newBuffer->p_type = Buffer::BufferType::Uniform; + newBuffer->p_size = size; + + glGenBuffers(1, &newBuffer->p_bufferIndex); + glBindBuffer(GL_UNIFORM_BUFFER, newBuffer->p_bufferIndex); + glBufferData(GL_UNIFORM_BUFFER, size, (void*)data, GL_DYNAMIC_DRAW); + + *buffer = static_cast(newBuffer); +} + +void Device::useVertexBuffer(gpu::Buffer *buffer, int stride, int offset) +{ + if (buffer == nullptr) + { + glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + } + else + { + auto* curBuffer = dynamic_cast(buffer); + + if (curBuffer->p_type == Buffer::BufferType::Vertex && p_currentVertexBuffer != buffer) + { + glBindVertexArray(curBuffer->p_vertexArrayIndex); + glBindBuffer(GL_ARRAY_BUFFER, curBuffer->p_bufferIndex); + + auto* curIndexBuffer = dynamic_cast(p_currentIndexBuffer); + + if (curIndexBuffer != nullptr) + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, curIndexBuffer->p_bufferIndex); + } + } + gpu::Device::useVertexBuffer(buffer, stride, offset); +} + +void Device::useIndexBuffer(gpu::Buffer *buffer, int offset) +{ + if (buffer == nullptr) + { + glBindVertexArray(0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + } + else + { + auto* curBuffer = dynamic_cast(buffer); + + if (curBuffer->p_type == Buffer::BufferType::Index && p_currentVertexBuffer != buffer) + { + glBindVertexArray(curBuffer->p_vertexArrayIndex); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, curBuffer->p_bufferIndex); + } + } + gpu::Device::useIndexBuffer(buffer, offset); +} + +void Device::useUniformBuffer(gpu::Buffer* buffer, int slot) +{ + if (buffer == nullptr) + { + glBindBuffer(GL_UNIFORM_BUFFER, 0); + } + else + { + auto* curBuffer = dynamic_cast(buffer); + glBindBufferRange(GL_UNIFORM_BUFFER, slot, curBuffer->p_bufferIndex, 0, buffer->size()); + } + gpu::Device::useUniformBuffer(buffer, slot); +} + +void Device::editBuffer(gpu::Buffer* buffer, char* data, int size, int offset) +{ + if (!checkCompability(buffer)) + throw std::runtime_error("Incompatible platform"); + if (buffer == nullptr || data == nullptr) + throw std::invalid_argument("Invalid argument"); + if (size + offset > buffer->size() || size < 0 || offset < 0) + throw std::out_of_range("Out of bounds"); + + auto* prevBuffer = dynamic_cast(activeBuffer(buffer->type())); + + auto* buffer_ = dynamic_cast(buffer); + glBindBuffer(buffer_->target(), buffer_->p_bufferIndex); + glBufferSubData(buffer_->target(), offset, size, data); + + if (prevBuffer != buffer_) + glBindBuffer(buffer_->target(), prevBuffer ? prevBuffer->p_bufferIndex : 0); + +} + +void Device::editBuffer(gpu::Buffer* buffer, char* data) +{ + editBuffer(buffer, data, buffer->size(), 0); +} + +void Device::destroyBuffer(gpu::Buffer *&buffer) +{ + if (buffer == nullptr) + throw std::invalid_argument("Invalid parameter"); + + auto* curBuffer = dynamic_cast(buffer); + glDeleteBuffers(1, &curBuffer->p_bufferIndex); + + if (curBuffer->p_type == Buffer::BufferType::Vertex) + glDeleteVertexArrays(1, &curBuffer->p_vertexArrayIndex); + + gpu::Device::destroyBuffer(buffer); +} + +// Shaders + +void Device::createVertexShader(gpu::Shader **shader, const std::string &filename, const std::string &label) +{ + if (shader == nullptr) + throw std::invalid_argument("Invalid parameter"); + + *shader = nullptr; + + unsigned int shaderIndex = glCreateShader(GL_VERTEX_SHADER); + if (shaderIndex == 0) + throw std::runtime_error("Could not create shader"); + + File file; + file.open(filename, File::Binary | File::Read); + std::string content = file.read().str(); + const char* shaderSource = content.c_str(); + + glShaderSource(shaderIndex, 1, &shaderSource, nullptr); + GLenum result = glGetError(); + glCompileShader(shaderIndex); + + int shaderStatus; + glGetShaderiv(shaderIndex, GL_COMPILE_STATUS, &shaderStatus); + if (!shaderStatus) + { + char error[2048 + 1]; + glGetShaderInfoLog(shaderIndex, 2048, nullptr, error); + + throw std::runtime_error(error); + } + + p_shaders.push(new opengl::Shader()); + auto* newShader = dynamic_cast(p_shaders.back()); + newShader->p_type = Shader::ShaderType::Vertex; + newShader->p_filename = filename; + newShader->p_shaderIndex = shaderIndex; + newShader->p_label = "VertexShader"; + + *shader = static_cast(newShader); +} + + +void Device::createFragmentShader(gpu::Shader **shader, const std::string &filename, const std::string &label) +{ + if (shader == nullptr) + throw std::invalid_argument("Invalid parameter"); + + *shader = nullptr; + + unsigned int shaderIndex = glCreateShader(GL_FRAGMENT_SHADER); + if (shaderIndex == 0) + throw std::runtime_error("Could not create shader"); + + File file; + file.open(filename, File::Binary | File::Read); + std::string content = file.read().str(); + const char* shaderSource = content.c_str(); + + glShaderSource(shaderIndex, 1, &shaderSource, nullptr); + GLenum result = glGetError(); + glCompileShader(shaderIndex); + + int shaderStatus; + glGetShaderiv(shaderIndex, GL_COMPILE_STATUS, &shaderStatus); + if (!shaderStatus) + { + char error[2048 + 1]; + glGetShaderInfoLog(shaderIndex, 2048, nullptr, error); + + throw std::runtime_error(error); + } + + p_shaders.push(new opengl::Shader()); + auto* newShader = dynamic_cast(p_shaders.back()); + newShader->p_type = Shader::ShaderType::Fragment; + newShader->p_filename = filename; + newShader->p_shaderIndex = shaderIndex; + newShader->p_label = "FragmentShader"; + + *shader = static_cast(newShader); +} + + +void Device::createGeometryShader(gpu::Shader **shader, const std::string &filename, const std::string &label) +{ + if (shader == nullptr) + throw std::invalid_argument("Invalid parameter"); + + *shader = nullptr; + + unsigned int shaderIndex = glCreateShader(GL_GEOMETRY_SHADER); + if (shaderIndex == 0) + throw std::runtime_error("Could not create shader"); + + File file; + file.open(filename, File::Binary | File::Read); + std::string content = file.read().str(); + const char* shaderSource = content.c_str(); + + glShaderSource(shaderIndex, 1, &shaderSource, nullptr); + GLenum result = glGetError(); + glCompileShader(shaderIndex); + + int shaderStatus; + glGetShaderiv(shaderIndex, GL_COMPILE_STATUS, &shaderStatus); + if (!shaderStatus) + { + char error[2048 + 1]; + glGetShaderInfoLog(shaderIndex, 2048, nullptr, error); + + throw std::runtime_error(error); + } + + p_shaders.push(new opengl::Shader()); + auto* newShader = dynamic_cast(p_shaders.back()); + newShader->p_type = Shader::ShaderType::Geometry; + newShader->p_filename = filename; + newShader->p_shaderIndex = shaderIndex; + newShader->p_label = "FragmentShader"; + + *shader = static_cast(newShader); +} + +void Device::destroyShader(gpu::Shader* shader) +{ + if (shader == nullptr) + throw std::invalid_argument("Invalid parameter"); + + auto* shader_ = dynamic_cast(shader); + glDeleteShader(shader_->p_shaderIndex); + + gpu::Device::destroyShader(shader); +} + +void Device::createShaderProgram(gpu::ShaderProgram** program) +{ + if (program == nullptr) + throw std::invalid_argument("Invalid parameter"); + + *program = nullptr; + + p_shaderPrograms.push(new opengl::ShaderProgram()); + auto* newProgram = dynamic_cast(p_shaderPrograms.back()); + newProgram->p_shaderProgramIndex = glCreateProgram(); + + *program = static_cast(newProgram); +} + +void Device::attachShader(gpu::ShaderProgram* program, gpu::Shader* shader) +{ + gpu::Device::attachShader(program, shader); + auto* program_ = dynamic_cast(program); + auto* shader_ = dynamic_cast(shader); + glAttachShader(program_->p_shaderProgramIndex, shader_->p_shaderIndex); +} + + +void Device::linkProgram(gpu::ShaderProgram* program) +{ + gpu::Device::linkProgram(program); + auto* program_ = dynamic_cast(program); + glLinkProgram(program_->p_shaderProgramIndex); + + GLint status; + glGetProgramiv(program_->p_shaderProgramIndex, GL_LINK_STATUS, &status); + + if (status == GL_FALSE) + throw std::runtime_error("Shader program link error"); +} + +void Device::useShaderProgram(gpu::ShaderProgram* program) +{ + gpu::ShaderProgram* currentProgram = p_currentShaderProgram; + gpu::Device::useShaderProgram(program); + + if (currentProgram == program) + return; + if (program == nullptr) + glUseProgram(0); + else + { + auto* program_ = dynamic_cast(program); + glUseProgram(program_->p_shaderProgramIndex); + } +} + +void Device::destroyShaderProgram(gpu::ShaderProgram*& program, bool withShaders) +{ + if (program == nullptr) + throw std::invalid_argument("Invalid parameter"); + + auto* program_ = dynamic_cast(program); + + for (int n = 0; n < static_cast(Shader::ShaderType::ShaderTypeCount); ++n) + { + auto* shader = dynamic_cast(program_->p_slots[n]); + if (shader != nullptr) + glDetachShader(program_->p_shaderProgramIndex, shader->p_shaderIndex); + } + glDeleteProgram(program_->p_shaderProgramIndex); + gpu::Device::destroyShaderProgram(program, withShaders); +} + +void Device::createTexture(gpu::Texture** texture, const std::string& filename) +{ + if (texture == nullptr) + throw std::invalid_argument("Invalid parameter"); + if (filename.empty()) + throw std::invalid_argument("Invalid parameter"); + + *texture = nullptr; + bool alphaChannel = true; + int internalFormat = GL_RGBA; + int imageFormat = GL_RGBA; + int width; + int height; + int channelsCount; + + stbi_set_flip_vertically_on_load(true); + unsigned char* source = stbi_load(filename.c_str(), &width, &height, &channelsCount, 0); + + if (!source) + throw std::runtime_error("Failed to load texture source"); + else + { + p_textures.push(new opengl::Texture()); + auto* texture_ = dynamic_cast(p_textures.back()); + texture_->p_filename = filename; + texture_->p_width = width; + texture_->p_height = height; + + glGenTextures(1, &texture_->p_textureIndex); + glBindTexture(GL_TEXTURE_2D, texture_->p_textureIndex); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + if (alphaChannel) + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + else + glPixelStorei(GL_UNPACK_ALIGNMENT, 4); + + glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, imageFormat, GL_UNSIGNED_BYTE, source); + glGenerateMipmap(GL_TEXTURE_2D); + + glBindTexture(GL_TEXTURE_2D, 0); + + *texture = static_cast(texture_); + } + + stbi_image_free(source); +} + +void Device::useTexture(gpu::Texture* texture, int slot) +{ + //gpu::Device::useTexture(texture, slot); + + if (texture == nullptr) + { + glActiveTexture(GL_TEXTURE0 + slot); + glBindTexture(GL_TEXTURE_2D, 0); + } + else + { + auto* texture_ = dynamic_cast(texture); + glActiveTexture(GL_TEXTURE0 + slot); + glBindTexture(GL_TEXTURE_2D, texture_->p_textureIndex); + } +} + +void Device::destroyTexture(gpu::Texture*& texture) +{ + if (texture == nullptr) + throw std::invalid_argument("Invalid parameter"); + + auto* texture_ = dynamic_cast(texture); + + glDeleteTextures(1, &texture_->p_textureIndex); + gpu::Device::destroyTexture(texture); +} + + +void Device::Draw(int numFaces, int indexOffset, int vertexOffset) +{ + if (p_currentVertexBuffer != nullptr) + glDrawElementsBaseVertex(GL_TRIANGLES, numFaces * 3, GL_UNSIGNED_SHORT, (void*)(indexOffset * 2), vertexOffset); +} + } \ No newline at end of file diff --git a/source/hyporo/gpu/opengl/device.hpp b/source/hpr/gpu/opengl/device.hpp similarity index 69% rename from source/hyporo/gpu/opengl/device.hpp rename to source/hpr/gpu/opengl/device.hpp index f9abe35..f99617a 100644 --- a/source/hyporo/gpu/opengl/device.hpp +++ b/source/hpr/gpu/opengl/device.hpp @@ -1,88 +1,106 @@ -#pragma once - -#include "../device.hpp" - -#include - - -namespace hpr::gpu::opengl -{ - -class Device : public gpu::Device -{ - -protected: - - bool p_isInitialized; - -public: - - // Constructors - - Device(); - - ~Device() override; - - - // Member functions - - // Setup - - bool initialize() override; - bool destroy() override; - - static - bool loadLoader(); - - // State - - void faceCulling(bool enableFaceCulling, CullMode faceCullingMode) override; - - // Render targets - - void createScreenRenderTarget(RenderTarget** target, Window* window) override; - void createFramebufferRenderTarget(RenderTarget** target, int width, int height) override; - void createSubRenderTarget(RenderTarget** target, RenderTarget* parent, int x, int y, int width, int height) override; - void moveRenderTarget(RenderTarget* target, int x, int y) override; - void scaleRenderTarget(RenderTarget* target, int width, int height) override; - void destroyRenderTarget(RenderTarget*& target) override; - - // Buffers - - void createVertexBuffer(Buffer **buffer, int size, char* data) override; - void createIndexBuffer(Buffer **buffer, int size, char* data) override; - void createUniformBuffer(Buffer **buffer, int size, char* data) override; - void useVertexBuffer(Buffer* buffer, int stride, int offset) override; - void useIndexBuffer(Buffer* buffer, int offset) override; - void useUniformBuffer(Buffer* buffer, int slot) override; - void editBuffer(Buffer* buffer, char* data, int size, int offset) override; - void editBuffer(Buffer* buffer, char* data) override; - void destroyBuffer(Buffer*& buffer) override; - - // Shaders - - void createVertexShader(Shader** shader, const std::string& filename, const std::string& label) override; - void createFragmentShader(Shader** shader, const std::string& filename, const std::string& label) override; - void createGeometryShader(Shader** shader, const std::string& filename, const std::string& label) override; - void destroyShader(Shader* shader) override; - - // Shader programs - - void createShaderProgram(ShaderProgram** program) override; - void attachShader(ShaderProgram* program, Shader* shader) override; - void linkProgram(ShaderProgram* program) override; - void useShaderProgram(ShaderProgram* program) override; - void destroyShaderProgram(ShaderProgram*& program, bool withShaders) override; - - // Textures - - void createTexture(Texture** texture, const std::string& filename) override; - void useTexture(Texture* texture, int slot) override; - void destroyTexture(Texture*& texture) override; - - // - - void Draw(int numFaces, int indexOffset, int vertexOffset); -}; - +#pragma once + +#include "../device.hpp" + +#include + + +namespace hpr::gpu::opengl +{ + +class Device : public gpu::Device +{ + +protected: + + bool p_isInitialized; + +public: + + // Constructors + + Device(); + + ~Device() override; + + + // Member functions + + // Setup + + bool initialize() override; + bool destroy() override; + + static + bool loadLoader(); + + // State + + void faceCulling(bool enableFaceCulling, CullMode faceCullingMode) override; + + // Render targets + + void createScreenRenderTarget(RenderTarget** target, Window* window) override; + void createFramebufferRenderTarget(RenderTarget** target, int width, int height) override; + void createSubRenderTarget(RenderTarget** target, RenderTarget* parent, int x, int y, int width, int height) override; + void moveRenderTarget(RenderTarget* target, int x, int y) override; + void scaleRenderTarget(RenderTarget* target, int width, int height) override; + void destroyRenderTarget(RenderTarget*& target) override; + + // Buffers + + virtual + void createVertexBuffer(Buffer **buffer, int size, char* data); + virtual + void createIndexBuffer(Buffer **buffer, int size, char* data); + virtual + void createUniformBuffer(Buffer **buffer, int size, char* data); + virtual + void useVertexBuffer(Buffer* buffer, int stride, int offset); + virtual + void useIndexBuffer(Buffer* buffer, int offset); + virtual + void useUniformBuffer(Buffer* buffer, int slot); + virtual + void editBuffer(Buffer* buffer, char* data, int size, int offset); + virtual + void editBuffer(Buffer* buffer, char* data); + virtual + void destroyBuffer(Buffer*& buffer); + + // Shaders + + virtual + void createVertexShader(Shader** shader, const std::string& filename, const std::string& label); + virtual + void createFragmentShader(Shader** shader, const std::string& filename, const std::string& label); + virtual + void createGeometryShader(Shader** shader, const std::string& filename, const std::string& label); + virtual + void destroyShader(Shader* shader); + + // Shader programs + + virtual + void createShaderProgram(ShaderProgram** program); + virtual + void attachShader(ShaderProgram* program, Shader* shader); + virtual + void linkProgram(ShaderProgram* program); + virtual + void useShaderProgram(ShaderProgram* program); + virtual + void destroyShaderProgram(ShaderProgram*& program, bool withShaders); + + // Textures + + void createTexture(Texture** texture, const std::string& filename) override; + void useTexture(Texture* texture, int slot) override; + void destroyTexture(Texture*& texture) override; + + // + + void Draw(int numFaces, int indexOffset, int vertexOffset); +}; + } \ No newline at end of file diff --git a/source/hyporo/gpu/opengl/render_target.cpp b/source/hpr/gpu/opengl/render_target.cpp similarity index 100% rename from source/hyporo/gpu/opengl/render_target.cpp rename to source/hpr/gpu/opengl/render_target.cpp diff --git a/source/hyporo/gpu/opengl/render_target.hpp b/source/hpr/gpu/opengl/render_target.hpp similarity index 100% rename from source/hyporo/gpu/opengl/render_target.hpp rename to source/hpr/gpu/opengl/render_target.hpp diff --git a/source/hyporo/gpu/opengl/shader.cpp b/source/hpr/gpu/opengl/shader.cpp similarity index 92% rename from source/hyporo/gpu/opengl/shader.cpp rename to source/hpr/gpu/opengl/shader.cpp index 606e01d..482c605 100644 --- a/source/hyporo/gpu/opengl/shader.cpp +++ b/source/hpr/gpu/opengl/shader.cpp @@ -1,18 +1,18 @@ -// -// Created by L-Nafaryus on 10/3/2022. -// - -#include "shader.hpp" - - -namespace hpr::gpu::opengl -{ - -Shader::Shader() : - gpu::Shader(DeviceAPI::OpenGL), - p_shaderIndex {0} -{} - -Shader::~Shader() = default; - +// +// Created by L-Nafaryus on 10/3/2022. +// + +#include "shader.hpp" + + +namespace hpr::gpu::opengl +{ + +Shader::Shader() : + gpu::Shader(DeviceAPI::OpenGL), + p_shaderIndex {0} +{} + +Shader::~Shader() = default; + } \ No newline at end of file diff --git a/source/hyporo/gpu/opengl/shader.hpp b/source/hpr/gpu/opengl/shader.hpp similarity index 90% rename from source/hyporo/gpu/opengl/shader.hpp rename to source/hpr/gpu/opengl/shader.hpp index 8018d97..5e359dd 100644 --- a/source/hyporo/gpu/opengl/shader.hpp +++ b/source/hpr/gpu/opengl/shader.hpp @@ -1,26 +1,26 @@ - -#include "../shader.hpp" - - -namespace hpr::gpu::opengl -{ - -class Shader : public gpu::Shader -{ - friend class Device; - -protected: - - unsigned int p_shaderIndex; - -public: - - // Constructors - - Shader(); - - ~Shader() override; - -}; - -} + +#include "../shader.hpp" + + +namespace hpr::gpu::opengl +{ + +class Shader : public gpu::Shader +{ + friend class Device; + +protected: + + unsigned int p_shaderIndex; + +public: + + // Constructors + + Shader(); + + ~Shader() override; + +}; + +} diff --git a/source/hyporo/gpu/opengl/shader_program.cpp b/source/hpr/gpu/opengl/shader_program.cpp similarity index 94% rename from source/hyporo/gpu/opengl/shader_program.cpp rename to source/hpr/gpu/opengl/shader_program.cpp index 05d4735..c6d2f67 100644 --- a/source/hyporo/gpu/opengl/shader_program.cpp +++ b/source/hpr/gpu/opengl/shader_program.cpp @@ -1,22 +1,22 @@ - -#include "shader_program.hpp" -#include "shader.hpp" - - -namespace hpr::gpu::opengl -{ - -ShaderProgram::ShaderProgram() : - gpu::ShaderProgram {DeviceAPI::OpenGL}, - p_shaderProgramIndex {0} -{} - -ShaderProgram::~ShaderProgram() -{} - -Shader* ShaderProgram::shader(gpu::Shader::ShaderType type) -{ - return static_cast(p_slots[(size_t)type]); -} - + +#include "shader_program.hpp" +#include "shader.hpp" + + +namespace hpr::gpu::opengl +{ + +ShaderProgram::ShaderProgram() : + gpu::ShaderProgram {DeviceAPI::OpenGL}, + p_shaderProgramIndex {0} +{} + +ShaderProgram::~ShaderProgram() +{} + +Shader* ShaderProgram::shader(gpu::Shader::ShaderType type) +{ + return static_cast(p_slots[(size_t)type]); +} + } \ No newline at end of file diff --git a/source/hyporo/gpu/opengl/shader_program.hpp b/source/hpr/gpu/opengl/shader_program.hpp similarity index 91% rename from source/hyporo/gpu/opengl/shader_program.hpp rename to source/hpr/gpu/opengl/shader_program.hpp index b1c6df5..bfb91ff 100644 --- a/source/hyporo/gpu/opengl/shader_program.hpp +++ b/source/hpr/gpu/opengl/shader_program.hpp @@ -1,35 +1,35 @@ -#pragma once - -#include "../shader_program.hpp" - - -namespace hpr::gpu::opengl -{ - -// Forward declarations - -class Shader; - -// - -class ShaderProgram : public gpu::ShaderProgram -{ - friend class Device; - -protected: - - unsigned int p_shaderProgramIndex; - -public: - - ShaderProgram(); - - ~ShaderProgram(); - -protected: - - Shader* shader(gpu::Shader::ShaderType type); - -}; - -} +#pragma once + +#include "../shader_program.hpp" + + +namespace hpr::gpu::opengl +{ + +// Forward declarations + +class Shader; + +// + +class ShaderProgram : public gpu::ShaderProgram +{ + friend class Device; + +protected: + + unsigned int p_shaderProgramIndex; + +public: + + ShaderProgram(); + + ~ShaderProgram(); + +protected: + + Shader* shader(gpu::Shader::ShaderType type); + +}; + +} diff --git a/source/hyporo/gpu/opengl/texture.cpp b/source/hpr/gpu/opengl/texture.cpp similarity index 92% rename from source/hyporo/gpu/opengl/texture.cpp rename to source/hpr/gpu/opengl/texture.cpp index b69f531..6fa139c 100644 --- a/source/hyporo/gpu/opengl/texture.cpp +++ b/source/hpr/gpu/opengl/texture.cpp @@ -1,15 +1,15 @@ - -#include "texture.hpp" - - -namespace hpr::gpu::opengl -{ - -Texture::Texture() : - gpu::Texture {DeviceAPI::OpenGL}, - p_textureIndex {0} -{} - -Texture::~Texture() = default; - + +#include "texture.hpp" + + +namespace hpr::gpu::opengl +{ + +Texture::Texture() : + gpu::Texture {DeviceAPI::OpenGL}, + p_textureIndex {0} +{} + +Texture::~Texture() = default; + } \ No newline at end of file diff --git a/source/hyporo/gpu/opengl/texture.hpp b/source/hpr/gpu/opengl/texture.hpp similarity index 90% rename from source/hyporo/gpu/opengl/texture.hpp rename to source/hpr/gpu/opengl/texture.hpp index 4ad3550..89b2fe4 100644 --- a/source/hyporo/gpu/opengl/texture.hpp +++ b/source/hpr/gpu/opengl/texture.hpp @@ -1,25 +1,25 @@ -#pragma once - -#include "../texture.hpp" - - -namespace hpr::gpu::opengl -{ - -class Texture : public gpu::Texture -{ - friend class Device; - -protected: - - unsigned int p_textureIndex; - -public: - - Texture(); - - ~Texture() override; - -}; - +#pragma once + +#include "../texture.hpp" + + +namespace hpr::gpu::opengl +{ + +class Texture : public gpu::Texture +{ + friend class Device; + +protected: + + unsigned int p_textureIndex; + +public: + + Texture(); + + ~Texture() override; + +}; + } \ No newline at end of file diff --git a/source/hyporo/gpu/render_target.cpp b/source/hpr/gpu/render_target.cpp similarity index 100% rename from source/hyporo/gpu/render_target.cpp rename to source/hpr/gpu/render_target.cpp diff --git a/source/hyporo/gpu/render_target.hpp b/source/hpr/gpu/render_target.hpp similarity index 100% rename from source/hyporo/gpu/render_target.hpp rename to source/hpr/gpu/render_target.hpp diff --git a/source/hyporo/gpu/shader.cpp b/source/hpr/gpu/shader.cpp similarity index 100% rename from source/hyporo/gpu/shader.cpp rename to source/hpr/gpu/shader.cpp diff --git a/source/hyporo/gpu/shader.hpp b/source/hpr/gpu/shader.hpp similarity index 100% rename from source/hyporo/gpu/shader.hpp rename to source/hpr/gpu/shader.hpp diff --git a/source/hyporo/gpu/shader_program.cpp b/source/hpr/gpu/shader_program.cpp similarity index 100% rename from source/hyporo/gpu/shader_program.cpp rename to source/hpr/gpu/shader_program.cpp diff --git a/source/hyporo/gpu/shader_program.hpp b/source/hpr/gpu/shader_program.hpp similarity index 92% rename from source/hyporo/gpu/shader_program.hpp rename to source/hpr/gpu/shader_program.hpp index 0952d46..7f3cad9 100644 --- a/source/hyporo/gpu/shader_program.hpp +++ b/source/hpr/gpu/shader_program.hpp @@ -3,7 +3,7 @@ #include "context.hpp" #include "shader.hpp" -#include "../hyplib/array/array.hpp" +#include "../containers/array.hpp" namespace hpr::gpu diff --git a/source/hyporo/gpu/shaders/base.frag.glsl b/source/hpr/gpu/shaders/base.frag.glsl similarity index 100% rename from source/hyporo/gpu/shaders/base.frag.glsl rename to source/hpr/gpu/shaders/base.frag.glsl diff --git a/source/hyporo/gpu/shaders/base.vert.glsl b/source/hpr/gpu/shaders/base.vert.glsl similarity index 100% rename from source/hyporo/gpu/shaders/base.vert.glsl rename to source/hpr/gpu/shaders/base.vert.glsl diff --git a/source/hyporo/gpu/texture.cpp b/source/hpr/gpu/texture.cpp similarity index 92% rename from source/hyporo/gpu/texture.cpp rename to source/hpr/gpu/texture.cpp index 8f6aeb6..bcf414c 100644 --- a/source/hyporo/gpu/texture.cpp +++ b/source/hpr/gpu/texture.cpp @@ -1,40 +1,40 @@ - -#include "texture.hpp" - - -namespace hpr::gpu -{ - -Texture::Texture() : - Context {DeviceAPI::Unknown}, - p_filename {}, - p_width {0}, - p_height {0} -{} - - -Texture::Texture(DeviceAPI api) : - Context {api}, - p_filename {}, - p_width {0}, - p_height {0} -{} - -Texture::~Texture() = default; - -std::string Texture::filename() const -{ - return p_filename; -} - -int Texture::width() const -{ - return p_width; -} - -int Texture::height() const -{ - return p_height; -} - -} + +#include "texture.hpp" + + +namespace hpr::gpu +{ + +Texture::Texture() : + Context {DeviceAPI::Unknown}, + p_filename {}, + p_width {0}, + p_height {0} +{} + + +Texture::Texture(DeviceAPI api) : + Context {api}, + p_filename {}, + p_width {0}, + p_height {0} +{} + +Texture::~Texture() = default; + +std::string Texture::filename() const +{ + return p_filename; +} + +int Texture::width() const +{ + return p_width; +} + +int Texture::height() const +{ + return p_height; +} + +} diff --git a/source/hyporo/gpu/texture.hpp b/source/hpr/gpu/texture.hpp similarity index 92% rename from source/hyporo/gpu/texture.hpp rename to source/hpr/gpu/texture.hpp index 68ddb16..b78af12 100644 --- a/source/hyporo/gpu/texture.hpp +++ b/source/hpr/gpu/texture.hpp @@ -1,42 +1,42 @@ -#pragma once - -#include "context.hpp" - -#include - - -namespace hpr::gpu -{ - -class Texture : public Context -{ -protected: - - std::string p_filename; - int p_width; - int p_height; - -public: - - // Constructors - - Texture(); - - explicit - Texture(DeviceAPI api); - - ~Texture() override; - - // Member functions - - [[nodiscard]] - std::string filename() const; - - [[nodiscard]] - int width() const; - - [[nodiscard]] - int height() const; -}; - +#pragma once + +#include "context.hpp" + +#include + + +namespace hpr::gpu +{ + +class Texture : public Context +{ +protected: + + std::string p_filename; + int p_width; + int p_height; + +public: + + // Constructors + + Texture(); + + explicit + Texture(DeviceAPI api); + + ~Texture() override; + + // Member functions + + [[nodiscard]] + std::string filename() const; + + [[nodiscard]] + int width() const; + + [[nodiscard]] + int height() const; +}; + } // end namespace hpr::gpu \ No newline at end of file diff --git a/source/hpr/hpr.cpp b/source/hpr/hpr.cpp new file mode 100644 index 0000000..d0ba75b --- /dev/null +++ b/source/hpr/hpr.cpp @@ -0,0 +1 @@ +#include "hpr.hpp" diff --git a/source/hpr/hpr.hpp b/source/hpr/hpr.hpp new file mode 100644 index 0000000..f3344d7 --- /dev/null +++ b/source/hpr/hpr.hpp @@ -0,0 +1,49 @@ +#pragma once + + +namespace hpr +{ + /* Core */ + // containers + // math + // io + + /* Graphics */ + namespace gpu + { + // gpu + // window_system + } + + /* Mesh */ + namespace mesh + { + // mesh + } + + /* CSG */ + namespace csg + { + // csg + } +} + +#include "containers.hpp" +#include "math.hpp" +#include "io.hpp" + +#if WITH_GPU +#include "gpu.hpp" +#endif + +#if WITH_WS +#include "window_system.hpp" +#endif + +#if WITH_MESH +#include "mesh.hpp" +#endif + +#if WITH_CSG +#include "csg.hpp" +#endif diff --git a/source/hpr/io.hpp b/source/hpr/io.hpp new file mode 100644 index 0000000..b0e8169 --- /dev/null +++ b/source/hpr/io.hpp @@ -0,0 +1,3 @@ +#pragma once + +#include "io/file.hpp" diff --git a/source/hpr/io/CMakeLists.txt b/source/hpr/io/CMakeLists.txt new file mode 100644 index 0000000..15049ef --- /dev/null +++ b/source/hpr/io/CMakeLists.txt @@ -0,0 +1,27 @@ + +set(COMPONENT_NAME io) + +add_component_library(${COMPONENT_NAME} STATIC + file.cpp +) + +#target_sources(${COMPONENT_NAME} PUBLIC +# file.hpp +# logger.hpp +#) + +install_component_headers(${COMPONENT_NAME}) +install_component_source(${COMPONENT_NAME}) + +if(0) + add_executable(${COMPONENT_NAME}-test + tests/${COMPONENT_NAME}-test.cpp + ) + + target_link_libraries(${COMPONENT_NAME}-test + ${CMAKE_PROJECT_NAME}::${COMPONENT_NAME} + GTest::gtest_main + ) + + gtest_add_tests(TARGET ${COMPONENT_NAME}-test) +endif() diff --git a/source/hyporo/hyplib/io/file.cpp b/source/hpr/io/file.cpp similarity index 100% rename from source/hyporo/hyplib/io/file.cpp rename to source/hpr/io/file.cpp diff --git a/source/hyporo/hyplib/io/file.hpp b/source/hpr/io/file.hpp similarity index 100% rename from source/hyporo/hyplib/io/file.hpp rename to source/hpr/io/file.hpp diff --git a/source/hyporo/hyplib/logger/logger.hpp b/source/hpr/io/logger.hpp similarity index 100% rename from source/hyporo/hyplib/logger/logger.hpp rename to source/hpr/io/logger.hpp diff --git a/source/hpr/math.hpp b/source/hpr/math.hpp new file mode 100644 index 0000000..bf29022 --- /dev/null +++ b/source/hpr/math.hpp @@ -0,0 +1,5 @@ +#pragma once + +#include "math/scalar.hpp" +#include "math/vector.hpp" +#include "math/matrix.hpp" \ No newline at end of file diff --git a/source/hpr/math/CMakeLists.txt b/source/hpr/math/CMakeLists.txt new file mode 100644 index 0000000..9ee624e --- /dev/null +++ b/source/hpr/math/CMakeLists.txt @@ -0,0 +1,34 @@ + +set(COMPONENT_NAME math) + +add_component_library(${COMPONENT_NAME} STATIC + vector/vector_space.cpp + matrix/matrix_space.cpp +) + +#target_sources(${COMPONENT_NAME} PUBLIC +# matrix/clip_space.hpp +# matrix.hpp +# matrix/matrix_space.hpp +# matrix/transform.hpp +# scalar.hpp +# scalar/scalar.hpp +# vector.hpp +# vector/vector_space.hpp +#) + +install_component_headers(${COMPONENT_NAME}) +install_component_source(${COMPONENT_NAME}) + +if(WITH_GTESTS) + add_executable(${COMPONENT_NAME}-test + tests/${COMPONENT_NAME}-test.cpp + ) + + target_link_libraries(${COMPONENT_NAME}-test + ${PROJECT_NAME}::${COMPONENT_NAME} + GTest::gtest_main + ) + + gtest_add_tests(TARGET ${COMPONENT_NAME}-test) +endif() diff --git a/source/hpr/math/matrix.hpp b/source/hpr/math/matrix.hpp new file mode 100644 index 0000000..a23fe30 --- /dev/null +++ b/source/hpr/math/matrix.hpp @@ -0,0 +1,5 @@ +#pragma once + +#include "matrix/matrix_space.hpp" +#include "matrix/transform.hpp" +#include "matrix/clip_space.hpp" diff --git a/source/hyporo/hyplib/matrix/clip_space.hpp b/source/hpr/math/matrix/clip_space.hpp similarity index 98% rename from source/hyporo/hyplib/matrix/clip_space.hpp rename to source/hpr/math/matrix/clip_space.hpp index dfdf505..eba6fd9 100644 --- a/source/hyporo/hyplib/matrix/clip_space.hpp +++ b/source/hpr/math/matrix/clip_space.hpp @@ -1,6 +1,6 @@ #pragma once -#include "matrix.hpp" +#include "../matrix.hpp" namespace hpr diff --git a/source/hyporo/hyplib/matrix/matrix_space.cpp b/source/hpr/math/matrix/matrix_space.cpp similarity index 100% rename from source/hyporo/hyplib/matrix/matrix_space.cpp rename to source/hpr/math/matrix/matrix_space.cpp diff --git a/source/hyporo/hyplib/matrix/matrix_space.hpp b/source/hpr/math/matrix/matrix_space.hpp similarity index 82% rename from source/hyporo/hyplib/matrix/matrix_space.hpp rename to source/hpr/math/matrix/matrix_space.hpp index 3cdacb3..9edbf35 100644 --- a/source/hyporo/hyplib/matrix/matrix_space.hpp +++ b/source/hpr/math/matrix/matrix_space.hpp @@ -1,6 +1,7 @@ #pragma once -#include "../vector/vector.hpp" +#include "../vector.hpp" + namespace hpr { @@ -20,6 +21,7 @@ public: using pointer = Type*; using reference = Type&; using iterator = Iterator; + using const_reference = Type const&; using const_iterator = Iterator; protected: @@ -47,7 +49,7 @@ public: p_cols {Cols} {} - inline + inline explicit MatrixSpace(const base& ms) : base {ms}, p_rows {Rows}, @@ -61,6 +63,13 @@ public: p_cols {Cols} {} + inline + MatrixSpace& operator=(const MatrixSpace& ms) + { + base::operator=(ms); + return *this; + } + inline MatrixSpace(typename base::iterator start, typename base::iterator end) : base {start, end}, @@ -104,6 +113,16 @@ public: return (*this)[col + p_rows * row]; } + inline + const_reference operator()(size_type row, size_type col) const + { + if (row >= p_rows || std::numeric_limits::max() - p_rows < row) + throw std::out_of_range("Row index is out of range"); + if (col >= p_cols || std::numeric_limits::max() - p_cols < col) + throw std::out_of_range("Column index is out of range"); + return (*this)[col + p_rows * row]; + } + VectorSpace row(size_type row) { VectorSpace vs; @@ -112,6 +131,14 @@ public: return vs; } + VectorSpace row(size_type row) const + { + VectorSpace vs; + for (auto n = 0; n < Cols; ++n) + vs[n] = (*this)(row, n); + return vs; + } + void row(size_type row, const VectorSpace& vs) { for (auto n = 0; n < Cols; ++n) @@ -198,7 +225,7 @@ public: inline MatrixSpace inv() { - return adj() / det(); + return MatrixSpace(adj() / det()); } // Friend functions @@ -225,6 +252,12 @@ public: return false; } + friend inline + VectorSpace operator*(const VectorSpace& vs, const MatrixSpace& ms) + { + + } + MatrixSpace& fill(value_type value) { for (auto n = 0; n < this->size(); ++n) @@ -245,5 +278,13 @@ public: } }; +// Aliases + +template +using mat = MatrixSpace; + +using mat2 = MatrixSpace; +using mat3 = MatrixSpace; +using mat4 = MatrixSpace; } \ No newline at end of file diff --git a/source/hyporo/hyplib/matrix/transform.hpp b/source/hpr/math/matrix/transform.hpp similarity index 52% rename from source/hyporo/hyplib/matrix/transform.hpp rename to source/hpr/math/matrix/transform.hpp index ee313b1..2c6de63 100644 --- a/source/hyporo/hyplib/matrix/transform.hpp +++ b/source/hpr/math/matrix/transform.hpp @@ -1,6 +1,6 @@ #pragma once -#include "matrix.hpp" +#include "../matrix.hpp" namespace hpr @@ -11,40 +11,62 @@ inline mat translate(const mat& ms, const vec& vs) { mat res {ms}; - res.row(3, ms.row(0) * vs[0] + ms.row(1) * vs[1] + ms.row(2) * vs[2] + ms.row(3)); + res.col(3, ms.row(0) * vs[0] + ms.row(1) * vs[1] + ms.row(2) * vs[2] + ms.row(3)); return res; } +template +inline +vec translate(const vec& vs1, const vec& vs2) +{ + mat res = mat::identity(); + res.row(3, vec(vs1, 0.)); + res = translate(res, vs2); + + return vec(res.row(3)); +} + template inline mat rotate(const mat& ms, const vec& vs, Type angle) { - const Type cos {cos(angle)}; - const Type sin {sin(angle)}; + const Type cosv = cos(angle); + const Type sinv = sin(angle); vec axis {normalize(vs)}; - vec temp {(1. - cos) * axis}; + vec temp {(1. - cosv) * axis}; mat rot; - rot(0, 0) = cos + temp[0] * axis[0]; - rot(0, 1) = temp[0] * axis[1] + sin * axis[2]; - rot(0, 2) = temp[0] * axis[2] - sin * axis[1]; - rot(1, 0) = temp[1] * axis[0] - sin * axis[2]; - rot(1, 1) = cos + temp[1] * axis[1]; - rot(1, 2) = temp[1] * axis[2] + sin * axis[0]; - rot(2, 0) = temp[2] * axis[0] + sin * axis[1]; - rot(2, 1) = temp[2] * axis[1] - sin * axis[0]; - rot(2, 2) = cos + temp[2] * axis[2]; + rot(0, 0) = cosv + temp[0] * axis[0]; + rot(0, 1) = temp[0] * axis[1] + sinv * axis[2]; + rot(0, 2) = temp[0] * axis[2] - sinv * axis[1]; + rot(1, 0) = temp[1] * axis[0] - sinv * axis[2]; + rot(1, 1) = cosv + temp[1] * axis[1]; + rot(1, 2) = temp[1] * axis[2] + sinv * axis[0]; + rot(2, 0) = temp[2] * axis[0] + sinv * axis[1]; + rot(2, 1) = temp[2] * axis[1] - sinv * axis[0]; + rot(2, 2) = cosv + temp[2] * axis[2]; mat res {ms}; res.row(0, ms.row(0) * rot(0, 0) + ms.row(1) * rot(0, 1) + ms.row(2) * rot(0, 2)); - res.row(0, ms.row(0) * rot(1, 0) + ms.row(1) * rot(1, 1) + ms.row(2) * rot(1, 2)); - res.row(0, ms.row(0) * rot(2, 0) + ms.row(1) * rot(2, 1) + ms.row(2) * rot(2, 2)); + res.row(1, ms.row(0) * rot(1, 0) + ms.row(1) * rot(1, 1) + ms.row(2) * rot(1, 2)); + res.row(2, ms.row(0) * rot(2, 0) + ms.row(1) * rot(2, 1) + ms.row(2) * rot(2, 2)); res.row(3, ms.row(3)); return res; } +template +inline +vec rotate(const vec& vs1, const vec& vs2, Type angle) +{ + mat res = mat::identity(); + res.row(3, vec(vs1, 0.)); + res = rotate(res, vs2, angle); + + return vec(res.row(3)); +} + template inline mat scale(const mat& ms, const vec& vs) diff --git a/source/hpr/math/scalar.hpp b/source/hpr/math/scalar.hpp new file mode 100644 index 0000000..1b8a4e1 --- /dev/null +++ b/source/hpr/math/scalar.hpp @@ -0,0 +1,3 @@ +#pragma once + +#include "scalar/scalar.hpp" diff --git a/source/hyporo/hyplib/scalar/scalar.hpp b/source/hpr/math/scalar/scalar.hpp similarity index 100% rename from source/hyporo/hyplib/scalar/scalar.hpp rename to source/hpr/math/scalar/scalar.hpp diff --git a/source/hpr/math/tests/math-test.cpp b/source/hpr/math/tests/math-test.cpp new file mode 100644 index 0000000..f9a9aac --- /dev/null +++ b/source/hpr/math/tests/math-test.cpp @@ -0,0 +1,47 @@ +#include + +#include "../vector.hpp" +#include "../matrix.hpp" + + +TEST(math, Vector) +{ + hpr::vec3 v1 {1, 3, 2}; + hpr::vec3 v2 {5, 7, -1}; + hpr::vec2 v31 {13, -2}; + hpr::vec3 v32 {v31, 9}; + EXPECT_EQ((hpr::vec3(v2.begin(), v2.end())), v2); + EXPECT_EQ(v32, hpr::vec3(13, -2, 9)); + EXPECT_EQ(-v1, hpr::vec3(-1, -3, -2)); + EXPECT_EQ(v1 + v2, hpr::vec3(6, 10, 1)); + EXPECT_EQ(v1 - v2, hpr::vec3(-4, -4, 3)); + EXPECT_EQ((hpr::dot(v1, v2) ), 24); + EXPECT_EQ((hpr::cross(hpr::vec3(1, 0, 0), hpr::vec3(0, 1, 0))), hpr::vec3(0, 0, 1)); + EXPECT_EQ((hpr::angle(hpr::vec3(1, 0, 0), hpr::vec3(0, 0, 1))), hpr::PI * 0.5); + EXPECT_EQ((hpr::normalize(hpr::vec3(1, 1, 1))), hpr::vec3(0.5773502691896258, 0.5773502691896258, 0.5773502691896258)); +} + +TEST(math, Matrix) +{ + hpr::mat2 m1; + hpr::vec4 v1; + EXPECT_FALSE(v1 == m1); + hpr::mat2 m2 {3, 2, 7, 4}; + hpr::vec2 v2 {2, 4}; + EXPECT_EQ(m2.col(1), v2); + hpr::vec2 v3 {13, 51}; + m2.col(1, v3); + EXPECT_EQ(m2.col(1), v3); + hpr::mat3 m4 {1, 2, 3, 4, 5, 6, 7, 8, 9}; + hpr::mat2 m41 {5, 6, 8, 9}; + EXPECT_EQ(m41.minor(0, 0), 9); + hpr::mat2 m5 {1, 2, 3, 4}; + + EXPECT_EQ((m4.det()), 0); + EXPECT_EQ(hpr::mat3(-9, 23, 3, 5, 5, 6, 7, -3, 9).det(), -786); + + hpr::mat2 m6 {2, 1, 7, 4}; + EXPECT_EQ(m6.inv(), hpr::mat2(4, -1, -7, 2)); + EXPECT_EQ(hpr::mat3(1, 0, 0, 0, 1, 0, 0, 0, 1).det(), 1.); + //EXPECT_EQ(m4.det(), 0); +} \ No newline at end of file diff --git a/source/hpr/math/vector.hpp b/source/hpr/math/vector.hpp new file mode 100644 index 0000000..2f33a32 --- /dev/null +++ b/source/hpr/math/vector.hpp @@ -0,0 +1,4 @@ +#pragma once + +#include "vector/vector_space.hpp" + diff --git a/source/hyporo/hyplib/vector/vector_space.cpp b/source/hpr/math/vector/vector_space.cpp similarity index 100% rename from source/hyporo/hyplib/vector/vector_space.cpp rename to source/hpr/math/vector/vector_space.cpp diff --git a/source/hyporo/hyplib/vector/vector_space.hpp b/source/hpr/math/vector/vector_space.hpp similarity index 94% rename from source/hyporo/hyplib/vector/vector_space.hpp rename to source/hpr/math/vector/vector_space.hpp index de1a0e6..218624b 100644 --- a/source/hyporo/hyplib/vector/vector_space.hpp +++ b/source/hpr/math/vector/vector_space.hpp @@ -1,7 +1,7 @@ #pragma once -#include "../scalar/scalar.hpp" -#include "../array/array.hpp" +#include "../scalar.hpp" +#include "../../containers/array/static_array.hpp" namespace hpr @@ -106,6 +106,14 @@ public: (*this)[subvs.size()] = v; } + template + inline + VectorSpace(const VectorSpace& vs) : + base {vs.begin(), vs.begin() + Size} + { + static_assert(BiggerSize > Size, "Size should be bigger"); + } + // Member functions // vector versus scalar (per element operations) @@ -390,4 +398,13 @@ bool all(const VectorSpace& vs) return res; } +// Aliases + +template +using vec = VectorSpace; + +using vec2 = VectorSpace; +using vec3 = VectorSpace; +using vec4 = VectorSpace; + } diff --git a/source/hpr/mesh.hpp b/source/hpr/mesh.hpp new file mode 100644 index 0000000..3b00b03 --- /dev/null +++ b/source/hpr/mesh.hpp @@ -0,0 +1,3 @@ +#pragma once + +#include "mesh/mesh.hpp" \ No newline at end of file diff --git a/source/hpr/mesh/CMakeLists.txt b/source/hpr/mesh/CMakeLists.txt new file mode 100644 index 0000000..61476f9 --- /dev/null +++ b/source/hpr/mesh/CMakeLists.txt @@ -0,0 +1,26 @@ + +set(COMPONENT_NAME mesh) + +add_component_library(${COMPONENT_NAME} STATIC + mesh.cpp +) + +#target_sources(${COMPONENT_NAME} PUBLIC + +#) + +install_component_headers(${COMPONENT_NAME}) +install_component_source(${COMPONENT_NAME}) + +if (0) + add_executable(${COMPONENT_NAME}-test + tests/${COMPONENT_NAME}-test.cpp + ) + + target_link_libraries(${COMPONENT_NAME}-test + ${PROJECT_NAME}::${COMPONENT_NAME} + GTest::gtest_main + ) + + gtest_add_tests(TARGET ${COMPONENT_NAME}-test) +endif() diff --git a/source/hyporo/hmesh/cell.hpp b/source/hpr/mesh/cell.hpp similarity index 77% rename from source/hyporo/hmesh/cell.hpp rename to source/hpr/mesh/cell.hpp index bb05754..cc30687 100644 --- a/source/hyporo/hmesh/cell.hpp +++ b/source/hpr/mesh/cell.hpp @@ -1,8 +1,8 @@ #pragma once -#include "../hyplib/scalar/scalar.hpp" -#include "../hyplib/array/array.hpp" -#include "../hyplib/vector/vector.hpp" +#include "../containers/array.hpp" +#include "../math/scalar/scalar.hpp" +#include "../math/vector.hpp" namespace hpr::mesh diff --git a/source/hyporo/hmesh/edge.hpp b/source/hpr/mesh/edge.hpp similarity index 87% rename from source/hyporo/hmesh/edge.hpp rename to source/hpr/mesh/edge.hpp index ab8e088..36ca52c 100644 --- a/source/hyporo/hmesh/edge.hpp +++ b/source/hpr/mesh/edge.hpp @@ -1,8 +1,9 @@ #pragma once -#include "../hyplib/scalar/scalar.hpp" -#include "../hyplib/array/array.hpp" -#include "../hyplib/vector/vector.hpp" +#include "../containers/array.hpp" +#include "../math/scalar/scalar.hpp" +#include "../math/vector.hpp" + namespace hpr::mesh diff --git a/source/hyporo/hmesh/face.hpp b/source/hpr/mesh/face.hpp similarity index 89% rename from source/hyporo/hmesh/face.hpp rename to source/hpr/mesh/face.hpp index 2466ded..b3eb833 100644 --- a/source/hyporo/hmesh/face.hpp +++ b/source/hpr/mesh/face.hpp @@ -1,8 +1,9 @@ #pragma once -#include "../hyplib/scalar/scalar.hpp" -#include "../hyplib/array/array.hpp" -#include "../hyplib/vector/vector.hpp" +#include "../containers/array.hpp" +#include "../math/scalar/scalar.hpp" +#include "../math/vector.hpp" + namespace hpr::mesh diff --git a/source/hyporo/hmesh/mesh.cpp b/source/hpr/mesh/mesh.cpp similarity index 100% rename from source/hyporo/hmesh/mesh.cpp rename to source/hpr/mesh/mesh.cpp diff --git a/source/hyporo/hmesh/mesh.hpp b/source/hpr/mesh/mesh.hpp similarity index 97% rename from source/hyporo/hmesh/mesh.hpp rename to source/hpr/mesh/mesh.hpp index 79e5cbf..13a5d02 100644 --- a/source/hyporo/hmesh/mesh.hpp +++ b/source/hpr/mesh/mesh.hpp @@ -1,8 +1,8 @@ #pragma once -#include "../hyplib/scalar/scalar.hpp" -#include "../hyplib/array/array.hpp" -#include "../hyplib/vector/vector.hpp" +#include "../containers/array.hpp" +#include "../math/scalar/scalar.hpp" +#include "../math/vector.hpp" #include "vertex.hpp" #include "edge.hpp" diff --git a/source/hyporo/hmesh/tests/hmesh-test.cpp b/source/hpr/mesh/tests/hmesh-test.cpp similarity index 100% rename from source/hyporo/hmesh/tests/hmesh-test.cpp rename to source/hpr/mesh/tests/hmesh-test.cpp diff --git a/source/hyporo/hmesh/vertex.hpp b/source/hpr/mesh/vertex.hpp similarity index 83% rename from source/hyporo/hmesh/vertex.hpp rename to source/hpr/mesh/vertex.hpp index b45cb5c..e5d3611 100644 --- a/source/hyporo/hmesh/vertex.hpp +++ b/source/hpr/mesh/vertex.hpp @@ -1,8 +1,9 @@ #pragma once -#include "../hyplib/scalar/scalar.hpp" -#include "../hyplib/array/array.hpp" -#include "../hyplib/vector/vector.hpp" +#include "../containers/array.hpp" +#include "../math/scalar/scalar.hpp" +#include "../math/vector.hpp" + namespace hpr::mesh diff --git a/source/hpr/window_system.hpp b/source/hpr/window_system.hpp new file mode 100644 index 0000000..9ee48f5 --- /dev/null +++ b/source/hpr/window_system.hpp @@ -0,0 +1,11 @@ +#pragma once + + +#include "window_system/monitor.hpp" +#include "window_system/window_context.hpp" +#include "window_system/window.hpp" +#include "window_system/window_system.hpp" + +#include "window_system/glfw/monitor.hpp" +#include "window_system/glfw/window.hpp" +#include "window_system/glfw/window_system.hpp" diff --git a/source/hpr/window_system/CMakeLists.txt b/source/hpr/window_system/CMakeLists.txt new file mode 100644 index 0000000..2327061 --- /dev/null +++ b/source/hpr/window_system/CMakeLists.txt @@ -0,0 +1,35 @@ + +set(COMPONENT_NAME window_system) + +add_component_library(${COMPONENT_NAME} OBJECT + window_system.cpp + monitor.cpp + window.cpp + glfw/window_system.cpp + glfw/monitor.cpp + glfw/window.cpp +) + +#target_sources(${COMPONENT_NAME} PUBLIC + +#) + +target_link_libraries(${COMPONENT_NAME} + glfw +) + +install_component_headers(${COMPONENT_NAME}) +install_component_source(${COMPONENT_NAME}) + +if (0) + add_executable(${COMPONENT_NAME}-test + tests/${COMPONENT_NAME}-test.cpp + ) + + target_link_libraries(${COMPONENT_NAME}-test + ${PROJECT_NAME}::${COMPONENT_NAME} + GTest::gtest_main + ) + + gtest_add_tests(TARGET ${COMPONENT_NAME}-test) +endif() \ No newline at end of file diff --git a/source/hyporo/window_system/glfw/monitor.cpp b/source/hpr/window_system/glfw/monitor.cpp similarity index 100% rename from source/hyporo/window_system/glfw/monitor.cpp rename to source/hpr/window_system/glfw/monitor.cpp diff --git a/source/hyporo/window_system/glfw/monitor.hpp b/source/hpr/window_system/glfw/monitor.hpp similarity index 91% rename from source/hyporo/window_system/glfw/monitor.hpp rename to source/hpr/window_system/glfw/monitor.hpp index 6cc6d1e..cb70949 100644 --- a/source/hyporo/window_system/glfw/monitor.hpp +++ b/source/hpr/window_system/glfw/monitor.hpp @@ -2,7 +2,7 @@ #include "../monitor.hpp" -#include "GLFW/glfw3.h" +#include namespace hpr::gpu::glfw diff --git a/source/hyporo/window_system/glfw/window.cpp b/source/hpr/window_system/glfw/window.cpp similarity index 100% rename from source/hyporo/window_system/glfw/window.cpp rename to source/hpr/window_system/glfw/window.cpp diff --git a/source/hyporo/window_system/glfw/window.hpp b/source/hpr/window_system/glfw/window.hpp similarity index 100% rename from source/hyporo/window_system/glfw/window.hpp rename to source/hpr/window_system/glfw/window.hpp diff --git a/source/hyporo/window_system/glfw/window_system.cpp b/source/hpr/window_system/glfw/window_system.cpp similarity index 100% rename from source/hyporo/window_system/glfw/window_system.cpp rename to source/hpr/window_system/glfw/window_system.cpp diff --git a/source/hyporo/window_system/glfw/window_system.hpp b/source/hpr/window_system/glfw/window_system.hpp similarity index 100% rename from source/hyporo/window_system/glfw/window_system.hpp rename to source/hpr/window_system/glfw/window_system.hpp diff --git a/source/hyporo/window_system/monitor.cpp b/source/hpr/window_system/monitor.cpp similarity index 100% rename from source/hyporo/window_system/monitor.cpp rename to source/hpr/window_system/monitor.cpp diff --git a/source/hyporo/window_system/monitor.hpp b/source/hpr/window_system/monitor.hpp similarity index 100% rename from source/hyporo/window_system/monitor.hpp rename to source/hpr/window_system/monitor.hpp diff --git a/source/hyporo/window_system/window.cpp b/source/hpr/window_system/window.cpp similarity index 100% rename from source/hyporo/window_system/window.cpp rename to source/hpr/window_system/window.cpp diff --git a/source/hyporo/window_system/window.hpp b/source/hpr/window_system/window.hpp similarity index 100% rename from source/hyporo/window_system/window.hpp rename to source/hpr/window_system/window.hpp diff --git a/source/hyporo/window_system/window_context.hpp b/source/hpr/window_system/window_context.hpp similarity index 100% rename from source/hyporo/window_system/window_context.hpp rename to source/hpr/window_system/window_context.hpp diff --git a/source/hyporo/window_system/window_system.cpp b/source/hpr/window_system/window_system.cpp similarity index 100% rename from source/hyporo/window_system/window_system.cpp rename to source/hpr/window_system/window_system.cpp diff --git a/source/hyporo/window_system/window_system.hpp b/source/hpr/window_system/window_system.hpp similarity index 95% rename from source/hyporo/window_system/window_system.hpp rename to source/hpr/window_system/window_system.hpp index 52b626d..946cbd8 100644 --- a/source/hyporo/window_system/window_system.hpp +++ b/source/hpr/window_system/window_system.hpp @@ -1,8 +1,8 @@ #pragma once +#include "../containers/array.hpp" #include "window.hpp" #include "monitor.hpp" -#include "../hyplib/array/array.hpp" #include diff --git a/source/hyporo/CMakeLists.txt b/source/hyporo/CMakeLists.txt deleted file mode 100644 index a2dfdd4..0000000 --- a/source/hyporo/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -add_subdirectory(csg) -add_subdirectory(gpu) -add_subdirectory(hmesh) -add_subdirectory(hyplib) -add_subdirectory(io) -add_subdirectory(window_system) diff --git a/source/hyporo/csg/CMakeLists.txt b/source/hyporo/csg/CMakeLists.txt deleted file mode 100644 index 632cc01..0000000 --- a/source/hyporo/csg/CMakeLists.txt +++ /dev/null @@ -1,44 +0,0 @@ - -include_directories( - . - ../hyplib/integer - ../hyplib/scalar - ../hyplib/vector -) - -add_library(hyporo-csg STATIC - - # Header files - shape.hpp - - # Source files - shape.cpp -) - -target_link_libraries(hyporo-csg - hyporo-hyplib - ${OCCT_LIBRARIES} - ) - -target_include_directories(hyporo-csg - PUBLIC - ${OCCT_INCLUDE_DIRS} - ) - -if(WITH_GTESTS) - add_executable(hyporo-csg-test - tests/csg-test.cpp - shape.cpp - ) - - target_link_libraries(hyporo-csg-test - GTest::gtest_main - ${OCCT_LIBRARIES} - ) - - target_include_directories(hyporo-csg-test - PUBLIC - ${OCCT_INCLUDE_DIRS} - ) - gtest_add_tests(TARGET hyporo-csg-test) -endif() diff --git a/source/hyporo/csg/csg.hpp b/source/hyporo/csg/csg.hpp deleted file mode 100644 index 613c95a..0000000 --- a/source/hyporo/csg/csg.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include "shape.hpp" -#include "vertex.hpp" -#include "edge.hpp" -#include "wire.hpp" -#include "face.hpp" -#include "shell.hpp" -#include "solid.hpp" -#include "compound.hpp" -#include "geometry.hpp" -#include "surface.hpp" - - -namespace hpr::csg -{} \ No newline at end of file diff --git a/source/hyporo/gpu/CMakeLists.txt b/source/hyporo/gpu/CMakeLists.txt deleted file mode 100644 index 3a866b3..0000000 --- a/source/hyporo/gpu/CMakeLists.txt +++ /dev/null @@ -1,41 +0,0 @@ - -include_directories( - ../hyplib -) - -add_library(hyporo-gpu STATIC - # Source files - buffer.cpp - context.cpp - device.cpp - opengl/buffer.cpp - opengl/context.cpp - opengl/device.cpp - opengl/shader.cpp - opengl/shader_program.cpp - opengl/texture.cpp - shader.cpp - shader_program.cpp - texture.cpp - - # Header files - context.hpp - shader.hpp - shader_program.hpp - buffer.hpp - device.hpp - texture.hpp - opengl/context.hpp - opengl/shader.hpp - opengl/shader_program.hpp - opengl/buffer.hpp - opengl/device.hpp - opengl/texture.hpp - -) - -target_link_libraries(hyporo-gpu - glad - stb - hyporo-hyplib -) \ No newline at end of file diff --git a/source/hyporo/hmesh/CMakeLists.txt b/source/hyporo/hmesh/CMakeLists.txt deleted file mode 100644 index 36dbe41..0000000 --- a/source/hyporo/hmesh/CMakeLists.txt +++ /dev/null @@ -1,29 +0,0 @@ - -include_directories( - . - ../hyplib/integer - ../hyplib/scalar - ../hyplib/vector -) - -add_library(hyporo-mesh STATIC - - # Header files - mesh.hpp - vertex.hpp - - # Source files - mesh.cpp -) - -if(WITH_GTESTS) - add_executable(hyporo-hmesh-test - tests/hmesh-test.cpp - ) - - target_link_libraries(hyporo-hmesh-test - GTest::gtest_main - ) - - gtest_add_tests(TARGET hyporo-hmesh-test) -endif() diff --git a/source/hyporo/hyplib/CMakeLists.txt b/source/hyporo/hyplib/CMakeLists.txt deleted file mode 100644 index 339c5b8..0000000 --- a/source/hyporo/hyplib/CMakeLists.txt +++ /dev/null @@ -1,54 +0,0 @@ - -include_directories( - integer - scalar - array - vector - matrix -) - -add_library(hyporo-hyplib STATIC - - # Header files - scalar/scalar.hpp - - array/iterator.hpp - array/dynamic_array.hpp - array/static_array.hpp - array/array.hpp - - vector/vector_space.hpp - - vector/vector.hpp - - matrix/matrix_space.hpp - - matrix/matrix.hpp - - io/file.hpp - io/io.hpp - - # Source files - io/file.cpp - - matrix/matrix_space.cpp -) - -if(WITH_GTESTS) - add_executable(hyporo-hyplib-test - tests/hyplib-test.cpp - - matrix/matrix_space.hpp - ) - - target_link_libraries(hyporo-hyplib-test - - hyporo-hyplib - GTest::gtest_main - ) - - gtest_add_tests(TARGET hyporo-hyplib-test) - - add_executable(mytest - tests/mytest.cpp) -endif() diff --git a/source/hyporo/hyplib/array/array.hpp b/source/hyporo/hyplib/array/array.hpp deleted file mode 100644 index 36eaf96..0000000 --- a/source/hyporo/hyplib/array/array.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include "dynamic_array.hpp" -#include "static_array.hpp" - - -namespace hpr -{ - -template -using darray = DynamicArray; - -template -using sarray = StaticArray; - -} \ No newline at end of file diff --git a/source/hyporo/hyplib/io/io.hpp b/source/hyporo/hyplib/io/io.hpp deleted file mode 100644 index 46d5fa6..0000000 --- a/source/hyporo/hyplib/io/io.hpp +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -#include "file.hpp" - diff --git a/source/hyporo/hyplib/matrix/matrix.hpp b/source/hyporo/hyplib/matrix/matrix.hpp deleted file mode 100644 index a30052b..0000000 --- a/source/hyporo/hyplib/matrix/matrix.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "../scalar/scalar.hpp" -#include "matrix_space.hpp" - - -namespace hpr -{ - -template -using mat = MatrixSpace; - -using mat2 = MatrixSpace; -using mat3 = MatrixSpace; -using mat4 = MatrixSpace; - -} diff --git a/source/hyporo/hyplib/tests/hyplib-test.cpp b/source/hyporo/hyplib/tests/hyplib-test.cpp deleted file mode 100644 index c74fb6a..0000000 --- a/source/hyporo/hyplib/tests/hyplib-test.cpp +++ /dev/null @@ -1,107 +0,0 @@ -#include - -#include "array.hpp" -#include "vector.hpp" -#include "matrix.hpp" - - -TEST(hyplib, StaticArray) -{ - hpr::StaticArray arr {1, 3, 2}; - hpr::StaticArray sarr {arr, 5}; - hpr::StaticArray sarr2 {1, 3, 2, 5}; - EXPECT_EQ(sarr, sarr2); -} - -TEST(hyplib, DynamicArray) -{ - hpr::DynamicArray arr {1, 3, 2}; - hpr::DynamicArray arr2 {1, 3, 2}; - EXPECT_EQ(arr, arr2); - arr.remove(1); - EXPECT_EQ(arr, hpr::darray({1, 2})); - auto iter = arr2.begin(); - ++iter; - arr2.remove(iter); - EXPECT_EQ(arr2, hpr::darray({1, 2})); - - hpr::DynamicArray arr3 {1, 3, 0, 2, 9, 0, 5}; - arr3.remove([](float num) { return num == 0; }); - EXPECT_EQ(arr3, hpr::darray({1, 3, 2, 9, 5})); - EXPECT_EQ(arr3.size(), 5); - - hpr::DynamicArray arr4; - arr4.push(new float(5)); - arr4.push(new float(7)); - arr4.push(new float(9)); - EXPECT_EQ(*arr4[0], 5.f); - EXPECT_EQ(*arr4[2], 9.f); -} - -TEST(hyplib, Vector) -{ - hpr::vec3 v1 {1, 3, 2}; - hpr::vec3 v2 {5, 7, -1}; - hpr::vec2 v31 {13, -2}; - hpr::vec3 v32 {v31, 9}; - EXPECT_EQ((hpr::vec3(v2.begin(), v2.end())), v2); - EXPECT_EQ(v32, hpr::vec3(13, -2, 9)); - EXPECT_EQ(-v1, hpr::vec3(-1, -3, -2)); - EXPECT_EQ(v1 + v2, hpr::vec3(6, 10, 1)); - EXPECT_EQ(v1 - v2, hpr::vec3(-4, -4, 3)); - EXPECT_EQ((hpr::dot(v1, v2) ), 24); - EXPECT_EQ((hpr::cross(hpr::vec3(1, 0, 0), hpr::vec3(0, 1, 0))), hpr::vec3(0, 0, 1)); - EXPECT_EQ((hpr::angle(hpr::vec3(1, 0, 0), hpr::vec3(0, 0, 1))), hpr::PI * 0.5); - EXPECT_EQ((hpr::normalize(hpr::vec3(1, 1, 1))), hpr::vec3(0.5773502691896258, 0.5773502691896258, 0.5773502691896258)); -} - -TEST(hyplib, Matrix) -{ - hpr::mat2 m1; - hpr::vec4 v1; - EXPECT_FALSE(v1 == m1); - hpr::mat2 m2 {3, 2, 7, 4}; - hpr::vec2 v2 {2, 4}; - EXPECT_EQ(m2.col(1), v2); - hpr::vec2 v3 {13, 51}; - m2.col(1, v3); - EXPECT_EQ(m2.col(1), v3); - hpr::mat3 m4 {1, 2, 3, 4, 5, 6, 7, 8, 9}; - hpr::mat2 m41 {5, 6, 8, 9}; - EXPECT_EQ(m41.minor(0, 0), 9); - hpr::mat2 m5 {1, 2, 3, 4}; - - EXPECT_EQ((m4.det()), 0); - EXPECT_EQ(hpr::mat3(-9, 23, 3, 5, 5, 6, 7, -3, 9).det(), -786); - - hpr::mat2 m6 {2, 1, 7, 4}; - EXPECT_EQ(m6.inv(), hpr::mat2(4, -1, -7, 2)); - //EXPECT_EQ(m4.det(), 0); -} -/*using Minor = typename std::conditional<(Rows > 2 && Cols > 2), - MatrixSpace, MatrixSpace>::type; - -value_type det() -{ - - if (!is_square()) - throw std::runtime_error("Matrix must be square"); - if (this->size() == 1) - return (*this)[0]; - else if (this->size() == 4) - return (*this)(0, 0) * (*this)(1, 1) - (*this)(0, 1) * (*this)(1, 0); - else { - auto res = 0; - for (auto m = 0; m < Cols; ++m) { - Minor minor; - auto minor_iter = minor.begin(); - for (auto n = 0; n < Rows; ++n) - for (auto k = 0; k < Cols; ++k) - if (k != 0 && n != m) - *(minor_iter++) = (*this)[k + p_rows * n]; - res += pow(-1, m) * (*this)(0, m) * minor.det(); - } - return res; - } -} -*/ \ No newline at end of file diff --git a/source/hyporo/hyplib/tests/mytest.cpp b/source/hyporo/hyplib/tests/mytest.cpp deleted file mode 100644 index c28d7a2..0000000 --- a/source/hyporo/hyplib/tests/mytest.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include "../vector/vector_space.hpp" - -template -using vs = hpr::VectorSpace; - -using std::cout, std::endl; - -template -void print(const vs& vec) -{ - cout << "Vector: "; - for (auto n = vec.begin(); n != vec.end(); ++n) - cout << *n << " "; - cout << endl; -} -int main(void) -{ - - vs v1 {3, 4, 7, 8}; - v1 += 3; - print(v1); - - - return 0; -} diff --git a/source/hyporo/hyplib/vector/vector.hpp b/source/hyporo/hyplib/vector/vector.hpp deleted file mode 100644 index bcfd27f..0000000 --- a/source/hyporo/hyplib/vector/vector.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "../scalar/scalar.hpp" -#include "vector_space.hpp" - - -namespace hpr -{ - -template -using vec = VectorSpace; - -using vec2 = VectorSpace; -using vec3 = VectorSpace; -using vec4 = VectorSpace; - -} diff --git a/source/hyporo/io/CMakeLists.txt b/source/hyporo/io/CMakeLists.txt deleted file mode 100644 index e69de29..0000000 diff --git a/source/hyporo/window_system/CMakeLists.txt b/source/hyporo/window_system/CMakeLists.txt deleted file mode 100644 index dd42056..0000000 --- a/source/hyporo/window_system/CMakeLists.txt +++ /dev/null @@ -1,28 +0,0 @@ - -include_directories( - ../hyplib -) - -add_library(hyporo-window-system STATIC - # Source files - window_system.cpp - monitor.cpp - window.cpp - glfw/window_system.cpp - glfw/monitor.cpp - glfw/window.cpp - - # Header files - monitor.hpp - window_context.hpp - window.hpp - window_system.hpp - glfw/monitor.hpp - glfw/window.hpp - glfw/window_system.hpp -) - -target_link_libraries(hyporo-window-system - glfw - hyporo-hyplib -) \ No newline at end of file