Compare commits
10
Commits
fa60274540
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de7d435ccb | ||
|
|
97aed4e5ca | ||
|
|
416446206f | ||
|
|
c6ab0327c1 | ||
|
|
e6a5c9e805 | ||
|
|
b9e8144519 | ||
|
|
06f261b793 | ||
|
|
e76ef32c45 | ||
|
|
f49650744a | ||
|
|
ab56a2cc95 |
+5
-2
@@ -1,8 +1,11 @@
|
|||||||
build/
|
build/
|
||||||
.vscode/
|
.*
|
||||||
|
!.gitignore
|
||||||
|
!.clangd
|
||||||
CMakeFiles/
|
CMakeFiles/
|
||||||
|
CMakeKits.json
|
||||||
CMakeCache.txt
|
CMakeCache.txt
|
||||||
docs/*
|
docs/*
|
||||||
!docs/CMake*
|
!docs/CMake*
|
||||||
!docs/Doxyfile.in
|
!docs/Doxyfile.in
|
||||||
!.clangd
|
|
||||||
|
|||||||
+19
-32
@@ -28,56 +28,43 @@ function(toAbsolutePath SETVAR PATHS)
|
|||||||
set(${SETVAR} ${RET_VAL} PARENT_SCOPE)
|
set(${SETVAR} ${RET_VAL} PARENT_SCOPE)
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
|
if (NOT CMAKE_BUILD_TYPE)
|
||||||
|
set(CMAKE_BUILD_TYPE "Debug")
|
||||||
|
endif()
|
||||||
|
|
||||||
if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||||
message(STATUS "Targeting Release build")
|
add_compile_definitions(_NB_BUILD_RELEASE)
|
||||||
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
message(STATUS "Targeting Debug build")
|
|
||||||
set(NB_DEBUG_BUILD ON)
|
set(NB_DEBUG_BUILD ON)
|
||||||
add_compile_definitions(_NB_BUILD_DEBUG)
|
add_compile_definitions(_NB_BUILD_DEBUG)
|
||||||
endif()
|
endif()
|
||||||
|
message(STATUS "Targeting ${CMAKE_BUILD_TYPE} build")
|
||||||
|
|
||||||
|
if (NB_DEBUG_BUILD)
|
||||||
|
set(NB_LOGGING ON)
|
||||||
|
set(NB_BUILD_TESTS ON)
|
||||||
|
set(NB_BUILD_DOCS ON)
|
||||||
|
set(NB_MONITOR_OPENGL_CALLS ON)
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
|
||||||
|
endif()
|
||||||
|
|
||||||
cmake_dependent_option(NB_LOGGING
|
|
||||||
"Creates a default logger and automatically logs with code locations."
|
|
||||||
ON
|
|
||||||
NB_DEBUG_BUILD
|
|
||||||
OFF
|
|
||||||
)
|
|
||||||
cmake_dependent_option(NB_BUILD_TESTS
|
|
||||||
"Build unit tests"
|
|
||||||
ON
|
|
||||||
NB_DEBUG_BUILD
|
|
||||||
OFF
|
|
||||||
)
|
|
||||||
cmake_dependent_option(NB_BUILD_DOCS
|
|
||||||
"Build documentation"
|
|
||||||
ON
|
|
||||||
NB_DEBUG_BUILD
|
|
||||||
OFF
|
|
||||||
)
|
|
||||||
cmake_dependent_option(NBENGINE_INSTALL
|
cmake_dependent_option(NBENGINE_INSTALL
|
||||||
"Install NBEngine"
|
"Install NBEngine"
|
||||||
ON
|
ON
|
||||||
NB_DEBUG_BUILD
|
NB_DEBUG_BUILD
|
||||||
OFF
|
OFF
|
||||||
)
|
)
|
||||||
cmake_dependent_option(NB_MONITOR_OPENGL_CALLS
|
|
||||||
"Monitors every OpenGL call for errors"
|
|
||||||
ON
|
|
||||||
NB_DEBUG_BUILD
|
|
||||||
OFF
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
|
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
|
||||||
message(STATUS "Building for Windows")
|
message(STATUS "Targeting Windows")
|
||||||
set(NB_TARGET_WINDOWS ON)
|
set(NB_TARGET_WINDOWS ON)
|
||||||
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
|
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
|
||||||
message(STATUS "Building for Linux")
|
message(STATUS "Targeting Linux")
|
||||||
set(NB_TARGET_LINUX ON)
|
set(NB_TARGET_LINUX ON)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(NB_BUILD_TESTS)
|
if(NB_BUILD_TESTS)
|
||||||
message(STATUS "Building tests")
|
message(STATUS "Targeting tests")
|
||||||
enable_testing()
|
enable_testing()
|
||||||
include(FetchContent)
|
include(FetchContent)
|
||||||
FetchContent_Declare(
|
FetchContent_Declare(
|
||||||
@@ -106,13 +93,13 @@ endif()
|
|||||||
|
|
||||||
get_filename_component(NBENGINE_ROOT ${CMAKE_CURRENT_LIST_DIR} ABSOLUTE)
|
get_filename_component(NBENGINE_ROOT ${CMAKE_CURRENT_LIST_DIR} ABSOLUTE)
|
||||||
|
|
||||||
add_subdirectory(./engine ${PROJECT_BINARY_DIR}/${CMAKE_BUILD_TYPE})
|
add_subdirectory(./engine)
|
||||||
|
|
||||||
# Toggle
|
# Toggle
|
||||||
set(NB_REBUILD_DOCS)
|
set(NB_REBUILD_DOCS)
|
||||||
|
|
||||||
if (NB_BUILD_DOCS)
|
if (NB_BUILD_DOCS)
|
||||||
add_subdirectory(./docs)
|
add_subdirectory(./docs)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
include(CMakePackageConfigHelpers)
|
include(CMakePackageConfigHelpers)
|
||||||
|
|||||||
+15
-28
@@ -1,30 +1,17 @@
|
|||||||
add_subdirectory(./NBCore)
|
set(NBENGINE_MODULES
|
||||||
|
NBCore
|
||||||
|
NBGraphics
|
||||||
|
NBData
|
||||||
|
)
|
||||||
|
|
||||||
get_target_property(NBCORE_INTERFACE_INCLUDES NBCore INTERFACE_INCLUDE_DIRECTORIES)
|
set(NB_INCLUDE_FILES "")
|
||||||
include_directories(${NBCORE_INTERFACE_INCLUDES})
|
set(NB_SOURCE_FILES "")
|
||||||
|
foreach(module ${NBENGINE_MODULES})
|
||||||
|
add_subdirectory("./${module}")
|
||||||
|
get_target_property(MODULE_INCLUDES ${module} INTERFACE_INCLUDE_DIRECTORIES)
|
||||||
|
list(APPEND NB_INCLUDE_FILES ${MODULE_INCLUDES})
|
||||||
|
list(APPEND NB_SOURCE_FILES ${MODULE_INCLUDES})
|
||||||
|
endforeach()
|
||||||
|
set(NB_INCLUDE_FILES ${NB_INCLUDE_FILES} PARENT_SCOPE)
|
||||||
|
set(NB_SOURCE_FILES ${NB_SOURCE_FILES} PARENT_SCOPE)
|
||||||
|
|
||||||
#add_subdirectory(./NBEvents)
|
|
||||||
add_subdirectory(./NBGraphics)
|
|
||||||
add_subdirectory(./NBData)
|
|
||||||
|
|
||||||
if (NB_CORE_SOURCE OR NB_EVENTS_SOURCE OR NB_GRAPHICS_SOURCE OR NB_DATA_SOURCE)
|
|
||||||
set(NB_SOURCE_FILES "")
|
|
||||||
list(APPEND NB_SOURCE_FILES
|
|
||||||
${NB_CORE_SOURCE}
|
|
||||||
${NB_GRAPHICS_SOURCE}
|
|
||||||
${NB_EVENTS_SOURCE}
|
|
||||||
${NB_DATA_SOURCE}
|
|
||||||
)
|
|
||||||
set(NB_SOURCE_FILES ${NB_SOURCE_FILES} PARENT_SCOPE)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if (NB_CORE_INCLUDE OR NB_EVENTS_INCLUDE OR NB_GRAPHICS_INCLUDE OR NB_DATA_INCLUDE)
|
|
||||||
set(NB_INCLUDE_FILES "")
|
|
||||||
list(APPEND NB_INCLUDE_FILES
|
|
||||||
${NB_CORE_INCLUDE}
|
|
||||||
${NB_GRAPHICS_INCLUDE}
|
|
||||||
${NB_EVENTS_INCLUDE}
|
|
||||||
${NB_DATA_INCLUDE}
|
|
||||||
)
|
|
||||||
set(NB_INCLUDE_FILES ${NB_INCLUDE_FILES} PARENT_SCOPE)
|
|
||||||
endif()
|
|
||||||
|
|||||||
@@ -8,17 +8,11 @@ set(CMAKE_CXX_EXTENSIONS OFF)
|
|||||||
include(GNUInstallDirs)
|
include(GNUInstallDirs)
|
||||||
include(CMakePackageConfigHelpers)
|
include(CMakePackageConfigHelpers)
|
||||||
|
|
||||||
toAbsolutePath(NB_CORE_SOURCE
|
|
||||||
./src/ErrorsImpl.cpp
|
|
||||||
./src/Logger.cpp
|
|
||||||
./src/Processes.cpp
|
|
||||||
./src/StringUtils.cpp
|
|
||||||
./src/Utils.cpp
|
|
||||||
)
|
|
||||||
toAbsolutePath(NB_CORE_INCLUDE
|
toAbsolutePath(NB_CORE_INCLUDE
|
||||||
./include/NBCore/ANSITerm.hpp
|
./include/NBCore/Arrays.hpp
|
||||||
|
./include/NBCore/CoreTypes.hpp
|
||||||
./include/NBCore/DataSink.hpp
|
./include/NBCore/DataSink.hpp
|
||||||
./include/NBCore/Errors.hpp
|
./include/NBCore/Errors.hpp
|
||||||
./include/NBCore/ErrorsImpl.hpp
|
./include/NBCore/ErrorsImpl.hpp
|
||||||
./include/NBCore/Logger.hpp
|
./include/NBCore/Logger.hpp
|
||||||
./include/NBCore/Printer.hpp
|
./include/NBCore/Printer.hpp
|
||||||
@@ -30,10 +24,14 @@ toAbsolutePath(NB_CORE_INCLUDE
|
|||||||
./include/NBCore/Utils.hpp
|
./include/NBCore/Utils.hpp
|
||||||
)
|
)
|
||||||
|
|
||||||
set(NB_CORE_SOURCE ${NB_CORE_SOURCE} PARENT_SCOPE)
|
add_library(NBCore
|
||||||
set(NB_CORE_INCLUDE ${NB_CORE_INCLUDE} PARENT_SCOPE)
|
./src/ErrorsImpl.cpp
|
||||||
|
./src/Logger.cpp
|
||||||
add_library(NBCore ${NB_CORE_SOURCE})
|
./src/Processes.cpp
|
||||||
|
./src/StringUtils.cpp
|
||||||
|
./src/TypeTraits.cpp
|
||||||
|
./src/Utils.cpp
|
||||||
|
)
|
||||||
add_library(NBEngine::Core ALIAS NBCore)
|
add_library(NBEngine::Core ALIAS NBCore)
|
||||||
target_include_directories(NBCore
|
target_include_directories(NBCore
|
||||||
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
|
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
|
||||||
|
|||||||
@@ -1,214 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_ANSI_TERM
|
|
||||||
#define _NB_ANSI_TERM
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
/*
|
|
||||||
----------- TECH DEBT ------------
|
|
||||||
Idk wtf to do here. This was originally to allow me to print
|
|
||||||
to terminal easily, but it's becoming too much of an investment.
|
|
||||||
For the time being, drop it.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
template <typename ST, typename U>
|
|
||||||
struct SmartText {
|
|
||||||
using Type = ST;
|
|
||||||
using ValType = U;
|
|
||||||
static constexpr bool HasChild = Type::HasChild;
|
|
||||||
|
|
||||||
SmartText() = default;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename Type, typename U>
|
|
||||||
struct StyleFormatter : SmartText<StyleFormatter<Type, U>, U> {
|
|
||||||
using Base = SmartText<StyleFormatter<Type, U>, U>;
|
|
||||||
using typename Base::ValType;
|
|
||||||
static constexpr bool HasChild = true;
|
|
||||||
|
|
||||||
StyleFormatter(const std::string& _fmt, const ValType& _val, const std::string& _closing) :
|
|
||||||
fmt{_fmt}, val{_val}, closing{_closing} {}
|
|
||||||
|
|
||||||
std::string fmt;
|
|
||||||
ValType val;
|
|
||||||
std::string closing;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename Child>
|
|
||||||
struct StyleFormatter<Child, void> : SmartText<StyleFormatter<Child, void>, void>{
|
|
||||||
using Base = SmartText<StyleFormatter<Child, void>, void>;
|
|
||||||
using Base::ValType;
|
|
||||||
static constexpr bool HasChild = false;
|
|
||||||
|
|
||||||
StyleFormatter(const std::string& _fmt) : fmt{_fmt} {}
|
|
||||||
|
|
||||||
std::string fmt;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename U=void>
|
|
||||||
struct ANSITextColor;
|
|
||||||
|
|
||||||
template <typename U>
|
|
||||||
struct ANSITextColor : StyleFormatter<ANSITextColor<U>, U> {
|
|
||||||
using Base = StyleFormatter<ANSITextColor, U>;
|
|
||||||
using typename Base::ValType;
|
|
||||||
using Base::HasChild;
|
|
||||||
|
|
||||||
template <typename = typename std::enable_if<HasChild, U>::type>
|
|
||||||
ANSITextColor(const U& _val) : Base{"\x1b[39m", _val, "\x1b[39m"} {}
|
|
||||||
template <typename = typename std::enable_if<!HasChild, U>>
|
|
||||||
ANSITextColor() : Base{"\x1b[92m"} {}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct ANSITextColor<std::string> : StyleFormatter<ANSITextColor<std::string>, std::string> {
|
|
||||||
using Base = StyleFormatter<ANSITextColor<std::string>, std::string>;
|
|
||||||
static constexpr bool HasChild = Base::HasChild;
|
|
||||||
|
|
||||||
template <std::size_t N>
|
|
||||||
ANSITextColor(char const(&_val) [N]) : Base{"\x1b[92m", _val, "\x1b[39m"} {}
|
|
||||||
ANSITextColor(const char* _val) : Base{"\x1b[92m", _val, "\x1b[39m"} {}
|
|
||||||
ANSITextColor(const std::string& _val) : Base{"\x1b[92m", _val, "\x1b[39m"} {}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
|
|
||||||
template<typename Type, typename U>
|
|
||||||
std::ostream& operator<<(std::ostream& stream, const nb::StyleFormatter<Type, U>& msg) {
|
|
||||||
if (&stream == &std::cout) {
|
|
||||||
stream << msg.fmt;
|
|
||||||
if (msg.HasChild) {
|
|
||||||
stream << msg.val << msg.closing;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (msg.HasChild) { stream << msg.val; }
|
|
||||||
}
|
|
||||||
stream << std::flush;
|
|
||||||
return stream;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* struct CellBase {
|
|
||||||
uint8_t width;
|
|
||||||
uint8_t height;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
struct Cell : SmartText<Cell<T>, T> {
|
|
||||||
using Base = SmartText<Cell<T>, T>;
|
|
||||||
using typename Base::ValType;
|
|
||||||
using Base::HasChild;
|
|
||||||
|
|
||||||
Cell(const ValType& _val, const uint8_t& w, const uint8_t& h=1)
|
|
||||||
: val(_val), width(w), height(h) {}
|
|
||||||
|
|
||||||
ValType val;
|
|
||||||
uint8_t width;
|
|
||||||
|
|
||||||
uint8_t height;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct Cell<std::string> : SmartText<Cell<std::string>, std::string> {
|
|
||||||
using Base = SmartText<Cell<std::string>, std::string>;
|
|
||||||
using typename Base::ValType;
|
|
||||||
using Base::HasChild;
|
|
||||||
|
|
||||||
template <std::size_t N>
|
|
||||||
Cell( const(&_val) [N], const uint8_t& w, const uint8_t& h=0) : Cell(std::string(_val), w, h) {}
|
|
||||||
Cell(char* _val, const uint8_t& w, const uint8_t& h=0) : Cell(std::string(_val), w, h) {}
|
|
||||||
Cell(const ValType& _val, const uint8_t& w, const uint8_t& h=0)
|
|
||||||
: val(_val), width(w) {
|
|
||||||
unsigned int strsize = _val.size();
|
|
||||||
height = strsize / width;
|
|
||||||
height += (strsize % width) ? 1 : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
ValType val;
|
|
||||||
uint8_t width;
|
|
||||||
|
|
||||||
uint8_t height;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
std::ostream& operator<<(std::ostream& stream, const Cell<T>& cll) {
|
|
||||||
if (&stream == &std::cout) {
|
|
||||||
stream << "\x1b[s" << cll.val << "\x1b[u\x1b[" << 1+cll.width << "C\x1b[" << 1+cll.height;
|
|
||||||
}
|
|
||||||
return stream;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void ChangeCellHeight(int i, Cell<T>& cll, const unsigned int& h) {
|
|
||||||
if (h < cll.height) { cll.height=h; }
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void GetTallestCell(int i, Cell<T>& cll, unsigned int& h) {
|
|
||||||
if (cll.height > h) { h = cll.height; }
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename... Pack>
|
|
||||||
struct TableRow {
|
|
||||||
using RowCells = std::tuple<Cell<Pack>...>;
|
|
||||||
using CellWidths = std::array<int, sizeof...(Pack)>;
|
|
||||||
using DelimiterArray = std::array<std::string, sizeof...(Pack)+1>;
|
|
||||||
|
|
||||||
TableRow(CellWidths&& widths_, DelimiterArray&& delims_, const Pack&... vals_)
|
|
||||||
: widths(widths_), delimiters(delims_), topBorder("") {
|
|
||||||
nb::ForEach(_cells, GetTallestCell, _height);
|
|
||||||
}
|
|
||||||
|
|
||||||
static constexpr int num_columns = sizeof...(Pack);
|
|
||||||
|
|
||||||
int height() const { return height; }
|
|
||||||
inline int height(const unsigned int newh) {
|
|
||||||
nb::ForEach(_cells, ChangeCellHeight, newh);
|
|
||||||
return (_height=newh);
|
|
||||||
}
|
|
||||||
|
|
||||||
const CellWidths widths;
|
|
||||||
const DelimiterArray delimiters;
|
|
||||||
const std::string topBorder;
|
|
||||||
const std::string bottomBorder;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
RowCells _cells;
|
|
||||||
int _height;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename... Pack>
|
|
||||||
std::ostream& operator<<(std::ostream& stream, const TableRow<Pack...>& row) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Pack>
|
|
||||||
class Table {
|
|
||||||
public:
|
|
||||||
Table(const Pack&... _vals) : row(std::array<int, sizeof...(Pack)>(10),
|
|
||||||
std::array<std::string, sizeof...(Pack)>(""), _vals...) {}
|
|
||||||
private:
|
|
||||||
TableRow<Pack...> row;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
|
|
||||||
#ifdef _NB_TARGET_WINDOWS
|
|
||||||
template<typename... Types>
|
|
||||||
std::ostream& operator<<(std::ostream& stream, const nb::TableRow<Types...>& row) {
|
|
||||||
using PosType = decltype(stream.tellp());
|
|
||||||
constexpr int num_cells = sizeof...(Types);
|
|
||||||
|
|
||||||
int col_num = 0;
|
|
||||||
int col_start = 0;
|
|
||||||
|
|
||||||
return stream;
|
|
||||||
}
|
|
||||||
#endif // _NB_TARGET_WINDOWS */
|
|
||||||
|
|
||||||
#endif // _NB_ANSI_TERM
|
|
||||||
@@ -0,0 +1,135 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _NB_DATA_TYPES
|
||||||
|
#define _NB_DATA_TYPES
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <array>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#include <NBCore/Errors.hpp>
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @brief A multidimensional array template class
|
||||||
|
*
|
||||||
|
* */
|
||||||
|
template<typename T, unsigned int N>
|
||||||
|
class NDArray {
|
||||||
|
public:
|
||||||
|
using Coordinate = std::array<unsigned int, N>;
|
||||||
|
|
||||||
|
private:
|
||||||
|
template<unsigned int M>
|
||||||
|
static Coordinate ProjectDimensions(const std::array<unsigned int, M>& dims) {
|
||||||
|
Coordinate p;
|
||||||
|
unsigned int i=0;
|
||||||
|
if constexpr (M > N) {
|
||||||
|
std::for_each(dims.begin(), dims.end(), [&](unsigned int x){
|
||||||
|
if (i < N) {
|
||||||
|
p[i] = x;
|
||||||
|
} else {
|
||||||
|
if (x != 1) {
|
||||||
|
THROW(Error(Error<>::VALUE_ERROR,
|
||||||
|
"Higher dimensions must have a size of 1"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
});
|
||||||
|
} else if constexpr (M < N) {
|
||||||
|
p.fill(1);
|
||||||
|
std::for_each(dims.begin(), dims.end(), [&](unsigned int x) {p[i] = x; i++;});
|
||||||
|
} else { p = dims; }
|
||||||
|
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
T* _data;
|
||||||
|
static unsigned int CalculateTotalSize(const std::array<unsigned int, N>& dims) {
|
||||||
|
unsigned int ret = 1;
|
||||||
|
std::for_each(dims.begin(), dims.end(), [&](unsigned int n){
|
||||||
|
ret *= n;
|
||||||
|
});
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
template<typename U, unsigned int M>
|
||||||
|
friend class NDArray;
|
||||||
|
|
||||||
|
static NDArray&& MemoryToNDArray(const Coordinate& dimensions_, T* data_) {
|
||||||
|
auto ret = NDArray(dimensions_);
|
||||||
|
ret._data = data_;
|
||||||
|
return std::move(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
const Coordinate dimensions;
|
||||||
|
const unsigned int totalSize;
|
||||||
|
NDArray(const Coordinate& dimensions_, T* data_=nullptr) :
|
||||||
|
dimensions(dimensions_),
|
||||||
|
totalSize(CalculateTotalSize(dimensions_))
|
||||||
|
{
|
||||||
|
if (data_) { data(data_); }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
NDArray(NDArray&& arr) :
|
||||||
|
dimensions(arr.dimensions),
|
||||||
|
totalSize(arr.totalSize),
|
||||||
|
_data(std::move(arr._data)) {}
|
||||||
|
|
||||||
|
NDArray(const NDArray& arr)=delete;
|
||||||
|
|
||||||
|
template<unsigned int M>
|
||||||
|
NDArray(NDArray<T, M>&& arr) :
|
||||||
|
dimensions(ProjectDimensions<M>(arr.dimensions)),
|
||||||
|
totalSize(arr.totalSize),
|
||||||
|
_data(std::move(arr._data)){}
|
||||||
|
|
||||||
|
NDArray& operator=(const NDArray& arr) = delete;
|
||||||
|
NDArray& operator=(NDArray&& arr) = delete;
|
||||||
|
~NDArray() {
|
||||||
|
if (_data) { delete[] _data; }
|
||||||
|
};
|
||||||
|
|
||||||
|
unsigned int coordinateToIndex(const Coordinate& coord) const {
|
||||||
|
unsigned int blockSize = 1;
|
||||||
|
unsigned int ret = 0;
|
||||||
|
for (auto i = 0; i < dimensions.size(); ++i) {
|
||||||
|
ret += coord[i] * blockSize;
|
||||||
|
blockSize *= dimensions[i];
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline NDArray& copy() const {
|
||||||
|
return NDArray(dimensions, _data);
|
||||||
|
}
|
||||||
|
inline const T& at(const Coordinate& coord) const {
|
||||||
|
return operator[](coord);
|
||||||
|
}
|
||||||
|
inline const T* data() const { return _data; }
|
||||||
|
inline const T& operator[](const Coordinate& coord) const {
|
||||||
|
return _data[coordinateToIndex(coord)];
|
||||||
|
}
|
||||||
|
|
||||||
|
inline T& at(const Coordinate& coord) {
|
||||||
|
return _data[coordinateToIndex(coord)];
|
||||||
|
}
|
||||||
|
inline T* data(const T* data_) {
|
||||||
|
if (!_data) { _data = new T[totalSize]; }
|
||||||
|
std::memcpy(_data, data_, totalSize * sizeof(T));
|
||||||
|
return _data;
|
||||||
|
}
|
||||||
|
inline T& operator[](const Coordinate& coord) {
|
||||||
|
return _data[coordinateToIndex(coord)];
|
||||||
|
}
|
||||||
|
|
||||||
|
}; // class NDArray<type, int>
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
|
#endif // _NB_DATA_TYPES
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _NB_CORE_TYPES
|
||||||
|
#define _NB_CORE_TYPES
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
template<typename T>
|
||||||
|
using SharedVector = std::vector<std::shared_ptr<T>>;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
using RValueVector = std::vector<T&&>;
|
||||||
|
|
||||||
|
using ByteVector = std::vector<uint8_t>;
|
||||||
|
|
||||||
|
template<typename A, typename B>
|
||||||
|
class ConstantMap : public std::unordered_map<A, B> {
|
||||||
|
using Base = std::unordered_map<A, B>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
using Base::at;
|
||||||
|
const B& operator[](const A& key) const {
|
||||||
|
return at(key);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
enum TypeCodes {
|
||||||
|
UINT8, UINT16, UINT32, UINT64, INT8, INT16, INT32, INT64,
|
||||||
|
FLOAT16, FLOAT32, DOUBLE64, BOOL, NONE
|
||||||
|
};
|
||||||
|
|
||||||
|
}; // namespace nb
|
||||||
|
|
||||||
|
#endif // _NB_NB_CORE_TYPES
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
#include <NBCore/StringUtils.hpp>
|
#include <NBCore/StringUtils.hpp>
|
||||||
#include <NBCore/Utils.hpp>
|
#include <NBCore/CoreTypes.hpp>
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
|
|||||||
@@ -2,9 +2,10 @@
|
|||||||
#ifndef _NB_TYPE_TRAITS
|
#ifndef _NB_TYPE_TRAITS
|
||||||
#define _NB_TYPE_TRAITS
|
#define _NB_TYPE_TRAITS
|
||||||
|
|
||||||
#include <tuple>
|
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <utility>
|
||||||
|
|
||||||
|
#include <NBCore/CoreTypes.hpp>
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
@@ -31,6 +32,12 @@ namespace detail {
|
|||||||
};
|
};
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
|
template<typename K, typename V>
|
||||||
|
std::pair<K, V> KeyVal(K key, V val) { return std::pair(key, val); }
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static constexpr TypeCodes GetTypeEnum = NONE;
|
||||||
|
|
||||||
struct NoneType{};
|
struct NoneType{};
|
||||||
|
|
||||||
template<typename T, unsigned int Arg>
|
template<typename T, unsigned int Arg>
|
||||||
@@ -90,37 +97,6 @@ struct RunAndOutput {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename A, typename B>
|
|
||||||
using SubtractType = decltype(std::declval<A>() - std::declval<B>());
|
|
||||||
|
|
||||||
template<typename A, typename B>
|
|
||||||
using MultiplyType = decltype(std::declval<A>() * std::declval<B>());
|
|
||||||
|
|
||||||
template<typename A, typename B>
|
|
||||||
using AdditionType = decltype(std::declval<A>() + std::declval<B>());
|
|
||||||
|
|
||||||
template<typename Func, std::size_t N=0, typename... Pack>
|
|
||||||
inline typename std::enable_if<N==sizeof...(Pack), void>::type
|
|
||||||
ForEach(std::tuple<Pack...>&, Func) {}
|
|
||||||
|
|
||||||
template<typename Func, std::size_t N=0, typename... Pack>
|
|
||||||
inline typename std::enable_if<N < sizeof...(Pack), void>::type
|
|
||||||
ForEach(std::tuple<Pack...>& tup, Func f) {
|
|
||||||
f(N, std::get<N>(tup));
|
|
||||||
ForEach<N+1, Func, Pack...>(tup, f);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<std::size_t N=0, typename Func, typename Args, typename... Pack>
|
|
||||||
inline typename std::enable_if<N==sizeof...(Pack), void>::type
|
|
||||||
ForEach(std::tuple<Pack...>&, Func, const Args&) {}
|
|
||||||
|
|
||||||
template<std::size_t N=0, typename Func, typename Args, typename... Pack>
|
|
||||||
inline typename std::enable_if<N < sizeof...(Pack), void>::type
|
|
||||||
ForEach(std::tuple<Pack...>& tup, Func f, const Args& arg) {
|
|
||||||
f(N, std::get<N>(tup), arg);
|
|
||||||
ForEach<N+1, Func, Args, Pack...>(tup, f, arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, typename... Pack>
|
template<typename T, typename... Pack>
|
||||||
struct Find;
|
struct Find;
|
||||||
|
|
||||||
@@ -156,48 +132,6 @@ struct Find<T, U> {
|
|||||||
>::value;
|
>::value;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T, typename Pack1, typename... Pack>
|
|
||||||
struct RefPackToPtrVec {
|
|
||||||
RefPackToPtrVec(std::vector<T*> _vec, Pack1& p1, Pack&... packs)
|
|
||||||
: vec(_vec) {
|
|
||||||
static_assert(std::is_base_of<T, Pack1>::value);
|
|
||||||
vec.push_back(&p1);
|
|
||||||
vec = RefPackToPtrVec<T, Pack...>(vec, packs...).vec;
|
|
||||||
}
|
|
||||||
|
|
||||||
RefPackToPtrVec(Pack1& p1, Pack&... packs)
|
|
||||||
: RefPackToPtrVec({}, p1, packs...) {}
|
|
||||||
|
|
||||||
std::vector<T*> vec;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T, typename Pack>
|
|
||||||
struct RefPackToPtrVec<T, Pack> {
|
|
||||||
RefPackToPtrVec(std::vector<T*> _vec, Pack& p1)
|
|
||||||
: vec(_vec) {
|
|
||||||
static_assert(std::is_base_of<T, Pack>::value);
|
|
||||||
vec.push_back(&p1);
|
|
||||||
}
|
|
||||||
|
|
||||||
RefPackToPtrVec(Pack& p1)
|
|
||||||
: RefPackToPtrVec({}, p1) {}
|
|
||||||
|
|
||||||
std::vector<T*> vec;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T, typename... Pack>
|
|
||||||
struct PackIsSameType;
|
|
||||||
|
|
||||||
template <typename T, typename PackType>
|
|
||||||
struct PackIsSameType<T, PackType> {
|
|
||||||
const bool value = std::is_base_of<T, PackType>::value;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T, typename FirstPack, typename... Pack>
|
|
||||||
struct PackIsSameType<T, FirstPack, Pack...> {
|
|
||||||
const bool value = std::is_base_of<T, FirstPack>::value && PackIsSameType<T, Pack...>::value;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
|
|
||||||
#endif // _NB_TYPE_TRAITS
|
#endif // _NB_TYPE_TRAITS
|
||||||
@@ -1,36 +1,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#ifndef _NB_CORE_TYPES
|
#ifndef _NB_UTILTIES
|
||||||
#define _NB_CORE_TYPES
|
#define _NB_UTILTIES
|
||||||
|
|
||||||
#include <exception>
|
|
||||||
#include <memory>
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
// #include <NBCore/Errors.hpp>
|
#include <NBCore/CoreTypes.hpp>
|
||||||
|
#include <NBCore/Errors.hpp>
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
template<typename A, typename B>
|
|
||||||
class ConstantMap : public std::unordered_map<A, B> {
|
|
||||||
using Base = std::unordered_map<A, B>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
|
||||||
using Base::at;
|
|
||||||
const B& operator[](const A& key) const {
|
|
||||||
return at(key);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
using SharedVector = std::vector<std::shared_ptr<T>>;
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
using RValueVector = std::vector<T&&>;
|
|
||||||
|
|
||||||
using ByteVector = std::vector<uint8_t>;
|
|
||||||
|
|
||||||
/* class ObjectManagerError : public Error<ObjectManagerError> {
|
/* class ObjectManagerError : public Error<ObjectManagerError> {
|
||||||
using Base = Error<ObjectManagerError>;
|
using Base = Error<ObjectManagerError>;
|
||||||
|
|
||||||
@@ -68,11 +46,10 @@ ByteVector concatVectorBytes(const std::vector<T>& vec1, const std::vector<S>& v
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
std::vector<T> bytesToVector(const ByteVector& vec) {
|
std::vector<T> bytesToVector(const ByteVector& vec) {
|
||||||
if (vec.size() % sizeof(T) != 0) {
|
if (vec.size() % sizeof(T) != 0) {
|
||||||
/* THROW(Error<NoneType>(
|
THROW(Error(
|
||||||
std::runtime_error("Data size does not align to std::vector<" + std::string(typeid(T).name()) + ">.")
|
std::runtime_error("Data size does not align to std::vector<" + std::string(typeid(T).name()) + ">.")
|
||||||
)); */
|
));
|
||||||
throw "byyyyeeee";
|
}
|
||||||
}
|
|
||||||
unsigned int num_elmts = vec.size() / sizeof(T);
|
unsigned int num_elmts = vec.size() / sizeof(T);
|
||||||
std::vector<T> ret(num_elmts);
|
std::vector<T> ret(num_elmts);
|
||||||
memcpy(ret.data(), vec.data(), vec.size());
|
memcpy(ret.data(), vec.data(), vec.size());
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#include <NBCore/TypeTraits.hpp>
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
template<> constexpr TypeCodes GetTypeEnum<unsigned char> = TypeCodes::UINT8;
|
||||||
|
template<> constexpr TypeCodes GetTypeEnum<unsigned short> = TypeCodes::UINT16;
|
||||||
|
template<> constexpr TypeCodes GetTypeEnum<unsigned int> = TypeCodes::UINT32;
|
||||||
|
template<> constexpr TypeCodes GetTypeEnum<unsigned long int> = TypeCodes::UINT64;
|
||||||
|
template<> constexpr TypeCodes GetTypeEnum<char> = TypeCodes::INT8;
|
||||||
|
template<> constexpr TypeCodes GetTypeEnum<short> = TypeCodes::INT16;
|
||||||
|
template<> constexpr TypeCodes GetTypeEnum<int> = TypeCodes::INT32;
|
||||||
|
template<> constexpr TypeCodes GetTypeEnum<long> = TypeCodes::INT64;
|
||||||
|
template<> constexpr TypeCodes GetTypeEnum<float> = TypeCodes::FLOAT32;
|
||||||
|
template<> constexpr TypeCodes GetTypeEnum<double> = TypeCodes::DOUBLE64;
|
||||||
|
template<> constexpr TypeCodes GetTypeEnum<bool> = TypeCodes::BOOL;
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
@@ -1,24 +1,24 @@
|
|||||||
cmake_minimum_required(VERSION 3.26.0)
|
cmake_minimum_required(VERSION 3.26.0)
|
||||||
|
|
||||||
if (NB_BUILD_TESTS)
|
enable_testing()
|
||||||
enable_testing()
|
include(GoogleTest)
|
||||||
include(GoogleTest)
|
add_executable(TestCore
|
||||||
add_executable(TestCore
|
./testArrays.cpp
|
||||||
./testErrors.cpp
|
./testErrors.cpp
|
||||||
#./testProcesses.cpp
|
#./testProcesses.cpp
|
||||||
./testUtils.cpp
|
./testUtils.cpp
|
||||||
)
|
)
|
||||||
target_link_libraries(TestCore
|
target_link_libraries(TestCore
|
||||||
NBCore
|
NBCore
|
||||||
GTest::gtest_main
|
GTest::gtest_main
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(LoggerTest
|
add_executable(LoggerTest
|
||||||
./testLogger.cpp
|
./testLogger.cpp
|
||||||
)
|
)
|
||||||
target_link_libraries(LoggerTest
|
target_link_libraries(LoggerTest
|
||||||
NBCore
|
NBCore
|
||||||
)
|
)
|
||||||
|
|
||||||
|
gtest_discover_tests(TestCore)
|
||||||
|
|
||||||
gtest_discover_tests(TestCore)
|
|
||||||
endif()
|
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
#define CODE_ERROR_LOCATIONS
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <NBCore/Arrays.hpp>
|
||||||
|
|
||||||
|
template<unsigned int N>
|
||||||
|
using Coord = typename nb::NDArray<uint8_t, N>::Coordinate;
|
||||||
|
|
||||||
|
template<typename A, typename B>
|
||||||
|
void ASSERT_ARRAY_EQ(A a, B b) {
|
||||||
|
unsigned int n = a.size();
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
ASSERT_EQ(a[i], b[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(TestNDArray, TestBasic) {
|
||||||
|
float* data_f = new float[9] {0,1,2,3,4,5,6,7,8};
|
||||||
|
nb::NDArray<float, 2> arr({3, 3}, data_f);
|
||||||
|
Coord<2> x{1,1};
|
||||||
|
ASSERT_ARRAY_EQ(arr.dimensions, Coord<2>{3,3});
|
||||||
|
ASSERT_FLOAT_EQ(data_f[4], arr.at(x));
|
||||||
|
arr.at(x) = 0.5;
|
||||||
|
ASSERT_FLOAT_EQ(0.5, arr.at(x));
|
||||||
|
delete[] data_f;
|
||||||
|
|
||||||
|
Coord<3> y{1,0,1};
|
||||||
|
unsigned int* data_ui = new unsigned int[8] {0,1,2,3,4,5,6,7};
|
||||||
|
nb::NDArray<unsigned int, 3> arr3d({2,2,2}, data_ui);
|
||||||
|
ASSERT_ARRAY_EQ(arr3d.dimensions, Coord<3>{2,2,2});
|
||||||
|
ASSERT_EQ(data_ui[5], arr3d.at(y));
|
||||||
|
arr3d[{0,1,1}] = 23;
|
||||||
|
ASSERT_EQ(23, arr3d.at({0,1,1}));
|
||||||
|
delete[] data_ui;
|
||||||
|
|
||||||
|
nb::NDArray<uint8_t, 4> arr4d({4,4,4,4});
|
||||||
|
arr4d[{0,1,2,3}] = 57;
|
||||||
|
ASSERT_ARRAY_EQ(arr4d.dimensions, Coord<4>{4,4,4,4});
|
||||||
|
ASSERT_EQ(57, arr4d.at({0,1,2,3}));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(TestNDArray, TestMove) {
|
||||||
|
unsigned int data_ui[8] = {0,1,2,3,4,5,6,7};
|
||||||
|
nb::NDArray<unsigned int, 2> arr_1({4, 2}, data_ui);
|
||||||
|
nb::NDArray<unsigned int, 2> arr_2(std::move(arr_1));
|
||||||
|
Coord<2> x{2, 1};
|
||||||
|
ASSERT_EQ(arr_1.data(), nullptr);
|
||||||
|
ASSERT_ARRAY_EQ(arr_1.dimensions, arr_2.dimensions);
|
||||||
|
ASSERT_EQ(arr_2.at(x), 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(TestNDArray, TestConversion) {
|
||||||
|
nb::NDArray<unsigned int, 2> arr1({3,3});
|
||||||
|
nb::NDArray<unsigned int, 3> arr2(std::move(arr1));
|
||||||
|
ASSERT_EQ(arr1.data(), nullptr);
|
||||||
|
ASSERT_ARRAY_EQ(arr2.dimensions, Coord<3>{3,3,1});
|
||||||
|
nb::NDArray<unsigned int, 2> arr3(std::move(arr2));
|
||||||
|
ASSERT_EQ(arr2.data(), nullptr);
|
||||||
|
ASSERT_ARRAY_EQ(arr3.dimensions, Coord<2>{3,3});
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(TestNDArray, TestFromMemory) {
|
||||||
|
auto data = new float[9]{1,2,3,4,5,6,7,8,9};
|
||||||
|
auto arr1 = nb::NDArray<float, 2>::MemoryToNDArray({3,3}, data);
|
||||||
|
ASSERT_EQ(arr1.data(), data);
|
||||||
|
ASSERT_FLOAT_EQ(arr1.at({1,2}), data[5]);
|
||||||
|
auto data2 = new float[9]{9,8,7,6,5,4,3,2,1};
|
||||||
|
arr1.data(data2);
|
||||||
|
ASSERT_EQ(arr1.data(), data);
|
||||||
|
ASSERT_FLOAT_EQ(arr1.at({3,0}), data2[6]);
|
||||||
|
delete[] data2;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -3,8 +3,9 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <NBCore/TypeTraits.hpp>
|
|
||||||
#include <NBCore/StringUtils.hpp>
|
#include <NBCore/StringUtils.hpp>
|
||||||
|
#include <NBCore/TypeTraits.hpp>
|
||||||
|
#include <NBCore/Utils.hpp>
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ TEST(UtilsTest, TestFindAndReplace) {
|
|||||||
|
|
||||||
tmp = find_and_replace("aeiou", "a", "afa");
|
tmp = find_and_replace("aeiou", "a", "afa");
|
||||||
tmp = find_and_replace(tmp, "e", "efe");
|
tmp = find_and_replace(tmp, "e", "efe");
|
||||||
tmp = find_and_replace(tmp, "i", "ifi");
|
tmp = find_and_replace(tmp, "i", "ifi");
|
||||||
tmp = find_and_replace(tmp, "o", "ofo");
|
tmp = find_and_replace(tmp, "o", "ofo");
|
||||||
tmp = find_and_replace(tmp, "u", "ufu");
|
tmp = find_and_replace(tmp, "u", "ufu");
|
||||||
ASSERT_STREQ(
|
ASSERT_STREQ(
|
||||||
@@ -46,7 +47,6 @@ TEST(UtilsTest, TestFindAndReplaceWstr) {
|
|||||||
find_and_replace<wchar_t>(L"Jeff", L"e", L"efe").c_str(),
|
find_and_replace<wchar_t>(L"Jeff", L"e", L"efe").c_str(),
|
||||||
L"Jefeff"
|
L"Jefeff"
|
||||||
);
|
);
|
||||||
|
|
||||||
std::wstring tmp = find_and_replace<wchar_t>(L"Naif", L"a", L"afa");
|
std::wstring tmp = find_and_replace<wchar_t>(L"Naif", L"a", L"afa");
|
||||||
ASSERT_STREQ(
|
ASSERT_STREQ(
|
||||||
find_and_replace<wchar_t>(tmp, L"i", L"ifi").c_str(),
|
find_and_replace<wchar_t>(tmp, L"i", L"ifi").c_str(),
|
||||||
@@ -74,44 +74,6 @@ TEST(UtilsTest, TestFindAndReplaceWstr) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TEST(UtilsTest, TestWstrToStr) {
|
|
||||||
ASSERT_STREQ(
|
|
||||||
nb::wstr_to_str(L"Hi!").c_str(),
|
|
||||||
"Hi!"
|
|
||||||
);
|
|
||||||
ASSERT_STREQ(
|
|
||||||
nb::wstr_to_str(L"Naif").c_str(),
|
|
||||||
"Naif"
|
|
||||||
);
|
|
||||||
ASSERT_STREQ(
|
|
||||||
nb::wstr_to_str(L"\r\r\r\n").c_str(),
|
|
||||||
"\r\r\r\n"
|
|
||||||
);
|
|
||||||
ASSERT_STREQ(
|
|
||||||
nb::wstr_to_str(L"Naif\ttalks\r\ra\t\nlot").c_str(),
|
|
||||||
"Naif\ttalks\r\ra\t\nlot"
|
|
||||||
);
|
|
||||||
} */
|
|
||||||
|
|
||||||
/* TEST(UtilsTest, TestStrToWstr) {
|
|
||||||
ASSERT_STREQ(
|
|
||||||
nb::str_to_wstr("Hi!").c_str(),
|
|
||||||
L"Hi!"
|
|
||||||
);
|
|
||||||
ASSERT_STREQ(
|
|
||||||
nb::str_to_wstr("Naif").c_str(),
|
|
||||||
L"Naif"
|
|
||||||
);
|
|
||||||
ASSERT_STREQ(
|
|
||||||
nb::str_to_wstr("\r\r\r\n").c_str(),
|
|
||||||
L"\r\r\r\n"
|
|
||||||
);
|
|
||||||
ASSERT_STREQ(
|
|
||||||
nb::str_to_wstr("Naif\ttalks\r\ra\t\nlot").c_str(),
|
|
||||||
L"Naif\ttalks\r\ra\t\nlot"
|
|
||||||
);
|
|
||||||
} */
|
|
||||||
|
|
||||||
struct A { bool x(); };
|
struct A { bool x(); };
|
||||||
struct B { bool y(); };
|
struct B { bool y(); };
|
||||||
struct C { bool x(); bool y; };
|
struct C { bool x(); bool y; };
|
||||||
|
|||||||
@@ -30,23 +30,26 @@ toAbsolutePath(NB_GRAPHICS_SOURCE
|
|||||||
./src/Buffers.cpp
|
./src/Buffers.cpp
|
||||||
./src/FrameBuffers.cpp
|
./src/FrameBuffers.cpp
|
||||||
./src/Image.cpp
|
./src/Image.cpp
|
||||||
|
./src/Mesh.cpp
|
||||||
./src/OGLObjects.cpp
|
./src/OGLObjects.cpp
|
||||||
./src/ProgramPipeline.cpp
|
./src/ProgramPipeline.cpp
|
||||||
./src/Textures.cpp
|
./src/Textures.cpp
|
||||||
./src/VertexArray.cpp
|
./src/VertexArray.cpp
|
||||||
|
./src/VertexData.cpp
|
||||||
./src/Window.cpp
|
./src/Window.cpp
|
||||||
)
|
)
|
||||||
toAbsolutePath(NB_GRAPHICS_INCLUDE
|
toAbsolutePath(NB_GRAPHICS_INCLUDE
|
||||||
./include/NBGraphics/Buffers.hpp
|
./include/NBGraphics/Buffers.hpp
|
||||||
./include/NBGraphics/Camera.hpp
|
./include/NBGraphics/Camera.hpp
|
||||||
./include/NBGraphics/Draw.hpp
|
|
||||||
./include/NBGraphics/FrameBuffers.hpp
|
./include/NBGraphics/FrameBuffers.hpp
|
||||||
./include/NBGraphics/GLLoad.hpp
|
./include/NBGraphics/GLLoad.hpp
|
||||||
./include/NBGraphics/Image.hpp
|
./include/NBGraphics/Image.hpp
|
||||||
|
./include/NBGraphics/Mesh.hpp
|
||||||
./include/NBGraphics/OGLObjects.hpp
|
./include/NBGraphics/OGLObjects.hpp
|
||||||
./include/NBGraphics/ProgramPipeline.hpp
|
./include/NBGraphics/ProgramPipeline.hpp
|
||||||
./include/NBGraphics/Textures.hpp
|
./include/NBGraphics/Textures.hpp
|
||||||
./include/NBGraphics/VertexArray.hpp
|
./include/NBGraphics/VertexArray.hpp
|
||||||
|
./include/NBGraphics/VertexData.hpp
|
||||||
./include/NBGraphics/Window.hpp
|
./include/NBGraphics/Window.hpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -57,7 +57,8 @@ class BufferError : public Error<BufferError> {
|
|||||||
enum Codes : unsigned int {
|
enum Codes : unsigned int {
|
||||||
UNDEFINED,
|
UNDEFINED,
|
||||||
DATA_OVERFLOW,
|
DATA_OVERFLOW,
|
||||||
DATA_ERROR
|
DATA_ERROR,
|
||||||
|
BAD_DATA_TYPE
|
||||||
};
|
};
|
||||||
static const std::string type;
|
static const std::string type;
|
||||||
static const ErrorCodeMap ErrorMessages;
|
static const ErrorCodeMap ErrorMessages;
|
||||||
@@ -69,17 +70,16 @@ class Buffer : public OpenGLObject {
|
|||||||
using OpenGLObject::_id;
|
using OpenGLObject::_id;
|
||||||
GLenum _usage = GL_STATIC_DRAW;
|
GLenum _usage = GL_STATIC_DRAW;
|
||||||
size_t _size;
|
size_t _size;
|
||||||
Buffer(GLenum target) : Target(target) {}
|
|
||||||
virtual GLuint declare() override {
|
virtual GLuint declare() override {
|
||||||
if (!_id) {
|
if (!_id) {
|
||||||
OPENGL_CALL(glGenBuffers(1, &_id));
|
OPENGL_CALL(glGenBuffers(1, &_id));
|
||||||
}
|
}
|
||||||
bind();
|
this->bind();
|
||||||
return _id;
|
return _id;
|
||||||
}
|
}
|
||||||
virtual void remove() override {
|
virtual void remove() override {
|
||||||
if (_id) {
|
if (_id) {
|
||||||
unbind();
|
this->unbind();
|
||||||
OPENGL_CALL(glDeleteBuffers(1, &_id));
|
OPENGL_CALL(glDeleteBuffers(1, &_id));
|
||||||
_id = 0;
|
_id = 0;
|
||||||
}
|
}
|
||||||
@@ -88,6 +88,8 @@ class Buffer : public OpenGLObject {
|
|||||||
public:
|
public:
|
||||||
const GLenum Target;
|
const GLenum Target;
|
||||||
using OpenGLObject::OpenGLObject;
|
using OpenGLObject::OpenGLObject;
|
||||||
|
Buffer(GLenum target);
|
||||||
|
Buffer(GLenum target, const ByteVector&, GLenum usage=GL_STATIC_DRAW);
|
||||||
Buffer(Buffer&& other);
|
Buffer(Buffer&& other);
|
||||||
Buffer& operator=(Buffer&& rhs);
|
Buffer& operator=(Buffer&& rhs);
|
||||||
|
|
||||||
@@ -151,67 +153,108 @@ protected:
|
|||||||
|
|
||||||
}; */
|
}; */
|
||||||
|
|
||||||
class ArrayBuffer : public Buffer {
|
class UniformBuffer : public Buffer {
|
||||||
|
protected:
|
||||||
|
GLuint _index;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template<typename T>
|
UniformBuffer();
|
||||||
ArrayBuffer(const std::vector<T>& data, GLenum usage=GL_STATIC_DRAW)
|
void bind() const override;
|
||||||
: Buffer(GL_ARRAY_BUFFER) {
|
GLuint bufferIndex() const;
|
||||||
Buffer::data(vectorToBytes(data), usage);
|
GLuint bufferIndex(GLuint);
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class ElementBuffer : protected Buffer {
|
template<typename T>
|
||||||
|
struct IsValidGLElementIndexType {
|
||||||
|
static constexpr bool value = false;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct ValidGLElementIndexType {
|
||||||
|
static constexpr bool value = true;
|
||||||
|
static constexpr GLenum glsltype = GLSLType<T>::value;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct IsValidGLElementIndexType<uint8_t> : public ValidGLElementIndexType<uint8_t> {};
|
||||||
|
template<>
|
||||||
|
struct IsValidGLElementIndexType<uint16_t> : public ValidGLElementIndexType<uint16_t> {};
|
||||||
|
template<>
|
||||||
|
struct IsValidGLElementIndexType<uint32_t> : public ValidGLElementIndexType<uint32_t> {};
|
||||||
|
|
||||||
|
class ElementBuffer : Buffer {
|
||||||
|
private:
|
||||||
|
template<typename T>
|
||||||
|
inline void checkForElementDataType() {
|
||||||
|
static_assert(IsValidGLElementIndexType<T>::value,
|
||||||
|
"ElementBuffer data type must resolve to one of GL_UNSIGNED_BYTE,"
|
||||||
|
"GL_UNSIGNED_SHORT, or GL_UNSIGNED_INT"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
GLenum _glslType;
|
using Codes = BufferError::Codes;
|
||||||
size_t _typeSize;
|
using OpenGLObject::_id;
|
||||||
|
using Buffer::_usage;
|
||||||
|
using Buffer::_size;
|
||||||
|
using Buffer::declare;
|
||||||
|
using Buffer::remove;
|
||||||
|
GLenum _datatype;
|
||||||
|
template<typename T>
|
||||||
|
inline void checkDatatypeConsistency() {
|
||||||
|
if (_datatype != ValidGLElementIndexType<T>::value) {
|
||||||
|
THROW(BufferError(
|
||||||
|
Codes::BAD_DATA_TYPE,
|
||||||
|
GLenumTable[ValidGLElementIndexType<T>::value] + " not same as buffer "
|
||||||
|
"data type "+GLenumTable[_datatype]
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using Buffer::Target;
|
using Buffer::Target;
|
||||||
|
using Buffer::OpenGLObject;
|
||||||
using Buffer::bind;
|
using Buffer::bind;
|
||||||
using Buffer::usage;
|
|
||||||
using Buffer::unbind;
|
using Buffer::unbind;
|
||||||
ElementBuffer();
|
using Buffer::usage;
|
||||||
|
using Buffer::size;
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ElementBuffer(const T* const data_ptr, size_t size, GLenum usage=GL_STATIC_DRAW)
|
ElementBuffer(const std::vector<T>& indices, GLenum usage=GL_STATIC_DRAW) : Buffer(GL_ELEMENT_ARRAY_BUFFER) {
|
||||||
: Buffer(GL_ELEMENT_ARRAY_BUFFER) {
|
data(indices, usage);
|
||||||
data(data_ptr, size, usage);
|
|
||||||
}
|
|
||||||
template<typename T>
|
|
||||||
ElementBuffer(const std::vector<T>& data_, GLenum usage_=GL_STATIC_DRAW)
|
|
||||||
: ElementBuffer(data_.data(), data_.size(), usage_) {}
|
|
||||||
|
|
||||||
size_t size() const;
|
|
||||||
size_t typeSize() const;
|
|
||||||
ByteVector data() const;
|
|
||||||
GLenum glslType() const;
|
|
||||||
template<typename T>
|
|
||||||
void data(const T* data_, size_t size_, GLenum usage_=GL_STATIC_DRAW) {
|
|
||||||
declare();
|
|
||||||
using TypeInfo = GLSLType<T>;
|
|
||||||
_typeSize = TypeInfo::size;
|
|
||||||
Buffer::data(data_, size_*_typeSize, usage_);
|
|
||||||
_glslType = TypeInfo::value;
|
|
||||||
}
|
|
||||||
template<typename T>
|
|
||||||
void data(const std::vector<T>& data_, GLenum usage_=GL_STATIC_DRAW) {
|
|
||||||
data(data_.data(), data_.size(), usage_);
|
|
||||||
}
|
|
||||||
template<typename T>
|
|
||||||
void subdata(const T* data, size_t size, size_t offset) {
|
|
||||||
using TypeInfo = GLSLType<T>;
|
|
||||||
if ( _glslType != TypeInfo::value ) {
|
|
||||||
THROW( BufferError(
|
|
||||||
Codes::DATA_ERROR,
|
|
||||||
"Check element buffer data types"
|
|
||||||
) );
|
|
||||||
}
|
|
||||||
Buffer::subdata(data, size*_typeSize, offset);
|
|
||||||
}
|
|
||||||
template<typename T>
|
|
||||||
void subdata(const std::vector<T>& data, size_t offset=0) {
|
|
||||||
subdata(data.data(), data.size(), offset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline GLenum datatype() { return _datatype; }
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void data(const std::vector<T>& data, GLenum usage=GL_STATIC_DRAW) {
|
||||||
|
checkForElementDataType<T>();
|
||||||
|
_datatype = ValidGLElementIndexType<T>::glsltype;
|
||||||
|
Buffer::data(vectorToBytes(data), usage);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void data(const T* data, size_t size, GLenum usage=GL_STATIC_DRAW) {
|
||||||
|
checkForElementDataType<T>();
|
||||||
|
_datatype = ValidGLElementIndexType<T>::glsltype;
|
||||||
|
Buffer::data((void*)data, size * sizeof(T), usage);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void subdata(const T* data, size_t size, GLintptr offset=0) {
|
||||||
|
checkForElementDataType<T>();
|
||||||
|
checkDatatypeConsistency<T>();
|
||||||
|
Buffer::subdata((void*)data, size * sizeof(T), offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void subdata(const std::vector<T>& data_, GLintptr offset=0) {
|
||||||
|
checkForElementDataType<T>();
|
||||||
|
checkDatatypeConsistency<T>();
|
||||||
|
Buffer::subdata(vectorToBytes(data_), offset);
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,83 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_DRAW
|
|
||||||
#define _NB_DRAW
|
|
||||||
|
|
||||||
#include <NBGraphics/GLLoad.hpp>
|
|
||||||
|
|
||||||
#include <exception>
|
|
||||||
#include <map>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <NBGraphics/Buffers.hpp>
|
|
||||||
#include <NBGraphics/VertexArray.hpp>
|
|
||||||
|
|
||||||
#define THROW_DRAW_ERROR(msg) throw DrawError(msg, __FILE__, __LINE__);
|
|
||||||
|
|
||||||
namespace nb{
|
|
||||||
|
|
||||||
class DrawError : public std::runtime_error {
|
|
||||||
public:
|
|
||||||
DrawError(const std::string&, const std::string& file="", int line=-1);
|
|
||||||
};
|
|
||||||
|
|
||||||
uint8_t VerticesFromPrimitives(GLenum, unsigned int num_prims=1);
|
|
||||||
|
|
||||||
uint8_t PrimitivesFromVertices(GLenum, unsigned int);
|
|
||||||
|
|
||||||
void copyVertexAttribute(const VertexAttribute&, VertexAttribute&);
|
|
||||||
|
|
||||||
class DrawBuffer {
|
|
||||||
public:
|
|
||||||
typedef std::shared_ptr<Buffer> BufferManagerPointer;
|
|
||||||
DrawBuffer(VertexAttributeList, GLenum prim_type=GL_TRIANGLES);
|
|
||||||
//DrawBuffer(std::initializer_list<VertexAttribute>, std::initializer_list<VertexAttributePointer>);
|
|
||||||
|
|
||||||
unsigned int vertSize() const;
|
|
||||||
const std::shared_ptr<ShaderProgram> shader() const;
|
|
||||||
unsigned int elementBufferID() const;
|
|
||||||
unsigned int vertexBufferID() const;
|
|
||||||
virtual void draw() const;
|
|
||||||
RawVec vertAttrData(unsigned int);
|
|
||||||
|
|
||||||
VertexAttributeList changeVertLayout(unsigned int, VertexAttributePointer);
|
|
||||||
RawVec vertAttrData(unsigned int, const RawVec&);
|
|
||||||
virtual void elementData(const std::vector<unsigned int>&);
|
|
||||||
virtual void elementData(unsigned int);
|
|
||||||
virtual void elementData(const std::vector<unsigned int>&, unsigned int offset);
|
|
||||||
virtual void vertexData(const RawVec&);
|
|
||||||
virtual void vertexData(unsigned int num_verts);
|
|
||||||
virtual void vertexData(const RawVec&, unsigned int offset);
|
|
||||||
|
|
||||||
BufferManagerPointer _vert_data;
|
|
||||||
BufferManagerPointer _elmt_data;
|
|
||||||
VAOManager _VAO;
|
|
||||||
const GLenum _gl_primitive_type=0;
|
|
||||||
virtual VertexAttributeList defaultVAO(VertexAttributeList);
|
|
||||||
|
|
||||||
unsigned int _num_verts, _num_prims;
|
|
||||||
protected:
|
|
||||||
};
|
|
||||||
|
|
||||||
class DrawInstanceBuffer : public DrawBuffer {
|
|
||||||
public:
|
|
||||||
DrawInstanceBuffer(VertexAttributeList, VertexAttributeList, GLenum primt_type=GL_TRIANGLES);
|
|
||||||
|
|
||||||
unsigned int instSize() const;
|
|
||||||
unsigned int instanceBufferID() const;
|
|
||||||
virtual void draw() const override;
|
|
||||||
|
|
||||||
VertexAttributeList changeInstanceLayout(unsigned int, VertexAttributePointer);
|
|
||||||
virtual void instanceData(const RawVec&);
|
|
||||||
virtual void instanceData(unsigned int);
|
|
||||||
virtual void instanceData(const RawVec&, unsigned int offset);
|
|
||||||
|
|
||||||
BufferManagerPointer _inst_data;
|
|
||||||
virtual VertexAttributeList defaultVAO(VertexAttributeList);
|
|
||||||
unsigned int _num_inst;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
@@ -158,6 +158,8 @@ class Image<Pixel<T, Channels...>> {
|
|||||||
using Codes = ImageError::Codes;
|
using Codes = ImageError::Codes;
|
||||||
bool _data_managed;
|
bool _data_managed;
|
||||||
T* _data;
|
T* _data;
|
||||||
|
Image(unsigned int x, unsigned int y, T* data, bool managed)
|
||||||
|
: width(x), height(y), _data(data), _data_managed(managed) {}
|
||||||
void clear() {
|
void clear() {
|
||||||
if (_data_managed) {
|
if (_data_managed) {
|
||||||
delete[] _data;
|
delete[] _data;
|
||||||
@@ -171,19 +173,21 @@ class Image<Pixel<T, Channels...>> {
|
|||||||
static constexpr size_t NumberChannels = sizeof...(Channels);
|
static constexpr size_t NumberChannels = sizeof...(Channels);
|
||||||
const unsigned int width;
|
const unsigned int width;
|
||||||
const unsigned int height;
|
const unsigned int height;
|
||||||
static Image&& CreateImageStorage(
|
static Image CreateImageStorage(
|
||||||
unsigned int width,
|
unsigned int width,
|
||||||
unsigned int height,
|
unsigned int height,
|
||||||
T* data=nullptr
|
T* data=nullptr
|
||||||
) {
|
) {
|
||||||
unsigned int data_size = NumberChannels*width*height*sizeof(T);
|
unsigned int data_size = NumberChannels*width*height*sizeof(T);
|
||||||
T* data_ptr = new T[data_size];
|
T* data_ptr = new T[data_size];
|
||||||
auto& ret = Image(width, height, data_ptr);
|
Image ret(width, height, data_ptr, true);
|
||||||
if (data) {
|
if (data) {
|
||||||
std::memcpy(data_ptr, data, data_size);
|
std::memcpy(data_ptr, data, data_size);
|
||||||
|
} else {
|
||||||
|
std::memset(data_ptr, 0x00, data_size);
|
||||||
}
|
}
|
||||||
ret._data_managed = true;
|
ret._data_managed = true;
|
||||||
return std::move(ret);
|
return ret;
|
||||||
}
|
}
|
||||||
Image(unsigned int x, unsigned int y, T* data=nullptr)
|
Image(unsigned int x, unsigned int y, T* data=nullptr)
|
||||||
: width(x), height(y), _data(data), _data_managed(false) {}
|
: width(x), height(y), _data(data), _data_managed(false) {}
|
||||||
@@ -201,8 +205,13 @@ class Image<Pixel<T, Channels...>> {
|
|||||||
}
|
}
|
||||||
const Error<> sizeErr = sizeCompare(cpy);
|
const Error<> sizeErr = sizeCompare(cpy);
|
||||||
if (sizeErr.code!=1) { THROW(sizeErr); }
|
if (sizeErr.code!=1) { THROW(sizeErr); }
|
||||||
_data = cpy._data;
|
if (!cpy._data_managed) {
|
||||||
return this;
|
_data = cpy._data;
|
||||||
|
_data_managed = false;
|
||||||
|
} else {
|
||||||
|
*this = cpy.copy();
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
Image& operator=(Image&& mv) {
|
Image& operator=(Image&& mv) {
|
||||||
if (_data_managed) {
|
if (_data_managed) {
|
||||||
@@ -213,7 +222,7 @@ class Image<Pixel<T, Channels...>> {
|
|||||||
_data = mv._data;
|
_data = mv._data;
|
||||||
_data_managed = mv._data_managed;
|
_data_managed = mv._data_managed;
|
||||||
mv._data_managed = false;
|
mv._data_managed = false;
|
||||||
return this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~Image() {
|
virtual ~Image() {
|
||||||
@@ -221,7 +230,7 @@ class Image<Pixel<T, Channels...>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const T* data() const { return _data; }
|
const T* data() const { return _data; }
|
||||||
Image<PixelType>&& copy() const {
|
Image<PixelType> copy() const {
|
||||||
return CreateImageStorage(width, height, _data);
|
return CreateImageStorage(width, height, _data);
|
||||||
}
|
}
|
||||||
PixelReference<PixelType> at(unsigned int x, unsigned int y) {
|
PixelReference<PixelType> at(unsigned int x, unsigned int y) {
|
||||||
|
|||||||
@@ -0,0 +1,81 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _NB_MESH
|
||||||
|
#define _NB_MESH
|
||||||
|
|
||||||
|
#include <NBGraphics/GLLoad.hpp>
|
||||||
|
|
||||||
|
#include <NBGraphics/Buffers.hpp>
|
||||||
|
#include <NBGraphics/VertexArray.hpp>
|
||||||
|
#include <NBGraphics/VertexData.hpp>
|
||||||
|
|
||||||
|
namespace nb{
|
||||||
|
|
||||||
|
class Mesh {
|
||||||
|
protected:
|
||||||
|
const GLenum _primitive;
|
||||||
|
const unsigned int _count;
|
||||||
|
std::shared_ptr<VAO> _vao;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Mesh(
|
||||||
|
VertexDataPointerList vao,
|
||||||
|
unsigned int count,
|
||||||
|
GLenum primitive=GL_TRIANGLES
|
||||||
|
);
|
||||||
|
Mesh(GLenum primitive, const std::shared_ptr<VAO>& vertex_array, unsigned int count);
|
||||||
|
Mesh(GLenum primitive, const VertexData& vertex_data, unsigned int count);
|
||||||
|
Mesh(
|
||||||
|
GLenum primitive,
|
||||||
|
const ByteVector& data,
|
||||||
|
unsigned int count,
|
||||||
|
const std::vector<VertexAttribute>& attributes
|
||||||
|
);
|
||||||
|
|
||||||
|
Mesh(Mesh&&);
|
||||||
|
Mesh(const Mesh&&) = delete;
|
||||||
|
Mesh& operator=(Mesh&&) = delete;
|
||||||
|
Mesh& operator=(const Mesh&) = delete;
|
||||||
|
|
||||||
|
inline virtual void draw() const { this->draw(_count); }
|
||||||
|
inline virtual void draw(unsigned int count) const { this->draw(0, count); }
|
||||||
|
virtual void draw(unsigned int offset, unsigned int count) const;
|
||||||
|
inline GLenum primitive() const { return _primitive; }
|
||||||
|
inline unsigned int count() const { return _count; }
|
||||||
|
inline std::shared_ptr<VAO> vao() const { return _vao; }
|
||||||
|
};
|
||||||
|
|
||||||
|
class InstancedMesh : public Mesh {
|
||||||
|
protected:
|
||||||
|
using Mesh::_primitive;
|
||||||
|
using Mesh::_count;
|
||||||
|
using Mesh::_vao;
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual void draw(unsigned int offset, unsigned int count) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
class IndexedMesh : public Mesh {
|
||||||
|
protected:
|
||||||
|
std::shared_ptr<ElementBuffer> _ebo;
|
||||||
|
|
||||||
|
public:
|
||||||
|
template<typename T>
|
||||||
|
IndexedMesh(
|
||||||
|
GLenum primitive,
|
||||||
|
const ByteVector& data,
|
||||||
|
const std::vector<VertexAttribute>& attributes,
|
||||||
|
const std::vector<T>& indices
|
||||||
|
) : Mesh(primitive, data, indices.size(), attributes) {
|
||||||
|
_vao->bind();
|
||||||
|
_ebo = std::make_shared<ElementBuffer>(indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
using Mesh::draw;
|
||||||
|
virtual void draw(unsigned int offset, unsigned int count) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
class InstancedIndexedMesh : virtual public IndexedMesh {};
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@@ -11,6 +11,7 @@ namespace nb {
|
|||||||
typedef ConstantMap<GLenum, std::string> GLEnumTableType;
|
typedef ConstantMap<GLenum, std::string> GLEnumTableType;
|
||||||
|
|
||||||
extern const GLEnumTableType GLenumTable;
|
extern const GLEnumTableType GLenumTable;
|
||||||
|
extern const ConstantMap<TypeCodes, GLint> TypeToGLSL;
|
||||||
|
|
||||||
class OpenGLError : public Error<OpenGLError> {
|
class OpenGLError : public Error<OpenGLError> {
|
||||||
using Base = Error<OpenGLError>;
|
using Base = Error<OpenGLError>;
|
||||||
@@ -32,6 +33,7 @@ class OpenGLError : public Error<OpenGLError> {
|
|||||||
static const std::string type;
|
static const std::string type;
|
||||||
static const ErrorCodeMap ErrorMessages;
|
static const ErrorCodeMap ErrorMessages;
|
||||||
static OpenGLError status();
|
static OpenGLError status();
|
||||||
|
static OpenGLError status(const std::string& note);
|
||||||
};
|
};
|
||||||
|
|
||||||
class OpenGLObjectError : public Error<OpenGLObjectError> {
|
class OpenGLObjectError : public Error<OpenGLObjectError> {
|
||||||
@@ -41,7 +43,7 @@ class OpenGLObjectError : public Error<OpenGLObjectError> {
|
|||||||
using Base::Base;
|
using Base::Base;
|
||||||
|
|
||||||
enum Codes : unsigned int {
|
enum Codes : unsigned int {
|
||||||
UNDEFINED, HANGING_OBJECT, INVALID_OBJECT
|
UNDEFINED, HANGING_OBJECT, INVALID_OBJECT, INVALID_CONTEXT
|
||||||
};
|
};
|
||||||
|
|
||||||
static const std::string type;
|
static const std::string type;
|
||||||
@@ -60,13 +62,16 @@ class OpenGLObject {
|
|||||||
virtual void unbind() const = 0;
|
virtual void unbind() const = 0;
|
||||||
GLuint id() const { return _id; }
|
GLuint id() const { return _id; }
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
using Codes = OpenGLObjectError::Codes;
|
using Codes = OpenGLObjectError::Codes;
|
||||||
OpenGLObject() = default;
|
OpenGLObject();
|
||||||
|
|
||||||
|
void checkObjectContextValidity();
|
||||||
virtual GLuint declare() = 0;
|
virtual GLuint declare() = 0;
|
||||||
virtual void remove() = 0;
|
virtual void remove() = 0;
|
||||||
|
|
||||||
|
const GLFWwindow* const _arena;
|
||||||
GLuint _id = 0;
|
GLuint _id = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -80,21 +85,21 @@ class OpenGLObject {
|
|||||||
unsigned int line,
|
unsigned int line,
|
||||||
Fn fn
|
Fn fn
|
||||||
) {
|
) {
|
||||||
auto check_ = [file, line]() {
|
auto check_ = [file, line](const std::string& note="") {
|
||||||
const auto no_err = nb::OpenGLError::NO_ERROR;
|
const auto no_err = nb::OpenGLError::NO_ERROR;
|
||||||
auto stat = nb::OpenGLError::status();
|
auto stat = nb::OpenGLError::status(note);
|
||||||
if (stat.code != no_err) {
|
if (stat.code != no_err) {
|
||||||
THROW_W_CODE_LOC(file, line, stat);
|
THROW_W_CODE_LOC(file, line, stat);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if constexpr(std::is_same_v<std::invoke_result_t<Fn>, void>) {
|
if constexpr(std::is_same_v<std::invoke_result_t<Fn>, void>) {
|
||||||
check_();
|
check_("Pre-call state");
|
||||||
fn();
|
fn();
|
||||||
check_();
|
check_("Post-call state");
|
||||||
} else {
|
} else {
|
||||||
check_();
|
check_("Pre-call state");
|
||||||
auto ret = fn();
|
auto ret = fn();
|
||||||
check_();
|
check_("Post-call state");
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#include <NBCore/Errors.hpp>
|
#include <NBCore/Errors.hpp>
|
||||||
#include <NBCore/Utils.hpp>
|
#include <NBCore/Utils.hpp>
|
||||||
|
|
||||||
|
#include <NBGraphics/Buffers.hpp>
|
||||||
#include <NBGraphics/OGLObjects.hpp>
|
#include <NBGraphics/OGLObjects.hpp>
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
@@ -112,17 +113,22 @@ class Program : public OpenGLObject {
|
|||||||
Program& operator=(Program&&) = delete;
|
Program& operator=(Program&&) = delete;
|
||||||
operator bool();
|
operator bool();
|
||||||
~Program() { remove(); }
|
~Program() { remove(); }
|
||||||
virtual void bind() const override {
|
void bind() const override {
|
||||||
OPENGL_CALL(glUseProgram(_id));
|
OPENGL_CALL(glUseProgram(_id));
|
||||||
}
|
}
|
||||||
virtual void unbind() const override { /* TODO: Some warning of some kind perhaps*/ }
|
void unbind() const override { /* TODO: Some warning of some kind perhaps*/ }
|
||||||
GLint status(GLenum) const;
|
GLint status(GLenum) const;
|
||||||
std::string log() const;
|
std::string log() const;
|
||||||
|
void setUniform(const std::string& blockname, std::shared_ptr<UniformBuffer>);
|
||||||
|
void setUniformBinding(const std::string& blockname, GLuint index);
|
||||||
|
GLuint getUniformBinding(const std::string& blockname) const;
|
||||||
|
|
||||||
friend Base;
|
friend Base;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
using Base::_id;
|
using Base::_id;
|
||||||
GLint _success;
|
GLint _success;
|
||||||
|
std::unordered_map<std::string, GLuint> _uniformBindings;
|
||||||
virtual GLuint declare() override {
|
virtual GLuint declare() override {
|
||||||
if (!_id) {
|
if (!_id) {
|
||||||
_id = OPENGL_CALL(glCreateProgram());
|
_id = OPENGL_CALL(glCreateProgram());
|
||||||
|
|||||||
@@ -24,11 +24,11 @@ class RenderTarget : public OpenGLObject {
|
|||||||
using Base = OpenGLObject;
|
using Base = OpenGLObject;
|
||||||
using Base::_id;
|
using Base::_id;
|
||||||
RenderTarget(GLenum target) : Target(target) {}
|
RenderTarget(GLenum target) : Target(target) {}
|
||||||
RenderTarget(RenderTarget&& rhs)
|
|
||||||
: Target(rhs.Target), Base(std::move(rhs)) {}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using Base::id;
|
using Base::id;
|
||||||
|
RenderTarget(RenderTarget&& mv);
|
||||||
|
RenderTarget& operator=(RenderTarget&& mv);
|
||||||
const GLenum Target;
|
const GLenum Target;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -57,8 +57,10 @@ class Texture : public RenderTarget {
|
|||||||
public:
|
public:
|
||||||
using Base::id;
|
using Base::id;
|
||||||
using Base::Target;
|
using Base::Target;
|
||||||
|
Texture(Texture&& mv);
|
||||||
|
Texture& operator=(Texture&& mv);
|
||||||
|
|
||||||
virtual void bind() const override {
|
void bind() const override {
|
||||||
if (_id) {
|
if (_id) {
|
||||||
OPENGL_CALL(glBindTexture(Target, _id));
|
OPENGL_CALL(glBindTexture(Target, _id));
|
||||||
} else {
|
} else {
|
||||||
@@ -68,7 +70,7 @@ class Texture : public RenderTarget {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
virtual void unbind() const override {
|
void unbind() const override {
|
||||||
OPENGL_CALL(glBindTexture(Target, 0));
|
OPENGL_CALL(glBindTexture(Target, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,13 +121,6 @@ struct OGLPixelFormat;
|
|||||||
template<typename T, typename... Channels>
|
template<typename T, typename... Channels>
|
||||||
struct OGLPixelFormat<Pixel<T, Channels...>>;
|
struct OGLPixelFormat<Pixel<T, Channels...>>;
|
||||||
|
|
||||||
template<>
|
|
||||||
struct OGLPixelFormat<Pixel<uint8_t, Red, Green, Blue>> {
|
|
||||||
static constexpr GLenum glBase = GL_RGB;
|
|
||||||
static constexpr GLenum glFormat = GL_RGB8UI;
|
|
||||||
static constexpr GLenum glData = GL_UNSIGNED_BYTE;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
struct OGLPixelFormat<Pixel<uint8_t, Red, Green, Blue, Alpha>> {
|
struct OGLPixelFormat<Pixel<uint8_t, Red, Green, Blue, Alpha>> {
|
||||||
static constexpr GLenum glBase = GL_RGBA;
|
static constexpr GLenum glBase = GL_RGBA;
|
||||||
@@ -133,6 +128,27 @@ struct OGLPixelFormat<Pixel<uint8_t, Red, Green, Blue, Alpha>> {
|
|||||||
static constexpr GLenum glData = GL_UNSIGNED_BYTE;
|
static constexpr GLenum glData = GL_UNSIGNED_BYTE;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct OGLPixelFormat<Pixel<float, Red, Green, Blue, Alpha>> {
|
||||||
|
static constexpr GLenum glBase = GL_RGBA;
|
||||||
|
static constexpr GLenum glFormat = GL_RGBA32F;
|
||||||
|
static constexpr GLenum glData = GL_FLOAT;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct OGLPixelFormat<Pixel<uint8_t, Red, Green, Blue>> {
|
||||||
|
static constexpr GLenum glBase = GL_RGB;
|
||||||
|
static constexpr GLenum glFormat = GL_RGB8UI;
|
||||||
|
static constexpr GLenum glData = GL_UNSIGNED_BYTE;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct OGLPixelFormat<Pixel<float, Red, Green, Blue>> {
|
||||||
|
static constexpr GLenum glBase = GL_RGB;
|
||||||
|
static constexpr GLenum glFormat = GL_RGB32F;
|
||||||
|
static constexpr GLenum glData = GL_FLOAT;
|
||||||
|
};
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
struct OGLPixelFormat<Pixel<float, Red, Green>> {
|
struct OGLPixelFormat<Pixel<float, Red, Green>> {
|
||||||
static constexpr GLenum glBase = GL_RG;
|
static constexpr GLenum glBase = GL_RG;
|
||||||
@@ -140,6 +156,13 @@ struct OGLPixelFormat<Pixel<float, Red, Green>> {
|
|||||||
static constexpr GLenum glData = GL_FLOAT;
|
static constexpr GLenum glData = GL_FLOAT;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct OGLPixelFormat<Pixel<float, Red>> {
|
||||||
|
static constexpr GLenum glBase = GL_RED;
|
||||||
|
static constexpr GLenum glFormat = GL_R32F;
|
||||||
|
static constexpr GLenum glData = GL_FLOAT;
|
||||||
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class Texture2D;
|
class Texture2D;
|
||||||
|
|
||||||
@@ -226,5 +249,19 @@ class Texture1D<Pixel<T, Channels...>> : public Texture {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class Texture1DArray;
|
||||||
|
|
||||||
|
template <typename... T>
|
||||||
|
class Texture1DArray<Pixel<T...>> : public Texture {
|
||||||
|
protected:
|
||||||
|
|
||||||
|
public:
|
||||||
|
using Texture::generateMipmaps;
|
||||||
|
Texture1DArray() : Texture(GL_TEXTURE_1D_ARRAY) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
#endif // _NB_TEXTURES
|
#endif // _NB_TEXTURES
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#include <NBGraphics/Buffers.hpp>
|
#include <NBGraphics/Buffers.hpp>
|
||||||
#include <NBGraphics/OGLObjects.hpp>
|
#include <NBGraphics/OGLObjects.hpp>
|
||||||
|
#include <NBGraphics/VertexData.hpp>
|
||||||
|
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
@@ -33,32 +34,13 @@ static uint8_t GLSLTypeSize(GLenum type) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct VertexAttributeLayout {
|
|
||||||
int32_t offset = 0;
|
|
||||||
GLsizei stride = 0;
|
|
||||||
GLuint divisor = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct VertexAttribute {
|
|
||||||
GLint GLSLSize;
|
|
||||||
GLenum GLSLType;
|
|
||||||
GLboolean GLSLNormalization;
|
|
||||||
VertexAttributeLayout layout;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct VertexAttributePointer {
|
|
||||||
VertexAttribute attribute;
|
|
||||||
GLuint buffer;
|
|
||||||
GLuint index;
|
|
||||||
};
|
|
||||||
|
|
||||||
static bool isIndexInVertexAttribute(int i, const VertexAttribute& va) {
|
static bool isIndexInVertexAttribute(int i, const VertexAttribute& va) {
|
||||||
if ((i -= va.layout.offset) < 0) { return false; }
|
if ((i -= va.layout.offset) < 0) { return false; }
|
||||||
return (i%va.layout.stride) < GLSLTypeSize(va.GLSLType) * va.GLSLSize;
|
return (i%va.layout.stride) < GLSLTypeSize(TypeToGLSL[va.type]) * va.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef std::vector<VertexAttribute> VertexAttributeList;
|
typedef std::vector<VertexData> VertexDataList;
|
||||||
typedef std::vector<VertexAttributePointer> VertexAttributePointerList;
|
typedef std::vector<VertexDataPointer> VertexDataPointerList;
|
||||||
|
|
||||||
class VAOError : public Error<VAOError> {
|
class VAOError : public Error<VAOError> {
|
||||||
using Base = Error<VAOError>;
|
using Base = Error<VAOError>;
|
||||||
@@ -78,10 +60,30 @@ class VAO : public OpenGLObject {
|
|||||||
using Base = OpenGLObject;
|
using Base = OpenGLObject;
|
||||||
using Codes = VAOError::Codes;
|
using Codes = VAOError::Codes;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
using Base::_id;
|
||||||
|
VertexDataPointerList _vertexdata;
|
||||||
|
|
||||||
|
virtual void remove() override {
|
||||||
|
if(_id) {
|
||||||
|
disable();
|
||||||
|
bind();
|
||||||
|
OPENGL_CALL(glDeleteVertexArrays(1, &_id));
|
||||||
|
_id = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
virtual GLuint declare() override {
|
||||||
|
if (!_id) {
|
||||||
|
OPENGL_CALL(glGenVertexArrays(1, &_id));
|
||||||
|
}
|
||||||
|
bind();
|
||||||
|
return _id;
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using Base::Base;
|
using Base::Base;
|
||||||
VAO() = default;
|
VAO() = default;
|
||||||
VAO(const VertexAttributePointerList&);
|
VAO(const VertexDataPointerList&);
|
||||||
VAO(VAO&&);
|
VAO(VAO&&);
|
||||||
|
|
||||||
VAO& operator=(VAO&&);
|
VAO& operator=(VAO&&);
|
||||||
@@ -100,34 +102,15 @@ class VAO : public OpenGLObject {
|
|||||||
OPENGL_CALL(glBindVertexArray(0));
|
OPENGL_CALL(glBindVertexArray(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexAttributePointerList attributes() const;
|
VertexDataPointerList attributes() const;
|
||||||
VertexAttributePointerList attributes(const VertexAttributePointerList&);
|
VertexDataPointerList attributes(const VertexDataPointerList&);
|
||||||
VertexAttributePointer attr(GLuint) const;
|
VertexDataPointerList attributes(const VertexDataList&);
|
||||||
|
VertexDataPointer attribute(GLuint) const;
|
||||||
void enable() const;
|
void enable() const;
|
||||||
void enable(GLuint) const;
|
void enable(GLuint) const;
|
||||||
void disable() const;
|
void disable() const;
|
||||||
void disable(GLuint) const;
|
void disable(GLuint) const;
|
||||||
|
|
||||||
protected:
|
|
||||||
using Base::_id;
|
|
||||||
VertexAttributePointerList _attrs;
|
|
||||||
|
|
||||||
virtual void remove() override {
|
|
||||||
if(_id) {
|
|
||||||
disable();
|
|
||||||
bind();
|
|
||||||
OPENGL_CALL(glDeleteVertexArrays(1, &_id));
|
|
||||||
_id = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
virtual GLuint declare() override {
|
|
||||||
if (!_id) {
|
|
||||||
OPENGL_CALL(glGenVertexArrays(1, &_id));
|
|
||||||
}
|
|
||||||
bind();
|
|
||||||
return _id;
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class VertexError : public Error<VertexError> {
|
class VertexError : public Error<VertexError> {
|
||||||
@@ -144,97 +127,5 @@ class VertexError : public Error<VertexError> {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
using VBO = ArrayBuffer;
|
|
||||||
using VertBufVec = SharedVector<VBO>;
|
|
||||||
using EBO = ElementBuffer;
|
|
||||||
|
|
||||||
struct VertexData {
|
|
||||||
std::shared_ptr<VBO> vbo;
|
|
||||||
VertexAttributeList attrs;
|
|
||||||
};
|
|
||||||
|
|
||||||
using VertexDataVec = std::vector<VertexData>;
|
|
||||||
|
|
||||||
class VertexGroup {
|
|
||||||
protected:
|
|
||||||
using Codes = VertexError::Codes;
|
|
||||||
VertexDataVec _vertex_data;
|
|
||||||
std::shared_ptr<EBO> _ebo;
|
|
||||||
std::shared_ptr<VAO> _vao;
|
|
||||||
GLenum _primitive;
|
|
||||||
|
|
||||||
public:
|
|
||||||
GLenum primitive=GL_TRIANGLES;
|
|
||||||
VertexGroup(VertexGroup&&);
|
|
||||||
VertexGroup& operator=(VertexGroup&&);
|
|
||||||
|
|
||||||
VertexGroup(
|
|
||||||
const VertexDataVec& vertex_data,
|
|
||||||
const std::shared_ptr<EBO>& ebo
|
|
||||||
)
|
|
||||||
: _vao(std::make_shared<VAO>()), _ebo(ebo) {
|
|
||||||
setBuffers(vertex_data);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... T>
|
|
||||||
VertexGroup(
|
|
||||||
const VertexDataVec& vertex_data,
|
|
||||||
T... args
|
|
||||||
)
|
|
||||||
: VertexGroup(vertex_data, std::make_shared<EBO>(args...)) {
|
|
||||||
setBuffers(vertex_data);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... T>
|
|
||||||
VertexGroup(
|
|
||||||
const VertexData& vertex_data,
|
|
||||||
T... args
|
|
||||||
)
|
|
||||||
: VertexGroup(VertexDataVec{vertex_data}, args...) {}
|
|
||||||
|
|
||||||
template<typename... T>
|
|
||||||
VertexGroup(
|
|
||||||
const ByteVector& vertex_data,
|
|
||||||
const VertexAttributeList& vertex_specification,
|
|
||||||
T... args
|
|
||||||
) : VertexGroup({
|
|
||||||
std::make_shared<VBO>(vertex_data),
|
|
||||||
vertex_specification},
|
|
||||||
args...
|
|
||||||
) {}
|
|
||||||
|
|
||||||
void bind() const {
|
|
||||||
_vao->bind();
|
|
||||||
_ebo->bind();
|
|
||||||
}
|
|
||||||
void unbind() const {
|
|
||||||
_vao->unbind();
|
|
||||||
_ebo->unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
void draw() const;
|
|
||||||
|
|
||||||
VertexDataVec getBuffers();
|
|
||||||
VertexData getBuffers(size_t);
|
|
||||||
size_t setBuffers(const VertexDataVec&);
|
|
||||||
size_t addBuffer(const VertexData&);
|
|
||||||
size_t addBuffer(const ByteVector& data, const VertexAttributeList& attrs);
|
|
||||||
VertexDataVec dropBuffers();
|
|
||||||
VertexData dropBuffer(size_t);
|
|
||||||
|
|
||||||
VertexAttributePointer attribute(size_t) const;
|
|
||||||
void enableAttr(GLuint);
|
|
||||||
void disableAttr(GLuint);
|
|
||||||
void enableBuffer(size_t);
|
|
||||||
void disableBuffer(size_t);
|
|
||||||
void enable();
|
|
||||||
void disable();
|
|
||||||
|
|
||||||
std::shared_ptr<EBO> indices() const;
|
|
||||||
std::shared_ptr<EBO> indices(const std::vector<unsigned int>& data);
|
|
||||||
std::shared_ptr<EBO> indices(std::shared_ptr<EBO> buffer);
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
#endif // _NB_VERTEX_ARRAY
|
#endif // _NB_VERTEX_ARRAY
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _NB_VERTEX_DATA
|
||||||
|
#define _NB_VERTEX_DATA
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <NBCore/TypeTraits.hpp>
|
||||||
|
#include <NBCore/Utils.hpp>
|
||||||
|
#include <NBGraphics/Buffers.hpp>
|
||||||
|
#include <NBGraphics/GLLoad.hpp>
|
||||||
|
|
||||||
|
// #include <NBGraphics/VertexArray.hpp>
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
struct VertexAtrributeLayout {
|
||||||
|
unsigned int offset = 0;
|
||||||
|
int stride = 0;
|
||||||
|
unsigned int divisor = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VertexAttribute {
|
||||||
|
unsigned int size;
|
||||||
|
TypeCodes type;
|
||||||
|
bool normalized;
|
||||||
|
VertexAtrributeLayout layout;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VertexData {
|
||||||
|
std::shared_ptr<Buffer> data;
|
||||||
|
std::vector<VertexAttribute> attributes;
|
||||||
|
VertexData(std::shared_ptr<Buffer>, const std::vector<VertexAttribute>&);
|
||||||
|
VertexData(const ByteVector&, const std::vector<VertexAttribute>&);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VertexDataPointer {
|
||||||
|
std::shared_ptr<VertexData> vertexdata;
|
||||||
|
unsigned int index;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
|
#endif
|
||||||
@@ -6,8 +6,12 @@
|
|||||||
#include <NBGraphics/OGLObjects.hpp>
|
#include <NBGraphics/OGLObjects.hpp>
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <map>
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <NBCore/Errors.hpp>
|
#include <NBCore/Errors.hpp>
|
||||||
#include <NBCore/Utils.hpp>
|
#include <NBCore/Utils.hpp>
|
||||||
@@ -21,67 +25,270 @@ class GLFWError : public Error<GLFWError> {
|
|||||||
using Base::Base;
|
using Base::Base;
|
||||||
|
|
||||||
enum Codes : unsigned int {
|
enum Codes : unsigned int {
|
||||||
UNDEFINED, INIT_FAILED, GLFW_INTIALIZED, GLAD_FAILED
|
UNDEFINED, INIT_FAILED, GLFW_INTIALIZED, GLAD_FAILED,
|
||||||
|
NOT_MAIN, WINDOW_FAILED
|
||||||
};
|
};
|
||||||
|
|
||||||
static const std::string type;
|
static const std::string type;
|
||||||
static const ErrorCodeMap ErrorMessages;
|
static const ErrorCodeMap ErrorMessages;
|
||||||
};
|
};
|
||||||
|
|
||||||
class WindowError : public Error<WindowError> {
|
class OGLWindow;
|
||||||
using Base = Error<WindowError>;
|
|
||||||
|
enum WindowCallbacks {
|
||||||
|
KEY, CHAR, CHAR_MODS, MOUSE_BUTTON, CURSOR_POS, CURSOR_ENTER,
|
||||||
|
SCROLL, DROP, WINDOW_POS, WINDOW_SIZE, WINDOW_CLOSE, WINDOW_REFRESH,
|
||||||
|
WINDOW_FOCUS, WINDOW_ICONIFY, WINDOW_MAX, FRAMEBUFFER_SIZE, WINDOW_SCALE,
|
||||||
|
};
|
||||||
|
|
||||||
|
template<WindowCallbacks>
|
||||||
|
struct OGLWindowCallbacks;
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct OGLWindowCallbacks<WindowCallbacks::KEY> {
|
||||||
|
using type = typename std::function<void(OGLWindow*, int, int, int, int)>;
|
||||||
|
};
|
||||||
|
template<>
|
||||||
|
struct OGLWindowCallbacks<WindowCallbacks::CHAR> {
|
||||||
|
using type = typename std::function<void(OGLWindow*, unsigned int)>;
|
||||||
|
};
|
||||||
|
template<>
|
||||||
|
struct OGLWindowCallbacks<WindowCallbacks::CHAR_MODS> {
|
||||||
|
using type = typename std::function<void(OGLWindow*, unsigned int, int)>;
|
||||||
|
};
|
||||||
|
template<>
|
||||||
|
struct OGLWindowCallbacks<WindowCallbacks::CURSOR_ENTER> {
|
||||||
|
using type = typename std::function<void(OGLWindow*, int)>;
|
||||||
|
};
|
||||||
|
template<>
|
||||||
|
struct OGLWindowCallbacks<WindowCallbacks::CURSOR_POS> {
|
||||||
|
using type = typename std::function<void(OGLWindow*, double, double)>;
|
||||||
|
};
|
||||||
|
template<>
|
||||||
|
struct OGLWindowCallbacks<WindowCallbacks::MOUSE_BUTTON> {
|
||||||
|
using type = typename std::function<void(OGLWindow*, int, int, int)>;
|
||||||
|
};
|
||||||
|
template<>
|
||||||
|
struct OGLWindowCallbacks<WindowCallbacks::SCROLL> {
|
||||||
|
using type = typename std::function<void(OGLWindow*, double, double)>;
|
||||||
|
};
|
||||||
|
template<>
|
||||||
|
struct OGLWindowCallbacks<WindowCallbacks::DROP> {
|
||||||
|
using type = typename std::function<void(OGLWindow*, int, const char**)>;
|
||||||
|
};
|
||||||
|
template<>
|
||||||
|
struct OGLWindowCallbacks<WindowCallbacks::WINDOW_POS> {
|
||||||
|
using type = typename std::function<void(OGLWindow*, int, int)>;
|
||||||
|
};
|
||||||
|
template<>
|
||||||
|
struct OGLWindowCallbacks<WindowCallbacks::WINDOW_SIZE> {
|
||||||
|
using type = typename std::function<void(OGLWindow*, int, int)>;
|
||||||
|
};
|
||||||
|
template<>
|
||||||
|
struct OGLWindowCallbacks<WindowCallbacks::WINDOW_CLOSE> {
|
||||||
|
using type = typename std::function<void(OGLWindow*)>;
|
||||||
|
};
|
||||||
|
template<>
|
||||||
|
struct OGLWindowCallbacks<WindowCallbacks::WINDOW_REFRESH> {
|
||||||
|
using type = typename std::function<void(OGLWindow*)>;
|
||||||
|
};
|
||||||
|
template<>
|
||||||
|
struct OGLWindowCallbacks<WindowCallbacks::WINDOW_FOCUS> {
|
||||||
|
using type = typename std::function<void(OGLWindow*, int)>;
|
||||||
|
};
|
||||||
|
template<>
|
||||||
|
struct OGLWindowCallbacks<WindowCallbacks::WINDOW_ICONIFY> {
|
||||||
|
using type = typename std::function<void(OGLWindow*, int)>;
|
||||||
|
};
|
||||||
|
template<>
|
||||||
|
struct OGLWindowCallbacks<WindowCallbacks::WINDOW_MAX> {
|
||||||
|
using type = typename std::function<void(OGLWindow*, int)>;
|
||||||
|
};
|
||||||
|
template<>
|
||||||
|
struct OGLWindowCallbacks<WindowCallbacks::FRAMEBUFFER_SIZE> {
|
||||||
|
using type = typename std::function<void(OGLWindow*, int, int)>;
|
||||||
|
};
|
||||||
|
template<>
|
||||||
|
struct OGLWindowCallbacks<WindowCallbacks::WINDOW_SCALE> {
|
||||||
|
using type = typename std::function<void(OGLWindow*, int)>;
|
||||||
|
};
|
||||||
|
|
||||||
|
class OGLWindow {
|
||||||
|
using GLFWHint = std::array<int, 2>;
|
||||||
|
private:
|
||||||
|
static inline void CheckForMainThread() {
|
||||||
|
if (nb::CurrentTID != nb::MainTID) {
|
||||||
|
THROW(GLFWError(GLFWError::Codes::NOT_MAIN));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
static std::atomic<unsigned int> _count;
|
||||||
|
static std::atomic<bool> _glfwInitialized;
|
||||||
|
static thread_local GLFWwindow* _currWindow;
|
||||||
|
static thread_local const GLFWwindow* _currArena;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static void SetHint(std::pair<int, T> hint) {
|
||||||
|
if constexpr ( std::is_same_v<T, std::string> ) {
|
||||||
|
glfwWindowHintString(std::get<0>(hint), std::get<1>(hint).c_str());
|
||||||
|
} else if constexpr ( std::is_same_v<T, int> ) {
|
||||||
|
glfwWindowHint(std::get<0>(hint), std::get<1>(hint));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string _title;
|
||||||
|
GLFWwindow* _window_ptr;
|
||||||
|
const GLFWwindow* _arena;
|
||||||
|
|
||||||
|
OGLWindow(
|
||||||
|
unsigned int x,
|
||||||
|
unsigned int y,
|
||||||
|
const std::string&,
|
||||||
|
GLFWwindow* sharingContext = NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
GLFWwindow* makeCurrent() const;
|
||||||
|
void detachWindow();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using Base::Base;
|
|
||||||
|
|
||||||
enum Codes : unsigned int {
|
static inline bool IsGLFWInitialized() { return _glfwInitialized; }
|
||||||
UNDEFINED, INITIALIZED_WINDOW, NO_GLFW, INIT_FAILED
|
static bool InitGLFW(const std::vector<GLFWHint>& hints={});
|
||||||
};
|
static bool TerminateGLFW();
|
||||||
|
static inline GLFWwindow* GetCurrentWindow() { return _currWindow; }
|
||||||
|
static inline const GLFWwindow* GetCurrentObjectArena() { return _currArena; }
|
||||||
|
static GLFWwindow* MakeCurrent(const std::shared_ptr<OGLWindow>&);
|
||||||
|
static inline GLFWwindow* DropCurrent() { return MakeCurrent(nullptr); }
|
||||||
|
static void DestroyWindow(std::shared_ptr<OGLWindow>&);
|
||||||
|
template <typename... T>
|
||||||
|
static std::shared_ptr<OGLWindow> CreateWindow(
|
||||||
|
unsigned int x,
|
||||||
|
unsigned int y,
|
||||||
|
const std::string& title,
|
||||||
|
const std::pair<int, T>&... windowHints
|
||||||
|
) {
|
||||||
|
(SetHint(std::pair(windowHints)),... );
|
||||||
|
return std::shared_ptr<OGLWindow>(new OGLWindow(
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
title,
|
||||||
|
nullptr
|
||||||
|
) );
|
||||||
|
}
|
||||||
|
template<typename... T>
|
||||||
|
static std::shared_ptr<OGLWindow> CreateSharedWindow(
|
||||||
|
unsigned int x,
|
||||||
|
unsigned int y,
|
||||||
|
const std::string& title,
|
||||||
|
std::shared_ptr<OGLWindow> sharingWindow = NULL,
|
||||||
|
const std::pair<int, T>&... windowHints
|
||||||
|
) {
|
||||||
|
(SetHint(std::pair(windowHints)),... );
|
||||||
|
return std::shared_ptr<OGLWindow>(new OGLWindow(
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
title,
|
||||||
|
sharingWindow->_window_ptr
|
||||||
|
) );
|
||||||
|
}
|
||||||
|
|
||||||
static const std::string type;
|
OGLWindow(OGLWindow&&) = delete;
|
||||||
static const ErrorCodeMap ErrorMessages;
|
OGLWindow(const OGLWindow&) = delete;
|
||||||
};
|
~OGLWindow();
|
||||||
|
inline bool operator==(const OGLWindow& rhs) { return _window_ptr==rhs._window_ptr; }
|
||||||
|
|
||||||
class Window {
|
int getWindowAttribute(int) const;
|
||||||
public:
|
inline GLFWwindow* getWindowPtr() const { return _window_ptr; }
|
||||||
static bool StrictInitialization;
|
inline const GLFWwindow* getObjectArena() const { return _arena; }
|
||||||
|
inline std::string getTitle() const { return _title; }
|
||||||
|
std::array<int, 2> getSize() const;
|
||||||
|
bool shouldClose() const;
|
||||||
|
inline void pollEvents() const { CheckForMainThread(); glfwPollEvents(); }
|
||||||
|
inline void swapBuffers() const { glfwSwapBuffers(_window_ptr); }
|
||||||
|
|
||||||
static int getGLFWHint(int);
|
void setSize(unsigned int x, unsigned int y);
|
||||||
static int setGLFWHint(int, int);
|
void setTitle(const std::string&);
|
||||||
static Window* getCurrent();
|
void shouldClose(bool);
|
||||||
|
|
||||||
Window(const std::array<uint16_t, 2>, const char*, GLFWmonitor* initMonitor=NULL, GLFWwindow* initWindow=NULL);
|
template<WindowCallbacks CallbackType, typename CallbackFunc>
|
||||||
Window(const uint16_t, const uint16_t, const char*, GLFWmonitor* initMonitor=NULL, GLFWwindow* initWindow=NULL);
|
void setCallback(CallbackFunc callback) {
|
||||||
~Window();
|
CheckForMainThread();
|
||||||
|
auto tmp = makeCurrent();
|
||||||
int getWindowHint(int) const;
|
static auto this_ptr = this;
|
||||||
GLFWwindow* getWindow() const;
|
static typename OGLWindowCallbacks<CallbackType>::type tmp_callback = callback;
|
||||||
std::array<uint16_t, 2> getSize() const;
|
if constexpr (CallbackType == KEY) {
|
||||||
float getAspectRatio() const;
|
glfwSetKeyCallback(_window_ptr, [](GLFWwindow*, int a, int b, int c, int d){
|
||||||
std::string getName() const;
|
tmp_callback(this_ptr, a,b,c,d);
|
||||||
|
});
|
||||||
int init();
|
} else if constexpr (CallbackType == CHAR) {
|
||||||
int setWindowHint(int, int);
|
glfwSetCharCallback(_window_ptr, [](GLFWwindow*, int a) {
|
||||||
void setCurrent();
|
tmp_callback(this_ptr, a);
|
||||||
void resize(const std::array<uint16_t, 2>);
|
});
|
||||||
void resize(const uint16_t x, const uint16_t y);
|
} else if constexpr (CallbackType == CHAR_MODS) {
|
||||||
|
glfwSetCharModsCallback(_window_ptr, [](GLFWwindow*, unsigned int a, int b) {
|
||||||
protected:
|
tmp_callback(this_ptr, a, b);
|
||||||
static std::map<GLFWwindow*, Window*> WindowContexts;
|
});
|
||||||
static std::map<int, int> GLFWHints;
|
} else if constexpr (CallbackType == CURSOR_ENTER) {
|
||||||
static Window* _current;
|
glfwSetCursorEnterCallback(_window_ptr, [](GLFWwindow*, int a) {
|
||||||
static bool _glfw_init;
|
tmp_callback(this_ptr, a);
|
||||||
static bool checkKillGLFW();
|
});
|
||||||
|
} else if constexpr (CallbackType == CURSOR_POS) {
|
||||||
std::map<int, int> windowHints;
|
glfwSetCursorPosCallback(_window_ptr, [](GLFWwindow*, double a, double b){
|
||||||
std::array<uint16_t, 2> windowSize;
|
tmp_callback(this_ptr, a,b);
|
||||||
float _aspect_ratio;
|
});
|
||||||
std::string windowName;
|
} else if constexpr (CallbackType == MOUSE_BUTTON) {
|
||||||
GLFWwindow* window;
|
glfwSetMouseButtonCallback(_window_ptr, [](GLFWwindow*, int a, int b, int c){
|
||||||
GLFWmonitor* monitor;
|
tmp_callback(this_ptr, a,b,c);
|
||||||
GLFWwindow* shareWindow;
|
});
|
||||||
bool _init = false;
|
} else if constexpr (CallbackType == SCROLL) {
|
||||||
int gladResponse = -1;
|
glfwSetScrollCallback(_window_ptr, [](GLFWwindow*, double a, double b) {
|
||||||
|
tmp_callback(this_ptr, a,b);
|
||||||
|
});
|
||||||
|
} else if constexpr (CallbackType == DROP) {
|
||||||
|
glfwSetDropCallback(_window_ptr, [](GLFWwindow*, int a, const char** b) {
|
||||||
|
tmp_callback(this_ptr, a, b);
|
||||||
|
});
|
||||||
|
} else if constexpr (CallbackType == WINDOW_POS) {
|
||||||
|
glfwSetWindowPosCallback(_window_ptr, [](GLFWwindow*, int a, int b){
|
||||||
|
tmp_callback(this_ptr, a,b);
|
||||||
|
});
|
||||||
|
} else if constexpr (CallbackType == WINDOW_SIZE) {
|
||||||
|
glfwSetWindowSizeCallback(_window_ptr, [](GLFWwindow*, int a, int b){
|
||||||
|
tmp_callback(this_ptr, a,b);
|
||||||
|
});
|
||||||
|
} else if constexpr (CallbackType == WINDOW_CLOSE) {
|
||||||
|
glfwSetWindowCloseCallback(_window_ptr, [](GLFWwindow*){
|
||||||
|
tmp_callback(this_ptr);
|
||||||
|
});
|
||||||
|
} else if constexpr (CallbackType == WINDOW_REFRESH) {
|
||||||
|
glfwSetWindowRefreshCallback(_window_ptr, [](GLFWwindow*){
|
||||||
|
tmp_callback(this_ptr);
|
||||||
|
});
|
||||||
|
} else if constexpr (CallbackType == WINDOW_FOCUS) {
|
||||||
|
glfwSetWindowFocusCallback(_window_ptr, [](GLFWwindow*, int a) {
|
||||||
|
tmp_callback(this_ptr, a);
|
||||||
|
});
|
||||||
|
} else if constexpr (CallbackType == WINDOW_ICONIFY) {
|
||||||
|
glfwSetWindowIconifyCallback(_window_ptr, [](GLFWwindow*, int a) {
|
||||||
|
tmp_callback(this_ptr, a);
|
||||||
|
});
|
||||||
|
} else if constexpr (CallbackType == WINDOW_MAX) {
|
||||||
|
glfwSetWindowMaximizeCallback(_window_ptr, [](GLFWwindow*, int a) {
|
||||||
|
tmp_callback(this_ptr, a);
|
||||||
|
});
|
||||||
|
} else if constexpr (CallbackType == FRAMEBUFFER_SIZE) {
|
||||||
|
glfwSetFramebufferSizeCallback(_window_ptr, [](GLFWwindow*, int a, int b) {
|
||||||
|
tmp_callback(this_ptr, a, b);
|
||||||
|
});
|
||||||
|
} else if constexpr (CallbackType == WINDOW_SCALE) {
|
||||||
|
glfwSetWindowContentScaleCallback(_window_ptr, [](GLFWwindow*, int a, int b) {
|
||||||
|
tmp_callback(this_ptr, a, b);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
static_assert(false, "No matching callback.");
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -12,10 +12,21 @@ const std::string BufferError::type = "nb::BufferError";
|
|||||||
const ErrorCodeMap BufferError::ErrorMessages = {
|
const ErrorCodeMap BufferError::ErrorMessages = {
|
||||||
{ BufferErrorCodes::UNDEFINED, "Error" },
|
{ BufferErrorCodes::UNDEFINED, "Error" },
|
||||||
{ BufferErrorCodes::DATA_OVERFLOW, "Attempting buffer overflow" },
|
{ BufferErrorCodes::DATA_OVERFLOW, "Attempting buffer overflow" },
|
||||||
{BufferErrorCodes::DATA_ERROR, "Invalid memory operation"}
|
{ BufferErrorCodes::DATA_ERROR, "Invalid memory operation" },
|
||||||
|
{ BufferErrorCodes::BAD_DATA_TYPE, "Entered data type not allowed for buffer type"}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// class Buffer
|
||||||
|
|
||||||
|
Buffer::Buffer(GLenum target_) : OpenGLObject(), Target(target_) {
|
||||||
|
declare();
|
||||||
|
}
|
||||||
|
|
||||||
|
Buffer::Buffer(GLenum target_, const ByteVector& data_, GLenum usage_) : Buffer(target_) {
|
||||||
|
bind();
|
||||||
|
data(data_, usage_);
|
||||||
|
}
|
||||||
|
|
||||||
Buffer::Buffer(Buffer&& rval) : Target(rval.Target) {
|
Buffer::Buffer(Buffer&& rval) : Target(rval.Target) {
|
||||||
*this = std::move(rval);
|
*this = std::move(rval);
|
||||||
}
|
}
|
||||||
@@ -74,17 +85,21 @@ void Buffer::subdata(const ByteVector& data_, GLintptr offset_) {
|
|||||||
subdata(data_.data(), size_);
|
subdata(data_.data(), size_);
|
||||||
}
|
}
|
||||||
|
|
||||||
ElementBuffer::ElementBuffer() : Buffer(GL_ELEMENT_ARRAY_BUFFER) {}
|
// class UniformBuffer
|
||||||
|
UniformBuffer::UniformBuffer() : Buffer(GL_UNIFORM_BUFFER) {}
|
||||||
|
|
||||||
ByteVector ElementBuffer::data() const {
|
void UniformBuffer::bind() const {
|
||||||
return data();
|
Buffer::bind();
|
||||||
|
OPENGL_CALL(glBindBufferBase(GL_UNIFORM_BUFFER, _index, id()));
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t ElementBuffer::size() const { return _size / _typeSize; }
|
GLuint UniformBuffer::bufferIndex() const { return _index; }
|
||||||
|
|
||||||
size_t ElementBuffer::typeSize() const { return _typeSize; }
|
GLuint UniformBuffer::bufferIndex(GLuint idx) {
|
||||||
|
return (_index = idx);
|
||||||
|
}
|
||||||
|
|
||||||
GLenum ElementBuffer::glslType() const { return _glslType; }
|
// class ElementBuffer
|
||||||
|
|
||||||
template void ElementBuffer::data<uint8_t>(const std::vector<uint8_t>& data, GLenum usage);
|
template void ElementBuffer::data<uint8_t>(const std::vector<uint8_t>& data, GLenum usage);
|
||||||
template void ElementBuffer::data<uint16_t>(const std::vector<uint16_t>& data, GLenum usage);
|
template void ElementBuffer::data<uint16_t>(const std::vector<uint16_t>& data, GLenum usage);
|
||||||
@@ -93,11 +108,11 @@ template void ElementBuffer::data<uint8_t>(const uint8_t* const data, size_t siz
|
|||||||
template void ElementBuffer::data<uint16_t>(const uint16_t* const data, size_t size, GLenum usage);
|
template void ElementBuffer::data<uint16_t>(const uint16_t* const data, size_t size, GLenum usage);
|
||||||
template void ElementBuffer::data<uint32_t>(const uint32_t* const data, size_t size, GLenum usage);
|
template void ElementBuffer::data<uint32_t>(const uint32_t* const data, size_t size, GLenum usage);
|
||||||
|
|
||||||
template void ElementBuffer::subdata<uint8_t>(const uint8_t* const data, size_t size, size_t offset=0);
|
template void ElementBuffer::subdata<uint8_t>(const uint8_t* const data, size_t size, GLintptr offset=0);
|
||||||
template void ElementBuffer::subdata<uint16_t>(const uint16_t* const data, size_t size, size_t offset=0);
|
template void ElementBuffer::subdata<uint16_t>(const uint16_t* const data, size_t size, GLintptr offset=0);
|
||||||
template void ElementBuffer::subdata<uint32_t>(const uint32_t* const data, size_t size, size_t offset=0);
|
template void ElementBuffer::subdata<uint32_t>(const uint32_t* const data, size_t size, GLintptr offset=0);
|
||||||
template void ElementBuffer::subdata<uint8_t>(const std::vector<uint8_t>& data, size_t offset=0);
|
template void ElementBuffer::subdata<uint8_t>(const std::vector<uint8_t>& data, GLintptr offset=0);
|
||||||
template void ElementBuffer::subdata<uint16_t>(const std::vector<uint16_t>& data, size_t offset=0);
|
template void ElementBuffer::subdata<uint16_t>(const std::vector<uint16_t>& data, GLintptr offset=0);
|
||||||
template void ElementBuffer::subdata<uint32_t>(const std::vector<uint32_t>& data, size_t offset=0);
|
template void ElementBuffer::subdata<uint32_t>(const std::vector<uint32_t>& data, GLintptr offset=0);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,307 +0,0 @@
|
|||||||
#include <NBGraphics/Draw.hpp>
|
|
||||||
|
|
||||||
namespace NB{
|
|
||||||
|
|
||||||
DrawError::DrawError(
|
|
||||||
const std::string& msg,
|
|
||||||
const std::string& file,
|
|
||||||
int line) : std::runtime_error(NB::formatDebugString(msg, file, line)) {}
|
|
||||||
|
|
||||||
uint8_t VerticesFromPrimitives(GLenum type, unsigned int num_prims) {
|
|
||||||
switch(type) {
|
|
||||||
case GL_POINTS:
|
|
||||||
return num_prims;
|
|
||||||
case GL_LINES:
|
|
||||||
return num_prims*2;
|
|
||||||
break;
|
|
||||||
case GL_LINE_STRIP:
|
|
||||||
return num_prims+1;
|
|
||||||
break;
|
|
||||||
case GL_LINE_LOOP:
|
|
||||||
return num_prims;
|
|
||||||
break;
|
|
||||||
case GL_TRIANGLES:
|
|
||||||
return num_prims*3;
|
|
||||||
break;
|
|
||||||
case GL_TRIANGLE_STRIP:
|
|
||||||
case GL_TRIANGLE_FAN:
|
|
||||||
return num_prims+2;
|
|
||||||
break;
|
|
||||||
case GL_QUADS:
|
|
||||||
return 4*num_prims;
|
|
||||||
break;
|
|
||||||
case GL_QUAD_STRIP:
|
|
||||||
return 2*num_prims+2;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return num_prims;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t PrimitivesFromVertices(GLenum type, unsigned int num_verts) {
|
|
||||||
switch(type) {
|
|
||||||
case GL_POINTS:
|
|
||||||
return num_verts;
|
|
||||||
case GL_LINES:
|
|
||||||
return num_verts/2;
|
|
||||||
break;
|
|
||||||
case GL_LINE_STRIP:
|
|
||||||
return num_verts-1;
|
|
||||||
break;
|
|
||||||
case GL_LINE_LOOP:
|
|
||||||
return num_verts;
|
|
||||||
break;
|
|
||||||
case GL_TRIANGLES:
|
|
||||||
return num_verts/3;
|
|
||||||
break;
|
|
||||||
case GL_TRIANGLE_STRIP:
|
|
||||||
case GL_TRIANGLE_FAN:
|
|
||||||
return num_verts-2;
|
|
||||||
break;
|
|
||||||
case GL_QUADS:
|
|
||||||
return num_verts/4;
|
|
||||||
break;
|
|
||||||
case GL_QUAD_STRIP:
|
|
||||||
return num_verts/2 - 1;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return num_verts;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// DrawBuffer class
|
|
||||||
DrawBuffer::DrawBuffer(VertexAttributeList vas, GLenum prim_type)
|
|
||||||
: _gl_primitive_type(prim_type), _VAO() {
|
|
||||||
_vert_data = std::make_shared<Buffer>(GL_ARRAY_BUFFER);
|
|
||||||
_elmt_data = std::make_shared<Buffer>(GL_ELEMENT_ARRAY_BUFFER);
|
|
||||||
_VAO.addVBO(_vert_data);
|
|
||||||
_VAO.changeEBO(_elmt_data);
|
|
||||||
try {
|
|
||||||
throw _VAO.checkValid(vas);
|
|
||||||
} catch (VAOError vaoe) {
|
|
||||||
if (vaoe.error) {
|
|
||||||
_VAO.addVertexAttributes(DrawBuffer::defaultVAO(vas));
|
|
||||||
} else {
|
|
||||||
_VAO.addVertexAttributes(vas);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexAttributeList DrawBuffer::defaultVAO(VertexAttributeList vert_attrs) {
|
|
||||||
GLuint vert_id = _vert_data->id();
|
|
||||||
unsigned int vert_size = 0;
|
|
||||||
for (const VertexAttribute& va : vert_attrs) {
|
|
||||||
vert_size += va.GLSLSize * GLSLTypeSize(va.GLSLType);
|
|
||||||
}
|
|
||||||
unsigned int curr_byte = 0;
|
|
||||||
for (VertexAttribute& va : vert_attrs) {
|
|
||||||
va.ptr.buffer = vert_id;
|
|
||||||
va.ptr.stride = vert_size;
|
|
||||||
va.ptr.offset = curr_byte;
|
|
||||||
va.ptr.divisor = 0;
|
|
||||||
curr_byte += va.GLSLSize * GLSLTypeSize(va.GLSLType);
|
|
||||||
}
|
|
||||||
return vert_attrs;
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexAttributeList DrawBuffer::changeVertLayout(unsigned int i, VertexAttributePointer vap) {
|
|
||||||
GLuint vert_id = _vert_data->id();
|
|
||||||
vap.buffer = (vap.buffer) ? vap.buffer : vert_id;
|
|
||||||
VertexAttributeList vas = _VAO.getLayout();
|
|
||||||
unsigned int idx = 0;
|
|
||||||
for (const VertexAttribute& va : vas) {
|
|
||||||
if (va.ptr.buffer == vert_id) {
|
|
||||||
if (idx == i) {
|
|
||||||
return _VAO.changeLayout(i, vap);
|
|
||||||
}
|
|
||||||
idx++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
THROW_DRAW_ERROR("Could not find vertex attribute with index " + std::to_string(i));
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int DrawBuffer::vertSize() const { return _VAO.vertSize(_vert_data->id()); }
|
|
||||||
|
|
||||||
void DrawBuffer::draw() const {
|
|
||||||
_VAO.bind();
|
|
||||||
glDrawElements(_gl_primitive_type, _elmt_data->size() / GLSLTypeSize(GL_INT), GL_UNSIGNED_INT, 0);
|
|
||||||
_VAO.unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int DrawBuffer::elementBufferID() const { return _elmt_data->id(); }
|
|
||||||
|
|
||||||
unsigned int DrawBuffer::vertexBufferID() const { return _vert_data->id(); }
|
|
||||||
|
|
||||||
RawVec DrawBuffer::vertAttrData(unsigned int i) {
|
|
||||||
if (!_vert_data->isInitialized()) {
|
|
||||||
throw DrawError("Vertex buffer is not initialized.");
|
|
||||||
}
|
|
||||||
return _VAO.attributeData(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
RawVec DrawBuffer::vertAttrData(unsigned int i, const RawVec& newdata) {
|
|
||||||
if (!(_vert_data->isInitialized())) {
|
|
||||||
VertexAttribute va = _VAO.getLayout()[i];
|
|
||||||
vertexData(newdata.size() / (va.GLSLSize*GLSLTypeSize(va.GLSLType)));
|
|
||||||
std::cout << "From DrawBuffer::vertAttrData(int, RawVec) - vert buf size: " << _vert_data->size() << "\n";
|
|
||||||
}
|
|
||||||
return _VAO.attributeData(i, newdata);
|
|
||||||
}
|
|
||||||
|
|
||||||
void DrawBuffer::elementData(unsigned int num_prims) {
|
|
||||||
elementData(std::vector<unsigned int>(VerticesFromPrimitives(_gl_primitive_type, num_prims)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void DrawBuffer::elementData(const std::vector<unsigned int>& elements) {
|
|
||||||
_num_prims = PrimitivesFromVertices(_gl_primitive_type, elements.size());
|
|
||||||
_elmt_data->data(vectorToRaw(elements));
|
|
||||||
}
|
|
||||||
|
|
||||||
void DrawBuffer::elementData(const std::vector<unsigned int>& elements, unsigned int offset) {
|
|
||||||
_VAO.bind();
|
|
||||||
if (!_elmt_data->isInitialized()) {
|
|
||||||
std::vector<unsigned int> init_elmts(elements);
|
|
||||||
init_elmts.insert(init_elmts.cbegin(), offset, 0);
|
|
||||||
elementData(init_elmts, 0);
|
|
||||||
} else {
|
|
||||||
_elmt_data->data(vectorToRaw(elements), offset);
|
|
||||||
}
|
|
||||||
_VAO.unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
void DrawBuffer::vertexData(unsigned int num_verts) {
|
|
||||||
vertexData(RawVec(num_verts * vertSize()));
|
|
||||||
}
|
|
||||||
|
|
||||||
void DrawBuffer::vertexData(const RawVec& vert_data) {
|
|
||||||
// Check for uninitialized Vertex Pointers. If so, will fallback to default VAO setup.
|
|
||||||
// Otherwise, set VAO to provided Vertex Atributes.
|
|
||||||
int vert_size = vertSize();
|
|
||||||
if (vert_data.size() % vert_size != 0) {
|
|
||||||
THROW_DRAW_ERROR("Initializing data does not match vertex size of " + std::to_string(vert_size) + ".");
|
|
||||||
}
|
|
||||||
_num_verts = vert_data.size() / vert_size;
|
|
||||||
// TODO: Make it not static draw all the time
|
|
||||||
|
|
||||||
_VAO.bind();
|
|
||||||
_vert_data->data(vert_data);
|
|
||||||
_VAO.unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
void DrawBuffer::vertexData(const RawVec& data, unsigned int offset) {
|
|
||||||
_VAO.bind();
|
|
||||||
if (!_vert_data->isInitialized()) {
|
|
||||||
RawVec load_data(offset);
|
|
||||||
load_data.insert(load_data.end(), data.begin(), data.end());
|
|
||||||
vertexData(load_data, 0);
|
|
||||||
} else {
|
|
||||||
try{
|
|
||||||
_vert_data->data(data, offset);
|
|
||||||
} catch(BufferError& e) {
|
|
||||||
THROW_DRAW_ERROR("NB::BufferError in loadVBO:\n\t" + std::string(e.what()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_VAO.unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
// DrawInstanceBufferClass
|
|
||||||
DrawInstanceBuffer::DrawInstanceBuffer(
|
|
||||||
VertexAttributeList vert_vas,
|
|
||||||
VertexAttributeList inst_vas,
|
|
||||||
GLenum prim_type
|
|
||||||
): DrawBuffer(vert_vas, prim_type) {
|
|
||||||
_inst_data = std::make_shared<Buffer>(GL_ARRAY_BUFFER);
|
|
||||||
_VAO.addVBO(_inst_data);
|
|
||||||
try {
|
|
||||||
throw _VAO.checkValid(inst_vas);
|
|
||||||
} catch (VAOError vaoe) {
|
|
||||||
if (vaoe.error) {
|
|
||||||
_VAO.addVertexAttributes(defaultVAO(inst_vas));
|
|
||||||
} else {
|
|
||||||
_VAO.addVertexAttributes(inst_vas);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int DrawInstanceBuffer::instSize() const { return _VAO.vertSize(_inst_data->id()); }
|
|
||||||
|
|
||||||
unsigned int DrawInstanceBuffer::instanceBufferID() const { return _inst_data->id(); }
|
|
||||||
|
|
||||||
void DrawInstanceBuffer::instanceData(unsigned int num_inst) {
|
|
||||||
instanceData(RawVec(instSize() * num_inst));
|
|
||||||
}
|
|
||||||
|
|
||||||
void DrawInstanceBuffer::instanceData(const RawVec& inst_data) {
|
|
||||||
int inst_size = instSize();
|
|
||||||
if (inst_data.size() % inst_size != 0) {
|
|
||||||
THROW_DRAW_ERROR("Initializing data does not match instance size of " + std::to_string(inst_size) + ".");
|
|
||||||
}
|
|
||||||
_num_inst = inst_data.size() / inst_size;
|
|
||||||
|
|
||||||
_VAO.bind();
|
|
||||||
_inst_data->data(inst_data);
|
|
||||||
_VAO.unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
void DrawInstanceBuffer::instanceData(const RawVec& data, unsigned int offset) {
|
|
||||||
_VAO.bind();
|
|
||||||
if (!_inst_data->isInitialized()) {
|
|
||||||
RawVec load_data(offset);
|
|
||||||
load_data.insert(load_data.end(), data.begin(), data.end());
|
|
||||||
instanceData(load_data, 0);
|
|
||||||
} else {
|
|
||||||
try{
|
|
||||||
_inst_data->data(data, offset);
|
|
||||||
} catch(BufferError& e) {
|
|
||||||
THROW_DRAW_ERROR("NB::BufferError in loadIBO:\n\t" + std::string(e.what()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_VAO.unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexAttributeList DrawInstanceBuffer::defaultVAO(VertexAttributeList inst_attrs) {
|
|
||||||
GLuint inst_id = _inst_data->id();
|
|
||||||
unsigned int vert_size = 0;
|
|
||||||
for (const VertexAttribute& va : inst_attrs) {
|
|
||||||
vert_size += va.GLSLSize * GLSLTypeSize(va.GLSLType);
|
|
||||||
}
|
|
||||||
unsigned int curr_byte = 0;
|
|
||||||
for (VertexAttribute& va : inst_attrs) {
|
|
||||||
va.ptr.buffer = inst_id;
|
|
||||||
va.ptr.stride = vert_size;
|
|
||||||
va.ptr.offset = curr_byte;
|
|
||||||
va.ptr.divisor = 1;
|
|
||||||
curr_byte += va.GLSLSize * GLSLTypeSize(va.GLSLType);
|
|
||||||
}
|
|
||||||
return inst_attrs;
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexAttributeList DrawInstanceBuffer::changeInstanceLayout(unsigned int i, VertexAttributePointer vap) {
|
|
||||||
GLuint inst_id = _inst_data->id();
|
|
||||||
vap.buffer = (vap.buffer) ? vap.buffer : inst_id;
|
|
||||||
VertexAttributeList vas = _VAO.getLayout();
|
|
||||||
unsigned int vas_size = vas.size();
|
|
||||||
unsigned int inst_idx = 0;
|
|
||||||
unsigned int idx = 0;
|
|
||||||
for (const VertexAttribute& va : vas) {
|
|
||||||
if (va.ptr.buffer == inst_id) {
|
|
||||||
if (inst_idx == i) {
|
|
||||||
return _VAO.changeLayout(idx, vap);
|
|
||||||
}
|
|
||||||
inst_idx++;
|
|
||||||
}
|
|
||||||
idx++;
|
|
||||||
}
|
|
||||||
THROW_DRAW_ERROR("Could not find instance attribute with index " + std::to_string(i));
|
|
||||||
}
|
|
||||||
|
|
||||||
void DrawInstanceBuffer::draw() const {
|
|
||||||
_VAO.bind();
|
|
||||||
glDrawElementsInstanced(_gl_primitive_type, _elmt_data->size() / GLSLTypeSize(GL_UNSIGNED_INT), GL_UNSIGNED_INT, (void*)_elmt_data->id(), _num_inst);
|
|
||||||
_VAO.unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
#pragma clang diagnostic ignored "-Wint-to-pointer-cast"
|
||||||
|
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
|
||||||
|
|
||||||
|
#include <NBGraphics/Mesh.hpp>
|
||||||
|
|
||||||
|
namespace nb{
|
||||||
|
|
||||||
|
// class Mesh
|
||||||
|
Mesh::Mesh(
|
||||||
|
VertexDataPointerList vertexdata_,
|
||||||
|
unsigned int count_,
|
||||||
|
GLenum primitive_
|
||||||
|
) :
|
||||||
|
_primitive(primitive_),
|
||||||
|
_count(count_),
|
||||||
|
_vao(std::make_shared<VAO>(vertexdata_))
|
||||||
|
{}
|
||||||
|
|
||||||
|
Mesh::Mesh(
|
||||||
|
GLenum primitive_,
|
||||||
|
const std::shared_ptr<VAO>& vao_,
|
||||||
|
unsigned int count_
|
||||||
|
) :
|
||||||
|
_primitive(primitive_),
|
||||||
|
_count(count_),
|
||||||
|
_vao(vao_)
|
||||||
|
{}
|
||||||
|
|
||||||
|
Mesh::Mesh(
|
||||||
|
GLenum primitive_,
|
||||||
|
const VertexData& vertex_data_,
|
||||||
|
unsigned int count_
|
||||||
|
) : Mesh(primitive_, std::make_shared<VAO>(), count_) {
|
||||||
|
_vao->attributes({vertex_data_});
|
||||||
|
}
|
||||||
|
|
||||||
|
Mesh::Mesh(
|
||||||
|
GLenum primitive_,
|
||||||
|
const ByteVector& data_,
|
||||||
|
unsigned int count_,
|
||||||
|
const std::vector<VertexAttribute>& attributes_
|
||||||
|
) : Mesh(
|
||||||
|
primitive_,
|
||||||
|
VertexData(data_, attributes_),
|
||||||
|
count_
|
||||||
|
) {}
|
||||||
|
|
||||||
|
void Mesh::draw(unsigned int offset, unsigned int count) const {
|
||||||
|
_vao->bind();
|
||||||
|
OPENGL_CALL(
|
||||||
|
glDrawArrays(_primitive, offset, count);
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// class InstancedMesh
|
||||||
|
|
||||||
|
void InstancedMesh::draw(unsigned int offset, unsigned instancecount) const {
|
||||||
|
_vao->bind();
|
||||||
|
OPENGL_CALL(
|
||||||
|
glDrawArraysInstanced(_primitive, 0, _count, instancecount);
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// class IndexMeshes
|
||||||
|
|
||||||
|
void IndexedMesh::draw(unsigned int offset, unsigned numindices) const {
|
||||||
|
_vao->bind();
|
||||||
|
OPENGL_CALL(
|
||||||
|
glDrawElements(Mesh::_primitive, _count, _ebo->datatype(), (void*)offset)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <NBGraphics/OGLObjects.hpp>
|
#include <NBGraphics/OGLObjects.hpp>
|
||||||
|
#include <NBGraphics/Window.hpp>
|
||||||
|
|
||||||
#ifndef GLENUMSTRPAIR
|
#ifndef GLENUMSTRPAIR
|
||||||
#define GLENUMSTRPAIR(x) {x, #x}
|
#define GLENUMSTRPAIR(x) {x, #x}
|
||||||
@@ -10,7 +11,8 @@ const std::string OpenGLObjectError::type = "nb::OpenGLObjectError";
|
|||||||
const ErrorCodeMap OpenGLObjectError::ErrorMessages = {
|
const ErrorCodeMap OpenGLObjectError::ErrorMessages = {
|
||||||
{ OpenGLObjectError::Codes::UNDEFINED, "Error" },
|
{ OpenGLObjectError::Codes::UNDEFINED, "Error" },
|
||||||
{OpenGLObjectError::Codes::HANGING_OBJECT, "Attempting to leave a hanging OpenGL object"},
|
{OpenGLObjectError::Codes::HANGING_OBJECT, "Attempting to leave a hanging OpenGL object"},
|
||||||
{OpenGLObjectError::Codes::INVALID_OBJECT, "Attempting operation with invalid object"}
|
{OpenGLObjectError::Codes::INVALID_OBJECT, "Attempting operation with invalid object"},
|
||||||
|
{OpenGLObjectError::Codes::INVALID_CONTEXT, "Object not valid with current OpengGL context"}
|
||||||
};
|
};
|
||||||
|
|
||||||
const std::string OpenGLError::type = "nb::OpenGLError";
|
const std::string OpenGLError::type = "nb::OpenGLError";
|
||||||
@@ -30,11 +32,39 @@ OpenGLError OpenGLError::status() {
|
|||||||
return OpenGLError(glGetError());
|
return OpenGLError(glGetError());
|
||||||
}
|
}
|
||||||
|
|
||||||
OpenGLObject::OpenGLObject(OpenGLObject&& rval) {
|
OpenGLError OpenGLError::status(const std::string& note) {
|
||||||
|
return OpenGLError(glGetError(), note);
|
||||||
|
}
|
||||||
|
|
||||||
|
const ConstantMap<TypeCodes, int> TypeToGLSL = {
|
||||||
|
{TypeCodes::FLOAT16, GL_HALF_FLOAT},
|
||||||
|
{TypeCodes::FLOAT32, GL_FLOAT},
|
||||||
|
{TypeCodes::DOUBLE64, GL_DOUBLE},
|
||||||
|
{TypeCodes::INT8, GL_BYTE},
|
||||||
|
{TypeCodes::INT16, GL_SHORT},
|
||||||
|
{TypeCodes::INT32, GL_INT},
|
||||||
|
{TypeCodes::UINT8, GL_UNSIGNED_BYTE},
|
||||||
|
{TypeCodes::UINT16, GL_UNSIGNED_SHORT},
|
||||||
|
{TypeCodes::UINT32, GL_UNSIGNED_INT},
|
||||||
|
};
|
||||||
|
|
||||||
|
// class OpenGLObject
|
||||||
|
|
||||||
|
void OpenGLObject::checkObjectContextValidity() {
|
||||||
|
if (_arena != OGLWindow::GetCurrentObjectArena()) {
|
||||||
|
THROW(OpenGLObjectError(OpenGLObjectError::INVALID_CONTEXT));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
OpenGLObject::OpenGLObject(OpenGLObject&& rval) : _arena(rval._arena) {
|
||||||
|
checkObjectContextValidity();
|
||||||
*this = std::move(rval);
|
*this = std::move(rval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
OpenGLObject::OpenGLObject() : _arena(OGLWindow::GetCurrentObjectArena()) {}
|
||||||
|
|
||||||
OpenGLObject& OpenGLObject::operator=(OpenGLObject&& rhs) {
|
OpenGLObject& OpenGLObject::operator=(OpenGLObject&& rhs) {
|
||||||
|
rhs.checkObjectContextValidity();
|
||||||
if (_id) { THROW(OpenGLObjectError(Codes::HANGING_OBJECT)); }
|
if (_id) { THROW(OpenGLObjectError(Codes::HANGING_OBJECT)); }
|
||||||
_id = rhs._id;
|
_id = rhs._id;
|
||||||
rhs._id = 0;
|
rhs._id = 0;
|
||||||
@@ -60,6 +90,10 @@ const GLEnumTableType GLenumTable = {
|
|||||||
GLENUMSTRPAIR(GL_RGB8UI),
|
GLENUMSTRPAIR(GL_RGB8UI),
|
||||||
GLENUMSTRPAIR(GL_RGBA8UI),
|
GLENUMSTRPAIR(GL_RGBA8UI),
|
||||||
GLENUMSTRPAIR(GL_RGBA),
|
GLENUMSTRPAIR(GL_RGBA),
|
||||||
|
GLENUMSTRPAIR(GL_ELEMENT_ARRAY_BUFFER),
|
||||||
|
GLENUMSTRPAIR(GL_UNSIGNED_INT),
|
||||||
|
GLENUMSTRPAIR(GL_UNSIGNED_SHORT),
|
||||||
|
GLENUMSTRPAIR(GL_UNSIGNED_BYTE),
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
@@ -15,6 +15,8 @@ const ErrorCodeMap ProgramError::ErrorMessages = {
|
|||||||
{ProgramErrCodes::LINKING_ERROR, "Linker error"}
|
{ProgramErrCodes::LINKING_ERROR, "Linker error"}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// class Shader
|
||||||
|
|
||||||
Shader::Shader(GLenum target_, const std::string& source_)
|
Shader::Shader(GLenum target_, const std::string& source_)
|
||||||
: Shader(target_, std::vector<std::string>({source_})) {}
|
: Shader(target_, std::vector<std::string>({source_})) {}
|
||||||
|
|
||||||
@@ -54,6 +56,8 @@ std::string Shader::log() const {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// class Program
|
||||||
|
|
||||||
Program::Program(SharedVector<Shader> shaders_) {
|
Program::Program(SharedVector<Shader> shaders_) {
|
||||||
declare();
|
declare();
|
||||||
for (auto shad_ptr : shaders_) {
|
for (auto shad_ptr : shaders_) {
|
||||||
@@ -83,6 +87,26 @@ std::string Program::log() const {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Program::setUniform(const std::string& blockname, std::shared_ptr<UniformBuffer> buffer) {
|
||||||
|
declare();
|
||||||
|
auto idx = OPENGL_CALL(glGetUniformBlockIndex(id(), blockname.data()));
|
||||||
|
if (idx == GL_INVALID_INDEX) {
|
||||||
|
THROW("something");
|
||||||
|
}
|
||||||
|
this->setUniformBinding(blockname, buffer->bufferIndex());
|
||||||
|
OPENGL_CALL(glUniformBlockBinding(
|
||||||
|
id(),
|
||||||
|
idx,
|
||||||
|
_uniformBindings[blockname]
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Program::setUniformBinding(const std::string& blockname, GLuint index) {
|
||||||
|
_uniformBindings[blockname] = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
GLuint Program::getUniformBinding(const std::string& blockname) const {
|
||||||
|
return _uniformBindings.at(blockname);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,33 @@
|
|||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
|
// class RenderTarget
|
||||||
|
|
||||||
|
RenderTarget::RenderTarget(RenderTarget&& mv) : Target(mv.Target) {
|
||||||
|
*this = std::move(mv);
|
||||||
|
}
|
||||||
|
|
||||||
|
RenderTarget& RenderTarget::operator=(RenderTarget&& mv) {
|
||||||
|
if (mv.Target != Target) {
|
||||||
|
THROW(OpenGLObjectError(
|
||||||
|
OpenGLObjectError::INVALID_OBJECT,
|
||||||
|
"Cannot assign RenderTarget of different types."
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Base::operator=(std::move(mv));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// class Texture
|
||||||
Texture::Texture(GLenum target_) : Base(target_) {}
|
Texture::Texture(GLenum target_) : Base(target_) {}
|
||||||
|
|
||||||
|
Texture::Texture(Texture&& mv) : Base(mv.Target) {
|
||||||
|
*this = std::move(mv);
|
||||||
|
}
|
||||||
|
|
||||||
|
Texture& Texture::operator=(Texture&& mv) {
|
||||||
|
Base::operator=(std::move(mv));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
|
#pragma clang diagnostic ignored "-Wint-to-pointer-cast"
|
||||||
|
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
|
||||||
|
|
||||||
#include <NBGraphics/VertexArray.hpp>
|
#include <NBGraphics/VertexArray.hpp>
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
@@ -16,7 +19,7 @@ const ErrorCodeMap VertexError::ErrorMessages = {
|
|||||||
{ VertexError::MISALIGNED_DATA, "Data does not match vertex layout"}
|
{ VertexError::MISALIGNED_DATA, "Data does not match vertex layout"}
|
||||||
};
|
};
|
||||||
|
|
||||||
VAO::VAO(const VertexAttributePointerList& attr_ptrs) {
|
VAO::VAO(const VertexDataPointerList& attr_ptrs) {
|
||||||
attributes(attr_ptrs);
|
attributes(attr_ptrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,41 +29,58 @@ VAO::VAO(VAO&& other) {
|
|||||||
|
|
||||||
VAO& VAO::operator=(VAO&& rhs) {
|
VAO& VAO::operator=(VAO&& rhs) {
|
||||||
OpenGLObject::operator=(std::move(rhs));
|
OpenGLObject::operator=(std::move(rhs));
|
||||||
_attrs = rhs._attrs;
|
_vertexdata = rhs._vertexdata;
|
||||||
rhs._attrs = {};
|
rhs._vertexdata = {};
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexAttributePointerList VAO::attributes() const {
|
VertexDataPointerList VAO::attributes() const {
|
||||||
return _attrs;
|
return _vertexdata;
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexAttributePointerList VAO::attributes(const VertexAttributePointerList& attrs_) {
|
VertexDataPointerList VAO::attributes(const VertexDataPointerList& vertdatalist) {
|
||||||
declare();
|
declare();
|
||||||
disable();
|
disable();
|
||||||
_attrs = attrs_;
|
_vertexdata = vertdatalist;
|
||||||
bind();
|
bind();
|
||||||
for (auto attr_ptr : attrs_) {
|
for (const auto vdata : _vertexdata) {
|
||||||
OPENGL_CALL(glBindBuffer(GL_ARRAY_BUFFER, attr_ptr.buffer));
|
OPENGL_CALL(glBindBuffer(GL_ARRAY_BUFFER, vdata.vertexdata->data->id()));
|
||||||
GLuint idx = attr_ptr.index;
|
for (const auto attr : vdata.vertexdata->attributes) {
|
||||||
OPENGL_CALL(glVertexAttribPointer(
|
GLuint idx = vdata.index;
|
||||||
idx,
|
OPENGL_CALL(glVertexAttribPointer(
|
||||||
attr_ptr.attribute.GLSLSize,
|
idx,
|
||||||
attr_ptr.attribute.GLSLType,
|
attr.size,
|
||||||
attr_ptr.attribute.GLSLNormalization,
|
TypeToGLSL[attr.type],
|
||||||
attr_ptr.attribute.layout.stride,
|
attr.normalized,
|
||||||
(void*)attr_ptr.attribute.layout.offset
|
attr.layout.stride,
|
||||||
));
|
(const void*) attr.layout.offset
|
||||||
OPENGL_CALL(glEnableVertexAttribArray(idx));
|
));
|
||||||
|
OPENGL_CALL(glEnableVertexAttribArray(idx));
|
||||||
|
if (attr.layout.divisor) {
|
||||||
|
OPENGL_CALL(glVertexAttribDivisor(idx, attr.layout.divisor));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
unbind();
|
unbind();
|
||||||
return _attrs;
|
enable();
|
||||||
|
return _vertexdata;
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexAttributePointer VAO::attr(GLuint idx) const {
|
VertexDataPointerList VAO::attributes(const VertexDataList& vertdatalist) {
|
||||||
for (auto attr_ptr : _attrs) {
|
VertexDataPointerList vdatas;
|
||||||
if (idx == attr_ptr.index) {
|
for (unsigned int i = 0; i < vertdatalist.size(); ++i) {
|
||||||
return attr_ptr;
|
vdatas.emplace_back(VertexDataPointer{
|
||||||
|
std::make_shared<VertexData>(vertdatalist[i]),
|
||||||
|
i
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return attributes(vdatas);
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexDataPointer VAO::attribute(GLuint idx) const {
|
||||||
|
for (auto vdata : _vertexdata) {
|
||||||
|
if (idx == vdata.index) {
|
||||||
|
return vdata;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
THROW(VAOError(Codes::INVALID_ATTRIBUTE));
|
THROW(VAOError(Codes::INVALID_ATTRIBUTE));
|
||||||
@@ -69,8 +89,8 @@ VertexAttributePointer VAO::attr(GLuint idx) const {
|
|||||||
void VAO::enable() const {
|
void VAO::enable() const {
|
||||||
if (_id) {
|
if (_id) {
|
||||||
bind();
|
bind();
|
||||||
for(auto attr_ptr : _attrs) {
|
for(auto vdata : _vertexdata) {
|
||||||
OPENGL_CALL(glEnableVertexAttribArray(attr_ptr.index));
|
OPENGL_CALL(glEnableVertexAttribArray(vdata.index));
|
||||||
}
|
}
|
||||||
unbind();
|
unbind();
|
||||||
}
|
}
|
||||||
@@ -79,7 +99,7 @@ void VAO::enable() const {
|
|||||||
void VAO::enable(GLuint idx) const {
|
void VAO::enable(GLuint idx) const {
|
||||||
if(_id) {
|
if(_id) {
|
||||||
bind();
|
bind();
|
||||||
OPENGL_CALL(glEnableVertexAttribArray(attr(idx).index));
|
OPENGL_CALL(glEnableVertexAttribArray(attribute(idx).index));
|
||||||
unbind();
|
unbind();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -87,8 +107,8 @@ void VAO::enable(GLuint idx) const {
|
|||||||
void VAO::disable() const {
|
void VAO::disable() const {
|
||||||
if(_id) {
|
if(_id) {
|
||||||
bind();
|
bind();
|
||||||
for(auto attr_ptr : _attrs) {
|
for(auto vdata : _vertexdata) {
|
||||||
OPENGL_CALL(glDisableVertexAttribArray(attr_ptr.index));
|
OPENGL_CALL(glDisableVertexAttribArray(vdata.index));
|
||||||
}
|
}
|
||||||
unbind();
|
unbind();
|
||||||
}
|
}
|
||||||
@@ -97,157 +117,10 @@ void VAO::disable() const {
|
|||||||
void VAO::disable(GLuint idx) const {
|
void VAO::disable(GLuint idx) const {
|
||||||
if(_id) {
|
if(_id) {
|
||||||
bind();
|
bind();
|
||||||
OPENGL_CALL(glDisableVertexAttribArray(attr(idx).index));
|
OPENGL_CALL(glDisableVertexAttribArray(attribute(idx).index));
|
||||||
unbind();
|
unbind();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexGroup::VertexGroup(VertexGroup&& other) {
|
|
||||||
*this = std::move(other);
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexGroup& VertexGroup::operator=(VertexGroup&& rhs) {
|
|
||||||
_vertex_data = rhs._vertex_data;
|
|
||||||
_ebo = rhs._ebo;
|
|
||||||
_vao = rhs._vao;
|
|
||||||
|
|
||||||
rhs._vertex_data = {};
|
|
||||||
rhs._ebo = nullptr;
|
|
||||||
rhs._vao = nullptr;
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
void VertexGroup::draw() const {
|
|
||||||
bind();
|
|
||||||
OPENGL_CALL(
|
|
||||||
glDrawElements(primitive, _ebo->size(), _ebo->glslType(), 0)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t VertexGroup::addBuffer(const VertexData& vertex_data_) {
|
|
||||||
VertexData data = vertex_data_;
|
|
||||||
_vertex_data.emplace_back(data);
|
|
||||||
VertexAttributePointerList attrs = _vao->attributes();
|
|
||||||
GLuint idx = attrs.size();
|
|
||||||
for (const auto& attr : data.attrs) {
|
|
||||||
attrs.emplace_back(
|
|
||||||
VertexAttributePointer{attr, data.vbo->id(),idx}
|
|
||||||
);
|
|
||||||
idx++;
|
|
||||||
}
|
|
||||||
_vao->attributes(attrs);
|
|
||||||
return _vertex_data.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t VertexGroup::addBuffer(const ByteVector& data_, const VertexAttributeList& attrs_) {
|
|
||||||
return addBuffer({
|
|
||||||
std::make_shared<VBO>(data_),
|
|
||||||
attrs_
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexData VertexGroup::getBuffers(size_t idx) {
|
|
||||||
return _vertex_data[idx];
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexDataVec VertexGroup::getBuffers() {
|
|
||||||
return _vertex_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t VertexGroup::setBuffers(const VertexDataVec& vertex_data_) {
|
|
||||||
_vertex_data = vertex_data_;
|
|
||||||
VertexAttributePointerList attr_ptrs;
|
|
||||||
GLuint idx = 0;
|
|
||||||
for (const auto& data : _vertex_data) {
|
|
||||||
for (const auto& attr : data.attrs) {
|
|
||||||
attr_ptrs.emplace_back(
|
|
||||||
VertexAttributePointer{attr, data.vbo->id(), idx}
|
|
||||||
);
|
|
||||||
idx++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_vao->attributes(attr_ptrs);
|
|
||||||
return _vertex_data.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexDataVec VertexGroup::dropBuffers() {
|
|
||||||
auto ret = _vertex_data;
|
|
||||||
setBuffers({});
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexData VertexGroup::dropBuffer(size_t idx) {
|
|
||||||
if (idx >= _vertex_data.size()) {
|
|
||||||
THROW(Error(Error<>::OUT_OF_RANGE));
|
|
||||||
}
|
|
||||||
VertexDataVec tmp;
|
|
||||||
VertexData ret;
|
|
||||||
size_t i = 0;
|
|
||||||
for (auto buf : _vertex_data) {
|
|
||||||
if (i == idx) {
|
|
||||||
ret = tmp[idx];
|
|
||||||
} else {
|
|
||||||
tmp.emplace_back(buf);
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
setBuffers(tmp);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexAttributePointer VertexGroup::attribute(size_t idx_) const {
|
|
||||||
return _vao->attr(idx_);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VertexGroup::enableAttr(GLuint idx) {
|
|
||||||
bind();
|
|
||||||
_vao->enable(idx);
|
|
||||||
unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
void VertexGroup::disableAttr(GLuint idx) {
|
|
||||||
bind();
|
|
||||||
_vao->disable(idx);
|
|
||||||
unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
void VertexGroup::disableBuffer(size_t idx) {
|
|
||||||
auto buf_idx = _vertex_data[idx].vbo->id();
|
|
||||||
bind();
|
|
||||||
for (auto attr : _vao->attributes()) {
|
|
||||||
if (attr.buffer == buf_idx) {
|
|
||||||
_vao->disable(attr.index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
void VertexGroup::enableBuffer(size_t idx) {
|
|
||||||
auto buf_idx = _vertex_data[idx].vbo->id();
|
|
||||||
bind();
|
|
||||||
for (auto attr : _vao->attributes()) {
|
|
||||||
if (attr.buffer == buf_idx) {
|
|
||||||
_vao->enable(attr.index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
void VertexGroup::enable() { _vao->enable(); }
|
|
||||||
|
|
||||||
void VertexGroup::disable() { _vao->disable(); }
|
|
||||||
|
|
||||||
std::shared_ptr<EBO> VertexGroup::indices() const { return _ebo; }
|
|
||||||
|
|
||||||
std::shared_ptr<EBO> VertexGroup::indices(const std::vector<uint32_t>& data) {
|
|
||||||
_ebo->data(data);
|
|
||||||
return _ebo;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<EBO> VertexGroup::indices(std::shared_ptr<EBO> buffer) {
|
|
||||||
_ebo = buffer;
|
|
||||||
return _ebo;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#include <NBGraphics/VertexData.hpp>
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
VertexData::VertexData(
|
||||||
|
std::shared_ptr<Buffer> data_,
|
||||||
|
const std::vector<VertexAttribute>& attrs_
|
||||||
|
) : data(data_), attributes(attrs_) {}
|
||||||
|
|
||||||
|
VertexData::VertexData(
|
||||||
|
const ByteVector& data_,
|
||||||
|
const std::vector<VertexAttribute>& attrs_
|
||||||
|
) : data(std::make_shared<Buffer>(GL_ARRAY_BUFFER, data_)),
|
||||||
|
attributes(attrs_) {}
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
+106
-141
@@ -2,185 +2,150 @@
|
|||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
std::map<GLFWwindow*, Window*> Window::WindowContexts = {};
|
|
||||||
std::map<int, int> Window::GLFWHints = {};
|
|
||||||
bool Window::StrictInitialization = true;
|
|
||||||
bool Window::_glfw_init = false;
|
|
||||||
Window* Window::_current = nullptr;
|
|
||||||
static std::map<int, int> defailt_window_hints = {
|
|
||||||
{GLFW_CONTEXT_VERSION_MAJOR, 3},
|
|
||||||
{GLFW_CONTEXT_VERSION_MINOR, 1},
|
|
||||||
{GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE}
|
|
||||||
#ifdef _NB_GL_DEBUG_ON
|
|
||||||
, {GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE}
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
using GLFWCodes = GLFWError::Codes;
|
using GLFWCodes = GLFWError::Codes;
|
||||||
const std::string GLFWError::type = "nb::GLFWError";
|
const std::string GLFWError::type = "nb::GLFWError";
|
||||||
const ErrorCodeMap GLFWError::ErrorMessages = {
|
const ErrorCodeMap GLFWError::ErrorMessages = {
|
||||||
{GLFWCodes::UNDEFINED, "Error"},
|
{GLFWCodes::UNDEFINED, "Error"},
|
||||||
{GLFWCodes::INIT_FAILED, "GLFW initialization failed"},
|
{GLFWCodes::INIT_FAILED, "GLFW initialization failed"},
|
||||||
{GLFWCodes::GLFW_INTIALIZED, "GLFW has already been initialized"},
|
{GLFWCodes::GLFW_INTIALIZED, "GLFW has already been initialized"},
|
||||||
{GLFWCodes::GLAD_FAILED, "GLAD initialization failed"}
|
{GLFWCodes::GLAD_FAILED, "GLAD initialization failed"},
|
||||||
|
{GLFWCodes::NOT_MAIN, "Action can only be done on main thread"},
|
||||||
|
{GLFWCodes::WINDOW_FAILED, "Failed to initialize window"}
|
||||||
};
|
};
|
||||||
|
|
||||||
using WindowErrorCodes = WindowError::Codes;
|
// class OGLWindow
|
||||||
const std::string WindowError::type = "nb::WindowError";
|
|
||||||
const ErrorCodeMap WindowError::ErrorMessages = {
|
|
||||||
{WindowErrorCodes::UNDEFINED, "Error"},
|
|
||||||
{WindowErrorCodes::INITIALIZED_WINDOW, "Window already initialized"},
|
|
||||||
{WindowErrorCodes::NO_GLFW, "GLFW has not been initialized"},
|
|
||||||
{WindowErrorCodes::INIT_FAILED, "Could not intialized window"}
|
|
||||||
};
|
|
||||||
|
|
||||||
int Window::getGLFWHint(int hint_key) {
|
std::atomic<bool> OGLWindow::_glfwInitialized = false;
|
||||||
if (GLFWHints.find(hint_key) == GLFWHints.end()) {
|
thread_local GLFWwindow* OGLWindow::_currWindow = nullptr;
|
||||||
return 0;
|
thread_local const GLFWwindow* OGLWindow::_currArena = nullptr;
|
||||||
} else {
|
std::atomic<unsigned int> OGLWindow::_count = 0;
|
||||||
return GLFWHints.at(hint_key);
|
|
||||||
|
bool OGLWindow::InitGLFW(const std::vector<GLFWHint>& hints) {
|
||||||
|
if (nb::MainTID != GetTID()) {
|
||||||
|
THROW(GLFWError(GLFWError::NOT_MAIN));
|
||||||
}
|
}
|
||||||
}
|
if (!IsGLFWInitialized()) {
|
||||||
|
for (const auto& hint : hints) {
|
||||||
int Window::setGLFWHint(int hint_key, int hint_val) {
|
glfwInitHint(hint[0], hint[1]);
|
||||||
if (Window::_glfw_init) {
|
|
||||||
THROW(GLFWError(GLFWCodes::GLFW_INTIALIZED));
|
|
||||||
} else {
|
|
||||||
GLFWHints[hint_key] = hint_val;
|
|
||||||
}
|
|
||||||
return GLFWHints[hint_key];
|
|
||||||
}
|
|
||||||
|
|
||||||
Window* Window::getCurrent() {
|
|
||||||
return _current;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Window::checkKillGLFW() {
|
|
||||||
if(Window::WindowContexts.size() == 0 && _glfw_init) {
|
|
||||||
glfwTerminate();
|
|
||||||
Window::_glfw_init = false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Window::Window(const uint16_t x, const uint16_t y, const char* initName, GLFWmonitor* initMonitor, GLFWwindow* initWindow) {
|
|
||||||
if (!Window::_glfw_init) {
|
|
||||||
for (const auto& hint : Window::GLFWHints) {
|
|
||||||
glfwInitHint(hint.first, hint.second);
|
|
||||||
}
|
}
|
||||||
int glfwResponse = glfwInit();
|
if (glfwInit()) {
|
||||||
if (glfwResponse) {
|
OGLWindow::_glfwInitialized.store(true, std::memory_order_release);
|
||||||
Window::_glfw_init = true;
|
|
||||||
} else {
|
} else {
|
||||||
if (Window::StrictInitialization) {
|
THROW(GLFWError(GLFWError::INIT_FAILED));
|
||||||
THROW(GLFWError(GLFWCodes::INIT_FAILED));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return IsGLFWInitialized();
|
||||||
windowSize = {x, y};
|
|
||||||
_aspect_ratio = float(x)/float(y);
|
|
||||||
windowName = std::string(initName);
|
|
||||||
monitor = initMonitor;
|
|
||||||
shareWindow = initWindow;
|
|
||||||
|
|
||||||
for (const auto& hint: defailt_window_hints) {
|
|
||||||
windowHints[hint.first] = hint.second;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Window::Window(const std::array<uint16_t, 2> initSize, const char* initName, GLFWmonitor* initMonitor, GLFWwindow* initWindow) :
|
bool OGLWindow::TerminateGLFW() {
|
||||||
Window(initSize[0], initSize[1], initName, initMonitor, initWindow){}
|
if (nb::MainTID != GetTID()) {
|
||||||
|
THROW(GLFWError(GLFWError::NOT_MAIN));
|
||||||
Window::~Window() {
|
}
|
||||||
glfwSetWindowShouldClose(window, true);
|
if (IsGLFWInitialized()) {
|
||||||
glfwDestroyWindow(window);
|
|
||||||
Window::WindowContexts.erase(window);
|
|
||||||
if (Window::WindowContexts.size()==0) {
|
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
OGLWindow::_glfwInitialized.store(false, std::memory_order_release);
|
||||||
}
|
}
|
||||||
|
return !IsGLFWInitialized();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::setCurrent() {
|
GLFWwindow* OGLWindow::MakeCurrent(const std::shared_ptr<OGLWindow>& window) {
|
||||||
_current = this;
|
auto tmp = _currWindow;
|
||||||
glfwMakeContextCurrent(window);
|
if (window) {
|
||||||
}
|
_currWindow = window->getWindowPtr();
|
||||||
|
_currArena = window->_arena;
|
||||||
int Window::getWindowHint(int hint_key) const {
|
|
||||||
if (windowHints.find(hint_key) == windowHints.end()) {
|
|
||||||
return 0;
|
|
||||||
} else {
|
} else {
|
||||||
return windowHints.at(hint_key);
|
_currWindow = nullptr;
|
||||||
|
_currArena = nullptr;
|
||||||
}
|
}
|
||||||
|
glfwMakeContextCurrent(_currWindow);
|
||||||
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Window::setWindowHint(int hint_key, int hint_val) {
|
OGLWindow::OGLWindow(
|
||||||
if (_init) {
|
unsigned int x,
|
||||||
THROW(WindowError(WindowErrorCodes::INITIALIZED_WINDOW));
|
unsigned int y,
|
||||||
} else {
|
const std::string&,
|
||||||
windowHints[hint_key] = hint_val;
|
GLFWwindow* sharingContext
|
||||||
|
) {
|
||||||
|
CheckForMainThread();
|
||||||
|
InitGLFW();
|
||||||
|
_window_ptr = glfwCreateWindow(
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
_title.c_str(),
|
||||||
|
NULL,
|
||||||
|
sharingContext
|
||||||
|
);
|
||||||
|
if (_window_ptr == NULL) {
|
||||||
|
THROW(GLFWError(GLFWError::Codes::WINDOW_FAILED));
|
||||||
}
|
}
|
||||||
return windowHints[hint_key];
|
_arena = _window_ptr;
|
||||||
|
auto tmp = makeCurrent();
|
||||||
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||||
|
THROW(GLFWError(GLFWError::Codes::GLAD_FAILED));
|
||||||
|
}
|
||||||
|
glfwMakeContextCurrent(tmp);
|
||||||
|
OGLWindow::_count.fetch_add(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Window::init() {
|
OGLWindow::~OGLWindow() {
|
||||||
if (!_glfw_init) {
|
CheckForMainThread();
|
||||||
THROW(WindowError(WindowErrorCodes::NO_GLFW));
|
if (_window_ptr) {
|
||||||
}
|
if (_window_ptr == _currWindow) {
|
||||||
for (const auto& hint : windowHints) {
|
_currWindow = nullptr;
|
||||||
glfwWindowHint(hint.first, hint.second);
|
_currArena = nullptr;
|
||||||
}
|
}
|
||||||
|
glfwDestroyWindow(_window_ptr);
|
||||||
window = glfwCreateWindow(windowSize[0], windowSize[1], windowName.c_str(), monitor, shareWindow);
|
_count.fetch_add(-1);
|
||||||
if (window == NULL) {
|
if (!_count.load()) {
|
||||||
Window::WindowContexts.erase(window);
|
TerminateGLFW();
|
||||||
if (Window::WindowContexts.size()==0 && Window::_glfw_init) { glfwTerminate(); }
|
|
||||||
if (Window::StrictInitialization) {
|
|
||||||
THROW(WindowError(WindowErrorCodes::INIT_FAILED));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
glfwMakeContextCurrent(window);
|
|
||||||
Window::WindowContexts[window] = this;
|
|
||||||
|
|
||||||
gladResponse = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
|
||||||
if (!gladResponse) {
|
|
||||||
Window::checkKillGLFW();
|
|
||||||
if (Window::StrictInitialization) {
|
|
||||||
THROW(GLFWError(GLFWCodes::GLAD_FAILED));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_init = true;
|
|
||||||
OPENGL_CALL(
|
|
||||||
glViewport(0, 0, windowSize[0], windowSize[1])
|
|
||||||
);
|
|
||||||
return gladResponse;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GLFWwindow* Window::getWindow() const {
|
void OGLWindow::detachWindow() {
|
||||||
return window;
|
_title = "";
|
||||||
|
_window_ptr = nullptr;
|
||||||
|
_arena = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Window::getAspectRatio() const { return _aspect_ratio; }
|
int OGLWindow::getWindowAttribute(int attr) const {
|
||||||
|
CheckForMainThread();
|
||||||
std::array<uint16_t, 2> Window::getSize() const {
|
return glfwGetWindowAttrib(_window_ptr, attr);
|
||||||
return windowSize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Window::getName() const {
|
std::array<int, 2> OGLWindow::getSize() const {
|
||||||
return windowName;
|
CheckForMainThread();
|
||||||
|
int x, y;
|
||||||
|
glfwGetWindowSize(_window_ptr, &x, &y);
|
||||||
|
return {x, y};
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::resize(const std::array<uint16_t, 2> newSize) {
|
bool OGLWindow::shouldClose() const {
|
||||||
windowSize = newSize;
|
CheckForMainThread();
|
||||||
_aspect_ratio = float(newSize[0]) / float(newSize[1]);
|
return glfwWindowShouldClose(_window_ptr);
|
||||||
OPENGL_CALL(
|
|
||||||
glViewport(0, 0, windowSize[0], windowSize[1])
|
|
||||||
);
|
|
||||||
glfwSetWindowSize(window, windowSize[0], windowSize[1]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::resize(const uint16_t x, const uint16_t y) { resize({x, y}); }
|
void OGLWindow::shouldClose(bool val) {
|
||||||
|
CheckForMainThread();
|
||||||
|
glfwSetWindowShouldClose(_window_ptr, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
GLFWwindow* OGLWindow::makeCurrent() const {
|
||||||
|
auto tmp = glfwGetCurrentContext();
|
||||||
|
glfwMakeContextCurrent(_window_ptr);
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OGLWindow::setSize(unsigned int x, unsigned int y) {
|
||||||
|
CheckForMainThread();
|
||||||
|
glfwSetWindowSize(_window_ptr, x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OGLWindow::setTitle(const std::string& name) {
|
||||||
|
CheckForMainThread();
|
||||||
|
_title = name;
|
||||||
|
glfwSetWindowTitle(_window_ptr, _title.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -3,19 +3,25 @@
|
|||||||
#include "stb_image.h"
|
#include "stb_image.h"
|
||||||
|
|
||||||
#include <NBCore/Errors.hpp>
|
#include <NBCore/Errors.hpp>
|
||||||
|
#include <NBCore/TypeTraits.hpp>
|
||||||
#include <NBGraphics/Image.hpp>
|
#include <NBGraphics/Image.hpp>
|
||||||
|
#include <NBGraphics/Mesh.hpp>
|
||||||
#include <NBGraphics/ProgramPipeline.hpp>
|
#include <NBGraphics/ProgramPipeline.hpp>
|
||||||
#include <NBGraphics/VertexArray.hpp>
|
#include <NBGraphics/VertexData.hpp>
|
||||||
#include <NBGraphics/Window.hpp>
|
#include <NBGraphics/Window.hpp>
|
||||||
#include <NBGraphics/Textures.hpp>
|
#include <NBGraphics/Textures.hpp>
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
LOG("Howdy!");
|
LOG("Howdy!");
|
||||||
nb::Window window(400, 400, "Hello!");
|
auto window = nb::OGLWindow::CreateWindow(
|
||||||
window.setWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
400,
|
||||||
window.setWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
400,
|
||||||
window.init();
|
"Howdy!",
|
||||||
|
nb::KeyVal(GLFW_CONTEXT_VERSION_MAJOR, 3),
|
||||||
|
nb::KeyVal(GLFW_CONTEXT_VERSION_MINOR, 3)
|
||||||
|
);
|
||||||
|
nb::OGLWindow::MakeCurrent(window);
|
||||||
|
|
||||||
auto vert = std::make_shared<nb::Shader>(
|
auto vert = std::make_shared<nb::Shader>(
|
||||||
GL_VERTEX_SHADER,
|
GL_VERTEX_SHADER,
|
||||||
@@ -56,23 +62,33 @@ int main() {
|
|||||||
|
|
||||||
std::vector<uint32_t> indxs = {0, 1, 2};
|
std::vector<uint32_t> indxs = {0, 1, 2};
|
||||||
|
|
||||||
nb::VertexGroup tri(data, {
|
nb::IndexedMesh tri(GL_TRIANGLES, data, {nb::VertexAttribute{
|
||||||
nb::VertexAttribute{
|
|
||||||
2,
|
2,
|
||||||
GL_FLOAT,
|
nb::TypeCodes::FLOAT32,
|
||||||
false,
|
false,
|
||||||
{0, 8}
|
{0, 8}
|
||||||
},
|
}}, indxs);
|
||||||
}, indxs);
|
|
||||||
LOG(prog.log());
|
LOG(prog.log());
|
||||||
|
|
||||||
GLFWwindow* window_ptr = window.getWindow();
|
using nb::WindowCallbacks;
|
||||||
while(!glfwWindowShouldClose(window_ptr)) {
|
|
||||||
|
window->setCallback<nb::KEY>(
|
||||||
|
[](nb::OGLWindow* w, int key, int, int, int)->void{
|
||||||
|
if (key == GLFW_KEY_Q) {
|
||||||
|
w->shouldClose(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
window->setCallback<nb::CURSOR_POS>(
|
||||||
|
[](nb::OGLWindow* w, double x, double y)->void{
|
||||||
|
nb::logger.msg("Pos in x is " + std::to_string(x));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
while(!window->shouldClose()) {
|
||||||
OPENGL_CALL(glClearColor(0.2f, 0.3f, 0.3f, 1.0f));
|
OPENGL_CALL(glClearColor(0.2f, 0.3f, 0.3f, 1.0f));
|
||||||
OPENGL_CALL(glClear(GL_COLOR_BUFFER_BIT));
|
OPENGL_CALL(glClear(GL_COLOR_BUFFER_BIT));
|
||||||
tri.draw();
|
tri.draw();
|
||||||
glfwPollEvents();
|
window->pollEvents();
|
||||||
glfwSwapBuffers(window_ptr);
|
window->swapBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user