Index

zydeco / 1467546

Experiment in graphics programming, C++, OpenGL, simulation techniques.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
1008 Dec 2023 15:215a717feFractal renderer update for articleJosh Stockin13423G

Blob @ zydeco / src / render / gl / GLTexture.cpp

text/plain1872 bytesdownload raw
1#include "ZydecoCommon.hpp"
2#include "GLTexture.hpp"
3
4
5static Logger LOGGER("GLTexture");
6
7
8std::map<uint32_t, std::array<uint32_t, 3>> TEXTURE_FORMAT_LOOKUP = {
9 {GL_RGB32F, {GL_RGB, GL_FLOAT}},
10 {GL_RGBA32F, {GL_RGBA, GL_FLOAT}},
11};
12
13
14GLTexture::GLTexture(uint32_t sized_format, void *data_source, uint32_t width, uint32_t height):
15 m_sizedFormat(sized_format),
16 m_baseFormat(TEXTURE_FORMAT_LOOKUP[sized_format][0]),
17 m_dataType(TEXTURE_FORMAT_LOOKUP[sized_format][1])
18{
19 glGenTextures(1, &m_glTextureID);
20 SetDataSourceAndReload(data_source, width, height);
21 assert(glIsTexture(m_glTextureID) == GL_TRUE);
22 assert(m_baseFormat == GL_RGBA);
23 assert(m_sizedFormat == GL_RGBA32F);
24}
25
26GLTexture::~GLTexture()
27{
28 glDeleteTextures(1, &m_glTextureID);
29}
30
31uint32_t GLTexture::GetID()
32{
33 return m_glTextureID;
34}
35
36void GLTexture::SetDataSourceAndReload(void *data_source, int width, int height)
37{
38 m_pDataSource = data_source;
39 m_width = width;
40 m_height = height;
41 ReloadFromDataSource();
42}
43
44void GLTexture::ReloadFromDataSource()
45{
46 glBindTexture(GL_TEXTURE_2D, m_glTextureID);
47 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
48 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
49 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
50 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
51 glTexImage2D(GL_TEXTURE_2D, 0, m_sizedFormat, m_width, m_height, 0, m_baseFormat, m_dataType, m_pDataSource);
52}
53
54void GLTexture::BindAsTexture(uint64_t texture_unit)
55{
56 glActiveTexture(GL_TEXTURE0 + texture_unit);
57 glBindTexture(GL_TEXTURE_2D, m_glTextureID);
58}
59
60void GLTexture::BindAsImage(uint64_t texture_unit)
61{
62 glActiveTexture(GL_TEXTURE0);
63 glBindImageTexture(texture_unit, m_glTextureID, 0, false, 0, GL_READ_WRITE, m_sizedFormat);
64}
65