implemented image reader and asset manager

This commit is contained in:
devZoGok
2022-03-02 20:24:04 +02:00
parent f64a3e13df
commit 82bf653e68
11 changed files with 171 additions and 22 deletions
+12
View File
@@ -0,0 +1,12 @@
#ifndef ASSET_H
#define ASSET_H
#include <string>
namespace vb01{
struct Asset{
std::string path;
};
}
#endif
+37
View File
@@ -0,0 +1,37 @@
#include "assetManager.h"
#include "imageReader.h"
#include "util.h"
#include <algorithm>
using namespace std;
namespace vb01{
AssetManager *assetManager = nullptr;
AssetManager* AssetManager::getSingleton(){
if(!assetManager)
assetManager = new AssetManager();
return assetManager;
}
void AssetManager::load(string path){
if(getAsset(path))
return;
string format = path.substr(path.find_last_of(".") + 1, string::npos);
vector<string> imageFormats = vector<string>{"png", "jpg", "jpeg"};
if(*find(imageFormats.begin(), imageFormats.end(), format) == format)
assets.push_back(ImageReader::getSingleton()->readImage(path, true));
}
Asset* AssetManager::getAsset(string path){
for(Asset *a : assets)
if(a->path == path)
return a;
return nullptr;
}
}
+20
View File
@@ -0,0 +1,20 @@
#ifndef ASSET_MANAGER_H
#define ASSET_MANAGER_H
#include "asset.h"
#include <vector>
namespace vb01{
class AssetManager{
public:
static AssetManager* getSingleton();
void load(std::string);
Asset* getAsset(std::string);
private:
AssetManager(){}
std::vector<Asset*> assets;
};
}
#endif
+12
View File
@@ -0,0 +1,12 @@
#ifndef FONT_ASSET_H
#define FONT_ASSET_H
#include "asset.h"
namespace vb01{
struct FontAsset : public Asset{
u32 **glyphImages;
};
}
#endif
+17
View File
@@ -0,0 +1,17 @@
#ifndef IMAGE_ASSET_H
#define IMAGE_ASSET_H
#include "asset.h"
namespace vb01{
struct ImageAsset : public Asset{
ImageAsset(std::string path, u8 *image){
this->path = path;
this->image = image;
}
u8 *image;
};
}
#endif
+22
View File
@@ -0,0 +1,22 @@
#include "imageReader.h"
#include "stb_image.h"
using namespace std;
namespace vb01{
ImageReader *imageReader = nullptr;
ImageReader* ImageReader::getSingleton(){
if(!imageReader)
imageReader = new ImageReader();
return imageReader;
}
ImageAsset* ImageReader::readImage(string path, bool flip){
stbi_set_flip_vertically_on_load(flip);
u8 *data = stbi_load(path.c_str(), nullptr, nullptr, nullptr, 0);
return new ImageAsset(path, data);
}
}
+17
View File
@@ -0,0 +1,17 @@
#ifndef IMAGE_READER_H
#define IMAGE_READER_H
#include "util.h"
#include "imageAsset.h"
namespace vb01{
class ImageReader{
public:
static ImageReader* getSingleton();
ImageAsset* readImage(std::string, bool);
private:
ImageReader(){}
};
}
#endif
+14
View File
@@ -0,0 +1,14 @@
#ifndef MODEL_ASSET_H
#define MODEL_ASSET_H
#include "asset.h"
namespace vb01{
class Node;
struct ModelAsset : public Asset{
Node *rootNode;
};
}
#endif
+12
View File
@@ -0,0 +1,12 @@
#ifndef TEXT_ASSET_H
#define TEXT_ASSET_H
#include "asset.h"
namespace vb01{
struct TextAsset : public Asset{
std::string text;
};
}
#endif
+7 -20
View File
@@ -5,6 +5,8 @@
#include FT_FREETYPE_H #include FT_FREETYPE_H
#include "texture.h" #include "texture.h"
#include "assetManager.h"
#include "imageAsset.h"
#include "glad.h" #include "glad.h"
#include <glfw3.h> #include <glfw3.h>
@@ -75,13 +77,10 @@ namespace vb01{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
stbi_set_flip_vertically_on_load(flip); u8 *image = ((ImageAsset*)AssetManager::getSingleton()->getAsset(paths[i]))->image;
data = stbi_load(paths[i].c_str(), &width, &height, &numChannels, 0); glTexImage2D(GL_TEXTURE_2D, 0, png ? GL_RGBA : GL_RGB, width, height, 0, png ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, image);
glTexImage2D(GL_TEXTURE_2D, 0, png ? GL_RGBA : GL_RGB, width, height, 0, png ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
} }
} }
@@ -98,29 +97,17 @@ namespace vb01{
glGenTextures(1, &texture[0]); glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_CUBE_MAP, texture[0]); glBindTexture(GL_TEXTURE_CUBE_MAP, texture[0]);
if(flip)
stbi_set_flip_vertically_on_load(flip);
for(int i = 0; i < 6; i++){ for(int i = 0; i < 6; i++){
if(!depth){ if(!depth){
if(fromFile){ if(fromFile){
data = stbi_load(paths[i].c_str(), &width, &height, &numChannels, 0); u8 *image = ((ImageAsset*)AssetManager::getSingleton()->getAsset(paths[i]))->image;
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB16F, width, width, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
if(data){
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB16F, width, width, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
else{
cout << "Failed to read cubemap texture " << i << endl;
exit(-1);
}
} }
else else
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB16F, width, width, 0, GL_RGB, GL_UNSIGNED_INT, NULL); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB16F, width, width, 0, GL_RGB, GL_UNSIGNED_INT, NULL);
} }
else{ else
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_DEPTH_COMPONENT, width, width, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_DEPTH_COMPONENT, width, width, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
}
} }
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+1 -2
View File
@@ -32,9 +32,8 @@ namespace vb01{
bool cubemap = false; bool cubemap = false;
u32 *texture = nullptr; u32 *texture = nullptr;
s64 lastUpdateTime = 0; s64 lastUpdateTime = 0;
int width, height, numChannels, updateRate = 0, numFrames = 0, frameA = 0, frameB = 0, mipmapLevel = 1; int width, height, updateRate = 0, numFrames = 0, frameA = 0, frameB = 0, mipmapLevel = 1;
float mixRatio; float mixRatio;
u8 *data;
std::string *paths = nullptr; std::string *paths = nullptr;
void createCubemap(bool, bool, bool); void createCubemap(bool, bool, bool);