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 764 additions and 106 deletions
/*
* 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 10/12/22.
//
#ifndef FGGL_DEMO_INCLUDE_HEXBOARD_SCENE_H
#define FGGL_DEMO_INCLUDE_HEXBOARD_SCENE_H
#include <memory>
#include <optional>
#include "fggl/scenes/game.hpp"
#include "fggl/grid/hexgrid.hpp"
#include "fggl/grid/layout.hpp"
#include "camera.hpp"
namespace demo::hexboard {
struct SelectionModel {
std::optional<fggl::grid::IntHex> selected;
std::optional<fggl::grid::IntHex> hover;
};
class Scene : public fggl::scenes::GameBase {
public:
explicit Scene(fggl::App& app);
void activate() override;
void deactivate() override;
void update(float delta) override;
void render(fggl::gfx::Graphics& gfx) override;
private:
std::unique_ptr<fggl::grid::HexGrid> m_board;
std::unique_ptr<fggl::grid::Layout> m_layout;
std::unique_ptr<SelectionModel> m_selections;
std::unique_ptr<Camera2D> m_camera;
fggl::math::vec2 m_screen;
std::optional<fggl::math::vec2> m_dragging;
void drawGrid(fggl::gfx::Paint&);
void drawSelections(fggl::gfx::Paint&);
};
} // namespace demo::hexboard
#endif //FGGL_DEMO_INCLUDE_HEXBOARD_SCENE_H
......@@ -20,6 +20,7 @@
#define FGGL_DEMO_INCLUDE_MODELS_VIEWER_HPP
#include "fggl/scenes/game.hpp"
#include "fggl/assets/types.hpp"
namespace demo {
......@@ -35,9 +36,13 @@ namespace demo {
void render(fggl::gfx::Graphics& gfx) override;
private:
fggl::entity::EntityID m_model;
fggl::entity::EntityID m_model = fggl::entity::INVALID;
std::vector< fggl::assets::AssetID> m_assets;
uint64_t m_lastAsset = 0;
bool m_debug = false;
void cycleAsset(uint64_t asset);
};
}
......
......@@ -5,6 +5,8 @@ file(GLOB_RECURSE HEADER_LIST CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/include/fgg
# Should be shared linkage for legal reasons (LGPL)
add_library(fggl ${HEADER_LIST})
target_link_libraries(fggl PUBLIC entt)
# we need to tell people using the library about our headers
target_include_directories(fggl
PUBLIC
......@@ -34,6 +36,7 @@ endif ()
# the important stuff everything else uses
add_subdirectory(math)
add_subdirectory(util)
add_subdirectory(grid)
add_subdirectory(assets)
add_subdirectory(phys)
......@@ -53,12 +56,10 @@ target_sources(${PROJECT_NAME}
input/input.cpp
input/mouse.cpp
input/camera_input.cpp
)
gui/widget.cpp
gui/widgets.cpp
gui/containers.cpp
gui/fonts.cpp
)
# GUI support
add_subdirectory(gui)
# yaml-cpp for configs and storage
find_package(yaml-cpp)
......@@ -67,9 +68,6 @@ target_link_libraries(fggl PUBLIC yaml-cpp)
# model loading
add_subdirectory(data/assimp)
find_package(Freetype)
target_link_libraries(${PROJECT_NAME} PUBLIC Freetype::Freetype)
# Graphics backend
add_subdirectory(gfx)
add_subdirectory(audio)
......
......@@ -13,7 +13,6 @@
*/
#include <cstdlib>
#include <memory>
#include <spdlog/spdlog.h>
#include <fggl/app.hpp>
......@@ -29,7 +28,7 @@ namespace fggl {
m_states(),
m_subsystems(services) {}
int App::run(int argc, const char **argv) {
auto App::run(int /*argc*/, const char **/*argv*/) -> int {
auto *windowing = m_subsystems->get<display::WindowService>();
{
......
target_sources(fggl PRIVATE
module.cpp
)
\ No newline at end of file
types.cpp
packed/module.cpp
)
\ No newline at end of file
......@@ -20,10 +20,11 @@
namespace fggl::assets {
bool AssetFolders::factory(modules::ModuleService service, modules::Services &services) {
auto AssetFolders::factory(modules::ServiceName service, modules::Services &services) -> bool {
if (service == Loader::service) {
auto storage = services.get<data::Storage>();
services.create<Loader>(storage);
auto *storage = services.get<data::Storage>();
auto *checkin = services.get<CheckinAdapted>();
services.create<Loader>(storage, checkin);
return true;
}
if (service == AssetManager::service) {
......
/*
* 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.
//
#include "fggl/assets/packed/module.hpp"
namespace fggl::assets {
auto PackedAssets::factory(modules::ServiceName service, modules::Services &services) -> bool {
if (service == RawCheckin::service) {
services.create<RawCheckin>();
return true;
}
if (service == CheckinAdapted::service) {
auto* storage = services.get<data::Storage>();
auto* rawCheckin = services.get<RawCheckin>();
services.create<CheckinAdapted>(storage, rawCheckin);
return true;
}
return false;
}
} // namespace fggl::assets
\ No newline at end of file
/*
* 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 19/11/22.
//
#include <stack>
#include <vector>
#include <memory>
#include <set>
#include <map>
#include <concepts>
#include "fggl/util/guid.hpp"
namespace fggl::assets {
using AssetName = util::OpaqueName<uint64_t, struct AssetRefStruct>;
/**
* The base class representing an asset.
*
* This can be combined with the templated version.
*/
class Asset {
public:
Asset(AssetName name) : m_name(name) {}
virtual ~Asset() = default;
inline void release() {
releaseImpl();
}
bool operator==(const Asset& asset) const {
return m_name == asset.m_name;
}
bool operator!=(const Asset& asset) const {
return m_name != asset.m_name;
}
private:
AssetName m_name;
virtual void releaseImpl() = 0;
};
/**
* Wrapper for types that cannot extend Asset themselves.
*
* @tparam T the asset to wrap
*/
template<typename T>
class AssetBox : public Asset {
public:
AssetBox(T* ptr) : m_ptr(ptr) {}
~AssetBox() override {
release();
}
T* ptr() {
return m_ptr;
}
private:
T* m_ptr;
void releaseImpl() override {
delete m_ptr;
m_ptr = nullptr;
}
};
/**
* Asset Library.
*
* The currently usable set of assets loaded into the engine.
*/
class AssetLibrary {
public:
void load(AssetName name);
void unload(AssetName name);
template<typename T>
requires std::derived_from<T, Asset>
void store(AssetName name, T* ptr) {
m_assets[name] = std::make_unique<T>(ptr);
}
template<typename T>
requires std::derived_from<T, Asset>
void get(AssetName name) const {
Asset* asset = m_assets.at(name).get();
return dynamic_cast<T>(asset);
}
inline Asset* get(AssetName name) const {
return m_assets.at( name ).get();
}
private:
std::map<AssetName, std::unique_ptr<Asset>> m_assets;
};
struct AssetRecord {
AssetName id;
std::vector<AssetName> dependencies;
bool hasDepends(AssetName name) const;
void addDepend(AssetName name);
};
class AssetGraph {
public:
AssetGraph(AssetLibrary* loader);
/**
*
*
* @param name
*/
void require(AssetName& name) {
if ( m_loaded.find(name) != m_loaded.end() ) {
return;
}
m_required.push(name);
}
/**
*
*
* @return
*/
[[nodiscard]]
inline bool isLoadComplete() const {
return m_required.empty();
}
/**
*
*
* @param name
* @return
*/
[[nodiscard]]
inline bool isLoaded(AssetName name) const {
return m_loaded.find(name) != m_loaded.end();
}
/**
*
* @param name
* @param description
*/
void addDependency(AssetName& name, AssetName& description) {
auto& assets = m_assets.at(name);
assets.addDepend(description);
}
/**
*
* @param name
* @param dependency
* @return
*/
[[nodiscard]]
bool hasDependency(AssetName& name , AssetName& dependency) const {
try {
auto &assets = m_assets.at(name);
return assets.hasDepends(dependency);
} catch ( std::out_of_range& ) {
return false;
}
}
/**
*
*/
void process() {
auto asset = m_required.top();
m_loader->load( asset );
m_required.pop();
}
/**
*
*/
void finishLoading() {
while ( !m_loaded.empty() ) {
process();
}
}
private:
std::stack<AssetName> m_required;
std::set<AssetName> m_loaded;
std::map<AssetName, AssetRecord> m_assets;
AssetLibrary* m_loader;
};
} // namespace fggl::assets
\ No newline at end of file
/*
* 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/11/22.
//
#include "fggl/assets/types.hpp"
namespace fggl::assets {
auto make_asset_id_rt(const std::string &pack, const std::string &path, const std::string &view) -> AssetID {
auto fullPath = pack + ":" + path;
if (!view.empty()) {
fullPath += "[" + view + "]";
}
#ifndef NDEBUG
util::intern_string(fullPath.c_str());
#endif
auto hash = util::hash_fnv1a_64(fullPath.c_str());
return AssetID::make(hash);
}
auto asset_from_user(const std::string &input, const std::string &pack) -> AssetID {
if (input.find(':') != std::string::npos ) {
// probably fully qualified
#ifndef NDEBUG
util::intern_string(input.c_str());
#endif
auto hash = util::hash_fnv1a_64(input.c_str());
return AssetID::make(hash);
}
// probably local
return make_asset_id_rt(pack, input);
}
}
\ No newline at end of file
......@@ -3,4 +3,5 @@ target_sources(fggl
types.cpp
)
add_subdirectory(openal)
\ No newline at end of file
add_subdirectory(openal)
add_subdirectory(fallback)
\ No newline at end of file
target_sources(fggl
PRIVATE
audio.cpp
)
/*
* 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 05/11/22.
//
#include "fggl/audio/null_audio.hpp"
namespace fggl::audio {
void NullAudioService::play(const std::string &, bool) {
}
void NullAudioService::play(const fggl::audio::AudioClipShort &, bool) {
}
auto NullAudio::factory(modules::ServiceName service, modules::Services &services) -> bool{
if (service == SERVICE_AUDIO_PLAYBACK) {
services.bind<audio::AudioService, audio::NullAudioService>();
return true;
}
return false;
}
} // namespace fggl::audio
\ No newline at end of file
......@@ -18,7 +18,8 @@ else ()
endif ()
target_sources(fggl
PRIVATE
PRIVATE
audio.cpp
)
module.cpp
)
......@@ -27,28 +27,98 @@
*/
#include "fggl/audio/openal/audio.hpp"
#include "fggl/data/storage.hpp"
#include "fggl/assets/types.hpp"
#include "fggl/assets/manager.hpp"
#include "../../stb/stb_vorbis.h"
namespace fggl::audio::openal {
auto load_vorbis(assets::Loader* /*loader*/, const assets::AssetID &guid, const assets::LoaderContext &data, void* userPtr) -> assets::AssetRefRaw {
auto *manager = static_cast<assets::AssetManager*>(userPtr);
auto filePath = data.assetPath;
// vorbis
auto* clip = new AudioClipShort();
clip->sampleCount = stb_vorbis_decode_filename( filePath.c_str(), &clip->channels, &clip->sampleRate, &clip->data);
debug::info("clip loaded: channels={}, sampleRate={}, sampleCount={}", clip->channels, clip->sampleRate, clip->sampleCount);
if ( clip->sampleCount == -1 ) {
return nullptr;
}
manager->set(guid, clip);
return nullptr;
}
auto load_vorbis_short(std::filesystem::path path, assets::MemoryBlock& /*block*/) -> bool {
// vorbis
auto* clip = new AudioClipShort();
clip->sampleCount = stb_vorbis_decode_filename( path.c_str(), &clip->channels, &clip->sampleRate, &clip->data);
debug::info("clip loaded: channels={}, sampleRate={}, sampleCount={}", clip->channels, clip->sampleRate, clip->sampleCount);
return clip->sampleCount != 1;
}
auto check_vorbis(const std::filesystem::path& path ) -> assets::AssetTypeID {
if ( path.extension() != ".ogg" ) {
return assets::INVALID_ASSET_TYPE;
}
auto* clip = new AudioClipShort();
clip->sampleCount = stb_vorbis_decode_filename( path.c_str(), &clip->channels, &clip->sampleRate, &clip->data);
if ( clip->sampleCount == -1 ) {
return assets::INVALID_ASSET_TYPE;
}
delete clip;
return ASSET_CLIP_SHORT;
}
void AudioSource::play(const AudioClipShort &clip, bool looping) {
check_error("pre play");
AudioType format = clip.channels == 1 ? AudioType::MONO_16 : AudioType::STEREO_16;
m_splat.setData(format, clip.data, clip.size(), clip.sampleRate);
check_error("saving to buffer");
play(m_splat, looping);
check_error("post play");
}
void AudioServiceOAL::play(const std::string &filename, bool looping) {
debug::info("beginning audio: {}", filename);
// load audio clip into temp storage
AudioClip clip;
bool result = m_storage->load(data::StorageType::Data, filename, &clip);
if (!result) {
std::cerr << "error: can't load audio data" << std::endl;
auto assetRef = assets::make_asset_id_rt("core", filename);
auto* clip = m_assets->get<AudioClipShort>(assetRef);
if ( clip == nullptr ) {
debug::warning("audio asset requested, but not loaded: {}", filename);
return;
}
play(clip, looping);
play(*clip, looping);
debug::info("played audio: {}", filename);
}
void AudioServiceOAL::play(AudioClip &clip, bool looping) {
// play the audio on the default (non-positioned) source
if (m_defaultSource != nullptr) {
m_defaultSource->play(clip, looping);
void AudioServiceOAL::play(const AudioClipShort &clip, bool looping) {
if ( m_defaultSource == nullptr ){
return;
}
// play the audio on the default (non-positioned) source
m_defaultSource->play(clip, looping);
}
void AudioServiceOAL::release() {
m_defaultSource = nullptr;
alcMakeContextCurrent(nullptr);
alcDestroyContext(m_context);
alcCloseDevice(m_device);
m_context = nullptr;
m_device = nullptr;
}
}
\ No newline at end of file
/*
* 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 05/11/22.
//
#include "fggl/audio/openal/module.hpp"
namespace fggl::audio {
auto OpenAL::factory(modules::ServiceName service, modules::Services &services) -> bool {
if (service == SERVICE_AUDIO_PLAYBACK) {
auto* assets = services.get<assets::AssetManager>();
{
auto *assetLoader = services.get<assets::Loader>();
assetLoader->setFactory( ASSET_CLIP_SHORT, openal::load_vorbis, assets::LoadType::PATH);
}
{
auto *checkin = services.get<assets::CheckinAdapted>();
checkin->setLoader( RES_OGG_VORBIS, openal::load_vorbis_short, openal::check_vorbis );
}
services.bind<audio::AudioService, openal::AudioServiceOAL>(assets);
return true;
}
return false;
}
}
\ No newline at end of file
......@@ -15,7 +15,6 @@
#include "fggl/data/storage.hpp"
#include "fggl/audio/audio.hpp"
#include "../stb/stb_vorbis.h"
namespace fggl::audio {
......@@ -23,13 +22,4 @@ namespace fggl::audio {
namespace fggl::data {
template<>
bool fggl_deserialize<audio::AudioClip>(std::filesystem::path &data, audio::AudioClip *out) {
out->sampleCount = stb_vorbis_decode_filename(data.string().c_str(),
&out->channels,
&out->sampleRate,
&out->data);
return out->sampleCount != -1;
}
} // namespace fggl::data
......@@ -17,7 +17,6 @@
//
#include "fggl/data/assimp/module.hpp"
#include "fggl/data/model.hpp"
#include "fggl/debug/logging.hpp"
#include "fggl/assets/manager.hpp"
#include "fggl/mesh/mesh.hpp"
......@@ -27,21 +26,27 @@
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <filesystem>
namespace fggl::data::models {
constexpr math::vec3 convert(aiVector3D& vec) {
constexpr auto convert(aiVector3D& vec) -> math::vec3 {
return {vec.x, vec.y, vec.z};
}
constexpr math::vec2 convert2(aiVector2D& vec) {
constexpr auto convert2(aiVector2D& vec) -> math::vec2 {
return {vec.x, vec.y};
}
constexpr math::vec2 convert2(aiVector3D& vec) {
constexpr auto convert2(aiVector3D& vec) -> math::vec2 {
return {vec.x, vec.y};
}
static void process_mesh(mesh::Mesh3D& mesh, aiMesh* assimpMesh, const aiScene* scene) {
constexpr auto convert(aiColor3D& col) -> math::vec3 {
return {col.r, col.g, col.b};
}
static void process_mesh(mesh::Mesh3D& mesh, aiMesh* assimpMesh, const aiScene* scene, const std::vector<assets::AssetID>& assets) {
assert( assimpMesh != nullptr );
assert( scene != nullptr );
......@@ -71,126 +76,261 @@ namespace fggl::data::models {
// process material
if ( assimpMesh->mMaterialIndex >= 0) {
mesh.material = "_MAT_" + std::to_string(assimpMesh->mMaterialIndex);
mesh.material = assets[assimpMesh->mMaterialIndex];
}
}
static void process_node(mesh::MultiMesh3D& mesh, aiNode* node, const aiScene* scene) {
static void process_node(mesh::MultiMesh3D& mesh, aiNode* node, const aiScene* scene, const std::vector<assets::AssetID>& assets) {
for ( auto idx = 0U; idx < node->mNumMeshes; ++idx ) {
auto *assimpMesh = scene->mMeshes[ node->mMeshes[idx] ];
// process assimp submesh
mesh::Mesh3D meshData;
process_mesh(meshData, assimpMesh, scene);
process_mesh(meshData, assimpMesh, scene, assets);
mesh.meshes.push_back(meshData);
}
for ( auto idx = 0U; idx < node->mNumChildren; ++idx) {
process_node(mesh, node->mChildren[idx], scene);
process_node(mesh, node->mChildren[idx], scene, assets);
}
}
static void process_material(const assets::AssetGUID& guid, aiMaterial* assimpMat, assets::Loader* loader, assets::AssetManager* manager) {
auto lastSlash = guid.rfind('/');
auto prefix = guid.substr(0, lastSlash);
mesh::Material* material = new mesh::Material();
struct AssetStuff {
assets::Loader* loader;
assets::AssetManager* manager;
// for each material, calculate what it's name would be then request it
for ( unsigned int i = 0U ; i < assimpMat->GetTextureCount(aiTextureType_DIFFUSE); ++i ) {
aiString texName;
assimpMat->GetTexture( aiTextureType_DIFFUSE, i, &texName );
inline void checkLoaded(assets::AssetID asset) {
if (!manager->has(asset)) {
debug::info("triggered JIT upload for {}, use the chain loader", asset);
loader->load(asset);
}
}
auto textureGuid = prefix + "/" + texName.C_Str();
loader->load(textureGuid, DATA_TEXTURE2D, manager);
inline auto isLoaded(assets::AssetID asset) const -> bool {
return manager->has(asset);
}
material->diffuseTextures.push_back(textureGuid);
inline void set(assets::AssetID asset, auto* assetPtr) {
assert(assetPtr != nullptr);
manager->set(asset, assetPtr);
}
for ( unsigned int i = 0U ; i < assimpMat->GetTextureCount(aiTextureType_NORMALS); ++i ) {
};
static auto process_texture( const aiTextureType type, const aiMaterial* assimpMat, AssetStuff& stuff, const assets::LoaderContext& config) -> std::vector<assets::AssetID> {
std::vector<assets::AssetID> matRefs;
matRefs.reserve( assimpMat->GetTextureCount(type) );
// iterate through things
for ( auto i = 0U ; i < assimpMat->GetTextureCount(type); ++i ) {
aiString texName;
assimpMat->GetTexture( aiTextureType_NORMALS, i, &texName );
assimpMat->GetTexture( type, i, &texName );
const auto texID = config.makeRef(texName.C_Str());
auto textureGuid = prefix + "/" + texName.C_Str();
loader->load(textureGuid, DATA_TEXTURE2D, manager);
// trigger asset loading
stuff.checkLoaded( texID );
material->normalTextures.push_back(textureGuid);
// assets
matRefs.push_back( texID );
}
return matRefs;
}
static auto process_mat_colour(const aiMaterial* mat, const char* name, int a1, int a2) -> math::vec3 {
aiColor3D col{0.0F, 0.0F, 0.0F};
mat->Get( name, a1, a2, col );
debug::info("read colour: {}, {}, {}, {}", name, col.r, col.g, col.b);
return convert(col);
}
static void process_material(const assets::AssetID& guid, aiMaterial* assimpMat, AssetStuff stuff, const assets::LoaderContext& config) {
auto* material = new mesh::Material();
manager->set( guid, material );
debug::info("processing: {}", guid);
// for each material, calculate what it's name would be then request it
material->diffuseTextures = process_texture( aiTextureType_DIFFUSE, assimpMat, stuff, config );
material->normalTextures = process_texture( aiTextureType_NORMALS, assimpMat, stuff, config );
material->specularTextures = process_texture( aiTextureType_SPECULAR, assimpMat, stuff, config );
material->ambient = process_mat_colour( assimpMat, AI_MATKEY_COLOR_AMBIENT );
material->diffuse = process_mat_colour( assimpMat, AI_MATKEY_COLOR_DIFFUSE );
material->specular = process_mat_colour( assimpMat, AI_MATKEY_COLOR_SPECULAR );
stuff.set( guid, material );
}
auto load_assimp_model(assets::Loader* loader, const assets::AssetID& guid, const assets::LoaderContext& data, void* userPtr) -> assets::AssetRefRaw {
// auto *filePath = std::get<assets::AssetPath *>(data);
auto filePath = data.assetPath;
static assets::AssetRefRaw load_assimp_model(assets::Loader* loader, const assets::AssetGUID& guid, const assets::AssetData& data, void* userPtr) {
auto *filePath = std::get<assets::AssetPath *>(data);
assets::AssetManager* manager = (assets::AssetManager*)userPtr;
AssetStuff stuff {
.loader = loader,
.manager = (assets::AssetManager*)userPtr
};
if ( stuff.isLoaded(guid) ) {
// asset in DB, what do you want me to do?
return nullptr;
}
// assimp stuff
Assimp::Importer importer;
// import the scene from disk
const aiScene *scene = importer.ReadFile(filePath->c_str(), aiProcess_Triangulate | aiProcess_FlipUVs);
if ( !scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode ) {
const aiScene *scene = importer.ReadFile(filePath.c_str(), aiProcessPreset_TargetRealtime_Fast | aiProcess_FlipUVs);
if ( scene == nullptr || (scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) != 0 || scene->mRootNode == nullptr ) {
debug::warning("unable to load required model asset");
return nullptr;
}
debug::debug("Processing assimp mesh, {} meshes, {} materials, {} textures", scene->mNumMeshes, scene->mNumMaterials, scene->mNumTextures);
// calculate the mapping from material => asset mappings
auto parentRel = std::filesystem::relative( filePath.parent_path(), filePath.parent_path().parent_path() );
std::vector<assets::AssetID> matAssets;
for ( auto idx = 0U; idx < scene->mNumMaterials; ++idx ) {
aiString matName = scene->mMaterials[idx]->GetName();
auto matRel = parentRel / matName.C_Str();
matAssets.push_back( assets::make_asset_id_rt( data.pack, matRel ) );
}
// now we can try importing the mesh data
mesh::MultiMesh3D* packedMesh = new mesh::MultiMesh3D();
process_node( *packedMesh, scene->mRootNode, scene);
std::map<std::string, std::string> materials;
{
// prefix the materials if required
for (auto& mesh : packedMesh->meshes) {
if (!mesh.material.empty()) {
mesh.material = guid + mesh.material;
}
}
auto* packedMesh = new mesh::MultiMesh3D();
process_node( *packedMesh, scene->mRootNode, scene, matAssets);
// try and decode the material data
for (auto idx = 0; idx < scene->mNumMaterials; ++idx) {
auto matName = guid +"_MAT_" + std::to_string(idx);
process_material(matName, scene->mMaterials[idx], loader, manager);
packedMesh->materials.push_back(matName);
}
// now we try importing the materials
for (auto idx = 0U; idx < scene->mNumMaterials; ++idx ) {
process_material( matAssets[idx], scene->mMaterials[idx], stuff, data );
}
// FIXME asset loading system needs rework, this is bonkers.
manager->set(guid, packedMesh);
stuff.set(guid, packedMesh);
return nullptr;
}
static assets::AssetRefRaw load_assimp_texture(assets::Loader* loader, const assets::AssetGUID& guid, const assets::AssetData& data, void* userPtr) {
auto *filePath = std::get<assets::AssetPath *>(data);
assets::AssetManager* manager = (assets::AssetManager*)userPtr;
auto load_assimp_texture(assets::Loader* /*loader*/, const assets::AssetID& guid, const assets::LoaderContext& data, void* userPtr) -> assets::AssetRefRaw {
auto* manager = (assets::AssetManager*)userPtr;
if ( manager->has(guid) ) {
// already loaded.
return nullptr;
}
stbi_set_flip_vertically_on_load(true);
auto filePath = data.assetPath;
stbi_set_flip_vertically_on_load(1);
//load the texture data into memory
data::Texture2D* image = new data::Texture2D();
image->data = stbi_load(filePath->c_str(), &image->size.x, &image->size.y, &image->channels, 3);
auto* image = new data::Texture2D();
image->data = stbi_load(filePath.c_str(), &image->size.x, &image->size.y, &image->channels, 3);
if ( image->data == nullptr ) {
debug::warning("error reading texture: {}", stbi_failure_reason());
return nullptr;
} else {
debug::info("image reports it loaded correctly, adding {} to database", guid);
manager->set(guid, image);
manager->set(guid, image );
}
return nullptr;
}
bool AssimpModule::factory(modules::ModuleService service, modules::Services &serviceManager) {
auto load_tex_stb(const std::filesystem::path& filePath, assets::MemoryBlock& block) -> bool {
stbi_set_flip_vertically_on_load(1);
//load the texture data into memory
auto* image = new data::Texture2D();
image->data = stbi_load(filePath.c_str(), &image->size.x, &image->size.y, &image->channels, 3);
if ( image->data == nullptr ) {
debug::warning("error reading texture: {}", stbi_failure_reason());
delete image;
return false;
} else {
// TODO pass metadata to loader in a sensible way
block.size = image->channels * image->size.x * image->size.y;
block.data = (std::byte*)image->data;
delete image;
return true;
}
}
auto is_tex_stb(const std::filesystem::path& filePath) -> assets::AssetTypeID {
// detect jpgs
if ( filePath.extension() == ".jpg" || filePath.extension() == ".jpeg" ) {
return TEXTURE_RGBA;
}
// detect png
if ( filePath.extension() == ".png" ) {
return TEXTURE_RGBA;
}
return assets::INVALID_ASSET_TYPE;
}
auto is_model_assimp(const std::filesystem::path& filePath) -> assets::AssetTypeID {
auto ext = filePath.extension().string();
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
if ( ext == ".obj" || ext == ".fbx" ) {
return MODEL_MULTI3D;
}
return assets::INVALID_ASSET_TYPE;
}
auto extract_requirements(const std::string& packName, const std::filesystem::path& packRoot, assets::ResourceRecord& rr) -> bool {
Assimp::Importer importer;
const aiScene *scene = importer.ReadFile( rr.m_path, aiProcess_Triangulate | aiProcess_FlipUVs);
if ( !scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode ) {
debug::warning("unable to load required model asset");
return false;
}
// we want to find out about dependencies
auto baseDir = std::filesystem::relative( rr.m_path.parent_path(), packRoot );
for ( auto idx = 0U; idx < scene->mNumMaterials; ++idx) {
const auto *mat = scene->mMaterials[idx];
std::array matTypes { aiTextureType_DIFFUSE, aiTextureType_NORMALS, aiTextureType_SPECULAR };
for ( auto& matType : matTypes ) {
const auto diffCount = mat->GetTextureCount(matType);
for (auto idx2 = 0U; idx2 < diffCount; ++idx2) {
aiString texName;
mat->GetTexture(matType, idx2, &texName);
auto assetPath = baseDir / texName.C_Str();
auto assetRequired = assets::make_asset_id_rt(packName, assetPath);
rr.m_requires.push_back(assetRequired);
}
}
}
return true;
}
auto AssimpModule::factory(modules::ServiceName service, modules::Services &serviceManager) -> bool {
if ( service == MODEL_PROVIDER ) {
auto assetLoader = serviceManager.get<assets::Loader>();
assetLoader->setFactory( ASSIMP_MODEL, load_assimp_model, assets::LoadType::PATH );
assetLoader->setFactory( DATA_TEXTURE2D, load_assimp_texture, assets::LoadType::PATH );
auto* assetLoader = serviceManager.get<assets::Loader>();
assetLoader->setFactory( MODEL_MULTI3D, load_assimp_model, assets::LoadType::PATH );
assetLoader->setFactory( TEXTURE_RGBA, load_assimp_texture, assets::LoadType::PATH );
// new loading system
auto* checkin = serviceManager.get<assets::CheckinAdapted>();
checkin->setLoader( MIME_JPG, load_tex_stb, is_tex_stb );
checkin->setLoader( MIME_PNG, load_tex_stb, is_tex_stb );
checkin->setLoader( MIME_OBJ, assets::NEEDS_CHECKIN, is_model_assimp );
checkin->setLoader( MIME_FBX, assets::NEEDS_CHECKIN, is_model_assimp );
checkin->setProcessor( MIME_OBJ, extract_requirements);
checkin->setProcessor( MIME_FBX, extract_requirements );
return false;
}
return false;
}
} // namespace fggl::data
......@@ -29,7 +29,7 @@ namespace fggl::data {
const int gridOffset = sizeX * sizeY;
// calculate normals for each triangle
math::vec3 *triNormals = new math::vec3[sizeX * sizeY * 2];
auto *triNormals = new math::vec3[sizeX * sizeY * 2];
for (int i = 0; i < sizeX - 1; i++) {
for (int j = 0; j < sizeY - 1; j++) {
// calculate vertex
......@@ -57,7 +57,7 @@ namespace fggl::data {
const auto lastRow = (i == sizeX - 1);
const auto lastCol = (i == sizeY - 1);
auto finalNormal = glm::vec3(0.0f, 0.0f, 0.0f);
auto finalNormal = glm::vec3(0.0F, 0.0F, 0.0F);
if (!firstRow && !firstCol) {
finalNormal += triNormals[idx(i - 1, j - 1, sizeY)];
......@@ -78,7 +78,7 @@ namespace fggl::data {
}
locations[idx(i, j, sizeY)].normal =
glm::normalize(finalNormal) * -1.0f; //FIXME the normals seem wrong.
glm::normalize(finalNormal) * -1.0F; //FIXME the normals seem wrong.
}
}
delete[] triNormals;
......@@ -98,8 +98,8 @@ namespace fggl::data {
auto zPos = float(z);
std::size_t idx1 = idx(x, z, data::heightMaxZ);
locations[idx1].colour = fggl::math::vec3(1.0f, 1.0f, 1.0f);
locations[idx1].posititon = math::vec3(-0.5f + xPos, level, -0.5f - zPos);
locations[idx1].colour = fggl::math::vec3(1.0F, 1.0F, 1.0F);
locations[idx1].posititon = math::vec3(-0.5F + xPos, level, -0.5F - zPos);
}
}
gridVertexNormals(locations.data());
......
......@@ -28,13 +28,13 @@ void Mesh::pushIndex(unsigned int idx) {
m_index.push_back(idx);
}
Mesh::IndexType Mesh::pushVertex(Vertex vert) {
auto Mesh::pushVertex(Vertex vert) -> Mesh::IndexType {
auto idx = m_verts.size();
m_verts.push_back(vert);
return idx;
}
Mesh::IndexType Mesh::indexOf(Vertex term) {
auto Mesh::indexOf(Vertex term) -> Mesh::IndexType {
auto itr = std::find(m_verts.begin(), m_verts.end(), term);
if (itr == m_verts.end()) {
return INVALID_IDX;
......