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 /*===========================================================================*\
43 * *
44  * $Revision$ *
45  * $LastChangedBy$ *
46  * $Date$ *
47  * *
48  \*===========================================================================*/
49 
50 #include <ACG/GL/GLFormatInfo.hh>
51 #include <iostream>
52 #include <QString>
53 
54 
55 namespace ACG {
56 
57 
58 // MACOS fix
59 
60 std::map<GLenum, GLFormatInfo> GLFormatInfo::formatMap_;
61 
62 GLFormatInfo::GLFormatInfo( GLenum _internalFormat )
63  : internalFormat_(_internalFormat), format_(GL_NONE), type_(GL_NONE),
64  channelCount_(0), bpp_(0), baseType_(UnsignedInt), normalized_(true)
65 {
66  channelBits_[0] =
67  channelBits_[1] =
68  channelBits_[2] =
69  channelBits_[3] = 0;
70  sizedName_[0] = 0;
71 
72  if (formatMap_.empty())
73  {
74  registerFmt(GL_RED, GL_RED, GL_UNSIGNED_BYTE, 8, 0, 0, 0, UnsignedInt, true);
75  registerFmt(GL_RG, GL_RG, GL_UNSIGNED_BYTE, 8, 8, 0, 0, UnsignedInt, true);
76  registerFmt(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, 8, 8, 8, 0, UnsignedInt, true);
77  registerFmt(GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 8, 8, 8, 8, UnsignedInt, true);
78 
79  registerFmt(GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 32, 0, 0, 0, UnsignedInt, false);
80  registerFmt(GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 32, 0, 0, 0, UnsignedInt, false);
81  registerFmt(GL_DEPTH_STENCIL, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 24, 8, 0, 0, UnsignedInt, false);
82  registerFmt(GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 24, 8, 0, 0, UnsignedInt, false);
83 
84 #ifdef GL_VERSION_3_0
85  // single channel formats
86  registerFmt(GL_R8, GL_RED, GL_UNSIGNED_BYTE, 8, 0, 0, 0, UnsignedInt, true);
87  registerFmt(GL_R8_SNORM, GL_RED, GL_BYTE, 8, 0, 0, 0, SignedInt, true);
88  registerFmt(GL_R8I, GL_RED_INTEGER, GL_BYTE, 8, 0, 0, 0, SignedInt, false);
89  registerFmt(GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE, 8, 0, 0, 0, UnsignedInt, false);
90 
91  registerFmt(GL_R16, GL_RED, GL_UNSIGNED_SHORT, 16, 0, 0, 0, UnsignedInt, true);
92  registerFmt(GL_R16I, GL_RED_INTEGER, GL_SHORT, 16, 0, 0, 0, SignedInt, false);
93  registerFmt(GL_R16UI, GL_RED_INTEGER, GL_UNSIGNED_SHORT, 16, 0, 0, 0, UnsignedInt, false);
94  registerFmt(GL_R16F, GL_RED, GL_HALF_FLOAT, 16, 0, 0, 0, FloatingPt, false);
95 
96  registerFmt(GL_R32I, GL_RED_INTEGER, GL_INT, 32, 0, 0, 0, SignedInt, false);
97  registerFmt(GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT, 32, 0, 0, 0, UnsignedInt, false);
98  registerFmt(GL_R32F, GL_RED, GL_FLOAT, 32, 0, 0, 0, FloatingPt, false);
99 
100  // dual channel formats
101  registerFmt(GL_RG8, GL_RG, GL_UNSIGNED_BYTE, 8, 8, 0, 0, UnsignedInt, true);
102  registerFmt(GL_RG8_SNORM, GL_RG, GL_BYTE, 8, 8, 0, 0, SignedInt, true);
103  registerFmt(GL_RG8I, GL_RG_INTEGER, GL_BYTE, 8, 8, 0, 0, SignedInt, false);
104  registerFmt(GL_RG8UI, GL_RG_INTEGER, GL_UNSIGNED_BYTE, 8, 8, 0, 0, UnsignedInt, false);
105 
106  registerFmt(GL_RG16, GL_RG, GL_UNSIGNED_SHORT, 16, 16, 0, 0, UnsignedInt, true);
107  registerFmt(GL_RG16I, GL_RG_INTEGER, GL_SHORT, 16, 16, 0, 0, SignedInt, false);
108  registerFmt(GL_RG16UI, GL_RG_INTEGER, GL_UNSIGNED_SHORT, 16, 16, 0, 0, UnsignedInt, false);
109  registerFmt(GL_RG16F, GL_RG, GL_HALF_FLOAT, 16, 16, 0, 0, FloatingPt, false);
110 
111  registerFmt(GL_RG32I, GL_RG_INTEGER, GL_INT, 32, 32, 0, 0, SignedInt, false);
112  registerFmt(GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT, 32, 32, 0, 0, UnsignedInt, false);
113  registerFmt(GL_RG32F, GL_RG, GL_FLOAT, 32, 32, 0, 0, FloatingPt, false);
114 
115 
116  // triple channel
117  registerFmt(GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, 8, 8, 8, 0, UnsignedInt, true);
118  registerFmt(GL_RGB8_SNORM, GL_RGB, GL_BYTE, 8, 8, 8, 0, SignedInt, true);
119  registerFmt(GL_RGB16F, GL_RGB, GL_HALF_FLOAT, 16, 16, 16, 0, FloatingPt, false);
120  registerFmt(GL_RGB32F, GL_RGB, GL_FLOAT, 32, 32, 32, 0, FloatingPt, false);
121 
122  // quad channel
123  registerFmt(GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, 8, 8, 8, 8, UnsignedInt, true);
124  registerFmt(GL_RGBA8_SNORM, GL_RGBA, GL_BYTE, 8, 8, 8, 8, SignedInt, true);
125 
126  registerFmt(GL_RGBA16I, GL_RGBA_INTEGER, GL_SHORT, 16, 16, 16, 16, SignedInt, false);
127  registerFmt(GL_RGBA16UI, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT, 16, 16, 16, 16, UnsignedInt, false);
128  registerFmt(GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT, 16, 16, 16, 16, FloatingPt, false);
129 
130  registerFmt(GL_RGBA32I, GL_RGBA_INTEGER, GL_INT, 32, 32, 32, 32, SignedInt, false);
131  registerFmt(GL_RGBA32UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT, 32, 32, 32, 32, UnsignedInt, false);
132  registerFmt(GL_RGBA32F, GL_RGBA, GL_FLOAT, 32, 32, 32, 32, FloatingPt, false);
133 #endif // GL_VERSION_3_0
134 
135  // depth
136  registerFmt(GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 24, 0, 0, 0, UnsignedInt, false);
137  registerFmt(GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, 16, 0, 0, 0, UnsignedInt, false);
138 #ifdef GL_ARB_depth_buffer_float
139  registerFmt(GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, 32, 0, 0, 0, FloatingPt, false);
140  registerFmt(GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, 32, 8, -24, 0, FloatingPt, false);
141 #endif // GL_ARB_depth_buffer_float
142 
143 #ifdef GL_ARB_texture_stencil8
144  registerFmt(GL_STENCIL_INDEX8, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, 8, 0, 0, 0, UnsignedInt, false);
145 #endif // GL_ARB_texture_stencil8
146  }
147 
148  std::map<GLenum, GLFormatInfo>::iterator f = formatMap_.find(_internalFormat);
149 
150  if (f != formatMap_.end())
151  *this = f->second;
152  else
153  {
154  // need to register new format
155  std::cout << "GLFormatInfo: unknown internalFormat " << _internalFormat << std::endl;
156  }
157 }
158 
159 GLFormatInfo::GLFormatInfo( GLenum _intfmt, GLenum _fmt, GLenum _type, int _r, int _g, int _b, int _a, BaseType _bt, bool _nrm )
160  : internalFormat_(_intfmt), format_(_fmt), type_(_type),
161  channelCount_(0), bpp_(0), baseType_(_bt), normalized_(_nrm)
162 {
163  // negative channel bits indicate unused channel of size abs(bits)
164  // it's assumed that the unused bits are just padding bits appended to the end
165 
166  channelBits_[0] = std::max(_r, 0);
167  channelBits_[1] = std::max(_g, 0);
168  channelBits_[2] = std::max(_b, 0);
169  channelBits_[3] = std::max(_a, 0);
170  sizedName_[0] = 0;
171 
172  if (_r > 0) ++channelCount_;
173  if (_g > 0) ++channelCount_;
174  if (_b > 0) ++channelCount_;
175  if (_a > 0) ++channelCount_;
176 
177  bpp_ = std::abs(_r) + std::abs(_g) + std::abs(_b) + std::abs(_a);
178 
179  if (channelCount_ >= 1 && channelCount_ <= 4)
180  {
181  switch (_intfmt)
182  {
183  case GL_DEPTH_STENCIL: strcpy(sizedName_, "GL_DEPTH_STENCIL"); break;
184  case GL_DEPTH_COMPONENT: strcpy(sizedName_, "GL_DEPTH_COMPONENT"); break;
185  case GL_DEPTH_COMPONENT32: strcpy(sizedName_, "GL_DEPTH_COMPONENT32"); break;
186 
187  default:
188  {
189  // construct sized format string
190  QString strfmt = "GL_R";
191 
192  if (channelCount_ > 1)
193  strfmt += "G";
194  if (channelCount_ > 2)
195  strfmt += "B";
196  if (channelCount_ > 3)
197  strfmt += "A";
198 
199  QString bitCount;
200  bitCount.sprintf("%i", channelBits_[0]);
201 
202  strfmt += bitCount;
203 
204  if (isNormalized())
205  {
206  if (isInt())
207  strfmt += "_SNORM";
208  }
209  else
210  {
211  if (isFloat())
212  strfmt += "F";
213  else if (isInt())
214  strfmt += "I";
215  else if (isUint())
216  strfmt += "UI";
217  }
218 
219  int len = std::min(strfmt.length(), 31);
220 
221  for (int i = 0; i < len; ++i)
222  sizedName_[i] = strfmt.at(i).toLatin1();
223  sizedName_[len] = 0;
224  } break;
225  }
226  }
227 }
228 
229 GLFormatInfo::GLFormatInfo()
230  : internalFormat_(GL_NONE), format_(GL_NONE), type_(GL_NONE),
231  channelCount_(0), bpp_(0), baseType_(UnsignedInt), normalized_(true)
232 {
233  channelBits_[0] =
234  channelBits_[1] =
235  channelBits_[2] =
236  channelBits_[3] = 0;
237  sizedName_[0] = 0;
238 }
239 
240 GLFormatInfo::~GLFormatInfo()
241 {
242 }
243 
244 void GLFormatInfo::registerFmt( GLenum _intfmt, GLenum _fmt, GLenum _type, int _r, int _g, int _b, int _a, BaseType _bt, bool _nrm )
245 {
246  formatMap_[_intfmt] = GLFormatInfo(_intfmt, _fmt, _type, _r, _g, _b, _a, _bt, _nrm);
247 }
248 
249 
250 } /* namespace ACG */
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51