m3g/m3gcore11/src/m3g_fog.c
changeset 0 5d03bc08d59c
child 18 5e30ef2e26cb
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     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: Fog implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /*!
       
    20  * \internal
       
    21  * \file
       
    22  * \brief Fog implementation
       
    23  */
       
    24 
       
    25 #ifndef M3G_CORE_INCLUDE
       
    26 #   error included by m3g_core.c; do not compile separately.
       
    27 #endif
       
    28 
       
    29 #include "m3g_fog.h"
       
    30 #include "m3g_animationtrack.h"
       
    31 
       
    32 /*----------------------------------------------------------------------
       
    33  * Internal functions
       
    34  *--------------------------------------------------------------------*/
       
    35 
       
    36 /*!
       
    37  * \internal
       
    38  * \brief Destroys this Fog object.
       
    39  *
       
    40  * \param obj Fog object
       
    41  */
       
    42 static void m3gDestroyFog(Object *obj)
       
    43 {
       
    44     Fog *fog = (Fog *) obj;
       
    45     M3G_VALIDATE_OBJECT(fog);
       
    46 
       
    47     m3gDestroyObject(&fog->object);
       
    48 }
       
    49 
       
    50 /*!
       
    51  * \internal
       
    52  * \brief Overloaded Object3D method.
       
    53  *
       
    54  * \param property      animation property
       
    55  * \retval M3G_TRUE     property supported
       
    56  * \retval M3G_FALSE    property not supported
       
    57  */
       
    58 static M3Gbool m3gFogIsCompatible(M3Gint property)
       
    59 {
       
    60     switch (property) {
       
    61     case M3G_ANIM_COLOR:
       
    62     case M3G_ANIM_DENSITY:
       
    63     case M3G_ANIM_FAR_DISTANCE:
       
    64     case M3G_ANIM_NEAR_DISTANCE:
       
    65         return M3G_TRUE;
       
    66     default:
       
    67         return m3gObjectIsCompatible(property);
       
    68     }
       
    69 }
       
    70 
       
    71 /*!
       
    72  * \internal
       
    73  * \brief Overloaded Object3D method.
       
    74  *
       
    75  * \param self          Fog object
       
    76  * \param property      animation property
       
    77  * \param valueSize     size of value array
       
    78  * \param value         value array
       
    79  */
       
    80 static void m3gFogUpdateProperty(Object *self,
       
    81                                  M3Gint property,
       
    82                                  M3Gint valueSize,
       
    83                                  const M3Gfloat *value)
       
    84 {
       
    85     Fog *fog = (Fog *)self;
       
    86     M3G_VALIDATE_OBJECT(fog);
       
    87     M3G_ASSERT_PTR(value);
       
    88 
       
    89     switch (property) {
       
    90     case M3G_ANIM_COLOR:
       
    91         M3G_ASSERT(valueSize >= 3);
       
    92         fog->color = m3gColor3f(value[0], value[1], value[2]) & M3G_RGB_MASK;
       
    93         break;
       
    94     case M3G_ANIM_DENSITY:
       
    95         M3G_ASSERT(valueSize >= 1);
       
    96         fog->density = (value[0] < 0.f) ? 0.f : value[0];
       
    97         break;
       
    98     case M3G_ANIM_FAR_DISTANCE:
       
    99         M3G_ASSERT(valueSize >= 1);
       
   100         fog->end = value[0];
       
   101         break;
       
   102     case M3G_ANIM_NEAR_DISTANCE:
       
   103         M3G_ASSERT(valueSize >= 1);
       
   104         fog->start = value[0];
       
   105         break;
       
   106     default:
       
   107         m3gObjectUpdateProperty(self, property, valueSize, value);
       
   108     }
       
   109 }
       
   110 
       
   111 /*!
       
   112  * \internal
       
   113  * \brief Overloaded Object3D method.
       
   114  *
       
   115  * \param originalObj original Fog object
       
   116  * \param cloneObj pointer to cloned Fog object
       
   117  * \param pairs array for all object-duplicate pairs
       
   118  * \param numPairs number of pairs
       
   119  */
       
   120 static M3Gbool m3gFogDuplicate(const Object *originalObj,
       
   121                                Object **cloneObj,
       
   122                                Object **pairs,
       
   123                                M3Gint *numPairs)
       
   124 {
       
   125     Fog *original = (Fog *)originalObj;
       
   126     Fog *clone = (Fog *)m3gCreateFog(originalObj->interface);
       
   127     *cloneObj = (Object *)clone;
       
   128     if (*cloneObj == NULL) {
       
   129         return M3G_FALSE;
       
   130     }
       
   131 
       
   132     if(m3gObjectDuplicate(originalObj, cloneObj, pairs, numPairs)) {
       
   133         clone->color = original->color;
       
   134         clone->density = original->density;
       
   135         clone->start = original->start;
       
   136         clone->end = original->end;
       
   137         clone->mode = original->mode;
       
   138         return M3G_TRUE;
       
   139     }
       
   140     else {
       
   141         return M3G_FALSE;
       
   142     }
       
   143 }
       
   144 
       
   145 /*!
       
   146  * \internal
       
   147  * \brief Initializes a Fog object. See specification
       
   148  * for default values.
       
   149  *
       
   150  * \param m3g           M3G interface
       
   151  * \param fog           Fog object
       
   152  */
       
   153 static void m3gInitFog(Interface *m3g, Fog *fog)
       
   154 {
       
   155 	/* Fog is derived from object */
       
   156 	m3gInitObject(&fog->object, m3g, M3G_CLASS_FOG);
       
   157 
       
   158 	fog->density = 1.0f;
       
   159 	fog->start = 0.0f;
       
   160 	fog->end = 1.0f;
       
   161 	fog->mode = M3G_LINEAR_FOG;
       
   162 }
       
   163 
       
   164 /*!
       
   165  * \internal
       
   166  * \brief Applies fog to OpenGL. This is used for
       
   167  * all Mesh objects.
       
   168  *
       
   169  * \param self          Fog object
       
   170  */
       
   171 static void m3gApplyFog(const Fog *self)
       
   172 {
       
   173 	if (self != NULL) {
       
   174 		GLfixed temp[4];
       
   175 
       
   176         m3gGLColor(self->color, temp);
       
   177 
       
   178 		switch (self->mode) {
       
   179 		case M3G_LINEAR_FOG:
       
   180 			glEnable(GL_FOG);
       
   181 			glFogf(GL_FOG_MODE, GL_LINEAR);
       
   182 			glFogf(GL_FOG_START, self->start);
       
   183 			glFogf(GL_FOG_END, self->end);
       
   184 			glFogxv(GL_FOG_COLOR, temp);
       
   185 			break;
       
   186 		case M3G_EXPONENTIAL_FOG:
       
   187 			glEnable(GL_FOG);
       
   188 			glFogf(GL_FOG_MODE, GL_EXP);
       
   189 			glFogf(GL_FOG_DENSITY, self->density);
       
   190 			glFogxv(GL_FOG_COLOR, temp);
       
   191 			break;
       
   192 		}
       
   193 	}
       
   194 	else {
       
   195 		glDisable(GL_FOG);
       
   196 	}
       
   197     M3G_ASSERT_GL;
       
   198 }
       
   199 
       
   200 /*!
       
   201  * \internal
       
   202  * \brief Applies fog to OpenGL. This is used for
       
   203  * Sprite3D objects only.
       
   204  *
       
   205  * \param self          Fog object
       
   206  * \param eyeZ          Eye space Z (e.g. after modelview)
       
   207  * \param finalZ        Final Z (e.g. after modelview and projection)
       
   208  */
       
   209 static void m3gApplySpriteFog(const Fog *self, M3Gfloat eyeZ, M3Gfloat finalZ)
       
   210 {
       
   211     if(self != NULL) {
       
   212 		M3Gint temp[4];
       
   213     	M3Gfloat fogValue = 1;
       
   214 
       
   215         /* Calculate fog value and use OpenGL linear fog
       
   216          * to result in same value. Sprites are drawn with
       
   217          * identity MV and P and therefore the fog has to
       
   218          * be adjusted like this */
       
   219         switch (self->mode) {
       
   220     	case M3G_LINEAR_FOG:
       
   221             fogValue = m3gDiv(m3gAdd(self->end, eyeZ), m3gSub(self->end, self->start));
       
   222     		break;
       
   223     	case M3G_EXPONENTIAL_FOG:
       
   224             fogValue = m3gExp(m3gMul(self->density, eyeZ));
       
   225     		break;
       
   226         default:
       
   227             M3G_ASSERT(M3G_FALSE);
       
   228             break;
       
   229     	}
       
   230 
       
   231         m3gGLColor(self->color, temp);
       
   232 
       
   233 		glEnable(GL_FOG);
       
   234 		glFogf(GL_FOG_MODE, GL_LINEAR);
       
   235 #ifdef M3G_USE_NGL_API
       
   236         /* NGL works differently in fog calculation */
       
   237 		glFogf(GL_FOG_START, -m3gDiv(finalZ, fogValue));
       
   238 #else
       
   239 		glFogf(GL_FOG_START, m3gAbs(m3gDiv(finalZ, fogValue)));
       
   240 #endif
       
   241 		glFogf(GL_FOG_END, 0.f);
       
   242 		glFogxv(GL_FOG_COLOR, temp);
       
   243     }
       
   244     else {
       
   245 		glDisable(GL_FOG);
       
   246     }
       
   247 }
       
   248 
       
   249 /*----------------------------------------------------------------------
       
   250  * Virtual function table
       
   251  *--------------------------------------------------------------------*/
       
   252 
       
   253 static const ObjectVFTable m3gvf_Fog = {
       
   254     m3gObjectApplyAnimation,
       
   255     m3gFogIsCompatible,
       
   256     m3gFogUpdateProperty,
       
   257     m3gObjectDoGetReferences,
       
   258     m3gObjectFindID,
       
   259     m3gFogDuplicate,
       
   260     m3gDestroyFog
       
   261 };
       
   262 
       
   263 
       
   264 /*----------------------------------------------------------------------
       
   265  * Public API functions
       
   266  *--------------------------------------------------------------------*/
       
   267 
       
   268 /*!
       
   269  * \brief Creates a Fog object.
       
   270  *
       
   271  * \param interface     M3G interface
       
   272  * \retval Fog new Fog object
       
   273  * \retval NULL Fog creating failed
       
   274  */
       
   275 M3G_API M3GFog m3gCreateFog(M3GInterface interface)
       
   276 {
       
   277     Interface *m3g = (Interface *) interface;
       
   278     M3G_VALIDATE_INTERFACE(m3g);
       
   279 
       
   280 	{
       
   281 		Fog *fog =  m3gAllocZ(m3g, sizeof(Fog));
       
   282 
       
   283         if (fog != NULL) {
       
   284     		m3gInitFog(m3g, fog);
       
   285         }
       
   286 
       
   287 		return (M3GFog) fog;
       
   288 	}
       
   289 }
       
   290 
       
   291 /*!
       
   292  * \brief Sets fog mode.
       
   293  *
       
   294  * \param handle        Fog object
       
   295  * \param mode          fog mode
       
   296  */
       
   297 M3G_API void m3gSetFogMode(M3GFog handle, M3Gint mode)
       
   298 {
       
   299 	Fog *fog = (Fog *) handle;
       
   300 	M3G_VALIDATE_OBJECT(fog);
       
   301 
       
   302 	/* Check for errors */
       
   303 	if(mode < M3G_EXPONENTIAL_FOG || mode > M3G_LINEAR_FOG) {
       
   304 		m3gRaiseError(M3G_INTERFACE(fog), M3G_INVALID_VALUE);
       
   305         return;
       
   306 	}
       
   307 
       
   308 	fog->mode = mode;
       
   309 }
       
   310 
       
   311 /*!
       
   312  * \brief Gets fog mode.
       
   313  *
       
   314  * \param handle        Fog object
       
   315  * \return              fog mode
       
   316  */
       
   317 M3G_API M3Gint m3gGetFogMode(M3GFog handle)
       
   318 {
       
   319 	Fog *fog = (Fog *) handle;
       
   320 	M3G_VALIDATE_OBJECT(fog);
       
   321 
       
   322 	return fog->mode;
       
   323 }
       
   324 
       
   325 /*!
       
   326  * \brief Sets linear fog parameters.
       
   327  *
       
   328  * \param handle        Fog object
       
   329  * \param fogNear       near distance
       
   330  * \param fogFar        far distance
       
   331  */
       
   332 M3G_API void m3gSetFogLinear(M3GFog handle, M3Gfloat fogNear, M3Gfloat fogFar)
       
   333 {
       
   334 	Fog *fog = (Fog *) handle;
       
   335 	M3G_VALIDATE_OBJECT(fog);
       
   336 
       
   337 	fog->start = fogNear;
       
   338 	fog->end = fogFar;
       
   339 }
       
   340 
       
   341 /*!
       
   342  * \brief Gets linear fog parameters.
       
   343  *
       
   344  * \param handle        Fog object
       
   345  * \param which         which parameter to return
       
   346  *                      \arg M3G_GET_NEAR
       
   347  *                      \arg M3G_GET_FAR
       
   348  * \return              near or far distance
       
   349  */
       
   350 M3G_API M3Gfloat m3gGetFogDistance(M3GFog handle, M3Gint which)
       
   351 {
       
   352 	Fog *fog = (Fog *) handle;
       
   353 	M3G_VALIDATE_OBJECT(fog);
       
   354 
       
   355 	switch(which) {
       
   356 	case M3G_GET_NEAR:
       
   357 		return fog->start;
       
   358 	case M3G_GET_FAR:
       
   359 	default:
       
   360 		return fog->end;
       
   361 	}
       
   362 }
       
   363 
       
   364 /*!
       
   365  * \brief Sets exponential fog density.
       
   366  *
       
   367  * \param handle        Fog object
       
   368  * \param density       fog density
       
   369  */
       
   370 M3G_API void m3gSetFogDensity(M3GFog handle, M3Gfloat density)
       
   371 {
       
   372 	Fog *fog = (Fog *) handle;
       
   373 	M3G_VALIDATE_OBJECT(fog);
       
   374 
       
   375 	if(density < 0.f) {
       
   376 		m3gRaiseError(M3G_INTERFACE(fog), M3G_INVALID_VALUE);
       
   377         return;
       
   378 	}
       
   379 
       
   380 	fog->density = density;
       
   381 }
       
   382 
       
   383 /*!
       
   384  * \brief Gets exponential fog density.
       
   385  *
       
   386  * \param handle        Fog object
       
   387  * \return              fog density
       
   388  */
       
   389 M3G_API M3Gfloat m3gGetFogDensity(M3GFog handle)
       
   390 {
       
   391 	Fog *fog = (Fog *) handle;
       
   392 	M3G_VALIDATE_OBJECT(fog);
       
   393 
       
   394 	return fog->density;
       
   395 }
       
   396 
       
   397 /*!
       
   398  * \brief Sets fog color as RGB.
       
   399  *
       
   400  * \param handle        Fog object
       
   401  * \param rgb           fog color as RGB
       
   402  */
       
   403 M3G_API void m3gSetFogColor(M3GFog handle, M3Guint rgb)
       
   404 {
       
   405 	Fog *fog = (Fog *) handle;
       
   406 	M3G_VALIDATE_OBJECT(fog);
       
   407 
       
   408     fog->color = rgb & M3G_RGB_MASK;
       
   409 }
       
   410 
       
   411 /*!
       
   412  * \brief Gets fog color as RGB.
       
   413  *
       
   414  * \param handle        Fog object
       
   415  * \return              fog color as RGB
       
   416  */
       
   417 M3G_API M3Guint m3gGetFogColor(M3GFog handle)
       
   418 {
       
   419 	Fog *fog = (Fog *) handle;
       
   420 	M3G_VALIDATE_OBJECT(fog);
       
   421 
       
   422     return fog->color;
       
   423 }
       
   424