From 22d3e193d8f131d7d6d2cc66bc52e4bcd79b6449 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Tue, 14 May 2024 15:19:43 -0700 Subject: [PATCH] Remove all the unused texture code. The app only ever draws cubemapped (environment reflection) textures. Remove all the 2D texture code and un-virtual the class. --- .../textured-teapot/src/main/cpp/Texture.cpp | 180 ++---------------- .../textured-teapot/src/main/cpp/Texture.h | 33 ++-- .../src/main/cpp/TexturedTeapotRender.cpp | 2 +- 3 files changed, 33 insertions(+), 182 deletions(-) diff --git a/teapots/textured-teapot/src/main/cpp/Texture.cpp b/teapots/textured-teapot/src/main/cpp/Texture.cpp index 7bf469ff7..4d966d1b1 100644 --- a/teapots/textured-teapot/src/main/cpp/Texture.cpp +++ b/teapots/textured-teapot/src/main/cpp/Texture.cpp @@ -26,91 +26,18 @@ #define MODULE_NAME "Teapot::Texture" #include "android_debug.h" -/** - * Cubemap and Texture2d implementations for Class Texture. - */ -class TextureCubemap : public Texture { - protected: - GLuint texId_ = GL_INVALID_VALUE; - bool activated_ = false; - - public: - virtual ~TextureCubemap(); - TextureCubemap(std::vector& texFiles, - AAssetManager* assetManager); - virtual bool GetActiveSamplerInfo(std::vector& names, - std::vector& units); - virtual bool Activate(void); - virtual GLuint GetTexType(); - virtual GLuint GetTexId(); -}; - -class Texture2d : public Texture { - protected: - GLuint texId_ = GL_INVALID_VALUE; - bool activated_ = false; - - public: - virtual ~Texture2d(); - // Implement just one texture - Texture2d(std::string& texFiles, AAssetManager* assetManager); - virtual bool GetActiveSamplerInfo(std::vector& names, - std::vector& units); - virtual bool Activate(void); - virtual GLuint GetTexType(); - virtual GLuint GetTexId(); -}; - -/** - * Capability debug string - */ -static const std::string supportedTextureTypes = - "GL_TEXTURE_2D(0x0DE1) GL_TEXTURE_CUBE_MAP(0x8513)"; - /** * Interface implementations */ -Texture::Texture() {} -Texture::~Texture() {} -/** - * Create Texture Object - * @param texFiles holds the texture file name(s) under APK's assets - * @param assetManager is used to open texture files inside assets - * @return is the newly created Texture Object - */ -Texture* Texture::Create(std::vector& texFiles, - AAssetManager* assetManager) { - return new TextureCubemap(texFiles, assetManager); -} - -/** - * TextureCubemap implementations - */ -bool TextureCubemap::Activate(void) { - assert(texId_ != GL_INVALID_VALUE); - - glBindTexture(texId_, GL_TEXTURE0); - glActiveTexture(GL_TEXTURE0 + 0); - activated_ = true; - return true; -} - -GLuint TextureCubemap::GetTexType() { return GL_TEXTURE_CUBE_MAP; } - -GLuint TextureCubemap::GetTexId() { - assert(texId_ != GL_INVALID_VALUE); - return texId_; -} - -TextureCubemap::TextureCubemap(std::vector& files, - AAssetManager* mgr) { +Texture::Texture(std::vector& asset_paths, + AAssetManager* asset_manager) { // For Cubemap, we use world normal to sample the textures // so no texture vbo necessary int32_t imgWidth, imgHeight, channelCount; std::vector fileBits; - if (!mgr || files.size() != 6) { + if (!asset_manager || asset_paths.size() != 6) { assert(false); return; } @@ -125,9 +52,9 @@ TextureCubemap::TextureCubemap(std::vector& files, for (GLuint i = 0; i < 6; i++) { fileBits.clear(); - AssetReadFile(mgr, files[i], fileBits); + AssetReadFile(asset_manager, asset_paths[i], fileBits); - // tga/bmp files are saved as vertical mirror images ( at least more than + // tga/bmp asset_paths are saved as vertical mirror images ( at least more than // half ). stbi_set_flip_vertically_on_load(1); @@ -147,110 +74,35 @@ TextureCubemap::TextureCubemap(std::vector& files, glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_REPEAT); glActiveTexture(GL_TEXTURE0); - activated_ = true; } -/** - * Dtor - * clean-up function - */ -TextureCubemap::~TextureCubemap() { +Texture::~Texture() { if (texId_ != GL_INVALID_VALUE) { glDeleteTextures(1, &texId_); texId_ = GL_INVALID_VALUE; - activated_ = false; } } /** - Return used sampler names and units - so application could configure shader's sampler uniform(s). - Cubemap just used one sampler at unit 0 with "samplerObj" as its name. + * TextureCubemap implementations */ -bool TextureCubemap::GetActiveSamplerInfo(std::vector& names, - std::vector& units) { - names.clear(); - names.push_back(std::string("samplerObj")); - units.clear(); - units.push_back(0); +bool Texture::Activate(void) { + assert(texId_ != GL_INVALID_VALUE); + glBindTexture(texId_, GL_TEXTURE0); + glActiveTexture(GL_TEXTURE0 + 0); return true; } /** - * Texture2D implementation - */ -Texture2d::Texture2d(std::string& fileName, AAssetManager* assetManager) { - if (!assetManager) { - LOGE("AssetManager to Texture2D() could not be null!!!"); - assert(false); - return; - } - - int32_t imgWidth, imgHeight, channelCount; - std::string texName(fileName); - std::vector fileBits; - - glGenTextures(1, &texId_); - glBindTexture(GL_TEXTURE_2D, texId_); - - if (texId_ == GL_INVALID_VALUE) { - assert(false); - return; - } - - AssetReadFile(assetManager, texName, fileBits); - - // tga/bmp files are saved as vertical mirror images ( at least more than half - // ). - stbi_set_flip_vertically_on_load(1); - - uint8_t* imageBits = - stbi_load_from_memory(fileBits.data(), fileBits.size(), &imgWidth, - &imgHeight, &channelCount, 4); - glTexImage2D(GL_TEXTURE_2D, 0, // mip level - GL_RGBA, imgWidth, imgHeight, - 0, // border color - GL_RGBA, GL_UNSIGNED_BYTE, imageBits); - - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - - glActiveTexture(GL_TEXTURE0); - - stbi_image_free(imageBits); -} - -Texture2d::~Texture2d() { - if (texId_ != GL_INVALID_VALUE) { - glDeleteTextures(1, &texId_); - texId_ = GL_INVALID_VALUE; - } - activated_ = false; -} - -/** - * Same as the Cubemap::GetActiveSamplerInfo() + Return used sampler names and units + so application could configure shader's sampler uniform(s). + Cubemap just used one sampler at unit 0 with "samplerObj" as its name. */ -bool Texture2d::GetActiveSamplerInfo(std::vector& names, - std::vector& units) { +void Texture::GetActiveSamplerInfo(std::vector& names, + std::vector& units) { names.clear(); names.push_back(std::string("samplerObj")); units.clear(); units.push_back(0); - - return true; -} - -bool Texture2d::Activate(void) { - glBindTexture(texId_, GL_TEXTURE0); - glActiveTexture(GL_TEXTURE0 + 0); - activated_ = true; - return true; } - -GLuint Texture2d::GetTexType() { return GL_TEXTURE_2D; } - -GLuint Texture2d::GetTexId() { return texId_; } diff --git a/teapots/textured-teapot/src/main/cpp/Texture.h b/teapots/textured-teapot/src/main/cpp/Texture.h index 8add46fb8..f0ddfd465 100644 --- a/teapots/textured-teapot/src/main/cpp/Texture.h +++ b/teapots/textured-teapot/src/main/cpp/Texture.h @@ -14,8 +14,7 @@ * limitations under the License. */ -#ifndef TEAPOTS_TEXTURE_H -#define TEAPOTS_TEXTURE_H +#pragma once #include #include @@ -37,24 +36,24 @@ */ class Texture { public: - Texture(); - virtual ~Texture(); - /** - * Create a texture object - * @param texFiles holds image file names under APK/assets. - * 2d texture uses the very first image texFiles[0] + * Create a texture object + * + * @param asset_paths holds image file names under APK/assets. * cube map needs 6 (direction of +x, -x, +y, -y, +z, -z) - * @param assetManager Java side assetManager object + * @param asset_manager Java side asset_manager object * @return newly created texture object, or nullptr in case of errors */ - static Texture* Create(std::vector& texFiles, - AAssetManager* assetManager); + Texture(std::vector& asset_paths, AAssetManager* asset_manager); + Texture(const Texture&) = delete; + ~Texture(); + + Texture& operator=(const Texture&) = delete; + + void GetActiveSamplerInfo(std::vector& names, + std::vector& units); + bool Activate(void); - virtual bool GetActiveSamplerInfo(std::vector& names, - std::vector& units) = 0; - virtual bool Activate(void) = 0; - virtual GLuint GetTexType() = 0; - virtual GLuint GetTexId() = 0; + private: + GLuint texId_; }; -#endif // TEAPOTS_TEXTURE_H diff --git a/teapots/textured-teapot/src/main/cpp/TexturedTeapotRender.cpp b/teapots/textured-teapot/src/main/cpp/TexturedTeapotRender.cpp index c065db845..43e6b8119 100644 --- a/teapots/textured-teapot/src/main/cpp/TexturedTeapotRender.cpp +++ b/teapots/textured-teapot/src/main/cpp/TexturedTeapotRender.cpp @@ -64,7 +64,7 @@ void TexturedTeapotRender::Init(AAssetManager* assetMgr) { std::string("Textures/back.tga") // GL_TEXTURE_CUBE_MAP_NEGATIVE_Z }; - texObj_ = Texture::Create(textures, assetMgr); + texObj_ = new Texture(textures, assetMgr); assert(texObj_); std::vector samplers;