Developer Documentation

GLFormatInfo.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41
42 #include <ACG/GL/GLFormatInfo.hh>
43 #include <iostream>
44 #include <QString>
45
46
47 namespace ACG {
48
49
50 // MACOS fix
51
52 std::map<GLenum, GLFormatInfo> GLFormatInfo::formatMap_;
53
54 GLFormatInfo::GLFormatInfo( GLenum _internalFormat )
55 : internalFormat_(_internalFormat), format_(GL_NONE), type_(GL_NONE),
56 channelCount_(0), bpp_(0), baseType_(UnsignedInt), normalized_(true)
57 {
58 channelBits_[0] =
59 channelBits_[1] =
60 channelBits_[2] =
61 channelBits_[3] = 0;
62 sizedName_[0] = 0;
63
64 if (formatMap_.empty())
65 {
66 registerFmt(GL_RED, GL_RED, GL_UNSIGNED_BYTE, 8, 0, 0, 0, UnsignedInt, true);
67 registerFmt(GL_RG, GL_RG, GL_UNSIGNED_BYTE, 8, 8, 0, 0, UnsignedInt, true);
68 registerFmt(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, 8, 8, 8, 0, UnsignedInt, true);
69 registerFmt(GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 8, 8, 8, 8, UnsignedInt, true);
70
71 registerFmt(GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 32, 0, 0, 0, UnsignedInt, false);
72 registerFmt(GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 32, 0, 0, 0, UnsignedInt, false);
73 registerFmt(GL_DEPTH_STENCIL, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 24, 8, 0, 0, UnsignedInt, false);
74 registerFmt(GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 24, 8, 0, 0, UnsignedInt, false);
75
76 #ifdef GL_VERSION_3_0
77 // single channel formats
78 registerFmt(GL_R8, GL_RED, GL_UNSIGNED_BYTE, 8, 0, 0, 0, UnsignedInt, true);
79 registerFmt(GL_R8_SNORM, GL_RED, GL_BYTE, 8, 0, 0, 0, SignedInt, true);
80 registerFmt(GL_R8I, GL_RED_INTEGER, GL_BYTE, 8, 0, 0, 0, SignedInt, false);
81 registerFmt(GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE, 8, 0, 0, 0, UnsignedInt, false);
82
83 registerFmt(GL_R16, GL_RED, GL_UNSIGNED_SHORT, 16, 0, 0, 0, UnsignedInt, true);
84 registerFmt(GL_R16I, GL_RED_INTEGER, GL_SHORT, 16, 0, 0, 0, SignedInt, false);
85 registerFmt(GL_R16UI, GL_RED_INTEGER, GL_UNSIGNED_SHORT, 16, 0, 0, 0, UnsignedInt, false);
86 registerFmt(GL_R16F, GL_RED, GL_HALF_FLOAT, 16, 0, 0, 0, FloatingPt, false);
87
88 registerFmt(GL_R32I, GL_RED_INTEGER, GL_INT, 32, 0, 0, 0, SignedInt, false);
89 registerFmt(GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT, 32, 0, 0, 0, UnsignedInt, false);
90 registerFmt(GL_R32F, GL_RED, GL_FLOAT, 32, 0, 0, 0, FloatingPt, false);
91
92 // dual channel formats
93 registerFmt(GL_RG8, GL_RG, GL_UNSIGNED_BYTE, 8, 8, 0, 0, UnsignedInt, true);
94 registerFmt(GL_RG8_SNORM, GL_RG, GL_BYTE, 8, 8, 0, 0, SignedInt, true);
95 registerFmt(GL_RG8I, GL_RG_INTEGER, GL_BYTE, 8, 8, 0, 0, SignedInt, false);
96 registerFmt(GL_RG8UI, GL_RG_INTEGER, GL_UNSIGNED_BYTE, 8, 8, 0, 0, UnsignedInt, false);
97
98 registerFmt(GL_RG16, GL_RG, GL_UNSIGNED_SHORT, 16, 16, 0, 0, UnsignedInt, true);
99 registerFmt(GL_RG16I, GL_RG_INTEGER, GL_SHORT, 16, 16, 0, 0, SignedInt, false);
100 registerFmt(GL_RG16UI, GL_RG_INTEGER, GL_UNSIGNED_SHORT, 16, 16, 0, 0, UnsignedInt, false);
101 registerFmt(GL_RG16F, GL_RG, GL_HALF_FLOAT, 16, 16, 0, 0, FloatingPt, false);
102
103 registerFmt(GL_RG32I, GL_RG_INTEGER, GL_INT, 32, 32, 0, 0, SignedInt, false);
104 registerFmt(GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT, 32, 32, 0, 0, UnsignedInt, false);
105 registerFmt(GL_RG32F, GL_RG, GL_FLOAT, 32, 32, 0, 0, FloatingPt, false);
106
107
108 // triple channel
109 registerFmt(GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, 8, 8, 8, 0, UnsignedInt, true);
110 registerFmt(GL_RGB8_SNORM, GL_RGB, GL_BYTE, 8, 8, 8, 0, SignedInt, true);
111 registerFmt(GL_RGB16F, GL_RGB, GL_HALF_FLOAT, 16, 16, 16, 0, FloatingPt, false);
112 registerFmt(GL_RGB32F, GL_RGB, GL_FLOAT, 32, 32, 32, 0, FloatingPt, false);
113
114 // quad channel
115 registerFmt(GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, 8, 8, 8, 8, UnsignedInt, true);
116 registerFmt(GL_RGBA8_SNORM, GL_RGBA, GL_BYTE, 8, 8, 8, 8, SignedInt, true);
117
118 registerFmt(GL_RGBA16I, GL_RGBA_INTEGER, GL_SHORT, 16, 16, 16, 16, SignedInt, false);
119 registerFmt(GL_RGBA16UI, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT, 16, 16, 16, 16, UnsignedInt, false);
120 registerFmt(GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT, 16, 16, 16, 16, FloatingPt, false);
121
122 registerFmt(GL_RGBA32I, GL_RGBA_INTEGER, GL_INT, 32, 32, 32, 32, SignedInt, false);
123 registerFmt(GL_RGBA32UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT, 32, 32, 32, 32, UnsignedInt, false);
124 registerFmt(GL_RGBA32F, GL_RGBA, GL_FLOAT, 32, 32, 32, 32, FloatingPt, false);
125 #endif // GL_VERSION_3_0
126
127 // depth
128 registerFmt(GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 24, 0, 0, 0, UnsignedInt, false);
129 registerFmt(GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, 16, 0, 0, 0, UnsignedInt, false);
130 #ifdef GL_ARB_depth_buffer_float
131 registerFmt(GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, 32, 0, 0, 0, FloatingPt, false);
132 registerFmt(GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, 32, 8, -24, 0, FloatingPt, false);
133 #endif // GL_ARB_depth_buffer_float
134
135 #ifdef GL_ARB_texture_stencil8
136 registerFmt(GL_STENCIL_INDEX8, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, 8, 0, 0, 0, UnsignedInt, false);
137 #endif // GL_ARB_texture_stencil8
138 }
139
140 std::map<GLenum, GLFormatInfo>::iterator f = formatMap_.find(_internalFormat);
141
142 if (f != formatMap_.end())
143 *this = f->second;
144 else
145 {
146 // need to register new format
147 std::cout << "GLFormatInfo: unknown internalFormat " << _internalFormat << std::endl;
148 }
149 }
150
151 GLFormatInfo::GLFormatInfo( GLenum _intfmt, GLenum _fmt, GLenum _type, int _r, int _g, int _b, int _a, BaseType _bt, bool _nrm )
152 : internalFormat_(_intfmt), format_(_fmt), type_(_type),
153 channelCount_(0), bpp_(0), baseType_(_bt), normalized_(_nrm)
154 {
155 // negative channel bits indicate unused channel of size abs(bits)
156 // it's assumed that the unused bits are just padding bits appended to the end
157
158 channelBits_[0] = std::max(_r, 0);
159 channelBits_[1] = std::max(_g, 0);
160 channelBits_[2] = std::max(_b, 0);
161 channelBits_[3] = std::max(_a, 0);
162 sizedName_[0] = 0;
163
164 if (_r > 0) ++channelCount_;
165 if (_g > 0) ++channelCount_;
166 if (_b > 0) ++channelCount_;
167 if (_a > 0) ++channelCount_;
168
169 bpp_ = std::abs(_r) + std::abs(_g) + std::abs(_b) + std::abs(_a);
170
171 if (channelCount_ >= 1 && channelCount_ <= 4)
172 {
173 switch (_intfmt)
174 {
175 case GL_DEPTH_STENCIL: strcpy(sizedName_, "GL_DEPTH_STENCIL"); break;
176 case GL_DEPTH_COMPONENT: strcpy(sizedName_, "GL_DEPTH_COMPONENT"); break;
177 case GL_DEPTH_COMPONENT32: strcpy(sizedName_, "GL_DEPTH_COMPONENT32"); break;
178
179 default:
180 {
181 // construct sized format string
182 QString strfmt = "GL_R";
183
184 if (channelCount_ > 1)
185 strfmt += "G";
186 if (channelCount_ > 2)
187 strfmt += "B";
188 if (channelCount_ > 3)
189 strfmt += "A";
190
191 QString bitCount;
192 bitCount.sprintf("%i", channelBits_[0]);
193
194 strfmt += bitCount;
195
196 if (isNormalized())
197 {
198 if (isInt())
199 strfmt += "_SNORM";
200 }
201 else
202 {
203 if (isFloat())
204 strfmt += "F";
205 else if (isInt())
206 strfmt += "I";
207 else if (isUint())
208 strfmt += "UI";
209 }
210
211 int len = std::min(strfmt.length(), 31);
212
213 for (int i = 0; i < len; ++i)
214 sizedName_[i] = strfmt.at(i).toLatin1();
215 sizedName_[len] = 0;
216 } break;
217 }
218 }
219 }
220
221 GLFormatInfo::GLFormatInfo()
222 : internalFormat_(GL_NONE), format_(GL_NONE), type_(GL_NONE),
223 channelCount_(0), bpp_(0), baseType_(UnsignedInt), normalized_(true)
224 {
225 channelBits_[0] =
226 channelBits_[1] =
227 channelBits_[2] =
228 channelBits_[3] = 0;
229 sizedName_[0] = 0;
230 }
231
232 GLFormatInfo::~GLFormatInfo()
233 {
234 }
235
236 void GLFormatInfo::registerFmt( GLenum _intfmt, GLenum _fmt, GLenum _type, int _r, int _g, int _b, int _a, BaseType _bt, bool _nrm )
237 {
238 formatMap_[_intfmt] = GLFormatInfo(_intfmt, _fmt, _type, _r, _g, _b, _a, _bt, _nrm);
239 }
240
241
242 } /* namespace ACG */
ACG
Namespace providing different geometric functions concerning angles.
Definition: BaseObjectData.hh:68