1 | #include "GLTexture.hpp" |
2 | #include "ZydecoCommon.hpp" |
3 |
|
4 |
|
5 | static Logger LOGGER("GLTexture"); |
6 |
|
7 |
|
8 | // static initialize |
9 | std::map<std::string, GLTexture*> GLTexture::s_textures = {}; |
10 |
|
11 |
|
12 | GLTexture& GLTexture::GetGLTexture(std::string name) |
13 | { |
14 | if (s_textures.contains(name)) { return *s_textures.at(name); } |
15 | |
16 | ZydecoFault("GetGLTexture({}): Texture not found", name); |
17 | } |
18 |
|
19 | GLTexture::GLTexture(std::string texture_name, void *data_source, uint32_t width, uint32_t height): |
20 | m_pDataSource(data_source), |
21 | m_name(texture_name), |
22 | m_width(width), |
23 | m_height(height) |
24 | { |
25 | s_textures.insert({m_name, this}); |
26 |
|
27 | glGenTextures(1, &m_glTextureID); |
28 | Regenerate(width, height); |
29 | } |
30 |
|
31 | GLTexture::~GLTexture() |
32 | { |
33 | s_textures.erase(m_name); |
34 | glDeleteTextures(1, &m_glTextureID); |
35 | } |
36 |
|
37 | void GLTexture::Regenerate(int width, int height) |
38 | { |
39 | glBindTexture(GL_TEXTURE_2D, m_glTextureID); |
40 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); |
41 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); |
42 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
43 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
44 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, m_pDataSource); |
45 | glFlush(); |
46 | } |
47 |
|
48 | void GLTexture::Bind(uint64_t texture_unit) |
49 | { |
50 | glActiveTexture(GL_TEXTURE0 + texture_unit); |
51 | //glBindTextureUnit(texture_unit, m_glTextureID); |
52 | glBindTexture(GL_TEXTURE_2D, m_glTextureID); |
53 | } |
54 |
|