loading new image data into a texture

additional ImageAsset property
This commit is contained in:
devZoGok
2024-09-27 17:25:18 +03:00
parent 63990e0ef0
commit c08ac0fd12
4 changed files with 19 additions and 12 deletions
+3 -2
View File
@@ -5,15 +5,16 @@
namespace vb01{ namespace vb01{
struct ImageAsset : public Asset{ struct ImageAsset : public Asset{
ImageAsset(std::string path, u8 *image, int width, int height){ ImageAsset(std::string path, u8 *image, int width, int height, int numChannels){
this->path = path; this->path = path;
this->image = image; this->image = image;
this->width = width; this->width = width;
this->height = height; this->height = height;
this->numChannels = numChannels;
} }
u8 *image; u8 *image;
int width, height; int width, height, numChannels;
}; };
} }
+1 -1
View File
@@ -18,6 +18,6 @@ namespace vb01{
stbi_set_flip_vertically_on_load(flip); stbi_set_flip_vertically_on_load(flip);
int width, height, numChannels; int width, height, numChannels;
u8 *data = stbi_load(path.c_str(), &width, &height, &numChannels, 0); u8 *data = stbi_load(path.c_str(), &width, &height, &numChannels, 0);
return new ImageAsset(path, data, width, height); return new ImageAsset(path, data, width, height, numChannels);
} }
} }
+12 -9
View File
@@ -60,6 +60,16 @@ namespace vb01{
} }
} }
void Texture::loadImageData(ImageAsset *asset, int i){
width = asset->width;
height = asset->height;
glGenTextures(1, &texture[i]);
glBindTexture(GL_TEXTURE_2D, texture[i]);
glTexImage2D(GL_TEXTURE_2D, 0, png ? GL_RGBA : GL_RGB, width, height, 0, png ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, asset->image);
glGenerateMipmap(GL_TEXTURE_2D);
}
void Texture::create2DTexture(bool flip){ void Texture::create2DTexture(bool flip){
mixRatio = .1; mixRatio = .1;
@@ -69,19 +79,12 @@ namespace vb01{
if(paths[i].substr(length - 4, string::npos) == ".png") if(paths[i].substr(length - 4, string::npos) == ".png")
png = true; png = true;
glGenTextures(1, &texture[i]); loadImageData((ImageAsset*)AssetManager::getSingleton()->getAsset(paths[i]));
glBindTexture(GL_TEXTURE_2D, texture[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
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);
ImageAsset *asset = (ImageAsset*)AssetManager::getSingleton()->getAsset(paths[i]);
width = asset->width;
height = asset->height;
glTexImage2D(GL_TEXTURE_2D, 0, png ? GL_RGBA : GL_RGB, width, height, 0, png ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, asset->image);
glGenerateMipmap(GL_TEXTURE_2D);
} }
} }
+3
View File
@@ -10,6 +10,8 @@
#include FT_FREETYPE_H #include FT_FREETYPE_H
namespace vb01{ namespace vb01{
struct ImageAsset;
class Texture : public Animatable{ class Texture : public Animatable{
public: public:
~Texture(); ~Texture();
@@ -20,6 +22,7 @@ namespace vb01{
void select(int = 0, int = 0); void select(int = 0, int = 0);
void update(int = 0); void update(int = 0);
void animate(float, KeyframeChannel); void animate(float, KeyframeChannel);
void loadImageData(ImageAsset*, int = 0);
inline u32* getTexture(int i = 0){return &(texture[i]);} inline u32* getTexture(int i = 0){return &(texture[i]);}
inline std::string* getPath(){return paths;} inline std::string* getPath(){return paths;}
inline int getNumFrames(){return numFrames;} inline int getNumFrames(){return numFrames;}