Skip to content

Commit

Permalink
finished Texture changes
Browse files Browse the repository at this point in the history
  • Loading branch information
SArpnt committed Sep 20, 2021
1 parent 84b456a commit 008c632
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
9 changes: 3 additions & 6 deletions include/cinnabar-render/assets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ namespace ce {
geom = "",
frag = "";
};
struct TextureFormat {
struct TextureFile {
unsigned char* data = NULL; // TODO: what type should this actually be? this has been a void* in some areas
GLsizei
width = 0,
height = 0;
GLint internalColorSpace = 0; // TODO: is there a better value for this? GL_NONE exists but doesn't seem correct
};
struct TextureFile {
unsigned char* data = NULL; // TODO: what type should this actually be? this has been a void* in some areas
TextureFormat format;
GLint internalColorSpace = 0; // TODO: is there a better value for this? GL_NONE exists but doesn't seem correct (also make changes in Texture)
};
struct MaterialFile {
glm::vec4
Expand Down
14 changes: 9 additions & 5 deletions include/cinnabar-render/texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@
namespace ce {
class Texture {
public:
Texture(std::string filename, GLenum colorSpace = 0, GLenum target = GL_TEXTURE_2D) // TODO: default colorSpace should change based on TextureFile.channelCount (GL_RED, GL_RG, GL_RGB, GL_RGBA)
: Texture(ce::assetManager::getTextureFile(filename), colorSpace, target){};
Texture(TextureFile textureFile, GLenum colorSpace = 0, GLenum target = GL_TEXTURE_2D);
Texture(std::string filename, GLenum colorSpace = 0, GLenum target = GL_TEXTURE_2D) {
TextureFile textureFile = ce::assetManager::getTextureFile(filename);
init(textureFile, colorSpace, target);
ce::assetManager::freeTextureFile(textureFile);
};
Texture(TextureFile textureFile, GLenum colorSpace = 0, GLenum target = GL_TEXTURE_2D) {
init(textureFile, colorSpace, target);
};
~Texture();

void bind(), unbind(), activate(int slot);

private:
GLuint m_texture;
TextureFormat m_format;
GLenum m_colorSpace;
GLenum m_target;

void init(TextureFile textureFile, GLenum colorSpace = 0, GLenum target = GL_TEXTURE_2D);
bool loadData(TextureFile textureFile, GLenum colorSpace = 0, GLenum target = GL_TEXTURE_2D);
};
}
12 changes: 6 additions & 6 deletions src/cinnabar-render/asset_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,23 @@ ce::TextureFile ce::assetManager::getTextureFile(std::string path) {
int channelCount;
textureFile.data = stbi_load(
(defaults::RESOURCE_FOLDER + "/" + defaults::TEXTURE_FOLDER + "/" + path).c_str(),
&textureFile.format.width,
&textureFile.format.height,
&textureFile.width,
&textureFile.height,
&channelCount,
0);

switch (channelCount) {
case 1:
textureFile.format.internalColorSpace = GL_RED;
textureFile.internalColorSpace = GL_RED;
break;
case 2:
textureFile.format.internalColorSpace = GL_RG;
textureFile.internalColorSpace = GL_RG;
break;
case 3:
textureFile.format.internalColorSpace = GL_RGB;
textureFile.internalColorSpace = GL_RGB;
break;
case 4:
textureFile.format.internalColorSpace = GL_RGBA;
textureFile.internalColorSpace = GL_RGBA;
break;
default:
LOG_WARN("Unsupported texture channel count: %i", channelCount);
Expand Down
17 changes: 6 additions & 11 deletions src/cinnabar-render/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

#include <cinnabar-render/asset_manager.hpp>

ce::Texture::Texture(TextureFile textureFile, GLenum colorSpace, GLenum target) {

void ce::Texture::init(TextureFile textureFile, GLenum colorSpace, GLenum target) {
glGenTextures(1, &m_texture);
bind();
glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT); // TODO: proper system for setting texture parameters
Expand All @@ -18,7 +17,6 @@ ce::Texture::Texture(TextureFile textureFile, GLenum colorSpace, GLenum target)
} else {
LOG_ERROR("Failed to load texture");
}
ce::assetManager::freeTextureFile(textureFile); // TODO: what if the TextureFile is used more than once? this should only be called if a string is passed in for the constructor
}

ce::Texture::~Texture() {
Expand All @@ -42,27 +40,24 @@ void ce::Texture::unbind() {
}

bool ce::Texture::loadData(TextureFile textureFile, GLenum colorSpace, GLenum target) {
m_format = textureFile.format; // TODO: are these variables needed, and are they redundantly set elsewhere?
m_target = target;
if (colorSpace)
m_colorSpace = colorSpace;
else
switch (m_format.internalColorSpace) {
if (!colorSpace)
switch (textureFile.internalColorSpace) {
case GL_RED:
case GL_RG:
case GL_RGB:
case GL_RGBA:
m_colorSpace = m_format.internalColorSpace;
colorSpace = textureFile.internalColorSpace;
break;

// TODO: add formats GL_RED_INTEGER, GL_RG_INTEGER, GL_RGB_INTEGER, GL_RGBA_INTEGER, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL
default:
LOG_WARN("Unrecognized internal color space: %i", textureFile.format.internalColorSpace);
LOG_WARN("Unrecognized internal color space: %i", textureFile.internalColorSpace);
}

if (textureFile.data) {
bind();
glTexImage2D(m_target, 0, m_format.internalColorSpace, m_format.width, m_format.height, 0, m_colorSpace,
glTexImage2D(m_target, 0, textureFile.internalColorSpace, textureFile.width, textureFile.height, 0, colorSpace,
GL_UNSIGNED_BYTE, textureFile.data);
glGenerateMipmap(m_target);
unbind();
Expand Down

0 comments on commit 008c632

Please sign in to comment.