Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/RenderSystems/GL/src/OgreGLPixelFormat.cpp @ 3

Last change on this file since 3 was 3, checked in by anonymous, 17 years ago

=update

File size: 10.3 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org
6
7Copyright (c) 2000-2006 Torus Knot Software Ltd
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29
30#include "OgreGLPixelFormat.h"
31#include "OgreRoot.h"
32#include "OgreRenderSystem.h"
33#include "OgreBitwise.h"
34
35namespace Ogre  {
36        //-----------------------------------------------------------------------------
37    GLenum GLPixelUtil::getGLOriginFormat(PixelFormat mFormat)
38    {
39        switch(mFormat)
40        {
41                        case PF_A8:
42                                return GL_ALPHA;
43            case PF_L8:
44                return GL_LUMINANCE;
45            case PF_L16:
46                return GL_LUMINANCE;
47                        case PF_BYTE_LA:
48                                return GL_LUMINANCE_ALPHA;
49                        case PF_R3G3B2:
50                                return GL_RGB;
51                        case PF_A1R5G5B5:
52                                return GL_BGRA;
53                        case PF_R5G6B5:
54                                return GL_RGB;
55                        case PF_B5G6R5:
56                                return GL_BGR;
57                        case PF_A4R4G4B4:
58                                return GL_BGRA;
59#if OGRE_ENDIAN == OGRE_ENDIAN_BIG
60            // Formats are in native endian, so R8G8B8 on little endian is
61            // BGR, on big endian it is RGB.
62            case PF_R8G8B8:
63                return GL_RGB;
64            case PF_B8G8R8:
65                return GL_BGR;
66#else
67            case PF_R8G8B8:
68                return GL_BGR;
69            case PF_B8G8R8:
70                return GL_RGB;
71#endif
72                        case PF_X8R8G8B8:
73                        case PF_A8R8G8B8:
74                                return GL_BGRA;
75                        case PF_X8B8G8R8:
76            case PF_A8B8G8R8:
77                return GL_RGBA;
78            case PF_B8G8R8A8:
79                return GL_BGRA;
80                        case PF_R8G8B8A8:
81                                return GL_RGBA;
82            case PF_A2R10G10B10:
83                return GL_BGRA;
84            case PF_A2B10G10R10:
85                return GL_RGBA;
86                        case PF_FLOAT16_R:
87                return GL_LUMINANCE;
88                        case PF_FLOAT16_GR:
89                                return GL_LUMINANCE_ALPHA;
90            case PF_FLOAT16_RGB:
91                return GL_RGB;
92            case PF_FLOAT16_RGBA:
93                return GL_RGBA;
94                        case PF_FLOAT32_R:
95                return GL_LUMINANCE;
96                        case PF_FLOAT32_GR:
97                                return GL_LUMINANCE_ALPHA;
98            case PF_FLOAT32_RGB:
99                return GL_RGB;
100            case PF_FLOAT32_RGBA:
101                return GL_RGBA;
102                        case PF_SHORT_RGBA:
103                                return GL_RGBA;
104            case PF_SHORT_RGB:
105                return GL_RGB;
106                        case PF_SHORT_GR:
107                                return GL_LUMINANCE_ALPHA;
108            case PF_DXT1:
109                return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
110            case PF_DXT3:
111                 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
112            case PF_DXT5:
113                 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
114            default:
115                return 0;
116        }
117    }
118        //-----------------------------------------------------------------------------
119    GLenum GLPixelUtil::getGLOriginDataType(PixelFormat mFormat)
120    {
121        switch(mFormat)
122        {
123                        case PF_A8:
124            case PF_L8:
125            case PF_R8G8B8:
126            case PF_B8G8R8:
127                        case PF_BYTE_LA:
128                return GL_UNSIGNED_BYTE;
129                        case PF_R3G3B2:
130                                return GL_UNSIGNED_BYTE_3_3_2;
131                        case PF_A1R5G5B5:
132                                return GL_UNSIGNED_SHORT_1_5_5_5_REV;
133                        case PF_R5G6B5:
134                        case PF_B5G6R5:
135                                return GL_UNSIGNED_SHORT_5_6_5;
136                        case PF_A4R4G4B4:
137                                return GL_UNSIGNED_SHORT_4_4_4_4_REV;
138            case PF_L16:
139                return GL_UNSIGNED_SHORT;
140#if OGRE_ENDIAN == OGRE_ENDIAN_BIG
141                        case PF_X8B8G8R8:
142                        case PF_A8B8G8R8:
143                return GL_UNSIGNED_INT_8_8_8_8_REV;
144                        case PF_X8R8G8B8:
145            case PF_A8R8G8B8:
146                                return GL_UNSIGNED_INT_8_8_8_8_REV;
147            case PF_B8G8R8A8:
148                return GL_UNSIGNED_BYTE;
149                        case PF_R8G8B8A8:
150                                return GL_UNSIGNED_BYTE;
151#else
152                        case PF_X8B8G8R8:
153                        case PF_A8B8G8R8:
154                return GL_UNSIGNED_BYTE;
155                        case PF_X8R8G8B8:
156            case PF_A8R8G8B8:
157                                return GL_UNSIGNED_BYTE;
158            case PF_B8G8R8A8:
159                return GL_UNSIGNED_INT_8_8_8_8;
160                        case PF_R8G8B8A8:
161                                return GL_UNSIGNED_INT_8_8_8_8;
162#endif
163            case PF_A2R10G10B10:
164                return GL_UNSIGNED_INT_2_10_10_10_REV;
165            case PF_A2B10G10R10:
166                return GL_UNSIGNED_INT_2_10_10_10_REV;
167                        case PF_FLOAT16_R:
168                        case PF_FLOAT16_GR:
169            case PF_FLOAT16_RGB:
170            case PF_FLOAT16_RGBA:
171                return GL_HALF_FLOAT_ARB;
172                        case PF_FLOAT32_R:
173                        case PF_FLOAT32_GR:
174            case PF_FLOAT32_RGB:
175            case PF_FLOAT32_RGBA:
176                return GL_FLOAT;
177                        case PF_SHORT_RGBA:
178            case PF_SHORT_RGB:
179                        case PF_SHORT_GR:
180                                return GL_UNSIGNED_SHORT;
181            default:
182                return 0;
183        }
184    }
185   
186    GLenum GLPixelUtil::getGLInternalFormat(PixelFormat mFormat)
187    {
188        switch(mFormat) {
189            case PF_L8:
190                return GL_LUMINANCE8;
191            case PF_L16:
192                return GL_LUMINANCE16;
193            case PF_A8:
194                return GL_ALPHA8;
195            case PF_A4L4:
196                return GL_LUMINANCE4_ALPHA4;
197                        case PF_BYTE_LA:
198                                return GL_LUMINANCE8_ALPHA8;
199                        case PF_R3G3B2:
200                                return GL_R3_G3_B2;
201                        case PF_A1R5G5B5:
202                                return GL_RGB5_A1;
203            case PF_R5G6B5:
204                        case PF_B5G6R5:
205                return GL_RGB5;
206            case PF_A4R4G4B4:
207                return GL_RGBA4;
208            case PF_R8G8B8:
209            case PF_B8G8R8:
210                        case PF_X8B8G8R8:
211                        case PF_X8R8G8B8:
212                return GL_RGB8;
213            case PF_A8R8G8B8:
214            case PF_B8G8R8A8:
215                return GL_RGBA8;
216            case PF_A2R10G10B10:
217            case PF_A2B10G10R10:
218                return GL_RGB10_A2;
219                        case PF_FLOAT16_R:
220                                return GL_LUMINANCE16F_ARB;
221            case PF_FLOAT16_RGB:
222                return GL_RGB16F_ARB;
223                        case PF_FLOAT16_GR: 
224                                return GL_LUMINANCE_ALPHA16F_ARB;
225            case PF_FLOAT16_RGBA:
226                return GL_RGBA16F_ARB;
227                        case PF_FLOAT32_R:
228                                return GL_LUMINANCE32F_ARB;
229                        case PF_FLOAT32_GR:
230                                return GL_LUMINANCE_ALPHA32F_ARB;
231            case PF_FLOAT32_RGB:
232                return GL_RGB32F_ARB;
233            case PF_FLOAT32_RGBA:
234                return GL_RGBA32F_ARB;
235                        case PF_SHORT_RGBA:
236                                return GL_RGBA16;
237            case PF_SHORT_RGB:
238                return GL_RGB16;
239                        case PF_SHORT_GR:
240                                return GL_LUMINANCE16_ALPHA16;
241                        case PF_DXT1:
242                return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
243            case PF_DXT3:
244                return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
245            case PF_DXT5:
246                return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
247            default:
248                return GL_NONE;
249        }
250    }
251
252    GLenum GLPixelUtil::getClosestGLInternalFormat(PixelFormat mFormat)
253    {
254        GLenum format = getGLInternalFormat(mFormat);
255        if(format==GL_NONE)
256            return GL_RGBA8;
257        else
258            return format;
259    }
260       
261        //-----------------------------------------------------------------------------         
262        PixelFormat GLPixelUtil::getClosestOGREFormat(GLenum fmt)
263        {
264                switch(fmt) 
265                {
266                case GL_LUMINANCE8:
267                        return PF_L8;
268                case GL_LUMINANCE16:
269                        return PF_L16;
270                case GL_ALPHA8:
271                        return PF_A8;
272                case GL_LUMINANCE4_ALPHA4:
273                        // Unsupported by GL as input format, use the byte packed format
274                        return PF_BYTE_LA;
275                case GL_LUMINANCE8_ALPHA8:
276                        return PF_BYTE_LA;
277                case GL_R3_G3_B2:
278                        return PF_R3G3B2;
279                case GL_RGB5_A1:
280                        return PF_A1R5G5B5;
281                case GL_RGB5:
282                        return PF_R5G6B5;
283                case GL_RGBA4:
284                        return PF_A4R4G4B4;
285                case GL_RGB8:
286                        return PF_X8R8G8B8;
287                case GL_RGBA8:
288                        return PF_A8R8G8B8;
289                case GL_RGB10_A2:
290                        return PF_A2R10G10B10;
291                case GL_RGBA16:
292                        return PF_SHORT_RGBA;
293        case GL_RGB16:
294            return PF_SHORT_RGB;
295                case GL_LUMINANCE16_ALPHA16:
296                        return PF_SHORT_GR;
297                case GL_LUMINANCE_FLOAT16_ATI:
298                        return PF_FLOAT16_R;
299                case GL_LUMINANCE_ALPHA_FLOAT16_ATI:
300                        return PF_FLOAT16_GR;
301                case GL_LUMINANCE_ALPHA_FLOAT32_ATI:
302                        return PF_FLOAT32_GR;
303                case GL_LUMINANCE_FLOAT32_ATI:
304                        return PF_FLOAT32_R;
305                case GL_RGB_FLOAT16_ATI: // GL_RGB16F_ARB
306                        return PF_FLOAT16_RGB;
307                case GL_RGBA_FLOAT16_ATI:
308                        return PF_FLOAT16_RGBA;
309                case GL_RGB_FLOAT32_ATI:
310                        return PF_FLOAT32_RGB;
311                case GL_RGBA_FLOAT32_ATI:
312                        return PF_FLOAT32_RGBA;
313                case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
314                case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
315                        return PF_DXT1;
316                case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
317                        return PF_DXT3;
318                case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
319                        return PF_DXT5;
320                default:
321                        return PF_A8R8G8B8;
322                };
323        }
324        //-----------------------------------------------------------------------------
325
326        size_t GLPixelUtil::getMaxMipmaps(size_t width, size_t height, size_t depth, PixelFormat format)
327        {
328                size_t count = 0;
329                do {
330                        if(width>1)             width = width/2;
331                        if(height>1)    height = height/2;
332                        if(depth>1)             depth = depth/2;
333                        /*
334                        NOT needed, compressed formats will have mipmaps up to 1x1
335                        if(PixelUtil::isValidExtent(width, height, depth, format))
336                                count ++;
337                        else
338                                break;
339                        */
340                               
341                        count ++;
342                } while(!(width == 1 && height == 1 && depth == 1));
343               
344                return count;
345        }
346    //-----------------------------------------------------------------------------   
347    size_t GLPixelUtil::optionalPO2(size_t value)
348    {
349        const RenderSystemCapabilities *caps = Root::getSingleton().getRenderSystem()->getCapabilities();
350        if(caps->hasCapability(RSC_NON_POWER_OF_2_TEXTURES))
351            return value;
352        else
353            return Bitwise::firstPO2From((uint32)value);
354    }   
355
356       
357};
Note: See TracBrowser for help on using the repository browser.