3#include "OpenGLCommon.Texture.hpp"
5#ifndef GL_DEPTH_COMPONENT
6# define GL_DEPTH_COMPONENT 0
18OpenGLCommon::Texture::Texture(
const Component::Texture & _Data) :
19 m_Destination(GetDestination(_Data)),
20 m_Format(GetFormat(_Data.Destination)),
21 m_TextureId(BuildTexture()),
22 m_IsMapping(_Data.IsMapping),
23 m_Capacity(_Data.Capacity)
25 MakeContent(_Data.Width, _Data.Height, _Data.pTextureData);
28 OpenGLCommon::Texture::~Texture(
void)
noexcept
30 glDeleteTextures(1, &m_TextureId);
34void OpenGLCommon::Texture::Bind(
const bool _IsActivate)
noexcept
36 glBindTexture(GL_TEXTURE_2D, _IsActivate ? m_TextureId : 0);
39void OpenGLCommon::Texture::MakeContent(
41 const GLsizei _Height,
42 const GLvoid * _pData)
46# if !defined(GL_RGBA32F)
47# define GL_RGBA32F GL_RGBA
50# if !defined(GL_RGBA16F)
51# define GL_RGBA16F GL_RGBA
54# if !defined(GL_HALF_FLOAT)
55# define GL_HALF_FLOAT GL_FLOAT
58 const GLint InternalFormat =
59 (m_Format == GL_DEPTH_COMPONENT) ? GL_DEPTH_COMPONENT :
60 (m_Capacity == 32) ? GL_RGBA32F :
61 (m_Capacity == 16) ? GL_RGBA16F :
65 (InternalFormat == GL_RGBA) ? GL_UNSIGNED_BYTE :
66 (m_Capacity == 32) ? GL_FLOAT :
67 (m_Capacity == 16) ? GL_HALF_FLOAT :
72 glTexImage2D(GL_TEXTURE_2D, 0,
76 m_Format == GL_DEPTH_COMPONENT ? GL_UNSIGNED_INT : Format,
96 const auto Error = glGetError();
97 if (Error != GL_NO_ERROR)
99 throw STD_EXCEPTION <<
"Create texture error: " << Error;
104 m_ReadCopyData.resize(
static_cast<size_t>(_Width) * _Height * 4, 0x00);
108 auto OpenGLCommon::Texture::GetDestination(
109 const Component::Texture & _TextureData) -> Destination_t
111 if (_TextureData.Index >= 0)
113 using namespace ::alicorn::extension::std;
116 string_cast<::std::string, Encoding::Ascii128>(_TextureData.Name);
117 return { _TextureData.Index, Name };
120 if (_TextureData.Destination == uT(
"diffuse"))
return { 0,
"TexDiffuse" };
122 static const ::std::vector<::std::pair<String_t, const char *>> Destinations =
124 { uT(
"albedo"),
"TexAlbedo" },
125 { uT(
"metalness"),
"TexMetalness" },
126 { uT(
"roughness"),
"TexRoughness" },
127 { uT(
"normal"),
"TexNormal" },
128 { uT(
"occlusion"),
"TexOcclusion" },
129 { uT(
"depth"),
"TexDepth" },
132 const auto itValue = ::std::find_if(Destinations.cbegin(),
133 Destinations.cend(), [&](const ::std::pair<String_t, const char *> & _Dest)
135 return (_TextureData.Destination == _Dest.first);
137 if (itValue == Destinations.cend())
139 throw STD_EXCEPTION <<
"Unexpected destination texture: " <<
140 _TextureData.Destination << uT(
" [id=???") << uT(
"].");
143 const auto IndexDestination =
144 static_cast<GLint
>(::std::distance(Destinations.cbegin(), itValue));
145 return { IndexDestination, Destinations[IndexDestination].second };
148 GLint OpenGLCommon::Texture::GetFormat(
const String_t & _Destination)
150 return (_Destination == uT(
"depth")) ? GL_DEPTH_COMPONENT : GL_RGBA;
153 GLuint OpenGLCommon::Texture::BuildTexture(
void)
noexcept
155 GLuint TextureId = 0;
156 glGenTextures(1, &TextureId);