MercuryLight.cpp

Go to the documentation of this file.
00001 #include "MercuryLight.h"
00002 #include "MercuryDisplay.h"
00003 #include "MercuryMath.h"
00004 
00005 MercuryLight::MercuryLight()
00006 :MercuryObject()
00007 {
00008 }
00009 
00010 MercuryLight::~MercuryLight()
00011 {
00012     DISPLAY->RemoveLight(this);
00013 }
00014 
00015 void MercuryLight::Init()
00016 {
00017     MercuryObject::Init();
00018 
00019     //  m_colors.m_diffuse = MercuryColor(1.0f, 1.0f, 1.0f, 1.0f);
00020     m_colors.m_specular = m_colors.m_diffuse = m_colors.m_ambient = MercuryColor(1.0f, 1.0f, 1.0f, 1.0f);
00021 
00022     m_attenuation[Attenuation::CONSTANT].value = 1; //Default
00023     m_attenuation[Attenuation::LINEAR].type = Attenuation::LINEAR;
00024     m_attenuation[Attenuation::QUADRATIC].type = Attenuation::QUADRATIC;
00025 
00026     m_pLT = LT_DIRECTIONAL;
00027 
00028     m_static = false;
00029 
00030     SetMaterial(&m_colors);
00031     ComputeRadius();
00032 }
00033 
00034 void MercuryLight::SetAttenuation(float value, Attenuation::Type type)
00035 {
00036     m_attenuation[type].type = type;
00037     m_attenuation[type].value = value;
00038     ComputeRadius();
00039 }
00040 
00041 float MercuryLight::ComputeBrightnessFromDistance(const MercuryPoint& objectPos) const
00042 {
00043     float dist = (GetPosition() - objectPos).Magnitude();
00044     float atten = 1.0f / ( m_attenuation[Attenuation::CONSTANT].value + m_attenuation[Attenuation::LINEAR].value*dist + m_attenuation[Attenuation::QUADRATIC].value*dist*dist);
00045     float ac = (m_finalMaterial.m_ambient.GetR() + m_finalMaterial.m_ambient.GetG() + m_finalMaterial.m_ambient.GetB()) * atten;
00046     float dc = (m_finalMaterial.m_diffuse.GetR() + m_finalMaterial.m_diffuse.GetG() + m_finalMaterial.m_diffuse.GetB()) * atten;
00047     float sc = (m_finalMaterial.m_specular.GetR() + m_finalMaterial.m_specular.GetG() + m_finalMaterial.m_specular.GetB()) * atten;
00048     return ac + dc + sc;
00049 }
00050 
00051 void MercuryLight::ComputeRadius()
00052 {
00053     //300 ensures that RGB of 255 reaches 0
00054     //at 50, 255 is about 5. Close enough for me
00055     const float maxDenom = 50;//300;
00056     float a = m_attenuation[Attenuation::QUADRATIC].value;
00057     float b = m_attenuation[Attenuation::LINEAR].value;
00058     float c = m_attenuation[Attenuation::CONSTANT].value;
00059     float d = 0;
00060 
00061     if ((a == 0)&&(b == 0))
00062     {
00063         d = 1000.0;
00064     }
00065     else if (a == 0)
00066     {
00067         d = (maxDenom - c)/b;
00068     }
00069     else if (b == 0)
00070     {
00071         d = SQRT((maxDenom - c)/a);
00072     }
00073     else
00074     {
00075         //reciprocal of the quadratic equation
00076         float bottom = 2*a;
00077         float s = SQRT((b*b)-(4*a*c));
00078         float x1 = ((-b) - s)/bottom;
00079         float x2 = ((-b) + s)/bottom;
00080         m_radius = max(x1,x2);
00081     }
00082 
00083     m_radius = d;
00084 }
00085 
00086 #include "MercuryObjectFactory.h"
00087 REGISTER_OBJECT_TYPE( MercuryLight );
00088 
00089 /* 
00090  * Copyright (c) 2005-2006, Joshua Allen
00091  * All rights reserved.
00092  *
00093  * Redistribution and use in source and binary forms, with or
00094  * without modification, are permitted provided that the following
00095  * conditions are met:
00096  *  -   Redistributions of source code must retain the above
00097  *      copyright notice, this list of conditions and the following disclaimer.
00098  *  -   Redistributions in binary form must reproduce the above copyright
00099  *      notice, this list of conditions and the following disclaimer in
00100  *      the documentation and/or other materials provided with the distribution.
00101  *  -   Neither the name of the Mercury Engine nor the names of its
00102  *      contributors may be used to endorse or promote products derived from
00103  *      this software without specific prior written permission.
00104  *
00105  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00106  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00107  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00108  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00109  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00110  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00111  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00112  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00113  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00114  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00115  */

Hosted by SourceForge.net Logo