Skip to content

Commit

Permalink
Remove all the unused texture code.
Browse files Browse the repository at this point in the history
The app only ever draws cubemapped (environment reflection) textures.
Remove all the 2D texture code and un-virtual the class.
  • Loading branch information
DanAlbert committed May 15, 2024
1 parent aef87ae commit 3930e39
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 182 deletions.
180 changes: 16 additions & 164 deletions teapots/textured-teapot/src/main/cpp/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& texFiles,
AAssetManager* assetManager);
virtual bool GetActiveSamplerInfo(std::vector<std::string>& names,
std::vector<GLint>& 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<std::string>& names,
std::vector<GLint>& 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<std::string>& 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<std::string>& files,
AAssetManager* mgr) {
Texture::Texture(std::vector<std::string>& 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<uint8_t> fileBits;

if (!mgr || files.size() != 6) {
if (!asset_manager || asset_paths.size() != 6) {
assert(false);
return;
}
Expand All @@ -125,9 +52,9 @@ TextureCubemap::TextureCubemap(std::vector<std::string>& 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);

Expand All @@ -147,110 +74,35 @@ TextureCubemap::TextureCubemap(std::vector<std::string>& 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<std::string>& names,
std::vector<GLint>& 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<uint8_t> 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<std::string>& names,
std::vector<GLint>& units) {
void Texture::GetActiveSamplerInfo(std::vector<std::string>& names,
std::vector<GLint>& 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_; }
33 changes: 16 additions & 17 deletions teapots/textured-teapot/src/main/cpp/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* limitations under the License.
*/

#ifndef TEAPOTS_TEXTURE_H
#define TEAPOTS_TEXTURE_H
#pragma once

#include <EGL/egl.h>
#include <GLES/gl.h>
Expand All @@ -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<std::string>& texFiles,
AAssetManager* assetManager);
Texture(std::vector<std::string>& asset_paths, AAssetManager* asset_manager);
Texture(const Texture&) = delete;
~Texture();

Texture& operator=(const Texture&) = delete;

void GetActiveSamplerInfo(std::vector<std::string>& names,
std::vector<GLint>& units);
bool Activate(void);

virtual bool GetActiveSamplerInfo(std::vector<std::string>& names,
std::vector<GLint>& units) = 0;
virtual bool Activate(void) = 0;
virtual GLuint GetTexType() = 0;
virtual GLuint GetTexId() = 0;
private:
GLuint texId_;
};
#endif // TEAPOTS_TEXTURE_H
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> samplers;
Expand Down

0 comments on commit 3930e39

Please sign in to comment.