From bde4a9f40bb4af39270a124d5ef9571a83305d0a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 30 Jan 2021 20:36:42 +0000 Subject: Restructure to allow a resource path and testing --- gfx/image.cpp | 20 ++++++++++++++++++++ gfx/image.h | 21 +++++++++++++++++++++ gfx/models/obj_loader.cpp | 7 ++++--- gfx/models/obj_loader.h | 3 ++- gfx/models/texture.cpp | 15 +++++---------- gfx/models/texture.h | 4 ++-- 6 files changed, 54 insertions(+), 16 deletions(-) create mode 100644 gfx/image.cpp create mode 100644 gfx/image.h (limited to 'gfx') diff --git a/gfx/image.cpp b/gfx/image.cpp new file mode 100644 index 0000000..20c862f --- /dev/null +++ b/gfx/image.cpp @@ -0,0 +1,20 @@ +#include "image.h" +#include +#include +#include + +Image::Image(const char * fileName, int flags) : width {}, height {}, numComponents {} +{ + unsigned char * bytes = stbi_load(fileName, &width, &height, &numComponents, flags); + + if (!bytes) { + throw std::runtime_error {std::string {"Unable to load image: "} + fileName}; + } + + data = {bytes, static_cast(width * height * numComponents)}; +} + +Image::~Image() +{ + stbi_image_free(data.data()); +} diff --git a/gfx/image.h b/gfx/image.h new file mode 100644 index 0000000..d43737f --- /dev/null +++ b/gfx/image.h @@ -0,0 +1,21 @@ +#ifndef IMAGE_H +#define IMAGE_H + +#include // IWYU pragma: export +#include +#include + +class Image { +public: + Image(const char * fileName, int flags); + Image(const std::string & fileName, int flags) : Image(fileName.c_str(), flags) { } + ~Image(); + + NO_COPY(Image); + NO_MOVE(Image); + + int width, height, numComponents; + std::span data; +}; + +#endif diff --git a/gfx/models/obj_loader.cpp b/gfx/models/obj_loader.cpp index 7611a2c..75ab251 100644 --- a/gfx/models/obj_loader.cpp +++ b/gfx/models/obj_loader.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -12,12 +13,12 @@ static inline unsigned int ParseOBJIndexValue(const std::string & token, unsigne static inline float ParseOBJFloatValue(const std::string & token, unsigned int start, unsigned int end); static inline std::vector SplitString(const std::string & s, char delim); -OBJModel::OBJModel(const std::string & fileName) +OBJModel::OBJModel(const std::filesystem::path & fileName) { hasUVs = false; hasNormals = false; std::ifstream file; - file.open(fileName.c_str()); + file.open(Resource::mapPath(fileName).c_str()); std::string line; if (file.is_open()) { @@ -55,7 +56,7 @@ OBJModel::OBJModel(const std::string & fileName) } } else { - throw std::runtime_error {"Unable to load mesh: " + fileName}; + throw std::runtime_error {"Unable to load mesh: " + fileName.string()}; } } diff --git a/gfx/models/obj_loader.h b/gfx/models/obj_loader.h index 0523a95..2fe3d35 100644 --- a/gfx/models/obj_loader.h +++ b/gfx/models/obj_loader.h @@ -1,6 +1,7 @@ #ifndef OBJ_LOADER_H_INCLUDED #define OBJ_LOADER_H_INCLUDED +#include #include #include #include @@ -36,7 +37,7 @@ public: bool hasUVs; bool hasNormals; - explicit OBJModel(const std::string & fileName); + explicit OBJModel(const std::filesystem::path & fileName); IndexedModel ToIndexedModel(); diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp index bc80a1c..342f890 100644 --- a/gfx/models/texture.cpp +++ b/gfx/models/texture.cpp @@ -1,18 +1,14 @@ #include "texture.h" #include "stb_image.h" #include -#include +#include +#include Cache Texture::cachedTexture; -Texture::Texture(const std::string & fileName) : m_texture {} +Texture::Texture(const std::filesystem::path & fileName) : m_texture {} { - int width, height, numComponents; - unsigned char * data = stbi_load((fileName).c_str(), &width, &height, &numComponents, STBI_rgb_alpha); - - if (!data) { - throw std::runtime_error {"Unable to load texture: " + fileName}; - } + const Image tex {Resource::mapPath(fileName).c_str(), STBI_rgb_alpha}; glGenTextures(1, &m_texture); glBindTexture(GL_TEXTURE_2D, m_texture); @@ -22,8 +18,7 @@ Texture::Texture(const std::string & fileName) : m_texture {} glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); - stbi_image_free(data); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width, tex.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.data.data()); } Texture::~Texture() diff --git a/gfx/models/texture.h b/gfx/models/texture.h index 30ba953..8bbba85 100644 --- a/gfx/models/texture.h +++ b/gfx/models/texture.h @@ -2,14 +2,14 @@ #define TEXTURE_H #include +#include #include -#include template class Cache; class Texture { public: - explicit Texture(const std::string & fileName); + explicit Texture(const std::filesystem::path & fileName); virtual ~Texture(); -- cgit v1.2.3