Untitled Game engine no.5  1.0
Texture.hpp
1 #ifndef TEXTURE_H
2 #define TEXTURE_H
3 #include "pch.hpp"
4 #include "stb_image.h"
5 
6 namespace Engine {
10  struct TextureCoord {
11  TextureCoord(glm::vec2 pos, glm::vec2 size): tex_pos(pos), tex_size(size) {};
13  TextureCoord(const TextureCoord&) = default;
16  TextureCoord() = default;
17  glm::vec2 tex_pos;
18  glm::vec2 tex_size;
19  };
20 
24  struct TextureMap {
25  std::vector<Ref<TextureCoord>> mapping = std::vector<Ref<TextureCoord>>();
26  static Ref<TextureMap> Map2D(float width, float height, float x, float y);
27  static Ref<TextureMap> CubeMap() {
28  auto map = CreateRef<TextureMap>();
29  auto map2d = TextureMap::Map2D(1, 1, 0, 0)->mapping;
30  for (int i = 0; i < 6; i++) {
31  for (auto a : map2d) {
32  map->mapping.emplace_back(a);
33  }
34  }
35  return map;
36  }
37  };
38 
42  struct ColorMap {
43  std::vector<glm::vec4> mapping;
44  };
45 
46 
50  class Texture {
51  friend TextureMap;
52  public:
53  virtual ~Texture() = default;
55  virtual uint32_t getID() const {return 0;};
57  virtual float getWidth() const {return -1;};
59  virtual float getHeight() const {return -1;};
61  virtual void setWidth(float width) {};
63  virtual void setHeight(float height) {};
65  virtual void setSize(float width, float height) {};
67  virtual void setID(uint32_t id) {};
70  virtual void Bind(uint32_t slot = 0) const {};
72  virtual Ref<TextureMap> getDefaultMapping() const {return nullptr;};
73  int renderSlot = -1;
74  };
75 
79  class Texture2D : public Texture {
80  public:
82  static Ref<Texture2D> Create(const std::string& path);
83  static Ref<Texture2D> Create();
85  virtual Ref<Texture> genClip(uint32_t width, uint32_t height, uint32_t x, uint32_t y) const {return nullptr;};
86  virtual void loadTextureData(stbi_uc *data, const int w, const int h, uint type) { };
87  };
88 
92  struct TextureLookup {
99  uint width;
101  uint height;
102  };
103 
104 }
105 #endif
106 
Engine::Texture2D
Representation of a 2D texture.
Definition: Texture.hpp:79
Engine::TextureLookup::map
Ref< TextureMap > map
Mapping reference.
Definition: Texture.hpp:97
Engine::Texture
Container for texture data.
Definition: Texture.hpp:50
Engine::Texture::getWidth
virtual float getWidth() const
visual width of the texture in game dimensions
Definition: Texture.hpp:57
Engine::TextureCoord
Texture Coordinate data.
Definition: Texture.hpp:10
Engine::Texture::setSize
virtual void setSize(float width, float height)
visual size of the texture in game dimensions
Definition: Texture.hpp:65
Engine::Ref
std::shared_ptr< T > Ref
Has stuff for making references a lot more easily shared smart pointer.
Definition: Base.hpp:21
Engine::TextureLookup
Wrapper for a texture reference and a texture mapping.
Definition: Texture.hpp:92
Engine::TextureLookup::tex
Ref< Texture > tex
texture reference
Definition: Texture.hpp:95
Engine::Texture2D::genClip
virtual Ref< Texture > genClip(uint32_t width, uint32_t height, uint32_t x, uint32_t y) const
generate a clipped texture from texture coordinates
Definition: Texture.hpp:85
Engine::Texture::setHeight
virtual void setHeight(float height)
visual height of the texture in game dimensions
Definition: Texture.hpp:63
Engine::TextureLookup::width
uint width
texture width
Definition: Texture.hpp:99
Engine::TextureCoord::TextureCoord
TextureCoord()=default
Engine::Texture::getID
virtual uint32_t getID() const
Texture id - an id link to the texture.
Definition: Texture.hpp:55
Engine::Texture::getHeight
virtual float getHeight() const
visual height of the texture in game dimensions
Definition: Texture.hpp:59
Engine::Texture::setWidth
virtual void setWidth(float width)
visual width of the texture in game dimensions
Definition: Texture.hpp:61
Engine::Texture::setID
virtual void setID(uint32_t id)
set texture id
Definition: Texture.hpp:67
Engine::Texture::getDefaultMapping
virtual Ref< TextureMap > getDefaultMapping() const
Get the texture mapping.
Definition: Texture.hpp:72
Engine
Definition: Animation.hpp:14
Engine::Texture::Bind
virtual void Bind(uint32_t slot=0) const
Definition: Texture.hpp:70
Engine::TextureMap
List of texture coordinates for indexing vector data.
Definition: Texture.hpp:24
Engine::TextureLookup::height
uint height
texture height
Definition: Texture.hpp:101
Engine::ColorMap
Vertex map for colors.
Definition: Texture.hpp:42
Engine::Texture2D::Create
static Ref< Texture2D > Create(const std::string &path)
Generate a texture from a string path.
Definition: Texture.cpp:6