Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • gamedev/fggl
  • onuralpsezer/fggl
2 results
Show changes
Showing
with 988 additions and 283 deletions
......@@ -21,8 +21,10 @@
#include "fggl/modules/module.hpp"
#include "fggl/math/types.hpp"
#include "fggl/gfx/interfaces.hpp"
//! Classes responsible for interacting with display managers
namespace fggl::display {
class Window {
......@@ -30,12 +32,6 @@ namespace fggl::display {
virtual ~Window() = default;
virtual void activate() const = 0;
template<typename T, typename ...Args>
void make_graphics(Args... args) {
activate();
m_graphics = std::make_unique<T>(*this, args...);
}
// window-related getters
[[nodiscard]]
virtual math::vec2i frameSize() const = 0;
......@@ -51,7 +47,7 @@ namespace fggl::display {
return *m_graphics;
}
virtual void setTitle(const char* title) = 0;
virtual void setTitle(const char *title) = 0;
virtual void setFullscreen(bool state) = 0;
......@@ -59,14 +55,15 @@ namespace fggl::display {
virtual bool isFullscreen() const = 0;
protected:
std::unique_ptr<gfx::Graphics> m_graphics;
gfx::Graphics* m_graphics;
};
class WindowService {
public:
constexpr static const modules::ModuleService service = modules::make_service("fggl::display::WindowService");
constexpr static const auto
service = modules::make_service("fggl::display::WindowService");
virtual Window* create() = 0;
virtual Window *create() = 0;
virtual void pollEvents() = 0;
};
......
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 25/03/23.
//
#ifndef FGGL_DS_GRAPH_HPP
#define FGGL_DS_GRAPH_HPP
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <set>
namespace fggl::ds {
template<typename T>
class DirectedGraph {
public:
/**
* Add a single edge to the graph.
*
* If the entry does not already exist, this will create the entry before adding the dependencies to it.
* If the entry already exists, this will append the provided dependencies to its existing list.
*
* @param start the entry which depends something else
* @param end the thing it depends on
*/
inline void addEdge(const T start, const T end) {
m_edges[start].push_back(end);
m_edges[end];
}
/**
* Add a single vertex to the graph.
*/
inline void addVertex(const T vertex) {
m_edges[vertex];
}
/**
* Add a series of dependencies for an entry.
*
* If the entry does not already exist, this will create the entry before adding the dependencies to it.
* If the entry already exists, this will append the provided dependencies to its existing list.
*
* @param name the entry having dependencies added
* @param dependencies the things it depends on
*/
void addEdges(const T name, const std::vector<T> &dependencies) {
auto existing = m_edges.find(name);
if (existing == m_edges.end()) {
m_edges[name] = dependencies;
} else {
existing->second.insert(existing->second.end(), dependencies.begin(), dependencies.end());
}
}
/**
* Clear all currently stored dependencies.
*
* This method will result in the dependency graph being empty, with no known modules.
*/
inline void clear() {
m_edges.clear();
}
inline auto begin() const {
return m_edges.begin();
}
inline auto end() const {
return m_edges.end();
}
bool getOrder(std::stack<T> &stack) {
std::set<T> visited{};
for (const auto &module : m_edges) {
if (!visited.contains(module.first)) {
sortUtil(module.first, visited, stack);
}
}
return true;
}
bool getOrderPartial(T first, std::stack<T> &stack) {
std::set<T> visited{};
sortUtil(first, visited, stack);
return true;
}
bool getOrderRev(std::queue<T> &stack) {
std::set<T> visited{};
for (const auto &module : m_edges) {
if (!visited.contains(module.first)) {
sortUtilRev(module.first, visited, stack);
}
}
return true;
}
bool getOrderPartialRev(T first, std::queue<T>& queue) {
std::set<T> visited{};
sortUtilRev(first, visited, queue);
return true;
}
private:
std::map<T, std::vector<T>> m_edges;
void sortUtil(T idx, std::set<T> &visited, std::stack<T> &stack) {
visited.emplace(idx);
for (auto dep : m_edges.at(idx)) {
if (!visited.contains(dep))
sortUtil(dep, visited, stack);
}
stack.push(idx);
}
void sortUtilRev(T idx, std::set<T> &visited, std::queue<T> &stack) {
visited.emplace(idx);
for (auto dep : m_edges.at(idx)) {
if (!visited.contains(dep))
sortUtilRev(dep, visited, stack);
}
stack.push(idx);
}
};
} // namespace fggl::ds
#endif //FGGL_DS_GRAPH_HPP
......@@ -37,7 +37,7 @@ namespace fggl::ds {
}
WeakRef allocate() {
if ( N <= m_data.size() ) {
if (N <= m_data.size()) {
assert(0 && "Fake slot map emulated out of space");
return BAD_INDEX;
}
......@@ -50,13 +50,13 @@ namespace fggl::ds {
m_data.erase(idx);
}
T& get(WeakRef idx) const {
assert( valid(idx) );
T &get(WeakRef idx) const {
assert(valid(idx));
return m_data[idx];
}
T* tryGet(WeakRef idx) const {
if( valid(idx) ) {
T *tryGet(WeakRef idx) const {
if (valid(idx)) {
return &m_data[idx];
}
return nullptr;
......@@ -67,7 +67,6 @@ namespace fggl::ds {
std::size_t m_nextIdx = 1;
};
} // namespace fggl::ds
#endif //FGGL_DS_PLACEHOLDER_HPP
......@@ -20,6 +20,8 @@
#define FGGL_ENTITY_ENTITY_HPP
#include <cstdint>
#include <map>
#include <vector>
#include "fggl/debug/logging.hpp"
#include "fggl/vendor/entt.hpp"
......@@ -34,38 +36,41 @@ namespace fggl::entity {
inline EntityID create() {
return m_registry.create();
}
inline void destroy(EntityID entity) {
m_registry.destroy(entity);
}
template<typename Component, typename... Args>
inline Component& add(EntityID entity, Args&&... args) {
return m_registry.emplace<Component>(entity, std::forward<Args>(args)...);
inline Component &add(EntityID entity, Args &&... args) {
return m_registry.get_or_emplace<Component>(entity, std::forward<Args>(args)...);
}
template<typename Component>
Component& get(EntityID entity) {
Component &get(EntityID entity) {
#ifndef NDEBUG
if ( !has<Component>(entity) ) {
debug::error("Entity {} has no component of type {}", (uint64_t)entity, typeid(Component).name());
}
if (!has<Component>(entity)) {
debug::error("Entity {} has no component of type {}", (uint64_t) entity, debug::demangle(typeid(Component).name()));
assert(false && "Entity was missing component - use tryGet or fix definition");
}
#endif
return m_registry.get<Component>(entity);
}
template<typename Component>
const Component& get(EntityID entity) const {
const Component &get(EntityID entity) const {
return m_registry.get<Component>(entity);
}
template<typename Component>
Component* tryGet(EntityID entity) {
Component *tryGet(EntityID entity) {
return m_registry.try_get<Component>(entity);
}
template<typename Component>
const Component* tryGet(EntityID entity) const {
return m_registry.try_get<Component>(entity);
const Component *tryGet(EntityID entity, const Component* defaultValue = nullptr) const {
auto* comp = m_registry.try_get<Component>(entity);
return comp == nullptr ? defaultValue : comp;
}
template<typename ...Components>
......@@ -73,39 +78,91 @@ namespace fggl::entity {
return m_registry.view<Components...>();
}
EntityID findByName(const std::string& name) const {
auto itr = m_names.find(name);
if ( itr == m_names.end() ){
return INVALID;
}
return m_registry.valid(itr->second) ? itr->second : INVALID;
}
inline void setName(const std::string& name, EntityID eid ) {
if ( eid == INVALID ) {
m_names.erase(name);
} else {
assert(m_registry.valid(eid));
m_names[name] = eid;
}
}
template<typename ...Components>
bool has(EntityID idx) const {
return m_registry.template all_of<Components...>(idx);
}
inline bool exists(EntityID idx) {
return m_registry.valid(idx);
bool hasTag(EntityID entity, fggl::util::GUID tag) {
const auto mapItr = m_tags.find( tag );
if ( mapItr == m_tags.end() || !m_registry.valid(entity) ) {
return false;
}
return std::find(mapItr->second.begin(), mapItr->second.end(), entity) != mapItr->second.end();
}
void addTag(const EntityID entity, const fggl::util::GUID tag) {
assert( m_registry.valid(entity) );
auto& tagged = m_tags[ tag ];
tagged.push_back( entity );
}
inline bool alive(EntityID idx) {
void removeTag(const EntityID entity, const fggl::util::GUID tag) {
auto mapItr = m_tags.find(tag);
if ( mapItr == m_tags.end() ) {
return;
}
std::remove_if( mapItr->second.begin(), mapItr->second.end(), [entity](auto other) { return other == entity;} );
}
inline std::vector<EntityID> findByTag(const char* tag) {
return findByTag( util::make_guid_rt(tag) );
}
std::vector<EntityID> findByTag(const fggl::util::GUID tag){
auto mapItr = m_tags.find(tag);
if ( mapItr == m_tags.end() ) {
return {};
}
return mapItr->second;
}
inline bool exists(EntityID idx) const {
return m_registry.valid(idx);
}
inline bool alive(EntityID idx) const {
return m_registry.valid(idx);
}
private:
entt::registry m_registry;
std::map<std::string, EntityID> m_names;
std::map<fggl::util::GUID, std::vector<EntityID>> m_tags;
};
struct Entity {
static Entity make(EntityManager& manager, EntityID idx) {
static Entity make(EntityManager &manager, EntityID idx) {
return Entity{idx, manager};
}
EntityID id;
EntityManager& manager;
EntityManager &manager;
template<typename Component>
Component& get() {
Component &get() {
return manager.get<Component>(id);
}
template<typename Component>
const Component& get() const {
const Component &get() const {
return manager.get<Component>(id);
}
};
......
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 20/08/22.
//
#ifndef FGGL_ENTITY_GRIDWORLD_ZONE_HPP
#define FGGL_ENTITY_GRIDWORLD_ZONE_HPP
#include <array>
#include <vector>
#include "fggl/math/types.hpp"
#include "fggl/assets/types.hpp"
#include "fggl/entity/entity.hpp"
namespace fggl::entity::grid {
using GridPos = math::vec2i;
constexpr auto ASSET_TILESET = assets::AssetType::make("tileset");
template<typename T, uint32_t width, uint32_t height>
struct Grid {
public:
Grid() = default;
inline T& get(GridPos pos) {
assert(inBounds(pos));
return m_cells[getCellIndex(pos)];
}
const T& get(GridPos pos) const {
assert(inBounds(pos));
return m_cells[getCellIndex(pos)];
}
inline void set(GridPos pos, T value) {
assert(inBounds(pos));
m_cells[getCellIndex(pos)] = value;
}
inline bool inBounds(GridPos pos) const {
return 0 <= pos.x && pos.x <= size.x &&
0 <= pos.y && pos.y <= size.y;
}
private:
constexpr static math::vec2i size = math::vec2ui(width, height);
std::array<T, size.x * size.y> m_cells;
inline uint32_t getCellIndex(GridPos pos) const {
assert( inBounds(pos));
return pos.y * size.x + pos.x;
}
};
struct FloorTile {
constexpr static uint8_t IMPOSSIBLE = 0;
uint8_t moveCost = IMPOSSIBLE;
math::vec3 colour = gfx::colours::CYAN;
};
struct WallTile {
bool render = false;
math::vec3 colour = gfx::colours::CYAN;
};
struct WallState {
uint32_t north = 0;
uint32_t west = 0;
};
struct TileSet {
std::vector<FloorTile> m_floors;
std::vector<WallTile> m_walls;
};
/**
* A 2D representation of a space.
*
* @tparam width the grid width in units
* @tparam height the grid height in units
*/
template<uint32_t width, uint32_t height>
struct Area2D {
public:
constexpr static std::array<math::vec2i, 4> DIRECTIONS{{ {-1, 0}, {0, -1}, {1, 0}, {0, 1} }};
inline explicit Area2D(TileSet& tiles) : m_tiles(tiles) {
clear();
}
[[nodiscard]]
inline bool inBounds(GridPos pos) const {
return 0 <= pos.x && pos.x <= width
&& 0 <= pos.y && pos.y <= height;
}
void clear() {
WallState noWall;
for (auto xPos = 0U; xPos<width; ++xPos) {
for (auto yPos=0U; yPos<height; ++yPos) {
m_floors.set({xPos, yPos}, 0);
m_walls.set({xPos, yPos}, noWall);
}
}
}
inline FloorTile& floorAt(uint32_t xPos, uint32_t yPos) {
return m_tiles.m_floors.at( m_floors.get({xPos, yPos}) );
}
inline void setFloorAt(uint32_t xPos, uint32_t yPos, uint32_t floor) {
m_floors.set({xPos, yPos}, floor);
}
inline WallTile& wallAt(uint32_t xPos, uint32_t yPos, bool north) {
if (north) {
return m_tiles.m_walls.at(m_walls.get({xPos, yPos}).north);
} else {
return m_tiles.m_walls.at(m_walls.get({xPos, yPos}).west);
}
}
inline void setWallAt(uint32_t xPos, uint32_t yPos, bool north, uint32_t wall) {
auto& state = m_walls.get({xPos, yPos});
if (north) {
state.north = wall;
} else {
state.west = wall;
}
}
inline bool canMove(GridPos pos) const {
if ( !inBounds(pos) ) {
return false;
}
return m_tiles.m_floors[m_floors.get(pos)].moveCost != FloorTile::IMPOSSIBLE;
}
inline bool canMove(GridPos pos, math::vec2i dir) const {
return canMove(pos + dir) && !blocked(pos, dir);
}
inline bool blocked(GridPos pos, math::vec2i dir) const;
EntityManager& entities() {
return m_entities;
}
inline void neighbours(math::vec2i pos, std::vector<math::vec2i> &neighbours) const {
for (auto direction : DIRECTIONS) {
if ( canMove(pos, direction) ) {
auto result = pos + direction;
neighbours.push_back(result);
}
}
}
private:
TileSet& m_tiles;
Grid<uint32_t, width, height> m_floors;
Grid<WallState, width + 1, height + 1> m_walls;
EntityManager m_entities;
};
template<uint32_t width, uint32_t height>
bool Area2D<width, height>::blocked(GridPos pos, math::vec2i dir) const {
auto targetPos = pos;
if ( dir.x == 1 || dir.y == 1 ) {
targetPos = pos + dir;
}
if ( !inBounds(targetPos) ) {
return true;
}
auto& wallObj = m_walls.get(targetPos);
if ( dir.y != 0 ) {
return wallObj.north != 0;
}
if (dir.x != 0) {
return wallObj.west != 0;
}
return true;
}
} // namespace fggl::entity::gridworld
#endif //FGGL_ENTITY_GRIDWORLD_ZONE_HPP
......@@ -31,9 +31,11 @@
namespace fggl::entity {
constexpr auto PROTOTYPE_ASSET = assets::AssetType::make("entity_prototype");
using FactoryFunc = std::function<void(const ComponentSpec& config, EntityManager&, const EntityID&)>;
using CustomiseFunc = std::function<void(EntityManager&, const EntityID&)>;
constexpr auto ENTITY_PROTOTYPE = assets::make_asset_type("entity/prototype");
constexpr auto ENTITY_SCENE = assets::make_asset_type("entity/scene");
using FactoryFunc = std::function<void(const ComponentSpec &config, EntityManager &, const EntityID &, modules::Services &svc)>;
using CustomiseFunc = std::function<void(EntityManager &, const EntityID &)>;
struct FactoryInfo {
FactoryFunc factory;
......@@ -42,84 +44,112 @@ namespace fggl::entity {
class EntityFactory {
public:
constexpr static const modules::ModuleService service = modules::make_service("fggl::entity:Factory");
constexpr static const modules::ServiceName service = modules::make_service("fggl::entity:Factory");
EntityID create(const EntityType& spec, EntityManager& manager, const CustomiseFunc& customise = nullptr) {
try {
std::vector<CustomiseFunc> finishers;
inline EntityFactory(modules::Services &services) : m_services(services) {}
// set up the components for the entity
auto entity = setupComponents(spec, manager, finishers);
if ( entity == entity::INVALID ) {
debug::error("Error attempting to create entity with type {}", std::to_string(spec.get()) );
return entity::INVALID;
}
EntityID create(const EntityType &spec, EntityManager &manager, const CustomiseFunc &customise = nullptr) {
std::vector<CustomiseFunc> finishers;
// allow the caller to perform any setup needed
if (customise != nullptr) {
customise(manager, entity);
}
// build the setup
auto entity = setupComponents(spec, manager, finishers);
if ( entity == entity::INVALID ) {
debug::error("EntityFactory: failed to build from prototype {}", std::to_string(spec.get()));
return entity::INVALID;
}
// finally, we run any cleanup/init setups required by the component factories
for( auto& finisher : finishers ) {
finisher(manager, entity);
}
// if requested, allow the user to customize the creation
if ( customise != nullptr ) {
customise(manager, entity);
}
// run finishers for components
for ( auto& finisher : finishers ) {
finisher( manager, entity );
}
// use metadata to finalise
processTags(manager, entity, spec);
return entity;
}
void processTags(EntityManager& manager, EntityID id, EntityType spec) {
auto type = m_prototypes.at(spec);
for ( auto& tag : type.tags ) {
manager.addTag(id, tag);
}
}
return entity;
} catch (std::out_of_range& ex) {
#ifndef NDEBUG
debug::log(debug::Level::error, "EntityFactory: Unknown entity type '{}'", fggl::util::guidToString(spec));
#endif
return fggl::entity::INVALID;
void log_known_types() const {
debug::debug("dumping known types:");
for(const auto& [k,v] : m_factories) {
debug::debug("\ttype: {}", k);
}
}
void define(EntityType type, const EntitySpec& spec) {
void define(EntityType type, const EntitySpec &spec) {
m_prototypes[type] = spec;
}
// ability to set and unset factory functions
inline void bind(const ComponentID& configNode, FactoryFunc factory, CustomiseFunc finalise = nullptr) {
inline void bind(const ComponentID &configNode, FactoryFunc factory, CustomiseFunc finalise = nullptr) {
m_factories[configNode].factory = std::move(factory);
m_factories[configNode].finalise = std::move(finalise);
}
inline void unbind(const ComponentID& configNode) {
inline void unbind(const ComponentID &configNode) {
m_factories.erase(configNode);
}
inline FactoryInfo& getInfo(ComponentID comp) {
return m_factories.at(comp);
}
private:
modules::Services m_services;
std::map<ComponentID, FactoryInfo> m_factories;
std::map<EntityType, EntitySpec> m_prototypes;
entity::EntityID setupComponents(EntityType entityType, EntityManager& manager, std::vector<CustomiseFunc>& finishers) {
entity::EntityID setupComponents(EntityType entityType,
EntityManager &manager,
std::vector<CustomiseFunc> &finishers) {
assert(entityType != NO_PARENT && "setup components called with NO_PARENT?!");
auto entity = manager.create();
std::vector<ComponentID> loadedComps;
auto currentType = entityType;
while ( currentType != NO_PARENT ) {
auto& entitySpec = m_prototypes.at(currentType);
while (currentType != NO_PARENT) {
const auto& specEntry = m_prototypes.find( currentType );
if ( specEntry == m_prototypes.end() ) {
debug::warning("Asked to setup {}, for {} but was not a known prototype", specEntry->first, entityType);
return entity::INVALID;
}
auto entitySpec = specEntry->second;
debug::debug("constructing {} for {} ({} comps)", currentType, entityType, entitySpec.components.size());
assert( entitySpec.ordering.size() == entitySpec.components.size() && "ordering incorrect size, bad things happend!" );
for ( auto& component : entitySpec.ordering ) {
for (auto &component : entitySpec.ordering) {
// skip comps loaded by children
if (std::find(loadedComps.begin(), loadedComps.end(), component) != loadedComps.end()) {
continue;
}
try {
auto& data = getComponent(entitySpec, component);
auto &data = getComponent(entitySpec, component);
loadedComps.push_back(component);
auto& info = m_factories.at(component);
info.factory(data, manager, entity);
auto &info = m_factories.at(component);
info.factory(data, manager, entity, m_services);
if ( info.finalise != nullptr ) {
if (info.finalise != nullptr) {
finishers.push_back(info.finalise);
}
} catch (std::out_of_range& ex) {
#ifndef NDEBUG
debug::log(debug::Level::error, "EntityFactory: Unknown component factory type '{}'", fggl::util::guidToString(component));
#endif
} catch (std::out_of_range &ex) {
debug::error( "EntityFactory: Unknown component factory type '{}'", component );
log_known_types();
manager.destroy(entity);
return entity::INVALID;
}
......@@ -130,20 +160,21 @@ namespace fggl::entity {
return entity;
}
ComponentSpec& getComponent(EntitySpec& prototype, util::GUID compToken) {
ComponentSpec &getComponent(EntitySpec &prototype, util::GUID compToken) {
auto compItr = prototype.components.find(compToken);
if ( compItr != prototype.components.end() ) {
if (compItr != prototype.components.end()) {
return compItr->second;
}
if ( prototype.parent == NO_PARENT ) {
if (prototype.parent == NO_PARENT) {
throw std::out_of_range("EntityFactory: no such component!");
}
return getComponent(m_prototypes.at(prototype.parent), compToken);
}
};
assets::AssetRefRaw load_prototype(EntityFactory* factory, const assets::AssetGUID& guid, assets::AssetData data);
assets::AssetRefRaw load_prototype(assets::Loader* loader, const assets::AssetID &guid, const assets::LoaderContext& data, EntityFactory* factory);
assets::AssetRefRaw load_scene(assets::Loader* loader, const assets::AssetID& asset, const assets::LoaderContext& data, void* ptr);
} // namespace fggl::entity
......
......@@ -29,7 +29,7 @@ namespace YAML {
template<>
struct convert<fggl::math::vec3> {
static Node encode(const fggl::math::vec3& rhs) {
static Node encode(const fggl::math::vec3 &rhs) {
Node node;
node.push_back(rhs.x);
node.push_back(rhs.y);
......@@ -37,7 +37,7 @@ namespace YAML {
return node;
}
static bool decode(const Node& node, fggl::math::vec3& rhs) {
static bool decode(const Node &node, fggl::math::vec3 &rhs) {
if (!node.IsSequence() || node.size() != 3) {
return false;
}
......@@ -51,14 +51,14 @@ namespace YAML {
template<>
struct convert<fggl::math::vec2> {
static Node encode(const fggl::math::vec2& rhs) {
static Node encode(const fggl::math::vec2 &rhs) {
Node node;
node.push_back(rhs.x);
node.push_back(rhs.y);
return node;
}
static bool decode(const Node& node, fggl::math::vec2& rhs) {
static bool decode(const Node &node, fggl::math::vec2 &rhs) {
if (!node.IsSequence() || node.size() != 2) {
return false;
}
......@@ -71,7 +71,7 @@ namespace YAML {
template<>
struct convert<fggl::data::Vertex> {
static Node encode(const fggl::data::Vertex& rhs) {
static Node encode(const fggl::data::Vertex &rhs) {
Node node;
node["position"] = rhs.posititon;
node["normal"] = rhs.normal;
......@@ -80,7 +80,7 @@ namespace YAML {
return node;
}
static bool decode(const Node& node, fggl::data::Vertex& rhs) {
static bool decode(const Node &node, fggl::data::Vertex &rhs) {
if (!node.IsSequence() || node.size() != 2) {
return false;
}
......@@ -95,31 +95,31 @@ namespace YAML {
template<>
struct convert<fggl::phys::BodyType> {
static Node encode(const fggl::phys::BodyType& rhs) {
static Node encode(const fggl::phys::BodyType &rhs) {
Node node;
if ( rhs == fggl::phys::BodyType::STATIC ) {
if (rhs == fggl::phys::BodyType::STATIC) {
node = "static";
} else if ( rhs == fggl::phys::BodyType::DYNAMIC ) {
} else if (rhs == fggl::phys::BodyType::DYNAMIC) {
node = "dynamic";
} else if ( rhs == fggl::phys::BodyType::KINEMATIC ) {
} else if (rhs == fggl::phys::BodyType::KINEMATIC) {
node = "kinematic";
}
return node;
}
static bool decode(const Node& node, fggl::phys::BodyType& rhs) {
static bool decode(const Node &node, fggl::phys::BodyType &rhs) {
auto strVal = node.as<std::string>();
if ( strVal == "static" ) {
if (strVal == "static") {
rhs = fggl::phys::BodyType::STATIC;
return true;
}
if ( strVal == "dynamic" ) {
if (strVal == "dynamic") {
rhs = fggl::phys::BodyType::DYNAMIC;
return true;
}
if ( strVal == "kinematic" ) {
if (strVal == "kinematic") {
rhs = fggl::phys::BodyType::KINEMATIC;
return true;
}
......@@ -130,16 +130,16 @@ namespace YAML {
template<>
struct convert<fggl::util::GUID> {
static Node encode(const fggl::util::GUID& rhs) {
static Node encode(const fggl::util::GUID &rhs) {
Node node;
node = rhs.get();
return node;
}
static bool decode(const Node& node, fggl::util::GUID& rhs) {
static bool decode(const Node &node, fggl::util::GUID &rhs) {
auto longVal = node.as<uint64_t>(0);
if ( longVal == 0 ) {
if (longVal == 0) {
// probably meant to hash it...
auto stringVal = node.as<std::string>();
rhs = fggl::util::make_guid_rt(stringVal);
......
......@@ -33,17 +33,17 @@ namespace fggl::entity {
struct ComponentSpec {
template<typename T>
T get(const std::string& key, const T& fallback) const {
T get(const std::string &key, const T &fallback) const {
return config[key].template as<T>(fallback);
}
template<typename T>
void set(const std::string& key, const T& value) {
void set(const std::string &key, const T &value) {
config[key] = value;
}
inline bool has(const std::string& key) const {
return (bool)(config[key]);
inline bool has(const std::string &key) const {
return (bool) (config[key]);
}
YAML::Node config;
......@@ -51,8 +51,14 @@ namespace fggl::entity {
struct EntitySpec {
EntityType parent = NO_PARENT;
std::vector<util::GUID> tags;
std::vector<ComponentID> ordering;
std::map<ComponentID, ComponentSpec> components;
inline void addComp(ComponentID cmp, const ComponentSpec& spec) {
components[cmp] = spec;
ordering.push_back(cmp);
}
};
} // namespace fggl::entity
......
......@@ -22,23 +22,26 @@
#include "fggl/modules/module.hpp"
#include "fggl/assets/loader.hpp"
#include "fggl/assets/packed/adapter.hpp"
#include "fggl/entity/loader/loader.hpp"
namespace fggl::entity {
constexpr auto MIME_SCENE = assets::from_mime("x-fggl/scene");
struct ECS {
constexpr static const char* name = "fggl::entity::ECS";
constexpr static const std::array<modules::ModuleService, 1> provides = {
constexpr static const char *name = "fggl::entity::ECS";
constexpr static const std::array<modules::ServiceName, 1> provides = {
EntityFactory::service
};
constexpr static const std::array<modules::ModuleService, 1> depends = {
assets::Loader::service
constexpr static const std::array<modules::ServiceName, 2> depends = {
assets::Loader::service,
assets::CheckinAdapted::service
};
static bool factory(modules::ModuleService name, modules::Services& serviceManager);
static bool factory(modules::ServiceName name, modules::Services &serviceManager);
};
void install_component_factories(EntityFactory* factory);
void install_component_factories(EntityFactory *factory);
} // namespace fggl::entity
......
......@@ -29,6 +29,7 @@
#include "fggl/audio/null_audio.hpp"
#include "fggl/audio/openal/module.hpp"
//! Root namespace
namespace fggl {
}
......
......@@ -27,36 +27,42 @@ namespace fggl::gfx {
float fov = glm::radians(45.0f);
float nearPlane = 0.1f;
float farPlane = 100.0f;
inline math::mat4 perspective() const {
return glm::perspective(fov, aspectRatio, nearPlane, farPlane);
}
};
inline math::mat4 calc_proj_matrix(const Camera& camera) {
inline math::mat4 calc_proj_matrix(const Camera &camera) {
return glm::perspective(camera.fov, camera.aspectRatio, camera.nearPlane, camera.farPlane);
}
inline math::Ray get_camera_ray(const entity::EntityManager& world, const entity::EntityID camera, math::vec2 position) {
auto& camTransform = world.get<fggl::math::Transform>(camera);
auto& camComp = world.get<fggl::gfx::Camera>(camera);
inline math::Ray get_camera_ray(const entity::EntityManager &world,
const entity::EntityID camera,
math::vec2 position) {
auto &camTransform = world.get<fggl::math::Transform>(camera);
auto &camComp = world.get<fggl::gfx::Camera>(camera);
const auto projMatrix = fggl::gfx::calc_proj_matrix(camComp);
const auto viewMatrix = fggl::math::calc_view_matrix(camTransform);
glm::vec4 startNDC {
glm::vec4 startNDC{
position.x,
position.y,
-1.0f,
1.0f
};
glm::vec4 endNDC {
glm::vec4 endNDC{
position.x,
position.y,
0.0f,
1.0f
};
fggl::math::mat4 M = glm::inverse( projMatrix * viewMatrix );
fggl::math::mat4 M = glm::inverse(projMatrix * viewMatrix);
glm::vec3 start = M * startNDC;
glm::vec3 end = M * endNDC;
return { start, glm::normalize(end - start) };
return {start, glm::normalize(end - start)};
}
};
......
......@@ -23,6 +23,7 @@
#include "fggl/entity/entity.hpp"
#include "fggl/modules/module.hpp"
//! Classes responsible for rendering content
namespace fggl::gfx {
struct Bounds {
......@@ -34,7 +35,7 @@ namespace fggl::gfx {
class Graphics {
public:
constexpr static const modules::ModuleService service = modules::make_service("fggl::gfx::Graphics");
constexpr static const auto service = modules::make_service("fggl::gfx::Graphics");
virtual ~Graphics() = default;
virtual void clear() = 0;
......@@ -43,10 +44,9 @@ namespace fggl::gfx {
virtual Bounds canvasBounds() = 0;
virtual void draw2D(const Paint &paint) = 0;
virtual void drawScene(entity::EntityManager&) = 0;
virtual void drawScene(entity::EntityManager &, bool debugMode = false) = 0;
};
} // namespace fggl::gfx
#endif //FGGL_GFX_INTERFACES_HPP
......@@ -24,7 +24,6 @@
* FGGL OpenGL 4.x rendering backend.
*/
namespace fggl::gfx {
}
#endif
......@@ -24,6 +24,7 @@
#endif
#include <glad/glad.h>
typedef void* (* GLADloadproc)(const char *name);
typedef void *(*GLADloadproc)(const char *name);
#endif
......@@ -30,21 +30,7 @@
namespace fggl::gfx {
enum GlRenderType {
triangles = GL_TRIANGLES,
triangle_strip = GL_TRIANGLE_STRIP
};
struct GlRenderToken {
constexpr static const char name[] = "RenderToken";
GLuint vao;
GLuint buffs[2];
GLuint idxOffset;
GLsizei idxSize;
GLuint pipeline;
GLuint restartVertex;
GlRenderType renderType = triangles;
};
using GlFunctionLoader = GLADloadproc;
/**
* Class responsible for managing the OpenGL context.
......@@ -55,16 +41,16 @@ namespace fggl::gfx {
*/
class OpenGL4Backend : public Graphics {
public:
explicit OpenGL4Backend(data::Storage* storage, gui::FontLibrary* fonts);
explicit OpenGL4Backend(data::Storage *storage, gui::FontLibrary *fonts, assets::AssetManager *assets, GlFunctionLoader loader);
~OpenGL4Backend() override = default;
// copy bad
OpenGL4Backend(const OpenGL4Backend&) = delete;
OpenGL4Backend& operator=(const OpenGL4Backend&) = delete;
OpenGL4Backend(const OpenGL4Backend &) = delete;
OpenGL4Backend &operator=(const OpenGL4Backend &) = delete;
// move (probably) bad
OpenGL4Backend(OpenGL4Backend&&) = delete;
OpenGL4Backend&& operator=(OpenGL4Backend&&) = delete;
OpenGL4Backend(OpenGL4Backend &&) = delete;
OpenGL4Backend &&operator=(OpenGL4Backend &&) = delete;
/**
* Clear the backing buffer.
......@@ -91,12 +77,12 @@ namespace fggl::gfx {
*
* @param world the world to render
*/
void drawScene(entity::EntityManager& world) override;
void drawScene(entity::EntityManager &world, bool debugMode=false) override;
/**
* Get the 2D canvas bounds.
*
* @return
* @return the canvas bounds
*/
inline Bounds canvasBounds() override {
return m_canvasRenderer->bounds();
......@@ -107,9 +93,9 @@ namespace fggl::gfx {
std::unique_ptr<ogl4::CanvasRenderer> m_canvasRenderer;
std::unique_ptr<ogl4::DebugRenderer> m_debugRenderer;
std::unique_ptr<ShaderCache> m_cache;
GLuint m_canvasPipeline;
data::Storage* m_storage;
gui::FontLibrary* m_fontLibrary;
std::shared_ptr<ogl::Shader> m_canvasPipeline;
data::Storage *m_storage;
gui::FontLibrary *m_fontLibrary;
};
}; // namespace fggl::gfx
......
......@@ -17,6 +17,8 @@
#include <cstdio>
#include <fggl/gfx/ogl/backend.hpp>
#include "fggl/gfx/ogl/types.hpp"
#include <fggl/data/storage.hpp>
#include <filesystem>
......@@ -36,6 +38,16 @@ namespace fggl::gfx {
// optional parts
std::string geometry;
bool hasGeom = false;
static ShaderConfig named(const std::string &name, bool hasGeom = false) {
return {
.name = name,
.vertex = name + "_vert.glsl",
.fragment = name + "_frag.glsl",
.geometry = hasGeom ? name + "_geom.glsl" : "",
.hasGeom = hasGeom
};
}
};
struct ShaderSources {
......@@ -45,14 +57,6 @@ namespace fggl::gfx {
std::string geometrySource;
};
inline ShaderConfig ShaderFromName(const std::string &name) {
return {
name,
name + "_vert.glsl",
name + "_frag.glsl"
};
}
struct BinaryCache {
void *data = nullptr;
GLsizei size = 0;
......@@ -61,15 +65,17 @@ namespace fggl::gfx {
class ShaderCache {
public:
ShaderCache(fggl::data::Storage* storage);
using ShaderPtr = std::shared_ptr<ogl::Shader>;
ShaderCache(fggl::data::Storage *storage);
~ShaderCache() = default;
GLuint load(const ShaderConfig &config);
GLuint load(const ShaderSources &sources, bool allowBinaryCache);
ShaderPtr load(const ShaderConfig &config);
ShaderPtr load(const ShaderSources &sources, bool allowBinaryCache);
GLuint getOrLoad(const ShaderConfig &config);
ShaderPtr getOrLoad(const ShaderConfig &config);
GLuint get(const std::string &name);
ShaderPtr get(const std::string &name);
/**
* Fallback pipelines.
......@@ -80,15 +86,15 @@ namespace fggl::gfx {
void initFallbackPipelines();
private:
std::shared_ptr<fggl::data::Storage> m_storage;
std::unordered_map<std::string, GLuint> m_shaders;
fggl::data::Storage* m_storage;
std::unordered_map<std::string, ShaderPtr> m_shaders;
// extensions
void setupIncludes();
// opengl operations
bool readAndCompileShader(const std::string& filename, GLuint shader);
bool compileShaderFromSource(const std::string& source, GLuint);
bool readAndCompileShader(const std::string &filename, GLuint shader);
bool compileShaderFromSource(const std::string &source, GLuint);
// file io operations
bool loadFromDisk(GLuint pid, const std::string &pipelineName);
......
......@@ -27,6 +27,7 @@
#include "fggl/gfx/ogl/common.hpp"
#include "fggl/math/types.hpp"
#include "fggl/data/texture.hpp"
namespace fggl::gfx::ogl {
......@@ -45,37 +46,43 @@ namespace fggl::gfx::ogl {
public:
Shader() = default;
inline Shader(GLuint obj) : m_obj(obj) {}
// copy constructor bad
Shader(const Shader&) = delete;
Shader& operator=(const Shader&) = delete;
Shader(const Shader &) = delete;
Shader &operator=(const Shader &) = delete;
// Move ok - if handled properly
Shader(Shader&& other);
Shader& operator=(Shader&& other);
Shader(Shader &&other);
Shader &operator=(Shader &&other);
void use() {
glUseProgram( m_obj );
glUseProgram(m_obj);
}
inline bool hasUniform(const std::string_view& name) const {
auto location = glGetUniformLocation( m_obj, name.data() );
inline bool hasUniform(const std::string_view &name) const {
auto location = glGetUniformLocation(m_obj, name.data());
return location != -1;
}
inline Location uniform(const std::string_view& name) const {
auto location = glGetUniformLocation( m_obj, name.data() );
if ( location == -1 ) {
std::cerr << "error: " << name << " does not exist" << std::endl;
inline Location uniform(const std::string_view &name) const {
auto location = glGetUniformLocation(m_obj, name.data());
if (location == -1) {
debug::warning("uniform {} does not exist", name);
}
return location;
}
inline GLuint shaderID() {
return m_obj;
}
// primatives
inline void setUniformF(Location name, GLfloat value) {
glProgramUniform1f(m_obj, name, value);
}
inline void setUniformI(Location name, GLint value) {
glProgramUniform1i(m_obj, name, value);
}
......@@ -85,15 +92,15 @@ namespace fggl::gfx::ogl {
}
// vector versions (float)
inline void setUniformF(Location name, const math::vec2f& value) {
inline void setUniformF(Location name, const math::vec2f &value) {
glProgramUniform2f(m_obj, name, value.x, value.y);
}
inline void setUniformF(Location name, const math::vec3f& value) {
inline void setUniformF(Location name, const math::vec3f &value) {
glProgramUniform3f(m_obj, name, value.x, value.y, value.z);
}
inline void setUniformF(Location name, const math::vec4f& value) {
inline void setUniformF(Location name, const math::vec4f &value) {
glProgramUniform4f(m_obj, name, value.x, value.y, value.z, value.w);
}
......@@ -111,15 +118,15 @@ namespace fggl::gfx::ogl {
}
// matrix versions
inline void setUniformMtx(Location name, const math::mat2& mtx) {
inline void setUniformMtx(Location name, const math::mat2 &mtx) {
glProgramUniformMatrix2fv(m_obj, name, 1, GL_FALSE, glm::value_ptr(mtx));
}
void setUniformMtx(Location name, const math::mat3& mtx) {
void setUniformMtx(Location name, const math::mat3 &mtx) {
glProgramUniformMatrix3fv(m_obj, name, 1, GL_FALSE, glm::value_ptr(mtx));
}
void setUniformMtx(Location name, const math::mat4& mtx) {
void setUniformMtx(Location name, const math::mat4 &mtx) {
glProgramUniformMatrix4fv(m_obj, name, 1, GL_FALSE, glm::value_ptr(mtx));
}
};
......@@ -165,61 +172,215 @@ namespace fggl::gfx::ogl {
};
enum class PixelFormat {
UNSIGNED_BYTE = GL_UNSIGNED_BYTE,
BYTE,
UNSIGNED_SHORT,
SHORT,
UNSIGNED_INT,
INT,
HALF_FLOAT,
FLOAT,
UNSIGNED_BYTE_3_3_2,
UNSIGNED_BYTE_2_3_3_REV,
UNSIGNED_SHORT_5_6_5,
UNSIGNED_SHORT_5_6_5_REV,
UNSINGED_SHORT_4_4_4_4,
UNSIGNED_SHORT_4_4_4_4_REV,
UNSIGNED_SHORT_5_5_5_1,
UNSIGNED_SHORT_1_5_5_5_REV,
UNSIGNED_INT_8_8_8_8,
UNSIGNED_INT_8_8_8_8_REV,
UNSIGNED_INT_10_10_10_10_2,
UNSIGNED_INT_10_10_10_10_2_REV,
UNSIGNED_BYTE = GL_UNSIGNED_BYTE,
BYTE = GL_BYTE,
UNSIGNED_SHORT = GL_UNSIGNED_SHORT,
SHORT = GL_SHORT,
UNSIGNED_INT = GL_UNSIGNED_INT,
INT = GL_INT,
//HALF_FLOAT = GL_HALF_FLOAT,
FLOAT = GL_FLOAT,
/*
UNSIGNED_BYTE_3_3_2,
UNSIGNED_BYTE_2_3_3_REV,
UNSIGNED_SHORT_5_6_5,
UNSIGNED_SHORT_5_6_5_REV,
UNSINGED_SHORT_4_4_4_4,
UNSIGNED_SHORT_4_4_4_4_REV,
UNSIGNED_SHORT_5_5_5_1,
UNSIGNED_SHORT_1_5_5_5_REV,
UNSIGNED_INT_8_8_8_8,
UNSIGNED_INT_8_8_8_8_REV,
UNSIGNED_INT_10_10_10_10_2,
UNSIGNED_INT_10_10_10_10_2_REV,*/
};
enum class InternalImageFormat {
DepthComponent = GL_DEPTH_COMPONENT,
DepthStencil = GL_DEPTH_STENCIL,
Red = GL_RED,
RedGreen = GL_RG,
RedGreenBlue = GL_RGB,
RedGreenBlueAlpha = GL_RGBA
DepthComponent = GL_DEPTH_COMPONENT,
DepthStencil = GL_DEPTH_STENCIL,
Red = GL_RED,
RedGreen = GL_RG,
RedGreenBlue = GL_RGB,
RedGreenBlueAlpha = GL_RGBA
};
enum class ImageFormat {
R = GL_RED,
RG,
RGB,
RGBA,
R_INT,
RG_INT,
RGB_INT,
RGBA_INT,
BGR,
BGRA,
BGR_INT,
BGRA_INT,
STENTICL_INDEX,
DEPTH_COMPONENT,
DEPTH_STENCIL
R = GL_RED,
RG = GL_RG,
RGB = GL_RGB,
RGBA = GL_RGBA,
R_INT = GL_RED_INTEGER,
RG_INT = GL_RG_INTEGER,
RGB_INT = GL_RGB_INTEGER,
RGBA_INT = GL_RGBA_INTEGER,
BGR = GL_BGR,
BGRA = GL_BGRA,
BGR_INT = GL_BGR_INTEGER,
BGRA_INT = GL_BGRA_INTEGER,
STENTICL_INDEX = GL_STENCIL_INDEX,
DEPTH_COMPONENT = GL_DEPTH_COMPONENT,
DEPTH_STENCIL = GL_DEPTH_STENCIL
};
struct Image {
struct PixelDataArray {
PixelFormat type;
union {
unsigned char *uc;
char *c;
std::uint16_t *us;
std::int16_t *s;
float *f;
std::int32_t *i;
std::uint32_t *ui;
};
bool owning;
inline PixelDataArray(PixelFormat fmt, std::size_t size) : type(fmt), owning(true) {
switch (type) {
case PixelFormat::UNSIGNED_BYTE:
uc = new unsigned char[size];
break;
case PixelFormat::BYTE:
c = new char[size];
break;
case PixelFormat::UNSIGNED_SHORT:
us = new std::uint16_t[size];
break;
case PixelFormat::SHORT:
s = new std::int16_t[size];
break;
case PixelFormat::FLOAT:
f = new float[size];
break;
case PixelFormat::INT:
i = new std::int32_t[size];
break;
case PixelFormat::UNSIGNED_INT:
ui = new std::uint32_t[size];
break;
}
}
inline explicit PixelDataArray(unsigned char* data) : type(PixelFormat::UNSIGNED_BYTE), uc(data), owning(false) {}
inline explicit PixelDataArray(char* data) : type(PixelFormat::BYTE), c(data), owning(false) {}
// no copy
PixelDataArray(const PixelDataArray&) = delete;
PixelDataArray& operator=(const PixelDataArray&) = delete;
// move ok
PixelDataArray(PixelDataArray&& other) : type(other.type), owning(other.owning) {
switch (type) {
case PixelFormat::UNSIGNED_BYTE:
uc = other.uc;
other.uc = nullptr;
break;
case PixelFormat::BYTE:
c = other.c;
other.c = nullptr;
break;
case PixelFormat::UNSIGNED_SHORT:
us = other.us;
other.us = nullptr;
break;
case PixelFormat::SHORT:
s = other.s;
other.s = nullptr;
break;
case PixelFormat::FLOAT:
f = other.f;
other.f = nullptr;
break;
case PixelFormat::INT:
i = other.i;
other.i = nullptr;
break;
case PixelFormat::UNSIGNED_INT:
ui = other.ui;
other.ui = nullptr;
break;
}
}
inline ~PixelDataArray() {
if (owning) {
switch (type) {
case PixelFormat::UNSIGNED_BYTE: delete[] uc;
uc = nullptr;
break;
case PixelFormat::BYTE: delete[] c;
c = nullptr;
break;
case PixelFormat::UNSIGNED_SHORT: delete[] us;
us = nullptr;
break;
case PixelFormat::SHORT: delete[] s;
s = nullptr;
break;
case PixelFormat::FLOAT: delete[] f;
f = nullptr;
break;
case PixelFormat::INT: delete[] i;
i = nullptr;
break;
case PixelFormat::UNSIGNED_INT:
delete[] ui;
ui = nullptr;
break;
}
}
}
void* data() {
switch (type) {
case PixelFormat::UNSIGNED_BYTE:
return uc;
case PixelFormat::BYTE:
return c;
case PixelFormat::UNSIGNED_SHORT:
return us;
case PixelFormat::SHORT:
return s;
case PixelFormat::FLOAT:
return f;
case PixelFormat::INT:
return i;
case PixelFormat::UNSIGNED_INT:
return ui;
}
// unknown type?
return nullptr;
}
};
struct Image {
ImageFormat format;
math::vec2i size;
void* data;
PixelDataArray data;
//Image() = default;
inline Image(ImageFormat fmt, PixelFormat pxFmt, math::vec2i asize) :
format(fmt),
size(asize),
data(pxFmt, asize.x * asize.y){}
inline Image(ImageFormat fmt, math::vec2i asize, PixelDataArray&& adata) :
format(fmt),
size(asize),
data(std::move(adata)) {}
Image(const Image&) = delete;
inline PixelFormat type() const {
return data.type;
}
void* dataPtr() {
return data.data();
}
};
class Texture {
......@@ -227,30 +388,112 @@ namespace fggl::gfx::ogl {
inline explicit Texture(TextureType type) : m_type(type) {
glGenTextures(1, &m_obj);
}
~Texture() {
glDeleteTextures(1, &m_obj);
}
void setup(InternalImageFormat iFmt, math::vec2i size) {
//bind();
glBindTexture( (GLenum)m_type, m_obj );
if ( iFmt == InternalImageFormat::DepthComponent ) {
glTexImage2D((GLenum)
m_type, 0, (GLint) iFmt, size.x, size.y, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, nullptr);
glBindTexture((GLenum) m_type, m_obj);
if (iFmt == InternalImageFormat::DepthComponent) {
glTexImage2D((GLenum)m_type,
0,
(GLint) iFmt,
size.x,
size.y,
0,
GL_DEPTH_COMPONENT,
GL_UNSIGNED_BYTE,
nullptr);
} else {
glTexImage2D((GLenum)
m_type, 0, (GLint) iFmt, size.x, size.y, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
m_type, 0, (GLint) iFmt, size.x, size.y, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
}
}
void setData(InternalImageFormat iFmt, Image& image) {
void setData(InternalImageFormat iFmt, Image &image, PixelFormat extFormat) {
//bind();
glBindTexture( (GLenum)m_type, m_obj );
glTexImage2D( (GLenum)m_type, 0, (GLint)iFmt, image.size.x, image.size.y, 0, (GLenum)image.format, (GLenum)image.type, image.data);
glBindTexture((GLenum) m_type, m_obj);
glTexImage2D((GLenum) m_type,
0,
(GLint) iFmt,
image.size.x,
image.size.y,
0,
(GLenum) image.format,
(GLenum) extFormat,
image.dataPtr());
if ( m_type == TextureType::Tex2D ) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
}
void setDataPart(math::vec2i offset, Image& image) {
glTexImage2D( (GLenum)m_type, 0, offset.x, offset.y, image.size.x, image.size.y, (GLenum)image.format, (GLenum)image.type, image.data);
void setData(InternalImageFormat iFmt, Image &image) {
//bind();
glBindTexture((GLenum) m_type, m_obj);
glTexImage2D((GLenum) m_type,
0,
(GLint) iFmt,
image.size.x,
image.size.y,
0,
(GLenum) image.format,
(GLenum) image.type(),
image.dataPtr());
if ( m_type == TextureType::Tex2D ) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
}
void setData(InternalImageFormat iFmt, const data::Texture2D *image) {
ImageFormat imageFormat;
if (image->channels == 1) {
imageFormat = ImageFormat::R;
} else if ( image->channels == 2) {
imageFormat = ImageFormat::RG;
} else if ( image->channels == 3) {
imageFormat = ImageFormat::RGB;
} else if ( image->channels == 4) {
imageFormat = ImageFormat::RGBA;
} else {
// unknown image format -> channels mapping, having a bad day!
return;
}
//bind();
glBindTexture((GLenum) m_type, m_obj);
glTexImage2D((GLenum) m_type,
0,
(GLint) iFmt,
image->size.x,
image->size.y,
0,
(GLenum) imageFormat,
GL_UNSIGNED_BYTE,
image->data);
if ( m_type == TextureType::Tex2D ) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
}
void setDataPart(math::vec2i offset, Image &image) {
glTexImage2D((GLenum) m_type,
0,
offset.x,
offset.y,
image.size.x,
image.size.y,
(GLenum) image.format,
(GLenum) image.type(),
image.dataPtr());
}
void wrapMode(Wrapping wrap);
......@@ -263,9 +506,9 @@ namespace fggl::gfx::ogl {
* @param textureUnit the texture unit to bind to
*/
inline void bind(unsigned int textureUnit) {
assert( textureUnit < GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS );
glActiveTexture( GL_TEXTURE0 + textureUnit );
glBindTexture( (GLenum) m_type, m_obj );
assert(textureUnit < GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS);
glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture((GLenum) m_type, m_obj);
}
private:
......@@ -275,6 +518,8 @@ namespace fggl::gfx::ogl {
void release();
};
constexpr std::size_t NO_RESTART_IDX = 0;
enum class BuffAttrF {
HALF_FLOAT = GL_HALF_FLOAT,
FLOAT = GL_FLOAT,
......@@ -282,7 +527,7 @@ namespace fggl::gfx::ogl {
FIXED = GL_FIXED
};
enum class BuffAttrI{
enum class BuffAttrI {
BYTE = GL_BYTE,
UBYTE = GL_UNSIGNED_BYTE,
SHORT = GL_SHORT,
......@@ -296,7 +541,7 @@ namespace fggl::gfx::ogl {
UINT_PACKED_F = GL_UNSIGNED_INT_10F_11F_11F_REV
};
enum class Primative {
enum class Primitive {
POINT = GL_POINT,
LINE = GL_LINES,
LINE_STRIP = GL_LINE_STRIP,
......@@ -365,51 +610,51 @@ namespace fggl::gfx::ogl {
}
// copy constructor bad
Buffer(const Buffer&) = delete;
Buffer& operator=(const Buffer&) = delete;
Buffer(const Buffer &) = delete;
Buffer &operator=(const Buffer &) = delete;
Buffer(Buffer&& other) : m_obj(other.m_obj), m_capacity(other.m_capacity) {
Buffer(Buffer &&other) : m_obj(other.m_obj), m_capacity(other.m_capacity) {
other.obj_ = 0;
other.m_capacity = 0;
}
Buffer& operator=(Buffer&& other) {
if ( this != &other) {
Buffer &operator=(Buffer &&other) {
if (this != &other) {
release();
std::swap( m_obj, other.m_obj );
std::swap( m_capacity, other.m_capacity );
std::swap(m_obj, other.m_obj);
std::swap(m_capacity, other.m_capacity);
}
}
void bind() const {
assert( m_obj != 0 );
glBindBuffer( (GLenum)T, m_obj );
assert(m_obj != 0);
glBindBuffer((GLenum) T, m_obj);
}
inline bool isValid() const {
return m_obj != 0;
};
void write(GLsizeiptr size, const GLvoid* data, BufUsage usage) {
void write(GLsizeiptr size, const GLvoid *data, BufUsage usage) {
bind();
glBufferData( (GLenum)T, size, data, (GLenum)usage);
glBufferData((GLenum) T, size, data, (GLenum) usage);
}
void update(GLintptr offset, GLsizeiptr size, const void* data) {
void update(GLintptr offset, GLsizeiptr size, const void *data) {
}
template<typename D>
void replace(std::size_t size, const D* data) {
void replace(std::size_t size, const D *data) {
bind();
GLsizeiptr sizePtr = size * sizeof(D);
if ( sizePtr > m_capacity) {
glBufferData( (GLenum)T, sizePtr, data, GL_STREAM_DRAW );
if (sizePtr > m_capacity) {
glBufferData((GLenum) T, sizePtr, data, GL_STREAM_DRAW);
m_capacity = sizePtr;
} else {
glBufferSubData((GLenum)T, 0, sizePtr, data);
glBufferSubData((GLenum) T, 0, sizePtr, data);
}
glBindBuffer( (GLenum)T, 0 );
glBindBuffer((GLenum) T, 0);
}
};
......@@ -432,9 +677,9 @@ namespace fggl::gfx::ogl {
std::size_t offset;
};
// type intrincs to make interface nicer
// type intrinsics to make interface nicer
template<typename T>
struct attr_type{
struct attr_type {
const static BuffAttrF attr;
const static GLint size;
};
......@@ -465,32 +710,32 @@ namespace fggl::gfx::ogl {
~VertexArray();
// copy constructors bad
VertexArray(const VertexArray&) = delete;
VertexArray& operator=(const VertexArray) = delete;
VertexArray(const VertexArray &) = delete;
VertexArray &operator=(const VertexArray) = delete;
// move constructors might be ok
VertexArray(VertexArray&& other) noexcept;
VertexArray& operator=(VertexArray&& other);
VertexArray(VertexArray &&other) noexcept;
VertexArray &operator=(VertexArray &&other);
inline void bind() const {
assert( m_obj != 0);
glBindVertexArray( m_obj );
assert(m_obj != 0);
glBindVertexArray(m_obj);
}
void setAttribute(const ArrayBuffer& buffer, GLuint idx, AttributeF& attr);
void setAttribute(const ArrayBuffer& buffer, GLuint idx, AttributeI& attr, bool normalized);
void setAttributeI(const ArrayBuffer& buffer, GLuint idx, AttributeI& attr);
void setAttribute(const ArrayBuffer &buffer, GLuint idx, AttributeF &attr);
void setAttribute(const ArrayBuffer &buffer, GLuint idx, AttributeI &attr, bool normalized);
void setAttributeI(const ArrayBuffer &buffer, GLuint idx, AttributeI &attr);
void drawElements(const ElementBuffer& buff, Primative drawType, std::size_t size);
void draw(Primative drawType, int first, std::size_t count);
void drawElements(const ElementBuffer &buff, Primitive drawType, std::size_t size);
void draw(Primitive drawType, int first, std::size_t count);
};
// paranoid functions
void bind_vertex_array(GLuint& marker);
void unbind_vertex_array(GLuint& marker);
void bind_vertex_array(GLuint &marker);
void unbind_vertex_array(GLuint &marker);
template<BufType T>
void bind_buffer(GLuint* marker, const Buffer<T>& buff) {
void bind_buffer(GLuint *marker, const Buffer<T> &buff) {
#ifdef FGGL_GL_PARANOID
assert( marker != nullptr );
glGetIntegerv( (GLenum)T, (GLint*) marker );
......@@ -499,7 +744,7 @@ namespace fggl::gfx::ogl {
}
template<BufType T>
void unbind_buffer(GLuint* marker, const Buffer<T>& buff) {
void unbind_buffer(GLuint *marker, const Buffer<T> &buff) {
#ifdef GL_FGGL_PARANOID
assert( marker != nullptr );
glBindVertexArray(marker);
......
......@@ -29,8 +29,8 @@ namespace fggl::gfx::ogl4 {
class CanvasRenderer {
public:
CanvasRenderer(gui::FontLibrary* fonts);
void render(GLuint shader, const gfx::Paint& paint);
CanvasRenderer(gui::FontLibrary *fonts);
void render(ogl::Shader& shader, const gfx::Paint &paint);
inline gfx::Bounds bounds() const {
return m_bounds;
......@@ -41,11 +41,11 @@ namespace fggl::gfx::ogl4 {
ogl::VertexArray m_vao;
ogl::ArrayBuffer m_vertexList;
ogl::ElementBuffer m_indexList;
gui::FontLibrary* m_fonts;
gui::FontLibrary *m_fonts;
ogl::Texture m_fontTex;
void renderShapes(const Paint &paint, GLuint shader);
void renderText(const Paint&, GLuint shader);
void renderShapes(const Paint &paint, ogl::Shader& shader);
void renderText(const Paint &, ogl::Shader& shader);
};
} // namespace fggl::gfx::ogl4
......
......@@ -25,13 +25,13 @@ namespace fggl::gfx::ogl4 {
class DebugRenderer : public dd::RenderInterface {
public:
explicit DebugRenderer(GLuint shader);
explicit DebugRenderer(std::shared_ptr<ogl::Shader> shader);
~DebugRenderer() override = default;
void drawLineList(const dd::DrawVertex * lines, int count, bool depthEnabled) override;
void drawLineList(const dd::DrawVertex *lines, int count, bool depthEnabled) override;
math::mat4 mvpMatrix;
private:
ogl::Shader m_lineShader;
std::shared_ptr<ogl::Shader> m_lineShader;
ogl::Location m_lineShaderMVP;
ogl::VertexArray m_lineVao;
ogl::ArrayBuffer m_lineVbo;
......
......@@ -19,6 +19,8 @@
#ifndef FGGL_GFX_OGL4_FALLBACK_HPP
#define FGGL_GFX_OGL4_FALLBACK_HPP
#include "fggl/assets/types.hpp"
/**
* Fallback shaders.
*
......@@ -26,9 +28,9 @@
*/
namespace fggl::gfx::ogl4 {
constexpr const char* FALLBACK_CANVAS_PIPELINE = "fallback_canvas";
constexpr const char *FALLBACK_CANVAS_PIPELINE = "fallback_canvas";
constexpr const char* FALLBACK_CANVAS_VERTEX_SHADER = R"glsl(
constexpr const char *FALLBACK_CANVAS_VERTEX_SHADER = R"glsl(
#version 330 core
layout (location = 0) in vec2 aPos;
layout (location = 1) in vec3 aColour;
......@@ -45,7 +47,7 @@ namespace fggl::gfx::ogl4 {
texPos = aTexPos;
})glsl";
constexpr const char* FALLBACK_CANVAS_FRAGMENT_SHADER = R"glsl(
constexpr const char *FALLBACK_CANVAS_FRAGMENT_SHADER = R"glsl(
#version 330 core
uniform sampler2D tex;
......@@ -58,6 +60,14 @@ namespace fggl::gfx::ogl4 {
fragColour = vec4(colour.xyz, texture(tex, texPos).r);
})glsl";
constexpr const GLuint TEX_CHECKER = 0x11FF11FF; //FIXME pixel order is reversed?!
constexpr const GLuint TEX_WHITE = 0xFF0000FF;
constexpr const assets::AssetID FALLBACK_TEX = assets::make_asset_id("fallback", "FALLBACK_TEX");
constexpr const assets::AssetID FALLBACK_MAT = assets::make_asset_id("fallback", "FALLBACK_MAT");
constexpr const assets::AssetID SOLID_TEX = assets::make_asset_id("fallback", "SOLID_TEX");
constexpr const math::vec3 FALLBACK_COLOUR {1.0F, 0.0F, 1.0F};
} // namespace fggl::gfx::ogl4
#endif //FGGL_GFX_OGL4_FALLBACK_HPP