Skip to content

Commit

Permalink
OpenGL generate textures
Browse files Browse the repository at this point in the history
  • Loading branch information
amasson committed Sep 4, 2024
1 parent eeedd96 commit 8ffd0ae
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Engine/Core/include/Core/Image/ImageSource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ImageSource : public Assets::Resource {
[[nodiscard]] Size getSize() const;

void unloadData();
void loadData(bool force);
void loadData(bool force = false);

[[nodiscard]] bool isLoaded() const;
[[nodiscard]] std::shared_ptr<ImageData> getLoadedImage() const;
Expand Down
66 changes: 66 additions & 0 deletions Engine/Render/src/Render/OpenGL/Renderable/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,95 @@

#pragma once

#include "Core/Image/ImageData.hpp"
#include "Core/Image/ImageSource.hpp"
#include "Render/OpenGL/OpenGLRenderer.hpp"
#include "Scene/Renderable/Texture.hpp"

#include <GL/glew.h>

namespace Stone::Render::OpenGL {

GLuint convert(Core::Image::Channel channel) {
switch (channel) {
case Core::Image::Channel::GREY: return GL_RED;
case Core::Image::Channel::DUAL: return GL_RG;
case Core::Image::Channel::RGB: return GL_RGB;
case Core::Image::Channel::RGBA: return GL_RGBA;
default: return GL_RGBA;
}
}

GLuint convert(Scene::TextureFilter filter) {
switch (filter) {
case Scene::TextureFilter::Nearest: return GL_NEAREST;
case Scene::TextureFilter::Linear: return GL_LINEAR;
case Scene::TextureFilter::Cubic: return GL_LINEAR;
default: return GL_LINEAR;
}
}

GLuint convert(Scene::TextureWrap wrap) {
switch (wrap) {
case Scene::TextureWrap::Repeat: return GL_REPEAT;
case Scene::TextureWrap::MirroredRepeat: return GL_MIRRORED_REPEAT;
case Scene::TextureWrap::ClampToEdge: return GL_CLAMP_TO_EDGE;
case Scene::TextureWrap::ClampToBorder: return GL_CLAMP_TO_BORDER;
default: return GL_REPEAT;
}
}


class Texture : public Scene::IRendererObject {
public:
Texture(const std::shared_ptr<Scene::Texture> &texture, const std::shared_ptr<OpenGLRenderer> &renderer)
: _texture(texture), _renderer(renderer) {

std::shared_ptr<Core::Image::ImageSource> imageSource = texture->getImage();
if (imageSource == nullptr) {
return;
}

imageSource->loadData();
std::shared_ptr<Core::Image::ImageData> imageData = imageSource->getLoadedImage();

int bpp = static_cast<int>(imageData->getChannels());
GLuint format = convert(imageData->getChannels());
glGenTextures(1, &_buffer);
if (_buffer == 0) {
std::runtime_error("Failed to create texture buffer.");
}
glBindTexture(GL_TEXTURE_2D, _buffer);
glTexImage2D(GL_TEXTURE_2D, 0, format, imageData->getSize().x, imageData->getSize().y, 0, format,
GL_UNSIGNED_BYTE, imageData->getData());

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, convert(texture->getWrap()));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, convert(texture->getWrap()));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, convert(texture->getMinFilter()));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, convert(texture->getMagFilter()));
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
}

~Texture() override {
if (_buffer != 0) {
glDeleteTextures(1, &_buffer);
}
}

void render(Scene::RenderContext &context) override {
(void)context;
}

GLuint getBuffer() const {
return _buffer;
}

private:
std::weak_ptr<Scene::Texture> _texture;
std::weak_ptr<OpenGLRenderer> _renderer;

GLuint _buffer = 0;
};

} // namespace Stone::Render::OpenGL

0 comments on commit 8ffd0ae

Please sign in to comment.