1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
#include "color.hpp"
#include <algorithm>
#include <array>
namespace SceneUtil
{
bool isColorFormat(GLenum format)
{
static constexpr std::array<GLenum, 42> formats = {
GL_RGB,
GL_RGB4,
GL_RGB5,
GL_RGB8,
GL_RGB8_SNORM,
GL_RGB10,
GL_RGB12,
GL_RGB16,
GL_RGB16_SNORM,
GL_SRGB,
GL_SRGB8,
GL_RGB16F,
GL_RGB32F,
GL_R11F_G11F_B10F,
GL_RGB9_E5,
GL_RGB8I,
GL_RGB8UI,
GL_RGB16I,
GL_RGB16UI,
GL_RGB32I,
GL_RGB32UI,
GL_RGBA,
GL_RGBA2,
GL_RGBA4,
GL_RGB5_A1,
GL_RGBA8,
GL_RGBA8_SNORM,
GL_RGB10_A2,
GL_RGB10_A2UI,
GL_RGBA12,
GL_RGBA16,
GL_RGBA16_SNORM,
GL_SRGB_ALPHA8,
GL_SRGB8_ALPHA8,
GL_RGBA16F,
GL_RGBA32F,
GL_RGBA8I,
GL_RGBA8UI,
GL_RGBA16I,
GL_RGBA16UI,
GL_RGBA32I,
GL_RGBA32UI,
};
return std::find(formats.cbegin(), formats.cend(), format) != formats.cend();
}
bool isFloatingPointColorFormat(GLenum format)
{
static constexpr std::array<GLenum, 5> formats = {
GL_RGB16F,
GL_RGB32F,
GL_R11F_G11F_B10F,
GL_RGBA16F,
GL_RGBA32F,
};
return std::find(formats.cbegin(), formats.cend(), format) != formats.cend();
}
int getColorFormatChannelCount(GLenum format)
{
static constexpr std::array<GLenum, 21> formats = {
GL_RGBA,
GL_RGBA2,
GL_RGBA4,
GL_RGB5_A1,
GL_RGBA8,
GL_RGBA8_SNORM,
GL_RGB10_A2,
GL_RGB10_A2UI,
GL_RGBA12,
GL_RGBA16,
GL_RGBA16_SNORM,
GL_SRGB_ALPHA8,
GL_SRGB8_ALPHA8,
GL_RGBA16F,
GL_RGBA32F,
GL_RGBA8I,
GL_RGBA8UI,
GL_RGBA16I,
GL_RGBA16UI,
GL_RGBA32I,
GL_RGBA32UI,
};
if (std::find(formats.cbegin(), formats.cend(), format) != formats.cend())
return 4;
return 3;
}
void getColorFormatSourceFormatAndType(GLenum internalFormat, GLenum& sourceFormat, GLenum& sourceType)
{
if (getColorFormatChannelCount(internalFormat == 4))
sourceFormat = GL_RGBA;
else
sourceFormat = GL_RGB;
if (isFloatingPointColorFormat(internalFormat))
sourceType = GL_FLOAT;
else
sourceType = GL_UNSIGNED_BYTE;
}
namespace Color
{
GLenum sColorInternalFormat;
GLenum sColorSourceFormat;
GLenum sColorSourceType;
GLenum colorInternalFormat()
{
return sColorInternalFormat;
}
GLenum colorSourceFormat()
{
return sColorSourceFormat;
}
GLenum colorSourceType()
{
return sColorSourceType;
}
void SelectColorFormatOperation::operator()([[maybe_unused]] osg::GraphicsContext* graphicsContext)
{
sColorInternalFormat = GL_RGB;
for (auto supportedFormat : mSupportedFormats)
{
if (isColorFormat(supportedFormat))
{
sColorInternalFormat = supportedFormat;
break;
}
}
getColorFormatSourceFormatAndType(sColorInternalFormat, sColorSourceFormat, sColorSourceType);
}
}
}
|