nux-1.14.0
GLResource.cpp
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 TCHAR *OGLDeviceErrorMessages[] =
00030   {
00031     TEXT ("OGL_OK"),
00032     TEXT ("OGL_ERROR_UNKNOWN"),
00033     TEXT ("OGL_INVALID_SURFACE_LEVEL"),
00034     TEXT ("OGL_INVALID_CALL"),
00035     TEXT ("OGL_INVALID_LOCK"),
00036     TEXT ("OGL_INVALID_UNLOCK"),
00037     TEXT ("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       case ATTRIB_DECLTYPE_FLOAT16_2:
00175       {
00176         NumComponent = 2;
00177         ComponentType = ATTRIB_CT_HALF_FLOAT;
00178         break;
00179       }
00180       case ATTRIB_DECLTYPE_FLOAT16_4:
00181       {
00182         NumComponent = 4;
00183         ComponentType = ATTRIB_CT_HALF_FLOAT;
00184         break;
00185       }
00186 
00187       case ATTRIB_DECLTYPE_UNUSED:
00188       default:
00189       {
00190         NumComponent = 0;
00191         ComponentType = ATTRIB_CT_UNKNOWN;
00192         nuxError (TEXT ("Unsupported Declaration Type. \n") );
00193       }
00194     }
00195   }
00196 
00197   UINT GetVertexElementSize (VERTEXELEMENT vtxelement)
00198   {
00199     ATTRIB_COMPONENT_TYPE type = vtxelement.Type;
00200     UINT NumComponent = vtxelement.NumComponent;
00201 
00202     switch (type)
00203     {
00204       case ATTRIB_CT_BYTE:
00205       case ATTRIB_CT_UNSIGNED_BYTE:
00206         return 1 * NumComponent;
00207 
00208       case ATTRIB_CT_SHORT:
00209       case ATTRIB_CT_UNSIGNED_SHORT:
00210         return 2 * NumComponent;
00211       case ATTRIB_CT_INT:
00212       case ATTRIB_CT_UNSIGNED_INT:
00213       case ATTRIB_CT_FLOAT:
00214         return 4 * NumComponent;
00215       case ATTRIB_CT_HALF_FLOAT:
00216         return 2 * NumComponent;
00217       case ATTRIB_CT_DOUBLE:
00218         return 8 * NumComponent;
00219       case ATTRIB_CT_UNKNOWN:
00220       default:
00221         nuxAssert (TEXT ("Unknown Component Type") );
00222         return 0;
00223     }
00224 
00225     return 0;
00226   }
00227 
00228   void AddVertexElement (std::vector<VERTEXELEMENT>& Elements,
00229                          WORD Stream,
00230                          WORD Offset,
00231                          //ubiU16 Stride,
00232                          ATTRIB_DECL_TYPE Type,
00233                          ATTRIB_USAGE_DECL Usage,
00234                          BYTE UsageIndex)
00235   {
00236     VERTEXELEMENT       Element;
00237     Element.Stream              = Stream;
00238     Element.Offset              = Offset;
00239     // We don't want to store ATTRIB_DECL_TYPE. We unpack it here so we don't have to do it every frame.
00240     // Opengl commands such as cgGLSetParameterPointer needs to know how many components there are in
00241     // a vertex attribute and what is the format of each component.
00242     DecomposeTypeDeclaraction (Type, Element.NumComponent, Element.Type);
00243     //Element.Stride    = Stride;
00244     //Element.Usage       = Usage;
00245     //Element.UsageIndex  = UsageIndex;
00246     Elements.push_back (Element);
00247   }
00248 
00249   unsigned int GetGLElementCount (PRIMITIVE_TYPE InPrimitiveType,
00250                                   unsigned int           InPrimitiveCount)
00251   {
00252     switch (InPrimitiveType)
00253     {
00254       case PRIMITIVE_TYPE_POINTLIST:
00255         return InPrimitiveCount;
00256       case PRIMITIVE_TYPE_LINELIST:
00257         return InPrimitiveCount * 2;
00258       case PRIMITIVE_TYPE_LINESTRIP:
00259         return InPrimitiveCount + 1;
00260       case PRIMITIVE_TYPE_TRIANGLELIST:
00261         return InPrimitiveCount * 3;
00262       case PRIMITIVE_TYPE_TRIANGLESTRIP:
00263         return InPrimitiveCount + 2;
00264       case PRIMITIVE_TYPE_TRIANGLEFAN:
00265         return InPrimitiveCount;
00266       case PRIMITIVE_TYPE_QUADLIST:
00267         return InPrimitiveCount * 4;
00268       case PRIMITIVE_TYPE_QUADSTRIP:
00269         return InPrimitiveCount * 2 + 2;
00270       default:
00271         return 0;
00272     }
00273 
00274     nuxAssertMsg (0, TEXT ("[GetGLElementCount] Invalid PRIMITIVE_TYPE") );
00275     return InPrimitiveCount;
00276   }
00277 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends