sl@0: /* sl@0: * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: Fog implementation sl@0: * sl@0: */ sl@0: sl@0: sl@0: /*! sl@0: * \internal sl@0: * \file sl@0: * \brief Fog implementation sl@0: */ sl@0: sl@0: #ifndef M3G_CORE_INCLUDE sl@0: # error included by m3g_core.c; do not compile separately. sl@0: #endif sl@0: sl@0: #include "m3g_fog.h" sl@0: #include "m3g_animationtrack.h" sl@0: sl@0: /*---------------------------------------------------------------------- sl@0: * Internal functions sl@0: *--------------------------------------------------------------------*/ sl@0: sl@0: /*! sl@0: * \internal sl@0: * \brief Destroys this Fog object. sl@0: * sl@0: * \param obj Fog object sl@0: */ sl@0: static void m3gDestroyFog(Object *obj) sl@0: { sl@0: Fog *fog = (Fog *) obj; sl@0: M3G_VALIDATE_OBJECT(fog); sl@0: sl@0: m3gDestroyObject(&fog->object); sl@0: } sl@0: sl@0: /*! sl@0: * \internal sl@0: * \brief Overloaded Object3D method. sl@0: * sl@0: * \param property animation property sl@0: * \retval M3G_TRUE property supported sl@0: * \retval M3G_FALSE property not supported sl@0: */ sl@0: static M3Gbool m3gFogIsCompatible(M3Gint property) sl@0: { sl@0: switch (property) { sl@0: case M3G_ANIM_COLOR: sl@0: case M3G_ANIM_DENSITY: sl@0: case M3G_ANIM_FAR_DISTANCE: sl@0: case M3G_ANIM_NEAR_DISTANCE: sl@0: return M3G_TRUE; sl@0: default: sl@0: return m3gObjectIsCompatible(property); sl@0: } sl@0: } sl@0: sl@0: /*! sl@0: * \internal sl@0: * \brief Overloaded Object3D method. sl@0: * sl@0: * \param self Fog object sl@0: * \param property animation property sl@0: * \param valueSize size of value array sl@0: * \param value value array sl@0: */ sl@0: static void m3gFogUpdateProperty(Object *self, sl@0: M3Gint property, sl@0: M3Gint valueSize, sl@0: const M3Gfloat *value) sl@0: { sl@0: Fog *fog = (Fog *)self; sl@0: M3G_VALIDATE_OBJECT(fog); sl@0: M3G_ASSERT_PTR(value); sl@0: sl@0: switch (property) { sl@0: case M3G_ANIM_COLOR: sl@0: M3G_ASSERT(valueSize >= 3); sl@0: fog->color = m3gColor3f(value[0], value[1], value[2]) & M3G_RGB_MASK; sl@0: break; sl@0: case M3G_ANIM_DENSITY: sl@0: M3G_ASSERT(valueSize >= 1); sl@0: fog->density = (value[0] < 0.f) ? 0.f : value[0]; sl@0: break; sl@0: case M3G_ANIM_FAR_DISTANCE: sl@0: M3G_ASSERT(valueSize >= 1); sl@0: fog->end = value[0]; sl@0: break; sl@0: case M3G_ANIM_NEAR_DISTANCE: sl@0: M3G_ASSERT(valueSize >= 1); sl@0: fog->start = value[0]; sl@0: break; sl@0: default: sl@0: m3gObjectUpdateProperty(self, property, valueSize, value); sl@0: } sl@0: } sl@0: sl@0: /*! sl@0: * \internal sl@0: * \brief Overloaded Object3D method. sl@0: * sl@0: * \param originalObj original Fog object sl@0: * \param cloneObj pointer to cloned Fog object sl@0: * \param pairs array for all object-duplicate pairs sl@0: * \param numPairs number of pairs sl@0: */ sl@0: static M3Gbool m3gFogDuplicate(const Object *originalObj, sl@0: Object **cloneObj, sl@0: Object **pairs, sl@0: M3Gint *numPairs) sl@0: { sl@0: Fog *original = (Fog *)originalObj; sl@0: Fog *clone = (Fog *)m3gCreateFog(originalObj->interface); sl@0: *cloneObj = (Object *)clone; sl@0: if (*cloneObj == NULL) { sl@0: return M3G_FALSE; sl@0: } sl@0: sl@0: if(m3gObjectDuplicate(originalObj, cloneObj, pairs, numPairs)) { sl@0: clone->color = original->color; sl@0: clone->density = original->density; sl@0: clone->start = original->start; sl@0: clone->end = original->end; sl@0: clone->mode = original->mode; sl@0: return M3G_TRUE; sl@0: } sl@0: else { sl@0: return M3G_FALSE; sl@0: } sl@0: } sl@0: sl@0: /*! sl@0: * \internal sl@0: * \brief Initializes a Fog object. See specification sl@0: * for default values. sl@0: * sl@0: * \param m3g M3G interface sl@0: * \param fog Fog object sl@0: */ sl@0: static void m3gInitFog(Interface *m3g, Fog *fog) sl@0: { sl@0: /* Fog is derived from object */ sl@0: m3gInitObject(&fog->object, m3g, M3G_CLASS_FOG); sl@0: sl@0: fog->density = 1.0f; sl@0: fog->start = 0.0f; sl@0: fog->end = 1.0f; sl@0: fog->mode = M3G_LINEAR_FOG; sl@0: } sl@0: sl@0: /*! sl@0: * \internal sl@0: * \brief Applies fog to OpenGL. This is used for sl@0: * all Mesh objects. sl@0: * sl@0: * \param self Fog object sl@0: */ sl@0: static void m3gApplyFog(const Fog *self) sl@0: { sl@0: if (self != NULL) { sl@0: GLfixed temp[4]; sl@0: sl@0: m3gGLColor(self->color, temp); sl@0: sl@0: switch (self->mode) { sl@0: case M3G_LINEAR_FOG: sl@0: glEnable(GL_FOG); sl@0: glFogf(GL_FOG_MODE, GL_LINEAR); sl@0: glFogf(GL_FOG_START, self->start); sl@0: glFogf(GL_FOG_END, self->end); sl@0: glFogxv(GL_FOG_COLOR, temp); sl@0: break; sl@0: case M3G_EXPONENTIAL_FOG: sl@0: glEnable(GL_FOG); sl@0: glFogf(GL_FOG_MODE, GL_EXP); sl@0: glFogf(GL_FOG_DENSITY, self->density); sl@0: glFogxv(GL_FOG_COLOR, temp); sl@0: break; sl@0: } sl@0: } sl@0: else { sl@0: glDisable(GL_FOG); sl@0: } sl@0: M3G_ASSERT_GL; sl@0: } sl@0: sl@0: #ifdef M3G_USE_NGL_API sl@0: /*! sl@0: * \internal sl@0: * \brief Applies fog to NGL. This is used for sl@0: * Sprite3D objects only. sl@0: * sl@0: * \param self Fog object sl@0: * \param eyeZ Eye space Z (e.g. after modelview) sl@0: * \param finalZ Final Z (e.g. after modelview and projection) sl@0: */ sl@0: static void m3gApplySpriteFog(const Fog *self, M3Gfloat eyeZ, M3Gfloat finalZ) sl@0: { sl@0: if(self != NULL) { sl@0: M3Gint temp[4]; sl@0: M3Gfloat fogValue = 1; sl@0: sl@0: /* Calculate fog value and use OpenGL linear fog sl@0: * to result in same value. Sprites are drawn with sl@0: * identity MV and P and therefore the fog has to sl@0: * be adjusted like this */ sl@0: switch (self->mode) { sl@0: case M3G_LINEAR_FOG: sl@0: fogValue = m3gDiv(m3gAdd(self->end, eyeZ), m3gSub(self->end, self->start)); sl@0: break; sl@0: case M3G_EXPONENTIAL_FOG: sl@0: fogValue = m3gExp(m3gMul(self->density, eyeZ)); sl@0: break; sl@0: default: sl@0: M3G_ASSERT(M3G_FALSE); sl@0: break; sl@0: } sl@0: sl@0: m3gGLColor(self->color, temp); sl@0: sl@0: glEnable(GL_FOG); sl@0: glFogf(GL_FOG_MODE, GL_LINEAR); sl@0: sl@0: /* NGL works differently in fog calculation */ sl@0: glFogf(GL_FOG_START, -m3gDiv(finalZ, fogValue)); sl@0: glFogf(GL_FOG_END, 0.f); sl@0: glFogxv(GL_FOG_COLOR, temp); sl@0: } sl@0: else { sl@0: glDisable(GL_FOG); sl@0: } sl@0: } sl@0: #endif sl@0: /*---------------------------------------------------------------------- sl@0: * Virtual function table sl@0: *--------------------------------------------------------------------*/ sl@0: sl@0: static const ObjectVFTable m3gvf_Fog = { sl@0: m3gObjectApplyAnimation, sl@0: m3gFogIsCompatible, sl@0: m3gFogUpdateProperty, sl@0: m3gObjectDoGetReferences, sl@0: m3gObjectFindID, sl@0: m3gFogDuplicate, sl@0: m3gDestroyFog sl@0: }; sl@0: sl@0: sl@0: /*---------------------------------------------------------------------- sl@0: * Public API functions sl@0: *--------------------------------------------------------------------*/ sl@0: sl@0: /*! sl@0: * \brief Creates a Fog object. sl@0: * sl@0: * \param interface M3G interface sl@0: * \retval Fog new Fog object sl@0: * \retval NULL Fog creating failed sl@0: */ sl@0: M3G_API M3GFog m3gCreateFog(M3GInterface interface) sl@0: { sl@0: Interface *m3g = (Interface *) interface; sl@0: M3G_VALIDATE_INTERFACE(m3g); sl@0: sl@0: { sl@0: Fog *fog = m3gAllocZ(m3g, sizeof(Fog)); sl@0: sl@0: if (fog != NULL) { sl@0: m3gInitFog(m3g, fog); sl@0: } sl@0: sl@0: return (M3GFog) fog; sl@0: } sl@0: } sl@0: sl@0: /*! sl@0: * \brief Sets fog mode. sl@0: * sl@0: * \param handle Fog object sl@0: * \param mode fog mode sl@0: */ sl@0: M3G_API void m3gSetFogMode(M3GFog handle, M3Gint mode) sl@0: { sl@0: Fog *fog = (Fog *) handle; sl@0: M3G_VALIDATE_OBJECT(fog); sl@0: sl@0: /* Check for errors */ sl@0: if(mode < M3G_EXPONENTIAL_FOG || mode > M3G_LINEAR_FOG) { sl@0: m3gRaiseError(M3G_INTERFACE(fog), M3G_INVALID_VALUE); sl@0: return; sl@0: } sl@0: sl@0: fog->mode = mode; sl@0: } sl@0: sl@0: /*! sl@0: * \brief Gets fog mode. sl@0: * sl@0: * \param handle Fog object sl@0: * \return fog mode sl@0: */ sl@0: M3G_API M3Gint m3gGetFogMode(M3GFog handle) sl@0: { sl@0: Fog *fog = (Fog *) handle; sl@0: M3G_VALIDATE_OBJECT(fog); sl@0: sl@0: return fog->mode; sl@0: } sl@0: sl@0: /*! sl@0: * \brief Sets linear fog parameters. sl@0: * sl@0: * \param handle Fog object sl@0: * \param fogNear near distance sl@0: * \param fogFar far distance sl@0: */ sl@0: M3G_API void m3gSetFogLinear(M3GFog handle, M3Gfloat fogNear, M3Gfloat fogFar) sl@0: { sl@0: Fog *fog = (Fog *) handle; sl@0: M3G_VALIDATE_OBJECT(fog); sl@0: sl@0: fog->start = fogNear; sl@0: fog->end = fogFar; sl@0: } sl@0: sl@0: /*! sl@0: * \brief Gets linear fog parameters. sl@0: * sl@0: * \param handle Fog object sl@0: * \param which which parameter to return sl@0: * \arg M3G_GET_NEAR sl@0: * \arg M3G_GET_FAR sl@0: * \return near or far distance sl@0: */ sl@0: M3G_API M3Gfloat m3gGetFogDistance(M3GFog handle, M3Gint which) sl@0: { sl@0: Fog *fog = (Fog *) handle; sl@0: M3G_VALIDATE_OBJECT(fog); sl@0: sl@0: switch(which) { sl@0: case M3G_GET_NEAR: sl@0: return fog->start; sl@0: case M3G_GET_FAR: sl@0: default: sl@0: return fog->end; sl@0: } sl@0: } sl@0: sl@0: /*! sl@0: * \brief Sets exponential fog density. sl@0: * sl@0: * \param handle Fog object sl@0: * \param density fog density sl@0: */ sl@0: M3G_API void m3gSetFogDensity(M3GFog handle, M3Gfloat density) sl@0: { sl@0: Fog *fog = (Fog *) handle; sl@0: M3G_VALIDATE_OBJECT(fog); sl@0: sl@0: if(density < 0.f) { sl@0: m3gRaiseError(M3G_INTERFACE(fog), M3G_INVALID_VALUE); sl@0: return; sl@0: } sl@0: sl@0: fog->density = density; sl@0: } sl@0: sl@0: /*! sl@0: * \brief Gets exponential fog density. sl@0: * sl@0: * \param handle Fog object sl@0: * \return fog density sl@0: */ sl@0: M3G_API M3Gfloat m3gGetFogDensity(M3GFog handle) sl@0: { sl@0: Fog *fog = (Fog *) handle; sl@0: M3G_VALIDATE_OBJECT(fog); sl@0: sl@0: return fog->density; sl@0: } sl@0: sl@0: /*! sl@0: * \brief Sets fog color as RGB. sl@0: * sl@0: * \param handle Fog object sl@0: * \param rgb fog color as RGB sl@0: */ sl@0: M3G_API void m3gSetFogColor(M3GFog handle, M3Guint rgb) sl@0: { sl@0: Fog *fog = (Fog *) handle; sl@0: M3G_VALIDATE_OBJECT(fog); sl@0: sl@0: fog->color = rgb & M3G_RGB_MASK; sl@0: } sl@0: sl@0: /*! sl@0: * \brief Gets fog color as RGB. sl@0: * sl@0: * \param handle Fog object sl@0: * \return fog color as RGB sl@0: */ sl@0: M3G_API M3Guint m3gGetFogColor(M3GFog handle) sl@0: { sl@0: Fog *fog = (Fog *) handle; sl@0: M3G_VALIDATE_OBJECT(fog); sl@0: sl@0: return fog->color; sl@0: } sl@0: