Skip to content

Commit

Permalink
Refactor images and textures (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
fszewczyk authored Nov 3, 2024
1 parent fb7e1f8 commit 23c6ef4
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 153 deletions.
62 changes: 15 additions & 47 deletions src/AssetManager/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace shkyera {

Image::Image(const std::string& path) : _wasAssignedTextureId(false) {
Image::Image(const std::string& path) {
load(path);
}

Expand Down Expand Up @@ -47,52 +47,20 @@ int Image::getChannels() const {
return _components;
}

void Image::updateTextureId() {
uint32_t textureId = _textureId;
std::string Image::ICON_CONSOLE_TOTAL = "resources/icons/console/total.png";
std::string Image::ICON_CONSOLE_ERROR = "resources/icons/console/error.png";
std::string Image::ICON_CONSOLE_INFO = "resources/icons/console/info.png";
std::string Image::ICON_CONSOLE_VERBOSE = "resources/icons/console/verbose.png";
std::string Image::ICON_CONSOLE_SUCCESS = "resources/icons/console/success.png";
std::string Image::ICON_COMPONENT_TRANSFORM = "resources/icons/components/transform.png";
std::string Image::ICON_COMPONENT_SCRIPT = "resources/icons/components/script.png";
std::string Image::ICON_COMPONENT_SHAPE = "resources/icons/components/shape.png";
std::string Image::ICON_FILES_FOLDER = "resources/icons/files/folder.png";
std::string Image::ICON_FILES_PYTHON = "resources/icons/files/python.png";
std::string Image::ICON_FILES_IMAGE = "resources/icons/files/image.png";
std::string Image::ICON_FILES_TEXT = "resources/icons/files/text.png";
std::string Image::ICON_BUTTON_PLAY = "resources/icons/buttons/play.png";
std::string Image::ICON_BUTTON_STOP = "resources/icons/buttons/stop.png";

if (_wasAssignedTextureId)
glDeleteTextures(1, &textureId);

glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
if (_components == 3)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, _width, _height, 0, GL_RGB,
GL_UNSIGNED_BYTE, _data);
else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _width, _height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, _data);

_wasAssignedTextureId = true;
_textureId = textureId;
}

uint64_t Image::getTextureId() const {
return _textureId;
}

Image Image::ICON_CONSOLE_TOTAL = Image("resources/icons/console/total.png");
Image Image::ICON_CONSOLE_ERROR = Image("resources/icons/console/error.png");
Image Image::ICON_CONSOLE_INFO = Image("resources/icons/console/info.png");
Image Image::ICON_CONSOLE_VERBOSE =
Image("resources/icons/console/verbose.png");
Image Image::ICON_CONSOLE_SUCCESS =
Image("resources/icons/console/success.png");

Image Image::ICON_COMPONENT_TRANSFORM =
Image("resources/icons/components/transform.png");
Image Image::ICON_COMPONENT_SCRIPT =
Image("resources/icons/components/script.png");
Image Image::ICON_COMPONENT_SHAPE =
Image("resources/icons/components/shape.png");

Image Image::ICON_FILES_FOLDER = Image("resources/icons/files/folder.png");
Image Image::ICON_FILES_PYTHON = Image("resources/icons/files/python.png");
Image Image::ICON_FILES_IMAGE = Image("resources/icons/files/image.png");
Image Image::ICON_FILES_TEXT = Image("resources/icons/files/text.png");

Image Image::ICON_BUTTON_PLAY = Image("resources/icons/buttons/play.png");
Image Image::ICON_BUTTON_STOP = Image("resources/icons/buttons/stop.png");

} // namespace shkyera
44 changes: 14 additions & 30 deletions src/AssetManager/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,51 +33,35 @@ class Image : public Asset {
*/
void save(const std::string& path) const;

/**
* @brief Update the OpenGL texture ID associated with the image.
*/
void updateTextureId();

/**
* @brief Get the OpenGL texture ID associated with the image.
*
* @return The OpenGL texture ID.
*/
uint64_t getTextureId() const;

uint8_t const* getData() const;
int getWidth() const;
int getHeight() const;
int getChannels() const;

// Static image objects representing icons
static Image ICON_CONSOLE_TOTAL;
static Image ICON_CONSOLE_ERROR;
static Image ICON_CONSOLE_INFO;
static Image ICON_CONSOLE_VERBOSE;
static Image ICON_CONSOLE_SUCCESS;
static std::string ICON_CONSOLE_TOTAL;
static std::string ICON_CONSOLE_ERROR;
static std::string ICON_CONSOLE_INFO;
static std::string ICON_CONSOLE_VERBOSE;
static std::string ICON_CONSOLE_SUCCESS;

static Image ICON_COMPONENT_TRANSFORM;
static Image ICON_COMPONENT_SCRIPT;
static Image ICON_COMPONENT_SHAPE;
static std::string ICON_COMPONENT_TRANSFORM;
static std::string ICON_COMPONENT_SCRIPT;
static std::string ICON_COMPONENT_SHAPE;

static Image ICON_FILES_FOLDER;
static Image ICON_FILES_PYTHON;
static Image ICON_FILES_IMAGE;
static Image ICON_FILES_TEXT;
static std::string ICON_FILES_FOLDER;
static std::string ICON_FILES_PYTHON;
static std::string ICON_FILES_IMAGE;
static std::string ICON_FILES_TEXT;

static Image ICON_BUTTON_PLAY;
static Image ICON_BUTTON_STOP;
static std::string ICON_BUTTON_PLAY;
static std::string ICON_BUTTON_STOP;

private:
uint8_t *_data; ///< The image pixel data.

int _width; ///< The width of the image.
int _height; ///< The height of the image.
int _components; ///< The number of color components.

bool _wasAssignedTextureId; ///< Flag indicating if the texture ID has been assigned.
uint64_t _textureId; ///< The OpenGL texture ID.
};

} // namespace shkyera
41 changes: 40 additions & 1 deletion src/Rendering/Texture.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include <Rendering/Texture.hpp>
#include <iostream>

#include <Rendering/Texture.hpp>
#include <AssetManager/AssetManager.hpp>


namespace shkyera {

Texture::Texture(GLenum minFilter, GLenum magFilter, GLenum wrapS, GLenum wrapT)
Expand All @@ -15,6 +18,12 @@ Texture::Texture(GLenum minFilter, GLenum magFilter, GLenum wrapS, GLenum wrapT)
unbind();
}

Texture::Texture(const std::string& path) : Texture()
{
const auto& image = AssetManager::getInstance().getAsset<Image>(path);
loadImage(image);
}

Texture::~Texture() {
if (_textureID) {
glDeleteTextures(1, &_textureID);
Expand All @@ -30,6 +39,36 @@ void Texture::unbind() const {
glBindTexture(GL_TEXTURE_2D, 0);
}

bool Texture::loadImage(std::shared_ptr<Image> imageAsset)
{
if (auto* data = imageAsset->getData())
{
auto width = imageAsset->getWidth();
auto height = imageAsset->getHeight();
auto channels = imageAsset->getChannels();

bind();
if (channels == 3)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, data);
}
else
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, data);
}
unbind();
}
else
{
std::cerr << "Image was not loaded. Could not load cubemap." << std::endl;
return false;
}

return true;
}

void Texture::setData(GLenum internalFormat, uint32_t width, uint32_t height, GLenum format, GLenum type, const void* data) {
bind();
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, type, data);
Expand Down
12 changes: 10 additions & 2 deletions src/Rendering/Texture.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#pragma once

#include <glad/glad.h>
#include <AssetManager/Image.hpp>

namespace shkyera {

class Texture {
class Texture : public Asset {
public:
// Constructor with configurable filter and wrap modes
Texture(GLenum minFilter = GL_LINEAR, GLenum magFilter = GL_LINEAR,
GLenum wrapS = GL_CLAMP_TO_EDGE, GLenum wrapT = GL_CLAMP_TO_EDGE);

Texture(const std::string& path);

~Texture();

Texture(const Texture& other) = delete;
Expand All @@ -19,17 +22,22 @@ class Texture {
void bind() const;
void unbind() const;

bool loadImage(std::shared_ptr<Image> imageAsset);

// Set texture data
void setData(GLenum internalFormat, uint32_t width, uint32_t height, GLenum format, GLenum type, const void* data = nullptr);

// Activate this texture on a specified texture unit
void activate(GLenum textureUnit) const;

// Get OpenGL texture ID
GLuint getID() const { return _textureID; }

void* getImguiTextureID() const { return reinterpret_cast<void *>(_textureID); }

private:
GLuint _textureID;
};

using TextureAsset = std::shared_ptr<Texture>;

}
11 changes: 11 additions & 0 deletions src/ui/ComponentUI.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
#pragma once

#include <AssetManager/AssetManager.hpp>
#include <AssetManager/Image.hpp>
#include <Rendering/Texture.hpp>

namespace shkyera {

class ComponentUI {
public:
ComponentUI() {
_icon = AssetManager::getInstance().getAsset<Texture>(Image::ICON_COMPONENT_TRANSFORM);
}

virtual ~ComponentUI() = default;

virtual void draw() = 0;

protected:
TextureAsset _icon;
};

}
2 changes: 1 addition & 1 deletion src/ui/Components/DirectionalLightComponentUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ DirectionalLightComponentUI::DirectionalLightComponentUI(DirectionalLightCompone
_directionalLightComponent(directionalLightComponent) {}

void DirectionalLightComponentUI::draw() {
ImGui::Image((ImTextureID)Image::ICON_COMPONENT_TRANSFORM.getTextureId(),
ImGui::Image(_icon->getImguiTextureID(),
ImVec2(16, 16));
ImGui::SameLine();
if (ImGui::TreeNodeEx("Directional Light", ImGuiTreeNodeFlags_DefaultOpen)) {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/Components/ModelComponentUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void ModelComponentUI::setOnMeshUpdate(std::function<void(std::shared_ptr<Mesh>)
}

void ModelComponentUI::draw() {
ImGui::Image((ImTextureID)Image::ICON_FILES_IMAGE.getTextureId(),
ImGui::Image(_icon->getImguiTextureID(),
ImVec2(16, 16));
ImGui::SameLine();
if (ImGui::TreeNodeEx("Model", ImGuiTreeNodeFlags_DefaultOpen)) {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/Components/PointLightComponentUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ PointLightComponentUI::PointLightComponentUI(PointLightComponent* pointLightComp
_pointLightComponent(pointLightComponent) {}

void PointLightComponentUI::draw() {
ImGui::Image((ImTextureID)Image::ICON_COMPONENT_TRANSFORM.getTextureId(),
ImGui::Image(_icon->getImguiTextureID(),
ImVec2(16, 16));
ImGui::SameLine();
if (ImGui::TreeNodeEx("Point Light", ImGuiTreeNodeFlags_DefaultOpen)) {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/Components/SkyboxComponentUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ SkyboxComponentUI::SkyboxComponentUI(SkyboxComponent* skyboxComponent) :
_skyboxComponent(skyboxComponent) {}

void SkyboxComponentUI::draw() {
ImGui::Image((ImTextureID)Image::ICON_COMPONENT_TRANSFORM.getTextureId(),
ImGui::Image(_icon->getImguiTextureID(),
ImVec2(16, 16));
ImGui::SameLine();
if (ImGui::TreeNodeEx("Skybox", ImGuiTreeNodeFlags_DefaultOpen)) {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/Components/TransformComponentUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void TransformComponentUI::setScaleGetter(
}

void TransformComponentUI::draw() {
ImGui::Image((ImTextureID)Image::ICON_COMPONENT_TRANSFORM.getTextureId(),
ImGui::Image(_icon->getImguiTextureID(),
ImVec2(16, 16));
ImGui::SameLine();
if (ImGui::TreeNodeEx("Transform", ImGuiTreeNodeFlags_DefaultOpen)) {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/Components/WireframeComponentUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ WireframeComponentUI::WireframeComponentUI(WireframeComponent* wireframeComponen
}

void WireframeComponentUI::draw() {
ImGui::Image((ImTextureID)Image::ICON_FILES_IMAGE.getTextureId(), ImVec2(16, 16));
ImGui::Image(_icon->getImguiTextureID(), ImVec2(16, 16));
ImGui::SameLine();
if (ImGui::TreeNodeEx("Wireframe", ImGuiTreeNodeFlags_DefaultOpen)) {
_wireframeSelector.draw();
Expand Down
21 changes: 0 additions & 21 deletions src/ui/UI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ void glfw_error_callback(int error, const char* description) {
void UI::initialize() {
initializeImgui();
initializeWidgets();
initializeAssets();
initializeInterpreter();
styleImgui();
}
Expand Down Expand Up @@ -108,26 +107,6 @@ void UI::initializeWidgets() {
_widgets.emplace_back(std::move(sceneWidget));
}

void UI::initializeAssets() {
Image::ICON_CONSOLE_TOTAL.updateTextureId();
Image::ICON_CONSOLE_ERROR.updateTextureId();
Image::ICON_CONSOLE_INFO.updateTextureId();
Image::ICON_CONSOLE_VERBOSE.updateTextureId();
Image::ICON_CONSOLE_SUCCESS.updateTextureId();

Image::ICON_COMPONENT_TRANSFORM.updateTextureId();
Image::ICON_COMPONENT_SCRIPT.updateTextureId();
Image::ICON_COMPONENT_SHAPE.updateTextureId();

Image::ICON_FILES_FOLDER.updateTextureId();
Image::ICON_FILES_IMAGE.updateTextureId();
Image::ICON_FILES_PYTHON.updateTextureId();
Image::ICON_FILES_TEXT.updateTextureId();

Image::ICON_BUTTON_PLAY.updateTextureId();
Image::ICON_BUTTON_STOP.updateTextureId();
}

void UI::initializeInterpreter() {}

void UI::styleImgui() {
Expand Down
1 change: 1 addition & 0 deletions src/ui/Widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cmath>
#include <string>

#include <glad/glad.h>
#include <GLFW/glfw3.h>

namespace shkyera {
Expand Down
Loading

0 comments on commit 23c6ef4

Please sign in to comment.