1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/graphics/m3g/m3gcore11/src/m3g_polygonmode.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,403 @@
1.4 +/*
1.5 +* Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description: PolygonMode implementation
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +/*!
1.23 + * \internal
1.24 + * \file
1.25 + * \brief PolygonMode implementation
1.26 + */
1.27 +
1.28 +#ifndef M3G_CORE_INCLUDE
1.29 +# error included by m3g_core.c; do not compile separately.
1.30 +#endif
1.31 +
1.32 +/*
1.33 + * Uncomment this line to switch tracing on for this file's functions
1.34 + */
1.35 +/* #define M3G_LOCAL_TRACEF_ON */
1.36 +
1.37 +#include "m3g_polygonmode.h"
1.38 +
1.39 +/*----------------------------------------------------------------------
1.40 + * Internal functions
1.41 + *--------------------------------------------------------------------*/
1.42 +
1.43 +/*!
1.44 + * \internal
1.45 + * \brief Applies default polygon mode to OpenGL.
1.46 + */
1.47 +static void m3gApplyDefaultPolygonMode()
1.48 +{
1.49 + /* cull = BACK */
1.50 + glCullFace(GL_BACK);
1.51 + glEnable(GL_CULL_FACE);
1.52 +
1.53 + /* shading = M3G_SHADE_SMOOTH */
1.54 + glShadeModel(GL_SMOOTH);
1.55 +
1.56 + /* winding = M3G_WINDING_CCW */
1.57 + glFrontFace(GL_CCW);
1.58 +
1.59 + /* perspective correction = false */
1.60 + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
1.61 +
1.62 + /* lighting = ONE_SIDED */
1.63 + glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
1.64 +}
1.65 +
1.66 +/*!
1.67 + * \internal
1.68 + * \brief Applies polygon mode to OpenGL.
1.69 + *
1.70 + * \param polygonMode PolygonMode object
1.71 + */
1.72 +static void m3gApplyPolygonMode(PolygonMode *polygonMode)
1.73 +{
1.74 + if (polygonMode != NULL) {
1.75 + if (polygonMode->cullingMode == M3G_CULL_NONE) {
1.76 + glDisable(GL_CULL_FACE);
1.77 + }
1.78 + else {
1.79 + if (polygonMode->cullingMode == M3G_CULL_BACK) {
1.80 + glCullFace(GL_BACK);
1.81 + }
1.82 + else {
1.83 + glCullFace(GL_FRONT);
1.84 + }
1.85 + glEnable(GL_CULL_FACE);
1.86 + }
1.87 +
1.88 + if (m3gGetTwoSidedLightingWorkaround(M3G_INTERFACE(polygonMode))) {
1.89 + glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
1.90 + }
1.91 + else {
1.92 + glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, polygonMode->enableTwoSidedLighting);
1.93 + }
1.94 +
1.95 + if (polygonMode->shadingMode == M3G_SHADE_FLAT) {
1.96 + glShadeModel(GL_FLAT);
1.97 + }
1.98 + else {
1.99 + glShadeModel(GL_SMOOTH);
1.100 + }
1.101 +
1.102 + if (polygonMode->windingMode == M3G_WINDING_CW) {
1.103 + glFrontFace(GL_CW);
1.104 + }
1.105 + else {
1.106 + glFrontFace(GL_CCW);
1.107 + }
1.108 +
1.109 + if (polygonMode->enablePerspectiveCorrection == GL_TRUE) {
1.110 + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
1.111 + }
1.112 + else {
1.113 + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
1.114 + }
1.115 + }
1.116 + else {
1.117 + m3gApplyDefaultPolygonMode();
1.118 + }
1.119 + M3G_ASSERT_GL;
1.120 +
1.121 +}
1.122 +
1.123 +/*!
1.124 + * \internal
1.125 + * \brief Overloaded Object3D method
1.126 + *
1.127 + * \param originalObj original PolygonMode object
1.128 + * \param cloneObj pointer to cloned PolygonMode object
1.129 + * \param pairs array for all object-duplicate pairs
1.130 + * \param numPairs number of pairs
1.131 + */
1.132 +static M3Gbool m3gPolygonModeDuplicate(const Object *originalObj,
1.133 + Object **cloneObj,
1.134 + Object **pairs,
1.135 + M3Gint *numPairs)
1.136 +{
1.137 + PolygonMode *original = (PolygonMode *)originalObj;
1.138 + PolygonMode *clone;
1.139 + M3G_ASSERT(*cloneObj == NULL); /* no derived classes */
1.140 +
1.141 + /* Create the clone object */
1.142 +
1.143 + clone = (PolygonMode *)m3gCreatePolygonMode(originalObj->interface);
1.144 + if (!clone) {
1.145 + return M3G_FALSE;
1.146 + }
1.147 + *cloneObj = (Object *)clone;
1.148 +
1.149 + /* Duplicate base class data */
1.150 +
1.151 + if (!m3gObjectDuplicate(originalObj, cloneObj, pairs, numPairs)) {
1.152 + return M3G_FALSE;
1.153 + }
1.154 +
1.155 + /* Duplicate our own data */
1.156 +
1.157 + clone->enableLocalCameraLighting = original->enableLocalCameraLighting;
1.158 + clone->enablePerspectiveCorrection = original->enablePerspectiveCorrection;
1.159 + clone->cullingMode = original->cullingMode;
1.160 + clone->windingMode = original->windingMode;
1.161 + clone->shadingMode = original->shadingMode;
1.162 + clone->enableTwoSidedLighting = original->enableTwoSidedLighting;
1.163 +
1.164 + return M3G_TRUE;
1.165 +}
1.166 +
1.167 +/*----------------------------------------------------------------------
1.168 + * Virtual function table
1.169 + *--------------------------------------------------------------------*/
1.170 +
1.171 +static const ObjectVFTable m3gvf_PolygonMode = {
1.172 + m3gObjectApplyAnimation,
1.173 + m3gObjectIsCompatible,
1.174 + m3gObjectUpdateProperty,
1.175 + m3gObjectDoGetReferences,
1.176 + m3gObjectFindID,
1.177 + m3gPolygonModeDuplicate,
1.178 + m3gDestroyObject
1.179 +};
1.180 +
1.181 +
1.182 +/*----------------------------------------------------------------------
1.183 + * Public API functions
1.184 + *--------------------------------------------------------------------*/
1.185 +
1.186 +/*!
1.187 + * \brief Creates a new PolygonMode object.
1.188 + *
1.189 + * \param interface M3G interface object
1.190 + * \retval PolygonMode new PolygonMode object
1.191 + * \retval NULL PolygonMode creating failed
1.192 + */
1.193 +M3G_API M3GPolygonMode m3gCreatePolygonMode(M3GInterface interface)
1.194 +{
1.195 + Interface *m3g = (Interface *) interface;
1.196 + M3G_VALIDATE_INTERFACE(m3g);
1.197 +
1.198 + {
1.199 + PolygonMode *polygonMode = m3gAllocZ(m3g, sizeof(PolygonMode));
1.200 +
1.201 + if (polygonMode != NULL) {
1.202 + m3gInitObject(&polygonMode->object, m3g, M3G_CLASS_POLYGON_MODE);
1.203 + polygonMode->enableLocalCameraLighting = GL_FALSE;
1.204 + polygonMode->enablePerspectiveCorrection = GL_FALSE;
1.205 + polygonMode->cullingMode = M3G_CULL_BACK;
1.206 + polygonMode->windingMode = M3G_WINDING_CCW;
1.207 + polygonMode->shadingMode = M3G_SHADE_SMOOTH;
1.208 + polygonMode->enableTwoSidedLighting = GL_FALSE;
1.209 + }
1.210 +
1.211 + return (M3GPolygonMode)polygonMode;
1.212 + }
1.213 +}
1.214 +
1.215 +/*!
1.216 + * \brief Set local camera lightning.
1.217 + *
1.218 + * \param handle PolygonMode object
1.219 + * \param enable enable flag
1.220 + */
1.221 +M3G_API void m3gSetLocalCameraLightingEnable(M3GPolygonMode handle,
1.222 + M3Gbool enable)
1.223 +{
1.224 + PolygonMode *polygonMode = (PolygonMode*)handle;
1.225 + M3G_VALIDATE_OBJECT(polygonMode);
1.226 + polygonMode->enableLocalCameraLighting = (GLboolean) enable;
1.227 +}
1.228 +
1.229 +/*!
1.230 + * \brief Set perspective correction.
1.231 + *
1.232 + * \param handle PolygonMode object
1.233 + * \param enable enable flag
1.234 + */
1.235 +void m3gSetPerspectiveCorrectionEnable(M3GPolygonMode handle, M3Gbool enable)
1.236 +{
1.237 + PolygonMode *polygonMode = (PolygonMode*)handle;
1.238 + M3G_VALIDATE_OBJECT(polygonMode);
1.239 + polygonMode->enablePerspectiveCorrection = (GLboolean) enable;
1.240 +}
1.241 +
1.242 +/*!
1.243 + * \brief Set culling mode.
1.244 + *
1.245 + * \param handle PolygonMode object
1.246 + * \param mode culling mode
1.247 + */
1.248 +void m3gSetCulling(M3GPolygonMode handle, M3Gint mode)
1.249 +{
1.250 + PolygonMode *polygonMode = (PolygonMode*)handle;
1.251 + M3G_VALIDATE_OBJECT(polygonMode);
1.252 +
1.253 + switch (mode) {
1.254 + case M3G_CULL_BACK:
1.255 + case M3G_CULL_FRONT:
1.256 + case M3G_CULL_NONE:
1.257 + polygonMode->cullingMode = mode;
1.258 + break;
1.259 + default:
1.260 + m3gRaiseError(M3G_INTERFACE(polygonMode), M3G_INVALID_VALUE);
1.261 + break;
1.262 + }
1.263 +}
1.264 +
1.265 +/*!
1.266 + * \brief Get culling mode.
1.267 + *
1.268 + * \param handle PolygonMode object
1.269 + * \return culling mode
1.270 + */
1.271 +M3G_API M3Gint m3gGetCulling(M3GPolygonMode handle)
1.272 +{
1.273 + PolygonMode *polygonMode = (PolygonMode*)handle;
1.274 + M3G_VALIDATE_OBJECT(polygonMode);
1.275 + return polygonMode->cullingMode;
1.276 +}
1.277 +
1.278 +/*!
1.279 + * \brief Set winding mode.
1.280 + *
1.281 + * \param handle PolygonMode object
1.282 + * \param mode winding mode
1.283 + */
1.284 +M3G_API void m3gSetWinding(M3GPolygonMode handle, M3Gint mode)
1.285 +{
1.286 + PolygonMode *polygonMode = (PolygonMode*)handle;
1.287 + M3G_VALIDATE_OBJECT(polygonMode);
1.288 +
1.289 + if (mode != M3G_WINDING_CCW && mode != M3G_WINDING_CW) {
1.290 + m3gRaiseError(M3G_INTERFACE(polygonMode), M3G_INVALID_VALUE);
1.291 + }
1.292 + else {
1.293 + polygonMode->windingMode = mode;
1.294 + }
1.295 +}
1.296 +
1.297 +/*!
1.298 + * \brief Get winding mode.
1.299 + *
1.300 + * \param handle PolygonMode object
1.301 + * \return winding mode
1.302 + */
1.303 +M3G_API M3Gint m3gGetWinding(M3GPolygonMode handle)
1.304 +{
1.305 + PolygonMode *polygonMode = (PolygonMode*)handle;
1.306 + M3G_VALIDATE_OBJECT(polygonMode);
1.307 + return polygonMode->windingMode;
1.308 +}
1.309 +
1.310 +/*!
1.311 + * \brief Set shading mode.
1.312 + *
1.313 + * \param handle PolygonMode object
1.314 + * \param mode shading mode
1.315 + */
1.316 +M3G_API void m3gSetShading(M3GPolygonMode handle, M3Gint mode)
1.317 +{
1.318 + PolygonMode *polygonMode = (PolygonMode*)handle;
1.319 + M3G_VALIDATE_OBJECT(polygonMode);
1.320 +
1.321 + if (mode != M3G_SHADE_FLAT && mode != M3G_SHADE_SMOOTH) {
1.322 + m3gRaiseError(M3G_INTERFACE(polygonMode), M3G_INVALID_VALUE);
1.323 + }
1.324 + else {
1.325 + polygonMode->shadingMode = mode;
1.326 + }
1.327 +}
1.328 +
1.329 +/*!
1.330 + * \brief Get shading mode.
1.331 + *
1.332 + * \param handle PolygonMode object
1.333 + * \return shading mode
1.334 + */
1.335 +M3G_API M3Gint m3gGetShading(M3GPolygonMode handle)
1.336 +{
1.337 + PolygonMode *polygonMode = (PolygonMode*)handle;
1.338 + M3G_VALIDATE_OBJECT(polygonMode);
1.339 + return polygonMode->shadingMode;
1.340 +}
1.341 +
1.342 +/*!
1.343 + * \brief Set two sided lighting.
1.344 + *
1.345 + * \param handle PolygonMode object
1.346 + * \param enable enable flag
1.347 + */
1.348 +M3G_API void m3gSetTwoSidedLightingEnable(M3GPolygonMode handle,
1.349 + M3Gbool enable)
1.350 +{
1.351 + PolygonMode *polygonMode = (PolygonMode*)handle;
1.352 + M3G_VALIDATE_OBJECT(polygonMode);
1.353 + polygonMode->enableTwoSidedLighting = (GLboolean) enable;
1.354 +}
1.355 +
1.356 +/*!
1.357 + * \brief Get two sided lighting.
1.358 + *
1.359 + * \param handle PolygonMode object
1.360 + * \retval M3G_TRUE enabled
1.361 + * \retval M3G_FALSE disabled
1.362 + */
1.363 +M3G_API M3Gbool m3gIsTwoSidedLightingEnabled(M3GPolygonMode handle)
1.364 +{
1.365 + PolygonMode *polygonMode = (PolygonMode*)handle;
1.366 + M3G_VALIDATE_OBJECT(polygonMode);
1.367 + return (M3Gbool) polygonMode->enableTwoSidedLighting;
1.368 +}
1.369 +
1.370 +/*!
1.371 + * \brief Get local camera lighting.
1.372 + *
1.373 + * \param handle PolygonMode object
1.374 + * \retval M3G_TRUE enabled
1.375 + * \retval M3G_FALSE disabled
1.376 + */
1.377 +
1.378 +M3G_API M3Gbool m3gIsLocalCameraLightingEnabled(M3GPolygonMode handle)
1.379 +{
1.380 + PolygonMode *polygonMode = (PolygonMode*)handle;
1.381 + M3G_VALIDATE_OBJECT(polygonMode);
1.382 + return (M3Gbool) polygonMode->enableLocalCameraLighting;
1.383 +}
1.384 +
1.385 +/*!
1.386 + * \brief Get perspective correction.
1.387 + *
1.388 + * \param handle PolygonMode object
1.389 + * \retval M3G_TRUE enabled
1.390 + * \retval M3G_FALSE disabled
1.391 + */
1.392 +M3G_API M3Gbool m3gIsPerspectiveCorrectionEnabled(M3GPolygonMode handle)
1.393 +{
1.394 + PolygonMode *polygonMode = (PolygonMode*)handle;
1.395 + M3G_VALIDATE_OBJECT(polygonMode);
1.396 + return (M3Gbool) polygonMode->enablePerspectiveCorrection;
1.397 +}
1.398 +
1.399 +/*
1.400 + * Uncomment these lines' opening pair at the begining of the file
1.401 + * if you want to switch tracing on for this file.
1.402 + */
1.403 +#ifdef M3G_LOCAL_TRACEF_ON
1.404 +#undef M3G_LOCAL_TRACEF_ON
1.405 +#endif
1.406 +