Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better Texture #100

Merged
merged 2 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/cinnabar-render/assets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ namespace ce {
frag = "";
};
struct TextureFile {
unsigned char* data = NULL;
int
unsigned char* data = NULL; // TODO: what type should this actually be? this has been a void* in some areas
GLsizei
width = 0,
height = 0,
channelCount = 0;
height = 0;
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
19 changes: 12 additions & 7 deletions include/cinnabar-render/texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@
namespace ce {
class Texture {
public:
Texture(std::string filename, GLenum type = GL_TEXTURE_2D)
: Texture(ce::assetManager::getTextureFile(filename), type){};
Texture(TextureFile textureFile, GLenum type = GL_TEXTURE_2D);
Texture(const void* data, GLsizei width, GLsizei height, GLenum color_space = GL_RGBA, GLenum type = 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;
int m_width, m_height, m_channelCount;
GLenum m_type;
bool loadData(const void* data, GLsizei width, GLsizei height, GLenum color_space = GL_RGBA, GLenum type = GL_TEXTURE_2D);
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);
};
}
10 changes: 10 additions & 0 deletions src/cinnabar-render-demo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ int main(int argc, char* argv[]) {

window->swapBuffers();

// error check
// TODO: make this into some function
while (true) {
GLenum tmp = glGetError();
if (tmp == GL_NO_ERROR)
break;
else
LOG_ERROR("Uncaught GL error: 0x%04x", tmp);
}

// framerate cap
time->waitUntilDelta(deltaTimeMin);
}
Expand Down
23 changes: 22 additions & 1 deletion src/cinnabar-render/asset_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,32 @@ ce::ShaderFile ce::assetManager::getShaderFile(std::string vert, std::string geo
ce::TextureFile ce::assetManager::getTextureFile(std::string path) {
// stbi_set_flip_vertically_on_load(1);
TextureFile textureFile;
int channelCount;
textureFile.data = stbi_load(
(defaults::RESOURCE_FOLDER + "/" + defaults::TEXTURE_FOLDER + "/" + path).c_str(),
&textureFile.width,
&textureFile.height,
&textureFile.channelCount,
&channelCount,
0);

switch (channelCount) {
case 1:
textureFile.internalColorSpace = GL_RED;
break;
case 2:
textureFile.internalColorSpace = GL_RG;
break;
case 3:
textureFile.internalColorSpace = GL_RGB;
break;
case 4:
textureFile.internalColorSpace = GL_RGBA;
break;
default:
LOG_WARN("Unsupported texture channel count: %i", channelCount);
textureFile.data == NULL;
}

if (textureFile.data == NULL) {
LOG_WARN("Failed to load texture: %s", path.c_str());
if (path == defaults::TEXTURE_MISSING)
Expand All @@ -57,6 +77,7 @@ ce::TextureFile ce::assetManager::getTextureFile(std::string path) {
return getTextureFile(defaults::TEXTURE_MISSING);
} else
LOG_SUCCESS("Loaded texture: %s", path.c_str());

return textureFile;
}

Expand Down
73 changes: 33 additions & 40 deletions src/cinnabar-render/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,15 @@

#include <cinnabar-render/asset_manager.hpp>

ce::Texture::Texture(TextureFile textureFile, GLenum type)
: m_width(0), m_height(0), m_channelCount(0), m_type(type) {
m_width = textureFile.width;
m_height = textureFile.height;
m_channelCount = textureFile.channelCount;

void ce::Texture::init(TextureFile textureFile, GLenum colorSpace, GLenum target) {
glGenTextures(1, &m_texture);
bind();
glTexParameteri(type, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(type, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_REPEAT);

glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT); // TODO: proper system for setting texture parameters
glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_REPEAT);

if (this->loadData(textureFile.data, textureFile.width, textureFile.height, textureFile.channelCount == 3 ? GL_RGB : GL_RGBA, type)) {
LOG_SUCCESS("Loaded texture");
} else {
LOG_ERROR("Failed to load texture");
}
ce::assetManager::freeTextureFile(textureFile);
}

ce::Texture::Texture(const void* data, GLsizei width, GLsizei height, GLenum color_space, GLenum type)
: m_width(0), m_height(0), m_channelCount(0), m_type(type) {

if (this->loadData(data, width, height, color_space, type)) {
if (this->loadData(textureFile, colorSpace, target)) {
LOG_SUCCESS("Loaded texture");
} else {
LOG_ERROR("Failed to load texture");
Expand All @@ -41,8 +24,8 @@ ce::Texture::~Texture() {
}

void ce::Texture::bind() {
glBindTexture(m_type, m_texture);
glEnable(m_type);
glBindTexture(m_target, m_texture);
glEnable(m_target);
}

void ce::Texture::activate(int slot = 0) {
Expand All @@ -51,24 +34,34 @@ void ce::Texture::activate(int slot = 0) {
}

void ce::Texture::unbind() {
glDisable(m_type);
glDisable(m_target);
glActiveTexture(0);
glBindTexture(m_type, 0);
glBindTexture(m_target, 0);
}

bool ce::Texture::loadData(const void* data, GLsizei width, GLsizei height, GLenum color_space, GLenum type) {
m_width = width;
m_height = height;
bool ce::Texture::loadData(TextureFile textureFile, GLenum colorSpace, GLenum target) {
m_target = target;
if (!colorSpace)
switch (textureFile.internalColorSpace) {
case GL_RED:
case GL_RG:
case GL_RGB:
case GL_RGBA:
colorSpace = textureFile.internalColorSpace;
break;

bool out = false;
bind();
// 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.internalColorSpace);
}

if (data) {
glTexImage2D(type, 0, color_space, m_width, m_height, 0, color_space,
GL_UNSIGNED_BYTE, data);
glGenerateMipmap(type);
out = true;
}
unbind();
return out;
if (textureFile.data) {
bind();
glTexImage2D(m_target, 0, textureFile.internalColorSpace, textureFile.width, textureFile.height, 0, colorSpace,
GL_UNSIGNED_BYTE, textureFile.data);
glGenerateMipmap(m_target);
unbind();
return true;
} else
return false;
}