Update contrib.
2 * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
14 * Description: PolygonMode implementation
22 * \brief PolygonMode implementation
25 #ifndef M3G_CORE_INCLUDE
26 # error included by m3g_core.c; do not compile separately.
30 * Uncomment this line to switch tracing on for this file's functions
32 /* #define M3G_LOCAL_TRACEF_ON */
34 #include "m3g_polygonmode.h"
36 /*----------------------------------------------------------------------
38 *--------------------------------------------------------------------*/
42 * \brief Applies default polygon mode to OpenGL.
44 static void m3gApplyDefaultPolygonMode()
48 glEnable(GL_CULL_FACE);
50 /* shading = M3G_SHADE_SMOOTH */
51 glShadeModel(GL_SMOOTH);
53 /* winding = M3G_WINDING_CCW */
56 /* perspective correction = false */
57 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
59 /* lighting = ONE_SIDED */
60 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
65 * \brief Applies polygon mode to OpenGL.
67 * \param polygonMode PolygonMode object
69 static void m3gApplyPolygonMode(PolygonMode *polygonMode)
71 if (polygonMode != NULL) {
72 if (polygonMode->cullingMode == M3G_CULL_NONE) {
73 glDisable(GL_CULL_FACE);
76 if (polygonMode->cullingMode == M3G_CULL_BACK) {
82 glEnable(GL_CULL_FACE);
85 if (m3gGetTwoSidedLightingWorkaround(M3G_INTERFACE(polygonMode))) {
86 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
89 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, polygonMode->enableTwoSidedLighting);
92 if (polygonMode->shadingMode == M3G_SHADE_FLAT) {
93 glShadeModel(GL_FLAT);
96 glShadeModel(GL_SMOOTH);
99 if (polygonMode->windingMode == M3G_WINDING_CW) {
106 if (polygonMode->enablePerspectiveCorrection == GL_TRUE) {
107 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
110 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
114 m3gApplyDefaultPolygonMode();
122 * \brief Overloaded Object3D method
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
129 static M3Gbool m3gPolygonModeDuplicate(const Object *originalObj,
134 PolygonMode *original = (PolygonMode *)originalObj;
136 M3G_ASSERT(*cloneObj == NULL); /* no derived classes */
138 /* Create the clone object */
140 clone = (PolygonMode *)m3gCreatePolygonMode(originalObj->interface);
144 *cloneObj = (Object *)clone;
146 /* Duplicate base class data */
148 if (!m3gObjectDuplicate(originalObj, cloneObj, pairs, numPairs)) {
152 /* Duplicate our own data */
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;
164 /*----------------------------------------------------------------------
165 * Virtual function table
166 *--------------------------------------------------------------------*/
168 static const ObjectVFTable m3gvf_PolygonMode = {
169 m3gObjectApplyAnimation,
170 m3gObjectIsCompatible,
171 m3gObjectUpdateProperty,
172 m3gObjectDoGetReferences,
174 m3gPolygonModeDuplicate,
179 /*----------------------------------------------------------------------
180 * Public API functions
181 *--------------------------------------------------------------------*/
184 * \brief Creates a new PolygonMode object.
186 * \param interface M3G interface object
187 * \retval PolygonMode new PolygonMode object
188 * \retval NULL PolygonMode creating failed
190 M3G_API M3GPolygonMode m3gCreatePolygonMode(M3GInterface interface)
192 Interface *m3g = (Interface *) interface;
193 M3G_VALIDATE_INTERFACE(m3g);
196 PolygonMode *polygonMode = m3gAllocZ(m3g, sizeof(PolygonMode));
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;
208 return (M3GPolygonMode)polygonMode;
213 * \brief Set local camera lightning.
215 * \param handle PolygonMode object
216 * \param enable enable flag
218 M3G_API void m3gSetLocalCameraLightingEnable(M3GPolygonMode handle,
221 PolygonMode *polygonMode = (PolygonMode*)handle;
222 M3G_VALIDATE_OBJECT(polygonMode);
223 polygonMode->enableLocalCameraLighting = (GLboolean) enable;
227 * \brief Set perspective correction.
229 * \param handle PolygonMode object
230 * \param enable enable flag
232 void m3gSetPerspectiveCorrectionEnable(M3GPolygonMode handle, M3Gbool enable)
234 PolygonMode *polygonMode = (PolygonMode*)handle;
235 M3G_VALIDATE_OBJECT(polygonMode);
236 polygonMode->enablePerspectiveCorrection = (GLboolean) enable;
240 * \brief Set culling mode.
242 * \param handle PolygonMode object
243 * \param mode culling mode
245 void m3gSetCulling(M3GPolygonMode handle, M3Gint mode)
247 PolygonMode *polygonMode = (PolygonMode*)handle;
248 M3G_VALIDATE_OBJECT(polygonMode);
254 polygonMode->cullingMode = mode;
257 m3gRaiseError(M3G_INTERFACE(polygonMode), M3G_INVALID_VALUE);
263 * \brief Get culling mode.
265 * \param handle PolygonMode object
266 * \return culling mode
268 M3G_API M3Gint m3gGetCulling(M3GPolygonMode handle)
270 PolygonMode *polygonMode = (PolygonMode*)handle;
271 M3G_VALIDATE_OBJECT(polygonMode);
272 return polygonMode->cullingMode;
276 * \brief Set winding mode.
278 * \param handle PolygonMode object
279 * \param mode winding mode
281 M3G_API void m3gSetWinding(M3GPolygonMode handle, M3Gint mode)
283 PolygonMode *polygonMode = (PolygonMode*)handle;
284 M3G_VALIDATE_OBJECT(polygonMode);
286 if (mode != M3G_WINDING_CCW && mode != M3G_WINDING_CW) {
287 m3gRaiseError(M3G_INTERFACE(polygonMode), M3G_INVALID_VALUE);
290 polygonMode->windingMode = mode;
295 * \brief Get winding mode.
297 * \param handle PolygonMode object
298 * \return winding mode
300 M3G_API M3Gint m3gGetWinding(M3GPolygonMode handle)
302 PolygonMode *polygonMode = (PolygonMode*)handle;
303 M3G_VALIDATE_OBJECT(polygonMode);
304 return polygonMode->windingMode;
308 * \brief Set shading mode.
310 * \param handle PolygonMode object
311 * \param mode shading mode
313 M3G_API void m3gSetShading(M3GPolygonMode handle, M3Gint mode)
315 PolygonMode *polygonMode = (PolygonMode*)handle;
316 M3G_VALIDATE_OBJECT(polygonMode);
318 if (mode != M3G_SHADE_FLAT && mode != M3G_SHADE_SMOOTH) {
319 m3gRaiseError(M3G_INTERFACE(polygonMode), M3G_INVALID_VALUE);
322 polygonMode->shadingMode = mode;
327 * \brief Get shading mode.
329 * \param handle PolygonMode object
330 * \return shading mode
332 M3G_API M3Gint m3gGetShading(M3GPolygonMode handle)
334 PolygonMode *polygonMode = (PolygonMode*)handle;
335 M3G_VALIDATE_OBJECT(polygonMode);
336 return polygonMode->shadingMode;
340 * \brief Set two sided lighting.
342 * \param handle PolygonMode object
343 * \param enable enable flag
345 M3G_API void m3gSetTwoSidedLightingEnable(M3GPolygonMode handle,
348 PolygonMode *polygonMode = (PolygonMode*)handle;
349 M3G_VALIDATE_OBJECT(polygonMode);
350 polygonMode->enableTwoSidedLighting = (GLboolean) enable;
354 * \brief Get two sided lighting.
356 * \param handle PolygonMode object
357 * \retval M3G_TRUE enabled
358 * \retval M3G_FALSE disabled
360 M3G_API M3Gbool m3gIsTwoSidedLightingEnabled(M3GPolygonMode handle)
362 PolygonMode *polygonMode = (PolygonMode*)handle;
363 M3G_VALIDATE_OBJECT(polygonMode);
364 return (M3Gbool) polygonMode->enableTwoSidedLighting;
368 * \brief Get local camera lighting.
370 * \param handle PolygonMode object
371 * \retval M3G_TRUE enabled
372 * \retval M3G_FALSE disabled
375 M3G_API M3Gbool m3gIsLocalCameraLightingEnabled(M3GPolygonMode handle)
377 PolygonMode *polygonMode = (PolygonMode*)handle;
378 M3G_VALIDATE_OBJECT(polygonMode);
379 return (M3Gbool) polygonMode->enableLocalCameraLighting;
383 * \brief Get perspective correction.
385 * \param handle PolygonMode object
386 * \retval M3G_TRUE enabled
387 * \retval M3G_FALSE disabled
389 M3G_API M3Gbool m3gIsPerspectiveCorrectionEnabled(M3GPolygonMode handle)
391 PolygonMode *polygonMode = (PolygonMode*)handle;
392 M3G_VALIDATE_OBJECT(polygonMode);
393 return (M3Gbool) polygonMode->enablePerspectiveCorrection;
397 * Uncomment these lines' opening pair at the begining of the file
398 * if you want to switch tracing on for this file.
400 #ifdef M3G_LOCAL_TRACEF_ON
401 #undef M3G_LOCAL_TRACEF_ON