# HG changeset patch # User František Kučera # Date 1701720233 -3600 # Node ID 262828ae96825998154b7a355aa20b97e9ad36ed # Parent c4384f43c7398a62dfff5918b818e33f011a8716 OOP for Texture diff -r c4384f43c739 -r 262828ae9682 Makefile --- a/Makefile Sun Dec 03 19:22:42 2023 +0100 +++ b/Makefile Mon Dec 04 21:03:53 2023 +0100 @@ -30,6 +30,7 @@ Shark.cpp \ shader-shark.cpp \ ImageLoader.cpp \ + Texture.cpp \ Shader.cpp \ Program.cpp \ FileMonitor.cpp diff -r c4384f43c739 -r 262828ae9682 Shark.cpp --- a/Shark.cpp Sun Dec 03 19:22:42 2023 +0100 +++ b/Shark.cpp Mon Dec 04 21:03:53 2023 +0100 @@ -29,6 +29,7 @@ #include "Logger.h" #include "MappedFile.h" #include "ImageLoader.h" +#include "Texture.h" #include "Shader.h" #include "Program.h" #include "FileMonitor.h" @@ -155,18 +156,6 @@ } initialCtx, ctx; - class Texture { - public: - GLuint id; - std::string fileName; - int width; - int height; - - GLfloat getRatio() const { - return (GLfloat) width / (GLfloat) height; - } - }; - Display* dpy; Window win; XVisualInfo* vi; @@ -177,7 +166,7 @@ ImageLoader imageLoader; std::vector> shaders; std::shared_ptr shaderProgram; - std::vector textures; + std::vector> textures; Configuration cfg; std::ostream& logOutput = std::cerr; @@ -192,8 +181,6 @@ void log(LogLevel level, std::string message); int setNonBlocking(int fd); void loadVertices(); - Texture loadTexture(const std::string& fileName); - void updateTexture(Texture& tex, const Buffer& file); bool reloadTexture(const std::string& fileName); void loadTextures(); std::shared_ptr loadShaders(); @@ -208,11 +195,10 @@ } Shark::~Shark() { - // TODO: more SBRM + impl->textures.clear(); impl->shaders.clear(); impl->shaderProgram = nullptr; XFree(impl->vi); - // for (auto page : pdfTextures) glDeleteTextures(1, &page.texture); glXMakeCurrent(impl->dpy, None, NULL); glXDestroyContext(impl->dpy, impl->glc); XDestroyWindow(impl->dpy, impl->win); @@ -498,8 +484,8 @@ void Shark::Impl::loadVertices() { for (int i = 0; i < textures.size(); i++) { - const Texture& tex = textures[i]; - GLfloat ratio = tex.getRatio(); + std::shared_ptr tex = textures[i]; + GLfloat ratio = tex->getRatio(); const std::vector vertices = { // Vertex XYZ Texture XY -0.80f * ratio, +0.80f, +0.0, /**/ 0.0, 0.0, @@ -543,7 +529,7 @@ } const std::string -Shark::Shark::Impl::getDefaultFile(const std::string& relativePath) { +Shark::Impl::getDefaultFile(const std::string& relativePath) { const char* envName = "SHADER_SHARK_DATA_DIR"; const char* envValue = ::getenv(envName); if (envValue) { @@ -555,44 +541,16 @@ } } -Shark::Impl::Texture Shark::Impl::loadTexture(const std::string& fileName) { - Texture tex; - tex.fileName = fileName; - MappedFile file(tex.fileName); - updateTexture(tex, file); - return tex; -} - -void Shark::Impl::updateTexture(Texture& tex, const Buffer& file) { - std::shared_ptr img(imageLoader.loadImage(file)); - - tex.width = img->width; - tex.height = img->height; - - glGenTextures(1, &tex.id); - glBindTexture(GL_TEXTURE_2D, tex.id); - auto GLT2D = GL_TEXTURE_2D; - glTexImage2D(GLT2D, 0, GL_RGBA, - tex.width, tex.height, - 0, GL_RGBA, GL_UNSIGNED_BYTE, - img->getData()); - glTexParameteri(GLT2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GLT2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameteri(GLT2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GLT2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glGenerateMipmap(GLT2D); - std::cerr << "loadTexture(\"" << tex.fileName.c_str() << "\", " - << tex.width << ", " << tex.height << ") = " << tex.id << std::endl; - checkError(&std::cerr); -} - void Shark::Impl::loadTextures() { // Load default texture if there is no configured: if (cfg.textures.empty()) cfg.textures.push_back({getDefaultFile("textures/default.png")}); for (const Configuration::Texture& tex : cfg.textures) { - textures.push_back(loadTexture(tex.fileName)); + std::shared_ptr + img(imageLoader.loadImage(MappedFile(tex.fileName))); + textures.push_back(std::make_shared( + img->width, img->height, *img, tex.fileName)); watchedFiles.push_back(fileMonitor.watch(tex.fileName)); // TODO: review texture loading and binding // works even without this - default texture @@ -602,9 +560,11 @@ } bool Shark::Impl::reloadTexture(const std::string& fileName) { - for (Texture& tex : textures) { - if (tex.fileName == fileName) { - updateTexture(tex, MappedFile(fileName)); + for (std::shared_ptr tex : textures) { + if (tex->getFileName() == fileName) { + std::shared_ptr + img(imageLoader.loadImage(MappedFile(fileName))); + tex->update(img->width, img->height, *img); loadVertices(); return true; } diff -r c4384f43c739 -r 262828ae9682 Texture.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Texture.cpp Mon Dec 04 21:03:53 2023 +0100 @@ -0,0 +1,86 @@ +/** + * ShaderShark + * Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include "opengl.h" +#include "Texture.h" + +class Texture::Impl { +public: + GLuint id; + std::string fileName; + int width; + int height; +}; + +Texture::Texture( + int width, + int height, + const Buffer& img, + const std::string& fileName) : impl(new Impl()) { + impl->fileName = fileName; + glGenTextures(1, &impl->id); + update(width, height, img); +} + +Texture::~Texture() { + glDeleteTextures(1, &impl->id); + delete impl; +} + +GLuint Texture::getId() const { + return impl->id; +} + +const std::string Texture::getFileName() const { + return impl->fileName; +} + +int Texture::getWidth() const { + return impl->width; +} + +int Texture::getHeight() const { + return impl->height; +} + +GLfloat Texture::getRatio() const { + return (GLfloat) impl->width / (GLfloat) impl->height; +} + +void Texture::update(int width, int height, const Buffer& img) { + impl->width = width; + impl->height = height; + + if (img.getSize() != impl->width * impl->height * 4) + throw std::invalid_argument("invalid image size"); + + glBindTexture(GL_TEXTURE_2D, impl->id); + auto GLT2D = GL_TEXTURE_2D; + glTexImage2D(GLT2D, 0, GL_RGBA, + impl->width, impl->height, + 0, GL_RGBA, GL_UNSIGNED_BYTE, + img.getData()); + glTexParameteri(GLT2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GLT2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GLT2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GLT2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glGenerateMipmap(GLT2D); + checkError(&std::cerr); +} diff -r c4384f43c739 -r 262828ae9682 Texture.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Texture.h Mon Dec 04 21:03:53 2023 +0100 @@ -0,0 +1,47 @@ +/** + * ShaderShark + * Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include + +#include "Buffer.h" + +class Texture { +public: + + /** + * @param width horizontal pixel count + * @param height vertical pixel count + * @param img RGBA pixels, 8 bits per channel, size == width * height * 4 + * @param fileName not used by this class, just stored for others + */ + Texture(int width, int height, const Buffer& img, + const std::string& fileName); + virtual ~Texture(); + GLuint getId() const; + const std::string getFileName() const; + int getWidth() const; + int getHeight() const; + GLfloat getRatio() const; + void update(int width, int height, const Buffer& img); +private: + class Impl; + Impl* impl; + Texture(const Texture&) = delete; + Texture& operator=(const Texture&) = delete; +}; diff -r c4384f43c739 -r 262828ae9682 nbproject/configurations.xml --- a/nbproject/configurations.xml Sun Dec 03 19:22:42 2023 +0100 +++ b/nbproject/configurations.xml Mon Dec 04 21:03:53 2023 +0100 @@ -7,6 +7,7 @@ Program.cpp Shader.cpp Shark.cpp + Texture.cpp shader-shark.cpp + + + +