Skip to content

Commit

Permalink
optional format for Texture constructor with path
Browse files Browse the repository at this point in the history
  • Loading branch information
Byte-White committed Jan 9, 2024
1 parent 4e1dfa2 commit bee608d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion include/Calamity/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace clm
std::string m_FilePath;
int m_Width, m_Height, m_BPP;
public:
Texture(const std::string& path);
Texture(const std::string& path,TextureFormat format = TextureFormat::None);
Texture(int width, int height,TextureFormat format, const void* data = nullptr);
void SetData(void* data);
void Resize(int width,int height);
Expand Down
54 changes: 38 additions & 16 deletions src/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace clm
{
Texture::Texture(const std::string& path)
Texture::Texture(const std::string& path,TextureFormat format)
: m_RendererID(0), m_FilePath(path), m_Width(0), m_Height(0), m_BPP(0)
{
unsigned char* m_LocalBuffer = nullptr;
Expand All @@ -31,23 +31,45 @@ namespace clm
{
GLenum internalFormat, dataFormat;
// Check what type of color channels the texture has and load it accordingly
switch (m_BPP)
if(format == TextureFormat::None)
{
case 4:
internalFormat = GL_RGBA;
dataFormat = GL_RGBA;
break;
case 3:
internalFormat = GL_RGB;
dataFormat = GL_RGB;
break;
case 1:
internalFormat = GL_RED;
dataFormat = GL_RED;
break;
default:
throw std::invalid_argument("Automatic Texture type recognition failed");
switch (m_BPP)
{
case 4:
internalFormat = GL_RGBA;
dataFormat = GL_RGBA;
break;
case 3:
internalFormat = GL_RGB;
dataFormat = GL_RGB;
break;
case 1:
internalFormat = GL_RED;
dataFormat = GL_RED;
break;
default:
throw std::invalid_argument("Automatic Texture type recognition failed");
}
}
else
{
switch (format)
{
case TextureFormat::RGBA:
internalFormat = GL_RGBA;
dataFormat = GL_RGBA;
break;
case TextureFormat::RGB:
internalFormat = GL_RGB;
dataFormat = GL_RGB;
break;
case TextureFormat::Red:
internalFormat = GL_RED;
dataFormat = GL_RED;
break;
}
}


glTexImage2D(
GL_TEXTURE_2D,
Expand Down

0 comments on commit bee608d

Please sign in to comment.