os/graphics/m3g/m3gcore11/src/m3g_polygonmode.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: PolygonMode implementation
    15 *
    16 */
    17 
    18 
    19 /*!
    20  * \internal
    21  * \file
    22  * \brief PolygonMode implementation
    23  */
    24 
    25 #ifndef M3G_CORE_INCLUDE
    26 #   error included by m3g_core.c; do not compile separately.
    27 #endif
    28 
    29 /*
    30  * Uncomment this line to switch tracing on for this file's functions
    31  */
    32 /* #define M3G_LOCAL_TRACEF_ON */
    33 
    34 #include "m3g_polygonmode.h"
    35 
    36 /*----------------------------------------------------------------------
    37  * Internal functions
    38  *--------------------------------------------------------------------*/
    39 
    40 /*!
    41  * \internal
    42  * \brief Applies default polygon mode to OpenGL.
    43  */
    44 static void m3gApplyDefaultPolygonMode()
    45 {
    46     /* cull = BACK */
    47     glCullFace(GL_BACK);
    48     glEnable(GL_CULL_FACE);
    49 
    50     /* shading = M3G_SHADE_SMOOTH */
    51     glShadeModel(GL_SMOOTH);
    52 
    53     /*  winding = M3G_WINDING_CCW */
    54     glFrontFace(GL_CCW);
    55 
    56     /* perspective correction = false */
    57     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
    58 
    59     /* lighting = ONE_SIDED */
    60     glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
    61 }
    62 
    63 /*!
    64  * \internal
    65  * \brief Applies polygon mode to OpenGL.
    66  *
    67  * \param polygonMode   PolygonMode object
    68  */
    69 static void m3gApplyPolygonMode(PolygonMode *polygonMode)
    70 {
    71     if (polygonMode != NULL) {
    72         if (polygonMode->cullingMode == M3G_CULL_NONE) {
    73             glDisable(GL_CULL_FACE);
    74         }
    75         else {
    76             if (polygonMode->cullingMode == M3G_CULL_BACK) {
    77                 glCullFace(GL_BACK);
    78             }
    79             else {
    80                 glCullFace(GL_FRONT);
    81             }
    82             glEnable(GL_CULL_FACE);
    83         }
    84 
    85         if (m3gGetTwoSidedLightingWorkaround(M3G_INTERFACE(polygonMode))) {
    86             glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
    87         }
    88         else {
    89             glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, polygonMode->enableTwoSidedLighting);
    90         }
    91 
    92         if (polygonMode->shadingMode == M3G_SHADE_FLAT) {
    93             glShadeModel(GL_FLAT);
    94         }
    95         else {
    96             glShadeModel(GL_SMOOTH);
    97         }
    98 
    99         if (polygonMode->windingMode == M3G_WINDING_CW) {
   100             glFrontFace(GL_CW);
   101         }
   102         else {
   103             glFrontFace(GL_CCW);
   104         }
   105 
   106         if (polygonMode->enablePerspectiveCorrection == GL_TRUE) {
   107             glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
   108         }
   109         else {
   110             glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
   111         }
   112     }
   113     else {
   114         m3gApplyDefaultPolygonMode();
   115     }
   116     M3G_ASSERT_GL;
   117 
   118 }
   119 
   120 /*!
   121  * \internal
   122  * \brief Overloaded Object3D method
   123  *
   124  * \param originalObj original PolygonMode object
   125  * \param cloneObj pointer to cloned PolygonMode object
   126  * \param pairs array for all object-duplicate pairs
   127  * \param numPairs number of pairs
   128  */
   129 static M3Gbool m3gPolygonModeDuplicate(const Object *originalObj,
   130                                        Object **cloneObj,
   131                                        Object **pairs,
   132                                        M3Gint *numPairs)
   133 {
   134     PolygonMode *original = (PolygonMode *)originalObj;
   135     PolygonMode *clone;
   136     M3G_ASSERT(*cloneObj == NULL); /* no derived classes */
   137 
   138     /* Create the clone object */
   139     
   140     clone = (PolygonMode *)m3gCreatePolygonMode(originalObj->interface);
   141     if (!clone) {
   142         return M3G_FALSE;
   143     }
   144     *cloneObj = (Object *)clone;
   145 
   146     /* Duplicate base class data */
   147     
   148     if (!m3gObjectDuplicate(originalObj, cloneObj, pairs, numPairs)) {
   149         return M3G_FALSE;
   150     }
   151 
   152     /* Duplicate our own data */
   153     
   154     clone->enableLocalCameraLighting = original->enableLocalCameraLighting;
   155     clone->enablePerspectiveCorrection = original->enablePerspectiveCorrection;
   156     clone->cullingMode = original->cullingMode;
   157     clone->windingMode = original->windingMode;
   158     clone->shadingMode = original->shadingMode;
   159     clone->enableTwoSidedLighting = original->enableTwoSidedLighting;
   160 
   161     return M3G_TRUE;
   162 }
   163 
   164 /*----------------------------------------------------------------------
   165  * Virtual function table
   166  *--------------------------------------------------------------------*/
   167 
   168 static const ObjectVFTable m3gvf_PolygonMode = {
   169     m3gObjectApplyAnimation,
   170     m3gObjectIsCompatible,
   171     m3gObjectUpdateProperty,
   172     m3gObjectDoGetReferences,
   173     m3gObjectFindID,
   174     m3gPolygonModeDuplicate,
   175     m3gDestroyObject
   176 };
   177 
   178 
   179 /*----------------------------------------------------------------------
   180  * Public API functions
   181  *--------------------------------------------------------------------*/
   182 
   183 /*!
   184  * \brief Creates a new PolygonMode object.
   185  *
   186  * \param interface     M3G interface object
   187  * \retval PolygonMode new PolygonMode object
   188  * \retval NULL PolygonMode creating failed
   189  */
   190 M3G_API M3GPolygonMode m3gCreatePolygonMode(M3GInterface interface)
   191 {
   192     Interface *m3g = (Interface *) interface;
   193     M3G_VALIDATE_INTERFACE(m3g);
   194 
   195     {
   196         PolygonMode *polygonMode = m3gAllocZ(m3g, sizeof(PolygonMode));
   197 
   198         if (polygonMode != NULL) {
   199             m3gInitObject(&polygonMode->object, m3g, M3G_CLASS_POLYGON_MODE);
   200             polygonMode->enableLocalCameraLighting = GL_FALSE;
   201             polygonMode->enablePerspectiveCorrection = GL_FALSE;
   202             polygonMode->cullingMode = M3G_CULL_BACK;
   203             polygonMode->windingMode = M3G_WINDING_CCW;
   204             polygonMode->shadingMode = M3G_SHADE_SMOOTH;
   205             polygonMode->enableTwoSidedLighting = GL_FALSE;
   206         }
   207 
   208         return (M3GPolygonMode)polygonMode;
   209     }
   210 }
   211 
   212 /*!
   213  * \brief Set local camera lightning.
   214  *
   215  * \param handle        PolygonMode object
   216  * \param enable        enable flag
   217  */
   218 M3G_API void m3gSetLocalCameraLightingEnable(M3GPolygonMode handle,
   219                                              M3Gbool enable)
   220 {
   221     PolygonMode *polygonMode = (PolygonMode*)handle;
   222     M3G_VALIDATE_OBJECT(polygonMode);
   223     polygonMode->enableLocalCameraLighting = (GLboolean) enable;
   224 }
   225 
   226 /*!
   227  * \brief Set perspective correction.
   228  *
   229  * \param handle        PolygonMode object
   230  * \param enable        enable flag
   231  */
   232 void m3gSetPerspectiveCorrectionEnable(M3GPolygonMode handle, M3Gbool enable)
   233 {
   234     PolygonMode *polygonMode = (PolygonMode*)handle;
   235     M3G_VALIDATE_OBJECT(polygonMode);
   236     polygonMode->enablePerspectiveCorrection = (GLboolean) enable;
   237 }
   238 
   239 /*!
   240  * \brief Set culling mode.
   241  *
   242  * \param handle        PolygonMode object
   243  * \param mode          culling mode
   244  */
   245 void m3gSetCulling(M3GPolygonMode handle, M3Gint mode)
   246 {
   247     PolygonMode *polygonMode = (PolygonMode*)handle;
   248     M3G_VALIDATE_OBJECT(polygonMode);
   249 
   250     switch (mode) {
   251     case M3G_CULL_BACK:
   252     case M3G_CULL_FRONT:
   253     case M3G_CULL_NONE:
   254         polygonMode->cullingMode = mode;
   255         break;
   256     default:
   257         m3gRaiseError(M3G_INTERFACE(polygonMode), M3G_INVALID_VALUE);
   258         break;
   259     }
   260 }
   261 
   262 /*!
   263  * \brief Get culling mode.
   264  *
   265  * \param handle        PolygonMode object
   266  * \return              culling mode
   267  */
   268 M3G_API M3Gint m3gGetCulling(M3GPolygonMode handle)
   269 {
   270     PolygonMode *polygonMode = (PolygonMode*)handle;
   271     M3G_VALIDATE_OBJECT(polygonMode);
   272     return polygonMode->cullingMode;
   273 }
   274 
   275 /*!
   276  * \brief Set winding mode.
   277  *
   278  * \param handle        PolygonMode object
   279  * \param mode          winding mode
   280  */
   281 M3G_API void m3gSetWinding(M3GPolygonMode handle, M3Gint mode)
   282 {
   283     PolygonMode *polygonMode = (PolygonMode*)handle;
   284     M3G_VALIDATE_OBJECT(polygonMode);
   285 
   286     if (mode != M3G_WINDING_CCW && mode != M3G_WINDING_CW) {
   287         m3gRaiseError(M3G_INTERFACE(polygonMode), M3G_INVALID_VALUE);
   288     }
   289     else {
   290         polygonMode->windingMode = mode;
   291     }
   292 }
   293 
   294 /*!
   295  * \brief Get winding mode.
   296  *
   297  * \param handle        PolygonMode object
   298  * \return              winding mode
   299  */
   300 M3G_API M3Gint m3gGetWinding(M3GPolygonMode handle)
   301 {
   302     PolygonMode *polygonMode = (PolygonMode*)handle;
   303     M3G_VALIDATE_OBJECT(polygonMode);
   304     return polygonMode->windingMode;
   305 }
   306 
   307 /*!
   308  * \brief Set shading mode.
   309  *
   310  * \param handle        PolygonMode object
   311  * \param mode          shading mode
   312  */
   313 M3G_API void m3gSetShading(M3GPolygonMode handle, M3Gint mode)
   314 {
   315     PolygonMode *polygonMode = (PolygonMode*)handle;
   316     M3G_VALIDATE_OBJECT(polygonMode);
   317 
   318     if (mode != M3G_SHADE_FLAT && mode != M3G_SHADE_SMOOTH) {
   319         m3gRaiseError(M3G_INTERFACE(polygonMode), M3G_INVALID_VALUE);
   320     }
   321     else {
   322         polygonMode->shadingMode = mode;
   323     }
   324 }
   325 
   326 /*!
   327  * \brief Get shading mode.
   328  *
   329  * \param handle        PolygonMode object
   330  * \return              shading mode
   331  */
   332 M3G_API M3Gint m3gGetShading(M3GPolygonMode handle)
   333 {
   334     PolygonMode *polygonMode = (PolygonMode*)handle;
   335     M3G_VALIDATE_OBJECT(polygonMode);
   336     return polygonMode->shadingMode;
   337 }
   338 
   339 /*!
   340  * \brief Set two sided lighting.
   341  *
   342  * \param handle        PolygonMode object
   343  * \param enable        enable flag
   344  */
   345 M3G_API void m3gSetTwoSidedLightingEnable(M3GPolygonMode handle,
   346                                           M3Gbool enable)
   347 {
   348     PolygonMode *polygonMode = (PolygonMode*)handle;
   349     M3G_VALIDATE_OBJECT(polygonMode);
   350     polygonMode->enableTwoSidedLighting = (GLboolean) enable;
   351 }
   352 
   353 /*!
   354  * \brief Get two sided lighting.
   355  *
   356  * \param handle        PolygonMode object
   357  * \retval M3G_TRUE     enabled
   358  * \retval M3G_FALSE    disabled
   359  */
   360 M3G_API M3Gbool m3gIsTwoSidedLightingEnabled(M3GPolygonMode handle)
   361 {
   362     PolygonMode *polygonMode = (PolygonMode*)handle;
   363     M3G_VALIDATE_OBJECT(polygonMode);
   364     return (M3Gbool) polygonMode->enableTwoSidedLighting;
   365 }
   366 
   367 /*!
   368  * \brief Get local camera lighting.
   369  *
   370  * \param handle        PolygonMode object
   371  * \retval M3G_TRUE     enabled
   372  * \retval M3G_FALSE    disabled
   373  */
   374 
   375 M3G_API M3Gbool m3gIsLocalCameraLightingEnabled(M3GPolygonMode handle)
   376 {
   377     PolygonMode *polygonMode = (PolygonMode*)handle;
   378     M3G_VALIDATE_OBJECT(polygonMode);
   379     return (M3Gbool) polygonMode->enableLocalCameraLighting;
   380 }
   381 
   382 /*!
   383  * \brief Get perspective correction.
   384  *
   385  * \param handle        PolygonMode object
   386  * \retval M3G_TRUE     enabled
   387  * \retval M3G_FALSE    disabled
   388  */
   389 M3G_API M3Gbool m3gIsPerspectiveCorrectionEnabled(M3GPolygonMode handle)
   390 {
   391     PolygonMode *polygonMode = (PolygonMode*)handle;
   392     M3G_VALIDATE_OBJECT(polygonMode);
   393     return (M3Gbool) polygonMode->enablePerspectiveCorrection;
   394 }
   395 
   396 /*
   397  * Uncomment these lines' opening pair at the begining of the file
   398  * if you want to switch tracing on for this file.
   399  */
   400 #ifdef M3G_LOCAL_TRACEF_ON
   401 #undef M3G_LOCAL_TRACEF_ON
   402 #endif
   403