Back to index

nux  3.0.0
GLResource.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "GLResource.h"
00024 #include "IOpenGLVertexBuffer.h"
00025 
00026 namespace nux
00027 {
00028 
00029   const char *OGLDeviceErrorMessages[] =
00030   {
00031     "OGL_OK",
00032     "OGL_ERROR_UNKNOWN",
00033     "OGL_INVALID_SURFACE_LEVEL",
00034     "OGL_INVALID_CALL",
00035     "OGL_INVALID_LOCK",
00036     "OGL_INVALID_UNLOCK",
00037     "OGL_INVALID_TEXTURE",
00038   };
00039 
00040 
00041   /*
00042       This table lists the most efficient combinations of internalformat(either generic or specific), format, and type:
00043       internalformat                    format                       type
00044       ------------------------------------------------------------------------------------
00045       GL_ALPHA16                  GL_ALPHA                    GL_UNSIGNED_SHORT
00046       GL_ALPHA8
00047       or GL_ALPHA                 GL_ALPHA                    GL_UNSIGNED_BYTE
00048       GL_INTENSITY16                    GL_INTENSITY                GL_UNSIGNED_SHORT
00049       GL_INTENSITY8              GL_INTENSITY                GL_UNSIGNED_BYTE
00050       GL_LUMINANCE16              GL_LUMINANCE                GL_UNSIGNED_SHORT
00051       GL_LUMINANCE8
00052       or GL_LUMINANCE             GL_LUMINANCE                GL_UNSIGNED_BYTE
00053       GL_LUMINANCE16_ALPHA16      GL_LUMINANCE_ALPHA            GL_UNSIGNED_INT_16_16_REV
00054       GL_LUMINANCE8_ALPHA8
00055       or GL_LUMINANCE_ALPHA       GL_LUMINANCE_ALPHA            GL_UNSIGNED_SHORT_8_8_REV
00056       GL_RGB8                     GL_RGBA                     GL_UNSIGNED_INT_8_8_8_8
00057       GL_RGBA8 or GL_RGBA8        GL_RGBA                     GL_UNSIGNED_INT_8_8_8_8
00058       GL_ARGB_SCE                 GL_BGRA                     GL_UNSIGNED_INT_8_8_8_8_REV
00059       GL_RGB16F                   GL_RGBA                     GL_HALF_FLOAT_ARB
00060       GL_RGBA16F                  GL_RGBA                     GL_HALF_FLOAT_ARB
00061       GL_LUMINANCE_ALPHA16F_ARB   GL_ALPHA_LUMINANCE_SCE      GL_HALF_FLOAT_ARB
00062       GL_LUMINANCE32F_ARB         GL_LUMINANCE                GL_FLOAT
00063       GL_RGB32F_ARB               GL_RGBA                     GL_FLOAT
00064       GL_RGBA32F_ARB              GL_RGBA                     GL_FLOAT
00065       GL_DEPTH_COMPONENT24
00066       or GL_DEPTH_COMPONENT            GL_DEPTH_COMPONENT              GL_UNSIGNED_INT_24_8_SCE
00067   */
00068 
00069 //
00070 //void AddVertexElement(std::vector<VERTEXELEMENT>& Elements,
00071 //                         WORD Stream,
00072 //                         WORD Offset,
00073 //                         //WORD Stride,
00074 //                         ATTRIB_DECL_TYPE Type,
00075 //                         ATTRIB_USAGE_DECL Usage,
00076 //                         BYTE UsageIndex)
00077 //{
00078 //    VERTEXELEMENT  Element;
00079 //    Element.Stream        = Stream;
00080 //    Element.Offset        = Offset;
00081 //    // We don't want to store ATTRIB_DECL_TYPE. We unpack it here so we don't have to do it every frame.
00082 //    // Opengl commands such as cgGLSetParameterPointer needs to know how many components there are in
00083 //    // a vertex attribute and what is the format of each component.
00084 //    DecomposeTypeDeclaraction(Type, &Element.NumComponent, &Element.Type);
00085 //    //Element.Stride    = Stride;
00086 //    Element.Usage       = Usage;
00087 //    Element.UsageIndex  = UsageIndex;
00088 //    Elements.AddItem(Element);
00089 //}
00090 
00091   void DecomposeTypeDeclaraction(ATTRIB_DECL_TYPE Type, int &NumComponent, ATTRIB_COMPONENT_TYPE &ComponentType)
00092   {
00093     switch(Type)
00094     {
00095       case ATTRIB_DECLTYPE_FLOAT1:
00096       {
00097         NumComponent = 1;
00098         ComponentType = ATTRIB_CT_FLOAT;
00099         break;
00100       }
00101       case ATTRIB_DECLTYPE_FLOAT2:
00102       {
00103         NumComponent = 2;
00104         ComponentType = ATTRIB_CT_FLOAT;
00105         break;
00106       }
00107       case ATTRIB_DECLTYPE_FLOAT3:
00108       {
00109         NumComponent = 3;
00110         ComponentType = ATTRIB_CT_FLOAT;
00111         break;
00112       }
00113       case ATTRIB_DECLTYPE_FLOAT4:
00114       {
00115         NumComponent = 4;
00116         ComponentType = ATTRIB_CT_FLOAT;
00117         break;
00118       }
00119       case ATTRIB_DECLTYPE_COLOR:
00120       {
00121         NumComponent = 4;
00122         ComponentType = ATTRIB_CT_UNSIGNED_BYTE;
00123         break;
00124       }
00125       case ATTRIB_DECLTYPE_UBYTE4:
00126       {
00127         NumComponent = 4;
00128         ComponentType = ATTRIB_CT_UNSIGNED_BYTE;
00129         break;
00130       }
00131       case ATTRIB_DECLTYPE_SHORT2:
00132       {
00133         NumComponent = 2;
00134         ComponentType = ATTRIB_CT_SHORT;
00135         break;
00136       }
00137       case ATTRIB_DECLTYPE_SHORT4:
00138       {
00139         NumComponent = 4;
00140         ComponentType = ATTRIB_CT_SHORT;
00141         break;
00142       }
00143       case ATTRIB_DECLTYPE_UBYTE4N:
00144       {
00145         NumComponent = 4;
00146         ComponentType = ATTRIB_CT_UNSIGNED_BYTE;
00147         break;
00148       }
00149       case ATTRIB_DECLTYPE_SHORT2N:
00150       {
00151         NumComponent = 2;
00152         ComponentType = ATTRIB_CT_SHORT;
00153         break;
00154       }
00155       case ATTRIB_DECLTYPE_SHORT4N:
00156       {
00157         NumComponent = 4;
00158         ComponentType = ATTRIB_CT_SHORT;
00159         break;
00160       }
00161       case ATTRIB_DECLTYPE_USHORT2N:
00162       {
00163         NumComponent = 2;
00164         ComponentType = ATTRIB_CT_UNSIGNED_SHORT;
00165         break;
00166       }
00167       case ATTRIB_DECLTYPE_USHORT4N:
00168       {
00169         NumComponent = 4;
00170         ComponentType = ATTRIB_CT_UNSIGNED_SHORT;
00171         break;
00172       }
00173 
00174 #ifndef NUX_OPENGLES_20
00175       case ATTRIB_DECLTYPE_FLOAT16_2:
00176       {
00177         NumComponent = 2;
00178         ComponentType = ATTRIB_CT_HALF_FLOAT;
00179         break;
00180       }
00181       case ATTRIB_DECLTYPE_FLOAT16_4:
00182       {
00183         NumComponent = 4;
00184         ComponentType = ATTRIB_CT_HALF_FLOAT;
00185         break;
00186       }
00187 #endif
00188 
00189       case ATTRIB_DECLTYPE_UNUSED:
00190       default:
00191       {
00192         NumComponent = 0;
00193         ComponentType = ATTRIB_CT_UNKNOWN;
00194         nuxError("Unsupported Declaration Type. \n");
00195       }
00196     }
00197   }
00198 
00199   UINT GetVertexElementSize(VERTEXELEMENT vtxelement)
00200   {
00201     ATTRIB_COMPONENT_TYPE type = vtxelement.Type;
00202     UINT NumComponent = vtxelement.NumComponent;
00203 
00204     switch(type)
00205     {
00206       case ATTRIB_CT_BYTE:
00207       case ATTRIB_CT_UNSIGNED_BYTE:
00208         return 1 * NumComponent;
00209 
00210       case ATTRIB_CT_SHORT:
00211       case ATTRIB_CT_UNSIGNED_SHORT:
00212         return 2 * NumComponent;
00213       case ATTRIB_CT_INT:
00214       case ATTRIB_CT_UNSIGNED_INT:
00215       case ATTRIB_CT_FLOAT:
00216         return 4 * NumComponent;
00217 #ifndef NUX_OPENGLES_20
00218       case ATTRIB_CT_HALF_FLOAT:
00219         return 2 * NumComponent;
00220       case ATTRIB_CT_DOUBLE:
00221         return 8 * NumComponent;
00222 #endif
00223       case ATTRIB_CT_UNKNOWN:
00224       default:
00225         nuxAssert("Unknown Component Type");
00226         return 0;
00227     }
00228 
00229     return 0;
00230   }
00231 
00232   void AddVertexElement(std::vector<VERTEXELEMENT>& Elements,
00233                          WORD Stream,
00234                          WORD Offset,
00235                          //ubiU16 Stride,
00236                          ATTRIB_DECL_TYPE Type,
00237                          ATTRIB_USAGE_DECL Usage,
00238                          BYTE UsageIndex)
00239   {
00240     VERTEXELEMENT    Element;
00241     Element.Stream          = Stream;
00242     Element.Offset          = Offset;
00243     // We don't want to store ATTRIB_DECL_TYPE. We unpack it here so we don't have to do it every frame.
00244     // Opengl commands such as cgGLSetParameterPointer needs to know how many components there are in
00245     // a vertex attribute and what is the format of each component.
00246     DecomposeTypeDeclaraction(Type, Element.NumComponent, Element.Type);
00247     //Element.Stride    = Stride;
00248     //Element.Usage       = Usage;
00249     //Element.UsageIndex  = UsageIndex;
00250     Elements.push_back(Element);
00251   }
00252 
00253   unsigned int GetGLElementCount(PRIMITIVE_TYPE InPrimitiveType,
00254                                   unsigned int           InPrimitiveCount)
00255   {
00256     switch(InPrimitiveType)
00257     {
00258       case PRIMITIVE_TYPE_POINTLIST:
00259         return InPrimitiveCount;
00260       case PRIMITIVE_TYPE_LINELIST:
00261         return InPrimitiveCount * 2;
00262       case PRIMITIVE_TYPE_LINESTRIP:
00263         return InPrimitiveCount + 1;
00264       case PRIMITIVE_TYPE_TRIANGLELIST:
00265         return InPrimitiveCount * 3;
00266       case PRIMITIVE_TYPE_TRIANGLESTRIP:
00267         return InPrimitiveCount + 2;
00268       case PRIMITIVE_TYPE_TRIANGLEFAN:
00269         return InPrimitiveCount;
00270 #ifndef NUX_OPENGLES_20
00271       case PRIMITIVE_TYPE_QUADLIST:
00272         return InPrimitiveCount * 4;
00273       case PRIMITIVE_TYPE_QUADSTRIP:
00274         return InPrimitiveCount * 2 + 2;
00275 #endif
00276       default:
00277         return 0;
00278     }
00279 
00280     nuxAssertMsg(0, "[GetGLElementCount] Invalid PRIMITIVE_TYPE");
00281     return InPrimitiveCount;
00282   }
00283 }