| 1 | /** |
|---|
| 2 | * This source file is part of OgreColladaPlugin |
|---|
| 3 | * an addon for OGRE (Object-oriented Graphics Rendering Engine) |
|---|
| 4 | * For the latest info, see http://www.ogre3d.org/ |
|---|
| 5 | * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify it under |
|---|
| 7 | * the terms of the GNU Lesser General Public License as published by the Free Software |
|---|
| 8 | * Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 9 | * version. |
|---|
| 10 | |
|---|
| 11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 13 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | * You should have received a copy of the GNU Lesser General Public License along with |
|---|
| 16 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
|---|
| 17 | * Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
|---|
| 18 | * http://www.gnu.org/copyleft/lesser.txt. |
|---|
| 19 | * |
|---|
| 20 | * @author Philipp Hartl |
|---|
| 21 | * @see README |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | #include "OgreColladaLight.h" |
|---|
| 25 | #include "OgreColladaDocument.h" |
|---|
| 26 | #include "OgreColladaScene.h" |
|---|
| 27 | #include "OgreColladaSyntax.h" |
|---|
| 28 | #include "OgreColladaUtils.h" |
|---|
| 29 | |
|---|
| 30 | #include "OgreSceneManager.h" |
|---|
| 31 | #include "OgreStringConverter.h" |
|---|
| 32 | |
|---|
| 33 | namespace Ogre |
|---|
| 34 | { |
|---|
| 35 | //------------------------------------------------------------------------- |
|---|
| 36 | ColladaLight::ColladaLight(ColladaDocument *d, xmlNode *n) : ColladaEntity(d, n) { } |
|---|
| 37 | |
|---|
| 38 | //------------------------------------------------------------------------- |
|---|
| 39 | ColladaLight::~ColladaLight(void) { } |
|---|
| 40 | |
|---|
| 41 | //------------------------------------------------------------------------- |
|---|
| 42 | MovableObject *ColladaLight::getOgreInstance(void) const |
|---|
| 43 | { |
|---|
| 44 | if (mType == ColladaLightSpecific::AMBIENT) |
|---|
| 45 | { |
|---|
| 46 | mDoc->getSceneManager()->setAmbientLight(mColour); |
|---|
| 47 | return NULL; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | // Light *light = new Light(mId); |
|---|
| 51 | Light *light = mDoc->getSceneManager()->createLight(mId); |
|---|
| 52 | |
|---|
| 53 | light->setDiffuseColour(mColour); |
|---|
| 54 | light->setSpecularColour(mColour); |
|---|
| 55 | light->setDirection(Vector3(0, 0, -1)); // see collada spec |
|---|
| 56 | |
|---|
| 57 | if (mType == ColladaLightSpecific::DIRECTIONAL) light->setType(Light::LT_DIRECTIONAL); |
|---|
| 58 | else if (mType == ColladaLightSpecific::POINT) light->setType(Light::LT_POINT); |
|---|
| 59 | else if (mType == ColladaLightSpecific::SPOT) light->setType(Light::LT_SPOTLIGHT); |
|---|
| 60 | |
|---|
| 61 | if (mType == ColladaLightSpecific::POINT || mType == ColladaLightSpecific::SPOT) |
|---|
| 62 | { |
|---|
| 63 | float constant = (mAttenuation == ColladaLightSpecific::CONSTANT) ? mAttenuationScale : 0; |
|---|
| 64 | float linear = (mAttenuation == ColladaLightSpecific::LINEAR) ? mAttenuationScale : 0; |
|---|
| 65 | float quadratic = (mAttenuation == ColladaLightSpecific::QUADRATIC) ? mAttenuationScale : 0; |
|---|
| 66 | |
|---|
| 67 | float range = 100000; |
|---|
| 68 | ColladaBoundingBox *scenebox = mDoc->getScene()->getBoundingBox(); |
|---|
| 69 | if (scenebox != NULL) range = scenebox->getRadius() * 2; |
|---|
| 70 | |
|---|
| 71 | light->setAttenuation(range, constant, linear, quadratic); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | if (mType == ColladaLightSpecific::SPOT) |
|---|
| 75 | { |
|---|
| 76 | light->setSpotlightRange(Radian(0), Radian(Math::DegreesToRadians(mAngle)), mFalloffScale); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | return light; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | //------------------------------------------------------------------------- |
|---|
| 83 | bool ColladaLight::doImport(void) |
|---|
| 84 | { |
|---|
| 85 | if (mLoaded) return mStatus; |
|---|
| 86 | else mLoaded = true; |
|---|
| 87 | |
|---|
| 88 | // get light type |
|---|
| 89 | String lighttype = ColladaUtils::getProperty(mNode, CS_ATR_TYPE); |
|---|
| 90 | mType = ColladaLightSpecific::getType(lighttype); |
|---|
| 91 | if (mType == ColladaLightSpecific::UNKNOWN) |
|---|
| 92 | { |
|---|
| 93 | LogManager::getSingleton().logMessage("ColladaLight::doImport - light type " + lighttype + " unknown! " + mId); |
|---|
| 94 | return false; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | // <asset> child |
|---|
| 98 | // xmlNode *asset = ColladaUtils::getChildByTagName(mNode, CS_ELM_ASSET); |
|---|
| 99 | |
|---|
| 100 | // <param> childs, describes the values that the light contributes |
|---|
| 101 | xmlNodePtrVector params = ColladaUtils::getChildsByTagName(mNode, CS_ELM_PARAM); |
|---|
| 102 | for (xmlNodePtrVector::iterator it = params.begin(); it != params.end(); ++it) |
|---|
| 103 | { |
|---|
| 104 | String name = ColladaUtils::getProperty(*it, CS_ATR_NAME); |
|---|
| 105 | String content = ColladaUtils::getContentDirect(*it); |
|---|
| 106 | |
|---|
| 107 | /** |
|---|
| 108 | * if the type attribute value is DIRECTIONAL or SPOT |
|---|
| 109 | * then the light's default direction vector in local |
|---|
| 110 | * coordinates is [0,0,-1], pointing down the -Z axis |
|---|
| 111 | */ |
|---|
| 112 | |
|---|
| 113 | // should be in every case |
|---|
| 114 | if (name == CS_VAL_LIGHT_PARAM_COLOR) |
|---|
| 115 | { |
|---|
| 116 | mColour = StringConverter::parseColourValue(content); |
|---|
| 117 | } |
|---|
| 118 | // point or spot |
|---|
| 119 | else if (name == CS_VAL_LIGHT_PARAM_ATTENUATION) |
|---|
| 120 | { |
|---|
| 121 | mAttenuation = ColladaLightSpecific::getFunction(content); |
|---|
| 122 | } |
|---|
| 123 | else if (name == CS_VAL_LIGHT_PARAM_ATTENUATION_SCALE) |
|---|
| 124 | { |
|---|
| 125 | mAttenuationScale = StringConverter::parseReal(content); |
|---|
| 126 | } |
|---|
| 127 | // especially for spot light |
|---|
| 128 | else if (name == CS_VAL_LIGHT_PARAM_ANGLE) |
|---|
| 129 | { |
|---|
| 130 | mAngle = StringConverter::parseReal(content); |
|---|
| 131 | } |
|---|
| 132 | else if (name == CS_VAL_LIGHT_PARAM_FALLOFF) |
|---|
| 133 | { |
|---|
| 134 | mFalloff = ColladaLightSpecific::getFunction(content); |
|---|
| 135 | } |
|---|
| 136 | else if (name == CS_VAL_LIGHT_PARAM_FALLOFF_SCALE) |
|---|
| 137 | { |
|---|
| 138 | mFalloffScale = StringConverter::parseReal(content); |
|---|
| 139 | } |
|---|
| 140 | else |
|---|
| 141 | { |
|---|
| 142 | LogManager::getSingleton().logMessage("ColladaLight::doImport - unknown attribute of light param " + name + "! " + mId); |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | mStatus = true; |
|---|
| 147 | return mStatus; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | //------------------------------------------------------------------------- |
|---|
| 151 | namespace ColladaLightSpecific |
|---|
| 152 | { |
|---|
| 153 | Type getType(const String &s) |
|---|
| 154 | { |
|---|
| 155 | if (s == CS_VAL_LIGHT_TYPE_AMBIENT) return AMBIENT; |
|---|
| 156 | else if (s == CS_VAL_LIGHT_TYPE_DIRECTIONAL) return DIRECTIONAL; |
|---|
| 157 | else if (s == CS_VAL_LIGHT_TYPE_POINT) return POINT; |
|---|
| 158 | else if (s == CS_VAL_LIGHT_TYPE_SPOT) return SPOT; |
|---|
| 159 | else return UNKNOWN; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | Function getFunction(const String &s) |
|---|
| 163 | { |
|---|
| 164 | if (s == CS_VAL_LIGHT_FUNCTION_CONSTANT) return CONSTANT; |
|---|
| 165 | else if (s == CS_VAL_LIGHT_FUNCTION_LINEAR) return LINEAR; |
|---|
| 166 | else if (s == CS_VAL_LIGHT_FUNCTION_QUADRATIC) return QUADRATIC; |
|---|
| 167 | else return CONSTANT; |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| 170 | } |
|---|