From cbbe6589ea2a32a7cf7cc0842c63847aa32a956f Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 20 Nov 2021 09:00:41 +0200 Subject: [PATCH] removed unneeded texture members, added mipmaps --- lightSample.cpp | 6 ++-- morphAnimationSample.cpp | 2 +- root.cpp | 2 +- shadowSample.cpp | 4 +-- skeletalAnimationSample.cpp | 2 +- textSample.cpp | 2 +- texture.cpp | 62 ++++++++++++++++++++++--------------- texture.h | 20 +++++------- 8 files changed, 54 insertions(+), 46 deletions(-) diff --git a/lightSample.cpp b/lightSample.cpp index 3254be6..58ea8cc 100644 --- a/lightSample.cpp +++ b/lightSample.cpp @@ -50,15 +50,15 @@ int main(){ mat->addBoolUniform("specularMapEnabled", true); string im0[] = {TEX_PATH + "bricks.jpg"}; - Texture *diffuseTex = new Texture(im0, 1); + Texture *diffuseTex = new Texture(im0, 1, false); mat->addTexUniform("textures[0]", diffuseTex, true); string im1[] = {TEX_PATH + "bricksNormal.jpg"}; - Texture *normalTex = new Texture(im1, 1, Texture::NORMAL); + Texture *normalTex = new Texture(im1, 1, false); mat->addTexUniform("textures[1]", normalTex, true); string im2[] = {TEX_PATH + "bricksSpecular.jpg"}; - Texture *specularTex = new Texture(im2, 1, Texture::SPECULAR); + Texture *specularTex = new Texture(im2, 1, false); mat->addTexUniform("textures[2]", specularTex, true); //Setting specular parameteres diff --git a/morphAnimationSample.cpp b/morphAnimationSample.cpp index 6aecc38..2f6684b 100644 --- a/morphAnimationSample.cpp +++ b/morphAnimationSample.cpp @@ -44,7 +44,7 @@ int main(){ mat->addBoolUniform("lightingEnabled", false); mat->addBoolUniform("texturingEnabled", true); string images[] = {TEX_PATH + "defaultTexture.jpg"}; - Texture *texture = new Texture(images, 1); + Texture *texture = new Texture(images, 1, false); mat->addTexUniform("textures[0]", texture, true); model->setMaterial(mat); rootNode->attachChild(model); diff --git a/root.cpp b/root.cpp index 1b74d6b..ddfdff2 100755 --- a/root.cpp +++ b/root.cpp @@ -196,7 +196,7 @@ namespace vb01{ void Root::createSkybox(string paths[6]){ skybox = new Box(Vector3(1, 1, 1) * 10); Material *skyboxMat = new Material(libPath + "skybox"); - Texture *texture = new Texture(paths); + Texture *texture = new Texture(paths, 6, true); skyboxMat->addTexUniform("skybox", texture, true); skybox->setMaterial(skyboxMat); } diff --git a/shadowSample.cpp b/shadowSample.cpp index 2a1b227..401c820 100644 --- a/shadowSample.cpp +++ b/shadowSample.cpp @@ -46,7 +46,7 @@ int main(){ boxMat->addBoolUniform("texturingEnabled", true); boxMat->addBoolUniform("lightingEnabled", true); string im0[] = {TEX_PATH + "bricks.jpg"}; - Texture *boxTex = new Texture(im0, 1); + Texture *boxTex = new Texture(im0, 1, false); boxMat->addTexUniform("textures[0]", boxTex, true); box->setMaterial(boxMat); boxNode->attachMesh(box); @@ -57,7 +57,7 @@ int main(){ jetMat->addBoolUniform("texturingEnabled", true); jetMat->addBoolUniform("lightingEnabled", true); string im1[] = {TEX_PATH + "defaultTexture.jpg"}; - Texture *jetTex = new Texture(im1, 1); + Texture *jetTex = new Texture(im1, 1, false); jetMat->addTexUniform("textures[0]", jetTex, true); jet->setMaterial(jetMat); rootNode->attachChild(jet); diff --git a/skeletalAnimationSample.cpp b/skeletalAnimationSample.cpp index 0dfc3b1..3c4c57d 100644 --- a/skeletalAnimationSample.cpp +++ b/skeletalAnimationSample.cpp @@ -44,7 +44,7 @@ int main(){ mat->addBoolUniform("lightingEnabled", false); mat->addBoolUniform("texturingEnabled", true); string images[] = {TEX_PATH + "defaultTexture.jpg"}; - Texture *texture = new Texture(images, 1); + Texture *texture = new Texture(images, 1, false); mat->addTexUniform("textures[0]", texture, true); model->setMaterial(mat); rootNode->attachChild(model); diff --git a/textSample.cpp b/textSample.cpp index 1ee0ddf..e6d4c93 100644 --- a/textSample.cpp +++ b/textSample.cpp @@ -55,7 +55,7 @@ int main(){ textMat->addBoolUniform("texturingEnabled", true); string frames[]{TEX_PATH + "bricks.jpg", TEX_PATH + "defaultTexture.jpg"}; - Texture *texture = new Texture(frames, 2); + Texture *texture = new Texture(frames, 2, false); textMat->addTexUniform("textures[0]", texture, true); for(int i = 0; i < numTexts; i++){ diff --git a/texture.cpp b/texture.cpp index da1b846..d0c30f1 100755 --- a/texture.cpp +++ b/texture.cpp @@ -20,6 +20,7 @@ namespace vb01{ glGenTextures(1, &texture[0]); glBindTexture(GL_TEXTURE_2D, texture[0]); + if(shadowMap){ glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); @@ -39,49 +40,53 @@ namespace vb01{ } } - Texture::Texture(string path[], int numPaths, TextureTypeId textureTypeId, bool flip){ + Texture::Texture(string paths[], int numPaths, bool cubemap, int mipmapLevel, bool flip){ + this->numFrames = numPaths; + this->cubemap = cubemap; + this->paths = new string[numPaths]; + + for(int i = 0; i < numPaths; i++) + this->paths[i] = paths[i]; + + if(cubemap){ + texture = new u32; + createCubemap(false, flip); + } + else{ + texture = new u32[numPaths]; + create2DTexture(flip); + } + } + + void Texture::create2DTexture(bool flip){ mixRatio = .1; - this->typeId = textureTypeId; - this->numFrames = numPaths; - texture = new u32[numPaths]; - - for(int i = 0; i < numPaths; i++){ + for(int i = 0; i < numFrames; i++){ bool png = false; - int length = path[i].length(); + int length = paths[i].length(); - if(path[i].substr(length - 4, string::npos) == ".png") + if(paths[i].substr(length - 4, string::npos) == ".png") png = true; glGenTextures(1, &texture[i]); glBindTexture(GL_TEXTURE_2D, texture[i]); 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_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); stbi_set_flip_vertically_on_load(flip); - data = stbi_load(path[i].c_str(), &width, &height, &numChannels, 0); + 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, data); - //glGenerateMipmap(GL_TEXTURE_2D); + glGenerateMipmap(GL_TEXTURE_2D); stbi_image_free(data); } } - Texture::Texture(string paths[6], bool flip){ - this->type = TextureType::TEXTURE_CUBEMAP; - texture = new u32; - - for(int i = 0; i < 6; i++) - this->paths[i] = paths[i]; - createCubemap(false, flip); - } - Texture::Texture(int width, bool depth){ this->width = width; - this->type = TextureType::TEXTURE_CUBEMAP; texture = new u32; createCubemap(true, false); @@ -97,23 +102,27 @@ namespace vb01{ for(int i = 0; i < 6; i++){ if(!depth){ data = stbi_load(paths[i].c_str(), &width, &height, &numChannels, 0); + if(data){ glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); stbi_image_free(data); } else{ - cout<<"Failed to read cubemap texture "< 0){ for(int i = 0; i < numFrames; i++) glDeleteTextures(1, &texture[i]); + delete[] texture; } else{ @@ -169,12 +180,13 @@ namespace vb01{ void Texture::update(int id){ select(id, frameA); + if(numFrames > 0) select(id + 1, frameB); } void Texture::select(int id, int fr){ glActiveTexture(GL_TEXTURE0 + id); - glBindTexture(type == TEXTURE_2D ? GL_TEXTURE_2D : GL_TEXTURE_CUBE_MAP, texture[fr]); + glBindTexture(cubemap ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D, texture[fr]); } } diff --git a/texture.h b/texture.h index 77cafd1..6084af9 100755 --- a/texture.h +++ b/texture.h @@ -12,34 +12,30 @@ namespace vb01{ class Texture : public Animatable{ public: - enum TextureType{TEXTURE_2D, TEXTURE_CUBEMAP}; - enum TextureTypeId{DIFFUSE, NORMAL, SPECULAR, PARALLAX, ENVIRONMENT}; - ~Texture(); Texture(int, int, bool = true); - Texture(std::string[], int, TextureTypeId = DIFFUSE, bool = false); - Texture(std::string[6], bool = false); + Texture(std::string[], int, bool, int = 0, bool = false); Texture(int, bool = true); Texture(FT_Face&, char); void select(int = 0, int = 0); void update(int = 0); void animate(float, KeyframeChannel); - inline unsigned int* getTexture(int i = 0){return &(texture[i]);} - inline std::string getPath(){return path;} + inline u32* getTexture(int i = 0){return &(texture[i]);} + inline std::string* getPath(){return paths;} inline int getNumFrames(){return numFrames;} + inline int getMipmapLevel(){return mipmapLevel;} inline float getMixRatio(){return mixRatio;} - inline TextureTypeId getTextureTypeId(){return typeId;} private: - TextureType type = TextureType::TEXTURE_2D; - TextureTypeId typeId = DIFFUSE; + bool cubemap = false; u32 *texture = nullptr; s64 lastUpdateTime = 0; - int width, height, numChannels, updateRate = 0, numFrames = 0, frameA = 0, frameB = 0; + int width, height, numChannels, updateRate = 0, numFrames = 0, frameA = 0, frameB = 0, mipmapLevel = 1; float mixRatio; u8 *data; - std::string path = "", paths[6]; + std::string *paths = nullptr; void createCubemap(bool, bool); + void create2DTexture(bool); inline int getNextFrame(int frameId){return (frameId + 1 < numFrames ? frameId + 1 : 0);} }; }