Skip to content

Commit ba8023e

Browse files
committed
Add methods to upload image data in Texture class
1 parent de1f1b6 commit ba8023e

2 files changed

Lines changed: 89 additions & 4 deletions

File tree

src/libprojectM/Renderer/Texture.cpp

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ Texture::Texture(std::string name, const int width, const int height, const bool
1818
CreateNewTexture();
1919
}
2020

21-
Texture::Texture(std::string name, int width, int height,
21+
Texture::Texture(std::string name, GLenum target, int width, int height, int depth,
2222
GLint internalFormat, GLenum format, GLenum type, bool isUserTexture)
23-
: m_target(GL_TEXTURE_2D)
23+
: m_target(target)
2424
, m_name(std::move(name))
2525
, m_width(width)
2626
, m_height(height)
27+
, m_depth(depth)
2728
, m_isUserTexture(isUserTexture)
2829
, m_internalFormat(internalFormat)
2930
, m_format(format)
@@ -43,6 +44,21 @@ Texture::Texture(std::string name, const GLuint texID, const GLenum target,
4344
{
4445
}
4546

47+
Texture::Texture(std::string name, const void* data, GLenum target, int width, int height, int depth, GLint internalFormat, GLenum format, GLenum type, bool isUserTexture)
48+
: m_target(target)
49+
, m_name(std::move(name))
50+
, m_width(width)
51+
, m_height(height)
52+
, m_depth(depth)
53+
, m_isUserTexture(isUserTexture)
54+
, m_internalFormat(internalFormat)
55+
, m_format(format)
56+
, m_type(type)
57+
{
58+
glGenTextures(1, &m_textureId);
59+
Update(data);
60+
}
61+
4662
Texture::~Texture()
4763
{
4864
if (m_textureId > 0)
@@ -94,6 +110,11 @@ auto Texture::Height() const -> int
94110
return m_height;
95111
}
96112

113+
auto Texture::Depth() const -> int
114+
{
115+
return m_depth;
116+
}
117+
97118
auto Texture::IsUserTexture() const -> bool
98119
{
99120
return m_isUserTexture;
@@ -104,11 +125,40 @@ auto Texture::Empty() const -> bool
104125
return m_textureId == 0;
105126
}
106127

128+
void Texture::Update(const void* data) const
129+
{
130+
glBindTexture(m_target, m_textureId);
131+
switch (m_target)
132+
{
133+
case GL_TEXTURE_2D:
134+
glTexImage2D(m_target, 0, m_internalFormat, m_width, m_height, 0, m_format, m_type, data);
135+
break;
136+
case GL_TEXTURE_3D:
137+
glTexImage3D(m_target, 0, m_internalFormat, m_width, m_height, m_depth, 0, m_format, m_type, data);
138+
break;
139+
default:
140+
// Unsupported, do nothing.
141+
break;
142+
}
143+
glBindTexture(m_target, 0);
144+
}
145+
107146
void Texture::CreateNewTexture()
108147
{
109148
glGenTextures(1, &m_textureId);
110149
glBindTexture(m_target, m_textureId);
111-
glTexImage2D(m_target, 0, m_internalFormat, m_width, m_height, 0, m_format, m_type, nullptr);
150+
switch (m_target)
151+
{
152+
case GL_TEXTURE_2D:
153+
glTexImage2D(m_target, 0, m_internalFormat, m_width, m_height, 0, m_format, m_type, nullptr);
154+
break;
155+
case GL_TEXTURE_3D:
156+
glTexImage3D(m_target, 0, m_internalFormat, m_width, m_height, m_depth, 0, m_format, m_type, nullptr);
157+
break;
158+
default:
159+
// Unsupported, do nothing.
160+
break;
161+
}
112162
glBindTexture(m_target, 0);
113163
}
114164

src/libprojectM/Renderer/Texture.hpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,16 @@ class Texture
3939
/**
4040
* @brief Constructor. Allocates a new, empty texture with the given size and format.
4141
* @param name Optional name of the texture for referencing in Milkdrop shaders.
42+
* @param target The texture target, either TEXTURE_2D or TEXTURE_3D.
4243
* @param width Width in pixels.
4344
* @param height Height in pixels.
45+
* @param depth Depth in pixels (for 3D textures).
46+
* @param internalFormat OpenGL internal texture format.
47+
* @param format OpenGL texture format.
48+
* @param type Storage type for each color channel.
4449
* @param isUserTexture true if the texture is an externally-loaded image, false if it's an internal texture.
4550
*/
46-
explicit Texture(std::string name, int width, int height,
51+
explicit Texture(std::string name, GLenum target, int width, int height, int depth,
4752
GLint internalFormat, GLenum format, GLenum type, bool isUserTexture);
4853

4954
/**
@@ -60,6 +65,22 @@ class Texture
6065
int width, int height,
6166
bool isUserTexture);
6267

68+
/**
69+
* @brief Constructor. Creates a new texture from image data with the given size and format.
70+
* @param name Optional name of the texture for referencing in Milkdrop shaders.
71+
* @param data The texture data to upload.
72+
* @param target The texture target, either TEXTURE_2D or TEXTURE_3D.
73+
* @param width Width in pixels.
74+
* @param height Height in pixels.
75+
* @param depth Depth in pixels (for 3D textures).
76+
* @param internalFormat OpenGL internal texture format.
77+
* @param format OpenGL texture format.
78+
* @param type Storage type for each color channel.
79+
* @param isUserTexture true if the texture is an externally-loaded image, false if it's an internal texture.
80+
*/
81+
explicit Texture(std::string name, const void* data, GLenum target, int width, int height, int depth,
82+
GLint internalFormat, GLenum format, GLenum type, bool isUserTexture);
83+
6384
Texture(Texture&& other) = default;
6485
auto operator=(Texture&& other) -> Texture& = default;
6586

@@ -109,6 +130,12 @@ class Texture
109130
*/
110131
auto Height() const -> int;
111132

133+
/**
134+
* @brief Returns the depth of the texture image in pixels.
135+
* @return The depth of the texture image in pixels.
136+
*/
137+
auto Depth() const -> int;
138+
112139
/**
113140
* @brief Returns if the texture is user-defined, e.g. loaded from an image.
114141
* @return true if the texture is a user texture, false if it's an internally generated texture.
@@ -121,6 +148,13 @@ class Texture
121148
*/
122149
auto Empty() const -> bool;
123150

151+
/**
152+
* @brief Uploads new image data for the texture.
153+
* @note Automatically binds and unbinds the texture.
154+
* @param data The new texture image data. Must match the size and format used to create the texture.
155+
*/
156+
void Update(const void* data) const;
157+
124158
private:
125159
/**
126160
* @brief Creates a new, blank texture with the given size.
@@ -133,6 +167,7 @@ class Texture
133167
std::string m_name; //!< The texture name for identifying it in shaders.
134168
int m_width{0}; //!< Texture width in pixels.
135169
int m_height{0}; //!< Texture height in pixels.
170+
int m_depth{0}; //!< Texture depth in pixels. Only used for 3D textures.
136171
bool m_isUserTexture{false}; //!< true if it's a user texture, false if an internal one.
137172

138173
GLint m_internalFormat{}; //!< OpenGL internal format, e.g. GL_RGBA8

0 commit comments

Comments
 (0)