os/graphics/m3g/m3gcore11/src/m3g_fog.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/m3g/m3gcore11/src/m3g_fog.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,422 @@
     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: Fog implementation
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +/*!
    1.23 + * \internal
    1.24 + * \file
    1.25 + * \brief Fog 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 +#include "m3g_fog.h"
    1.33 +#include "m3g_animationtrack.h"
    1.34 +
    1.35 +/*----------------------------------------------------------------------
    1.36 + * Internal functions
    1.37 + *--------------------------------------------------------------------*/
    1.38 +
    1.39 +/*!
    1.40 + * \internal
    1.41 + * \brief Destroys this Fog object.
    1.42 + *
    1.43 + * \param obj Fog object
    1.44 + */
    1.45 +static void m3gDestroyFog(Object *obj)
    1.46 +{
    1.47 +    Fog *fog = (Fog *) obj;
    1.48 +    M3G_VALIDATE_OBJECT(fog);
    1.49 +
    1.50 +    m3gDestroyObject(&fog->object);
    1.51 +}
    1.52 +
    1.53 +/*!
    1.54 + * \internal
    1.55 + * \brief Overloaded Object3D method.
    1.56 + *
    1.57 + * \param property      animation property
    1.58 + * \retval M3G_TRUE     property supported
    1.59 + * \retval M3G_FALSE    property not supported
    1.60 + */
    1.61 +static M3Gbool m3gFogIsCompatible(M3Gint property)
    1.62 +{
    1.63 +    switch (property) {
    1.64 +    case M3G_ANIM_COLOR:
    1.65 +    case M3G_ANIM_DENSITY:
    1.66 +    case M3G_ANIM_FAR_DISTANCE:
    1.67 +    case M3G_ANIM_NEAR_DISTANCE:
    1.68 +        return M3G_TRUE;
    1.69 +    default:
    1.70 +        return m3gObjectIsCompatible(property);
    1.71 +    }
    1.72 +}
    1.73 +
    1.74 +/*!
    1.75 + * \internal
    1.76 + * \brief Overloaded Object3D method.
    1.77 + *
    1.78 + * \param self          Fog object
    1.79 + * \param property      animation property
    1.80 + * \param valueSize     size of value array
    1.81 + * \param value         value array
    1.82 + */
    1.83 +static void m3gFogUpdateProperty(Object *self,
    1.84 +                                 M3Gint property,
    1.85 +                                 M3Gint valueSize,
    1.86 +                                 const M3Gfloat *value)
    1.87 +{
    1.88 +    Fog *fog = (Fog *)self;
    1.89 +    M3G_VALIDATE_OBJECT(fog);
    1.90 +    M3G_ASSERT_PTR(value);
    1.91 +
    1.92 +    switch (property) {
    1.93 +    case M3G_ANIM_COLOR:
    1.94 +        M3G_ASSERT(valueSize >= 3);
    1.95 +        fog->color = m3gColor3f(value[0], value[1], value[2]) & M3G_RGB_MASK;
    1.96 +        break;
    1.97 +    case M3G_ANIM_DENSITY:
    1.98 +        M3G_ASSERT(valueSize >= 1);
    1.99 +        fog->density = (value[0] < 0.f) ? 0.f : value[0];
   1.100 +        break;
   1.101 +    case M3G_ANIM_FAR_DISTANCE:
   1.102 +        M3G_ASSERT(valueSize >= 1);
   1.103 +        fog->end = value[0];
   1.104 +        break;
   1.105 +    case M3G_ANIM_NEAR_DISTANCE:
   1.106 +        M3G_ASSERT(valueSize >= 1);
   1.107 +        fog->start = value[0];
   1.108 +        break;
   1.109 +    default:
   1.110 +        m3gObjectUpdateProperty(self, property, valueSize, value);
   1.111 +    }
   1.112 +}
   1.113 +
   1.114 +/*!
   1.115 + * \internal
   1.116 + * \brief Overloaded Object3D method.
   1.117 + *
   1.118 + * \param originalObj original Fog object
   1.119 + * \param cloneObj pointer to cloned Fog object
   1.120 + * \param pairs array for all object-duplicate pairs
   1.121 + * \param numPairs number of pairs
   1.122 + */
   1.123 +static M3Gbool m3gFogDuplicate(const Object *originalObj,
   1.124 +                               Object **cloneObj,
   1.125 +                               Object **pairs,
   1.126 +                               M3Gint *numPairs)
   1.127 +{
   1.128 +    Fog *original = (Fog *)originalObj;
   1.129 +    Fog *clone = (Fog *)m3gCreateFog(originalObj->interface);
   1.130 +    *cloneObj = (Object *)clone;
   1.131 +    if (*cloneObj == NULL) {
   1.132 +        return M3G_FALSE;
   1.133 +    }
   1.134 +
   1.135 +    if(m3gObjectDuplicate(originalObj, cloneObj, pairs, numPairs)) {
   1.136 +        clone->color = original->color;
   1.137 +        clone->density = original->density;
   1.138 +        clone->start = original->start;
   1.139 +        clone->end = original->end;
   1.140 +        clone->mode = original->mode;
   1.141 +        return M3G_TRUE;
   1.142 +    }
   1.143 +    else {
   1.144 +        return M3G_FALSE;
   1.145 +    }
   1.146 +}
   1.147 +
   1.148 +/*!
   1.149 + * \internal
   1.150 + * \brief Initializes a Fog object. See specification
   1.151 + * for default values.
   1.152 + *
   1.153 + * \param m3g           M3G interface
   1.154 + * \param fog           Fog object
   1.155 + */
   1.156 +static void m3gInitFog(Interface *m3g, Fog *fog)
   1.157 +{
   1.158 +	/* Fog is derived from object */
   1.159 +	m3gInitObject(&fog->object, m3g, M3G_CLASS_FOG);
   1.160 +
   1.161 +	fog->density = 1.0f;
   1.162 +	fog->start = 0.0f;
   1.163 +	fog->end = 1.0f;
   1.164 +	fog->mode = M3G_LINEAR_FOG;
   1.165 +}
   1.166 +
   1.167 +/*!
   1.168 + * \internal
   1.169 + * \brief Applies fog to OpenGL. This is used for
   1.170 + * all Mesh objects.
   1.171 + *
   1.172 + * \param self          Fog object
   1.173 + */
   1.174 +static void m3gApplyFog(const Fog *self)
   1.175 +{
   1.176 +	if (self != NULL) {
   1.177 +		GLfixed temp[4];
   1.178 +
   1.179 +        m3gGLColor(self->color, temp);
   1.180 +
   1.181 +		switch (self->mode) {
   1.182 +		case M3G_LINEAR_FOG:
   1.183 +			glEnable(GL_FOG);
   1.184 +			glFogf(GL_FOG_MODE, GL_LINEAR);
   1.185 +			glFogf(GL_FOG_START, self->start);
   1.186 +			glFogf(GL_FOG_END, self->end);
   1.187 +			glFogxv(GL_FOG_COLOR, temp);
   1.188 +			break;
   1.189 +		case M3G_EXPONENTIAL_FOG:
   1.190 +			glEnable(GL_FOG);
   1.191 +			glFogf(GL_FOG_MODE, GL_EXP);
   1.192 +			glFogf(GL_FOG_DENSITY, self->density);
   1.193 +			glFogxv(GL_FOG_COLOR, temp);
   1.194 +			break;
   1.195 +		}
   1.196 +	}
   1.197 +	else {
   1.198 +		glDisable(GL_FOG);
   1.199 +	}
   1.200 +    M3G_ASSERT_GL;
   1.201 +}
   1.202 +
   1.203 +#ifdef M3G_USE_NGL_API
   1.204 +/*!
   1.205 + * \internal
   1.206 + * \brief Applies fog to NGL. This is used for
   1.207 + * Sprite3D objects only.
   1.208 + *
   1.209 + * \param self          Fog object
   1.210 + * \param eyeZ          Eye space Z (e.g. after modelview)
   1.211 + * \param finalZ        Final Z (e.g. after modelview and projection)
   1.212 + */
   1.213 +static void m3gApplySpriteFog(const Fog *self, M3Gfloat eyeZ, M3Gfloat finalZ)
   1.214 +{
   1.215 +    if(self != NULL) {
   1.216 +		M3Gint temp[4];
   1.217 +    	M3Gfloat fogValue = 1;
   1.218 +
   1.219 +        /* Calculate fog value and use OpenGL linear fog
   1.220 +         * to result in same value. Sprites are drawn with
   1.221 +         * identity MV and P and therefore the fog has to
   1.222 +         * be adjusted like this */
   1.223 +        switch (self->mode) {
   1.224 +    	case M3G_LINEAR_FOG:
   1.225 +            fogValue = m3gDiv(m3gAdd(self->end, eyeZ), m3gSub(self->end, self->start));
   1.226 +    		break;
   1.227 +    	case M3G_EXPONENTIAL_FOG:
   1.228 +            fogValue = m3gExp(m3gMul(self->density, eyeZ));
   1.229 +    		break;
   1.230 +        default:
   1.231 +            M3G_ASSERT(M3G_FALSE);
   1.232 +            break;
   1.233 +    	}
   1.234 +
   1.235 +        m3gGLColor(self->color, temp);
   1.236 +
   1.237 +		glEnable(GL_FOG);
   1.238 +		glFogf(GL_FOG_MODE, GL_LINEAR);
   1.239 +
   1.240 +        /* NGL works differently in fog calculation */
   1.241 +		glFogf(GL_FOG_START, -m3gDiv(finalZ, fogValue));
   1.242 +		glFogf(GL_FOG_END, 0.f);
   1.243 +		glFogxv(GL_FOG_COLOR, temp);
   1.244 +    }
   1.245 +    else {
   1.246 +		glDisable(GL_FOG);
   1.247 +    }
   1.248 +}
   1.249 +#endif
   1.250 +/*----------------------------------------------------------------------
   1.251 + * Virtual function table
   1.252 + *--------------------------------------------------------------------*/
   1.253 +
   1.254 +static const ObjectVFTable m3gvf_Fog = {
   1.255 +    m3gObjectApplyAnimation,
   1.256 +    m3gFogIsCompatible,
   1.257 +    m3gFogUpdateProperty,
   1.258 +    m3gObjectDoGetReferences,
   1.259 +    m3gObjectFindID,
   1.260 +    m3gFogDuplicate,
   1.261 +    m3gDestroyFog
   1.262 +};
   1.263 +
   1.264 +
   1.265 +/*----------------------------------------------------------------------
   1.266 + * Public API functions
   1.267 + *--------------------------------------------------------------------*/
   1.268 +
   1.269 +/*!
   1.270 + * \brief Creates a Fog object.
   1.271 + *
   1.272 + * \param interface     M3G interface
   1.273 + * \retval Fog new Fog object
   1.274 + * \retval NULL Fog creating failed
   1.275 + */
   1.276 +M3G_API M3GFog m3gCreateFog(M3GInterface interface)
   1.277 +{
   1.278 +    Interface *m3g = (Interface *) interface;
   1.279 +    M3G_VALIDATE_INTERFACE(m3g);
   1.280 +
   1.281 +	{
   1.282 +		Fog *fog =  m3gAllocZ(m3g, sizeof(Fog));
   1.283 +
   1.284 +        if (fog != NULL) {
   1.285 +    		m3gInitFog(m3g, fog);
   1.286 +        }
   1.287 +
   1.288 +		return (M3GFog) fog;
   1.289 +	}
   1.290 +}
   1.291 +
   1.292 +/*!
   1.293 + * \brief Sets fog mode.
   1.294 + *
   1.295 + * \param handle        Fog object
   1.296 + * \param mode          fog mode
   1.297 + */
   1.298 +M3G_API void m3gSetFogMode(M3GFog handle, M3Gint mode)
   1.299 +{
   1.300 +	Fog *fog = (Fog *) handle;
   1.301 +	M3G_VALIDATE_OBJECT(fog);
   1.302 +
   1.303 +	/* Check for errors */
   1.304 +	if(mode < M3G_EXPONENTIAL_FOG || mode > M3G_LINEAR_FOG) {
   1.305 +		m3gRaiseError(M3G_INTERFACE(fog), M3G_INVALID_VALUE);
   1.306 +        return;
   1.307 +	}
   1.308 +
   1.309 +	fog->mode = mode;
   1.310 +}
   1.311 +
   1.312 +/*!
   1.313 + * \brief Gets fog mode.
   1.314 + *
   1.315 + * \param handle        Fog object
   1.316 + * \return              fog mode
   1.317 + */
   1.318 +M3G_API M3Gint m3gGetFogMode(M3GFog handle)
   1.319 +{
   1.320 +	Fog *fog = (Fog *) handle;
   1.321 +	M3G_VALIDATE_OBJECT(fog);
   1.322 +
   1.323 +	return fog->mode;
   1.324 +}
   1.325 +
   1.326 +/*!
   1.327 + * \brief Sets linear fog parameters.
   1.328 + *
   1.329 + * \param handle        Fog object
   1.330 + * \param fogNear       near distance
   1.331 + * \param fogFar        far distance
   1.332 + */
   1.333 +M3G_API void m3gSetFogLinear(M3GFog handle, M3Gfloat fogNear, M3Gfloat fogFar)
   1.334 +{
   1.335 +	Fog *fog = (Fog *) handle;
   1.336 +	M3G_VALIDATE_OBJECT(fog);
   1.337 +
   1.338 +	fog->start = fogNear;
   1.339 +	fog->end = fogFar;
   1.340 +}
   1.341 +
   1.342 +/*!
   1.343 + * \brief Gets linear fog parameters.
   1.344 + *
   1.345 + * \param handle        Fog object
   1.346 + * \param which         which parameter to return
   1.347 + *                      \arg M3G_GET_NEAR
   1.348 + *                      \arg M3G_GET_FAR
   1.349 + * \return              near or far distance
   1.350 + */
   1.351 +M3G_API M3Gfloat m3gGetFogDistance(M3GFog handle, M3Gint which)
   1.352 +{
   1.353 +	Fog *fog = (Fog *) handle;
   1.354 +	M3G_VALIDATE_OBJECT(fog);
   1.355 +
   1.356 +	switch(which) {
   1.357 +	case M3G_GET_NEAR:
   1.358 +		return fog->start;
   1.359 +	case M3G_GET_FAR:
   1.360 +	default:
   1.361 +		return fog->end;
   1.362 +	}
   1.363 +}
   1.364 +
   1.365 +/*!
   1.366 + * \brief Sets exponential fog density.
   1.367 + *
   1.368 + * \param handle        Fog object
   1.369 + * \param density       fog density
   1.370 + */
   1.371 +M3G_API void m3gSetFogDensity(M3GFog handle, M3Gfloat density)
   1.372 +{
   1.373 +	Fog *fog = (Fog *) handle;
   1.374 +	M3G_VALIDATE_OBJECT(fog);
   1.375 +
   1.376 +	if(density < 0.f) {
   1.377 +		m3gRaiseError(M3G_INTERFACE(fog), M3G_INVALID_VALUE);
   1.378 +        return;
   1.379 +	}
   1.380 +
   1.381 +	fog->density = density;
   1.382 +}
   1.383 +
   1.384 +/*!
   1.385 + * \brief Gets exponential fog density.
   1.386 + *
   1.387 + * \param handle        Fog object
   1.388 + * \return              fog density
   1.389 + */
   1.390 +M3G_API M3Gfloat m3gGetFogDensity(M3GFog handle)
   1.391 +{
   1.392 +	Fog *fog = (Fog *) handle;
   1.393 +	M3G_VALIDATE_OBJECT(fog);
   1.394 +
   1.395 +	return fog->density;
   1.396 +}
   1.397 +
   1.398 +/*!
   1.399 + * \brief Sets fog color as RGB.
   1.400 + *
   1.401 + * \param handle        Fog object
   1.402 + * \param rgb           fog color as RGB
   1.403 + */
   1.404 +M3G_API void m3gSetFogColor(M3GFog handle, M3Guint rgb)
   1.405 +{
   1.406 +	Fog *fog = (Fog *) handle;
   1.407 +	M3G_VALIDATE_OBJECT(fog);
   1.408 +
   1.409 +    fog->color = rgb & M3G_RGB_MASK;
   1.410 +}
   1.411 +
   1.412 +/*!
   1.413 + * \brief Gets fog color as RGB.
   1.414 + *
   1.415 + * \param handle        Fog object
   1.416 + * \return              fog color as RGB
   1.417 + */
   1.418 +M3G_API M3Guint m3gGetFogColor(M3GFog handle)
   1.419 +{
   1.420 +	Fog *fog = (Fog *) handle;
   1.421 +	M3G_VALIDATE_OBJECT(fog);
   1.422 +
   1.423 +    return fog->color;
   1.424 +}
   1.425 +