Conquer Space 0.0.0
A space themed grand strategy game set in the near future, with realistic orbital mechanics, and an emphasis on economics and politics.
modelloader.h
Go to the documentation of this file.
1/* Conquer Space
2 * Copyright (C) 2021-2025 Conquer Space
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include <assimp/postprocess.h>
20#include <assimp/scene.h>
21
22#include <map>
23#include <string>
24#include <vector>
25
26#include <assimp/IOStream.hpp>
27#include <assimp/IOSystem.hpp>
28#include <assimp/Importer.hpp>
29#include <glm/glm.hpp>
30
36
37namespace cqsp::asset {
38class IOSystem;
39class IOStream : public Assimp::IOStream {
40 friend class IOSystem;
41
42 protected:
43 explicit IOStream(IVirtualFilePtr _ivfp) : ivfp(_ivfp) {}
44
46
47 public:
48 size_t Read(void* pvBuffer, size_t pSize, size_t pCount);
49 size_t Write(const void* pvBuffer, size_t pSize, size_t pCount) {
50 return pSize; // We won't ever write because we are only going to open game files
51 }
52 aiReturn Seek(size_t pOffset, aiOrigin pOrigin);
53 size_t Tell() const;
54 size_t FileSize() const;
55 void Flush();
56
57 void Close();
58};
59
60class IOSystem : public Assimp::IOSystem {
62
63 public:
64 explicit IOSystem(VirtualMounter* mount);
65 ~IOSystem() = default;
66
67 bool Exists(const char* file) const override;
68 char getOsSeparator() const override { return '/'; }
69
70 Assimp::IOStream* Open(const char* pFile, const char* pMode) override;
71 void Close(Assimp::IOStream* pFile) override;
72};
73
75 std::vector<Vertex> vertices;
76 std::vector<unsigned int> indices;
77 std::vector<std::string> textures;
79};
80
82 int width;
83 int height;
85
86 unsigned char* texture_data;
87};
88
90 std::map<std::string, std::string> texture_map;
91 std::map<std::string, glm::vec3> base_map;
92};
93
95 public:
96 std::vector<MeshPrototype> prototypes;
97 std::map<std::string, ModelTexturePrototype> texture_map;
98 std::map<int, MaterialPrototype> material_map;
99 std::map<std::string, int> texture_idx_map; // Probably should be per material but
100 // Let's figure that out later
101 std::string shader;
102
104};
105
107 int m_count = 0;
109 const aiScene* scene;
110 std::string asset_path;
111 ModelLoader(const aiScene* _scene, std::string asset_path) : scene(_scene), asset_path(asset_path) {
113 }
114
115 void LoadModel();
116
117 void LoadNode(aiNode* node);
118
119 void LoadMesh(aiMesh* mesh);
120
121 void LoadMaterials();
122
123 void LoadMaterial(int idx, aiMaterial* material);
124
125 bool LoadMaterialTexture(aiMaterial* material, const aiTextureType type, std::string& str);
126};
127
128void LoadModelPrototype(ModelPrototype* prototype, Model* asset);
129
130void LoadModelData(engine::Mesh* mesh, std::vector<Vertex>& vertices, std::vector<unsigned int>& indices);
131} // namespace cqsp::asset
Definition: assetprototype.h:37
Definition: modelloader.h:39
size_t Tell() const
Definition: modelloader.cpp:93
void Flush()
Definition: modelloader.cpp:95
size_t FileSize() const
Definition: modelloader.cpp:94
void Close()
Definition: modelloader.cpp:64
size_t Write(const void *pvBuffer, size_t pSize, size_t pCount)
Definition: modelloader.h:49
aiReturn Seek(size_t pOffset, aiOrigin pOrigin)
Definition: modelloader.cpp:75
IVirtualFilePtr ivfp
Definition: modelloader.h:45
IOStream(IVirtualFilePtr _ivfp)
Definition: modelloader.h:43
size_t Read(void *pvBuffer, size_t pSize, size_t pCount)
Definition: modelloader.cpp:66
Definition: modelloader.h:60
Assimp::IOStream * Open(const char *pFile, const char *pMode) override
Definition: modelloader.cpp:46
IOSystem(VirtualMounter *mount)
Definition: modelloader.cpp:39
void Close(Assimp::IOStream *pFile) override
Definition: modelloader.cpp:42
bool Exists(const char *file) const override
Definition: modelloader.cpp:40
char getOsSeparator() const override
Definition: modelloader.h:68
VirtualMounter * mount
Definition: modelloader.h:61
Definition: vfs.h:154
Definition: mesh.h:24
lib tracy file(GLOB_RECURSE CPP_FILES *.cpp) list(FILTER CPP_FILES EXCLUDE REGEX ".*main.cpp$") file(GLOB_RECURSE H_FILES *.h) set(SOURCE_FILES $
Definition: CMakeLists.txt:22
When adding assets, it is extremely crucial that you read cqsp::asset::AssetLoader::LoadResources to ...
Definition: asset.h:29
void LoadModelPrototype(ModelPrototype *prototype, Model *asset)
Definition: modelloader.cpp:155
void LoadModelData(engine::Mesh *mesh, std::vector< Vertex > &vertices, std::vector< unsigned int > &indices)
Definition: modelloader.cpp:196
@ MODEL
Model prototype.
Definition: assetprototype.h:30
std::shared_ptr< IVirtualFile > IVirtualFilePtr
Definition: vfs.h:33
Definition: modelloader.h:89
std::map< std::string, std::string > texture_map
Definition: modelloader.h:90
std::map< std::string, glm::vec3 > base_map
Definition: modelloader.h:91
Definition: modelloader.h:74
std::vector< unsigned int > indices
Definition: modelloader.h:76
std::vector< Vertex > vertices
Definition: modelloader.h:75
std::vector< std::string > textures
Definition: modelloader.h:77
int material_id
Definition: modelloader.h:78
Definition: modelloader.h:106
int m_count
Definition: modelloader.h:107
void LoadMaterial(int idx, aiMaterial *material)
Definition: modelloader.cpp:304
bool LoadMaterialTexture(aiMaterial *material, const aiTextureType type, std::string &str)
Definition: modelloader.cpp:200
ModelLoader(const aiScene *_scene, std::string asset_path)
Definition: modelloader.h:111
ModelPrototype * model_prototype
Definition: modelloader.h:108
const aiScene * scene
Definition: modelloader.h:109
std::string asset_path
Definition: modelloader.h:110
void LoadMesh(aiMesh *mesh)
Definition: modelloader.cpp:248
void LoadMaterials()
Definition: modelloader.cpp:293
void LoadModel()
Definition: modelloader.cpp:229
void LoadNode(aiNode *node)
Definition: modelloader.cpp:234
Definition: modelloader.h:94
std::map< std::string, int > texture_idx_map
Definition: modelloader.h:99
std::string shader
Definition: modelloader.h:101
int GetPrototypeType()
Definition: modelloader.h:103
std::vector< MeshPrototype > prototypes
Definition: modelloader.h:96
std::map< std::string, ModelTexturePrototype > texture_map
Definition: modelloader.h:97
std::map< int, MaterialPrototype > material_map
Definition: modelloader.h:98
Definition: modelloader.h:81
int height
Definition: modelloader.h:83
int width
Definition: modelloader.h:82
unsigned char * texture_data
Definition: modelloader.h:86
int channels
Definition: modelloader.h:84