YourGameLib
Loading...
Searching...
No Matches
texture.h
Go to the documentation of this file.
1/*
2Copyright (c) 2019-2024 Alexander Scholz
3
4This software is provided 'as-is', without any express or implied
5warranty. In no event will the authors be held liable for any damages
6arising from the use of this software.
7
8Permission is granted to anyone to use this software for any purpose,
9including commercial applications, and to alter it and redistribute it
10freely, subject to the following restrictions:
11
121. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
162. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
183. This notice may not be removed or altered from any source distribution.
19*/
20#ifndef YOURGAME_GLTEXTURE_H
21#define YOURGAME_GLTEXTURE_H
22
23#include <map>
24#include <string>
25#include <vector>
26#include "yourgame/gl_include.h"
27
28namespace yourgame
29{
30 namespace gl
31 {
33 {
34 public:
36
37 TextureCoords(int x, int y, int width, int height, int texWidth, int texHeight, bool cwRot)
38 {
39 if (width != 0 && height != 0 && texWidth != 0 && texHeight != 0)
40 {
41 uMin = static_cast<float>(x) / static_cast<float>(texWidth);
42 uMax = static_cast<float>(x + width) / static_cast<float>(texWidth);
43 vMin = 1.0f - (static_cast<float>(y + height) / static_cast<float>(texHeight));
44 vMax = 1.0f - (static_cast<float>(y) / static_cast<float>(texHeight));
45 xMinPixel = x;
46 xMaxPixel = x + width;
47 yMinPixel = y;
48 yMaxPixel = y + height;
49 aspectRatioPixel = static_cast<float>(width) / static_cast<float>(height);
50 aspectRatioPixelInverse = static_cast<float>(height) / static_cast<float>(width);
51 cwRotated = cwRot;
52 }
53 }
54
55 float uMin = 0.0f;
56 float uMax = 1.0f;
57 float vMin = 0.0f;
58 float vMax = 1.0f;
59 int xMinPixel = 0;
60 int xMaxPixel = 1;
61 int yMinPixel = 0;
62 int yMaxPixel = 1;
63 float aspectRatioPixel = 1.0f;
65 bool cwRotated = false;
66 };
67
68 class Texture
69 {
70 public:
71 static Texture *make(GLenum target,
72 GLenum unit,
73 const std::vector<std::pair<GLenum, GLint>> &parameteri);
74
75 /* deleting the copy constructor and the copy assignment operator
76 prevents copying (and moving) of the object. */
77 Texture(Texture const &) = delete;
78 Texture &operator=(Texture const &) = delete;
79
80 ~Texture();
81 void bind() const;
82 void unbindTarget() const;
83
84 void updateData(GLenum target,
85 GLint level,
86 GLint internalformat,
87 GLsizei width,
88 GLsizei height,
89 GLint border,
90 GLenum format,
91 GLenum type,
92 const void *data,
93 bool generateMipmap);
94
95 GLuint handle() const { return m_handle; }
96 GLenum getTarget() const { return m_target; }
97 GLsizei getWidth() const { return m_width; }
98 GLsizei getHeight() const { return m_height; }
99
100 void insertCoords(std::string name, int x, int y, int width, int height, bool cwRot);
101
107 TextureCoords getCoords(std::string name) const;
108
115 TextureCoords getFrameCoords(std::string sequenceName, int frame);
116
122 int getNumFrames(std::string sequenceName) const;
123
127 std::vector<std::string> getSequenceNames() const;
128
136 TextureCoords getGridCoords(int gridWidth, int gridHeight, int index) const;
137
138 private:
139 Texture() {}
140 GLuint m_handle;
141 GLenum m_target;
142 GLenum m_unit;
143 GLsizei m_width;
144 GLsizei m_height;
145
146 struct Sequence
147 {
148 std::map<int, TextureCoords> frames;
149 bool framesInvalidated;
150 std::vector<TextureCoords> framesConsec;
151 };
152
153 std::map<std::string, TextureCoords> m_coords;
154 std::map<std::string, Sequence> m_sequences;
155 };
156 } // namespace gl
157} // namespace yourgame
158
159#endif
Definition texture.h:69
static Texture * make(GLenum target, GLenum unit, const std::vector< std::pair< GLenum, GLint > > &parameteri)
Definition texture.cpp:28
GLuint handle() const
Definition texture.h:95
GLenum getTarget() const
Definition texture.h:96
void bind() const
Definition texture.cpp:55
TextureCoords getFrameCoords(std::string sequenceName, int frame)
returns texture coordinates of subtexture which is part of a sequence
Definition texture.cpp:138
std::vector< std::string > getSequenceNames() const
returns a list of the names of all sequences
Definition texture.cpp:176
void updateData(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *data, bool generateMipmap)
Definition texture.cpp:67
TextureCoords getCoords(std::string name) const
returns texture coordinates of subtexture with name name
Definition texture.cpp:127
void unbindTarget() const
Definition texture.cpp:61
Texture & operator=(Texture const &)=delete
void insertCoords(std::string name, int x, int y, int width, int height, bool cwRot)
Definition texture.cpp:103
~Texture()
Definition texture.cpp:50
GLsizei getWidth() const
Definition texture.h:97
GLsizei getHeight() const
Definition texture.h:98
Texture(Texture const &)=delete
TextureCoords getGridCoords(int gridWidth, int gridHeight, int index) const
returns texture coordinates of grid subtexture
Definition texture.cpp:186
int getNumFrames(std::string sequenceName) const
returns the number of frames of a sequence
Definition texture.cpp:170
Definition audio.h:27
Definition texture.h:33
float uMax
Definition texture.h:56
bool cwRotated
Definition texture.h:65
int xMinPixel
Definition texture.h:59
TextureCoords(int x, int y, int width, int height, int texWidth, int texHeight, bool cwRot)
Definition texture.h:37
float aspectRatioPixelInverse
Definition texture.h:64
float vMin
Definition texture.h:57
int yMinPixel
Definition texture.h:61
float aspectRatioPixel
Definition texture.h:63
int xMaxPixel
Definition texture.h:60
float vMax
Definition texture.h:58
int yMaxPixel
Definition texture.h:62
float uMin
Definition texture.h:55
TextureCoords()
Definition texture.h:35