Skip to content

Commit

Permalink
added programatic texture creation
Browse files Browse the repository at this point in the history
  • Loading branch information
tumble1999 committed Aug 19, 2021
1 parent 7e098c5 commit 8eac544
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
34 changes: 29 additions & 5 deletions src/cinnabar-engine/cinnabar-render/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@ ce::Texture::Texture(std::string filename, GLenum type)
glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_REPEAT);

if (textureFile.data) {
glTexImage2D(type, 0, GL_RGBA, m_width, m_height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, textureFile.data);
glGenerateMipmap(type);

if (this->loadData(textureFile.data, textureFile.width, textureFile.height, GL_RGBA, type)) {
LOG_SUCCESS("Loaded texture: %s", filename.c_str());
} else
LOG_ERROR("TEXTURE_LOADING_FAILED: %s", filename.c_str());
unbind();
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)) {
LOG_SUCCESS("Loaded texture");
} else {
LOG_ERROR("TEXTURE_LOADING_FAILED");
}
}

ce::Texture::~Texture() {
glDeleteTextures(1, &m_texture);
}
Expand All @@ -48,3 +55,20 @@ void ce::Texture::unbind() {
glActiveTexture(0);
glBindTexture(m_type, 0);
}

bool ce::Texture::loadData(const void* data, GLsizei width, GLsizei height, GLenum color_space, GLenum type) {
m_width = width;
m_height = height;

bool out = false;
bind();

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;
}
4 changes: 3 additions & 1 deletion src/cinnabar-engine/cinnabar-render/texture.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#pragma once

#include <GL/glew.h>
#include "asset_manager.hpp"
#include <GL/glew.h>

namespace ce {
class Texture {
public:
Texture(std::string filename, GLenum type = GL_TEXTURE_2D);
Texture(const void* data, GLsizei width, GLsizei height, GLenum color_space = GL_RGBA, GLenum type = GL_TEXTURE_2D);
~Texture();

void bind(), unbind(), activate(int slot);
Expand All @@ -15,5 +16,6 @@ namespace ce {
GLuint m_texture;
int m_width, m_height, m_channelCount;
unsigned int m_type;
bool loadData(const void* data, GLsizei width, GLsizei height, GLenum color_space = GL_RGBA, GLenum type = GL_TEXTURE_2D);
};
}

0 comments on commit 8eac544

Please sign in to comment.