Show klight.cpp syntax highlighted
/*
Copyright (C) 2003, 2004, 2005 by Luca Cappa
Written by Luca Cappa groton@users.sourceforge.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "cssysdef.h"
#include "csutil/sysfunc.h"
#include "csutil/cscolor.h"
#include "csutil/cmdhelp.h"
#include "csutil/cspmeter.h"
#include "csutil/csstring.h"
#include "csutil/scfstr.h"
#include "cstool/csview.h"
#include "cstool/initapp.h"
#include "cstool/collider.h"
#include "iutil/vfs.h"
#include "iutil/eventq.h"
#include "iutil/event.h"
#include "iutil/objreg.h"
#include "iutil/csinput.h"
#include "iutil/virtclk.h"
#include "iutil/plugin.h"
#include "iutil/string.h"
#include "iutil/document.h"
#include "iengine/sector.h"
#include "iengine/engine.h"
#include "iengine/camera.h"
#include "iengine/light.h"
#include "iengine/texture.h"
#include "iengine/mesh.h"
#include "iengine/movable.h"
#include "iengine/material.h"
#include "imesh/thing.h"
#include "imesh/object.h"
#include "imesh/sprite3d.h"
#include "imesh/ball.h"
#include "ivideo/graph3d.h"
#include "ivideo/graph2d.h"
#include "ivideo/natwin.h"
#include "ivideo/txtmgr.h"
#include "ivideo/texture.h"
#include "ivideo/material.h"
#include "ivideo/fontserv.h"
#include "igraphic/imageio.h"
#include "imap/loader.h"
#include "ivaria/reporter.h"
#include "ivaria/stdrep.h"
#include "ivaria/conout.h"
#include "ivaria/reporter.h"
#include "ivaria/stdrep.h"
#include "ivaria/collider.h"
#include "csgeom/quaterni.h"
#include "csgeom/transfrm.h"
#include "csgeom/math3d_d.h"
#include "csgeom/math3d.h"
#include "igeom/polymesh.h"
#include "igeom/objmodel.h"
#include "iaws/aws.h"
#include "iaws/awscnvs.h"
#include "korientation.h"
#include "kblock.h"
#include "kmap.h"
#include "kblockid.h"
#include "kflags.h"
#include "kappstate.h"
#include "ikxmlwriter.h"
#include "ikxmlreader.h"
#include "ske.h"
//Include for this file.
#include "klight.h"
SCF_IMPLEMENT_IBASE_EXT (KLight)
SCF_IMPLEMENTS_INTERFACE (KLight)
SCF_IMPLEMENTS_INTERFACE (iKXMLReader)
SCF_IMPLEMENTS_INTERFACE (iKXMLWriter)
SCF_IMPLEMENTS_EMBEDDED_INTERFACE (iKDraggable)
SCF_IMPLEMENTS_EMBEDDED_INTERFACE (iKDragHandler)
SCF_IMPLEMENT_IBASE_EXT_END
SCF_IMPLEMENT_EMBEDDED_IBASE (KLight::eiKDraggable)
SCF_IMPLEMENTS_INTERFACE (iKDraggable)
SCF_IMPLEMENT_EMBEDDED_IBASE_END
SCF_IMPLEMENT_EMBEDDED_IBASE (KLight::eiKDragHandler)
SCF_IMPLEMENTS_INTERFACE (iKDragHandler)
SCF_IMPLEMENT_EMBEDDED_IBASE_END
KLight::KLight ()
{
SCF_CONSTRUCT_IBASE (0);
SCF_CONSTRUCT_EMBEDDED_IBASE (scfiKDraggable);
SCF_CONSTRUCT_EMBEDDED_IBASE (scfiKDragHandler);
m_engine = CS_QUERY_REGISTRY (g_objReg, iEngine);
m_g3d = CS_QUERY_REGISTRY (g_objReg, iGraphics3D);
m_loader = CS_QUERY_REGISTRY (g_objReg, iLoader);
m_visible = true;
m_relighted = m_constructedProperly = false;
//
//Set the internal draggable interface.
csRef<iKDraggable> l_d = SCF_QUERY_INTERFACE (this, iKDraggable);
csRef<iKDragHandler> l_dH = SCF_QUERY_INTERFACE (this, iKDragHandler);
l_d->SetDragHandler (l_dH);
}
KLight::~KLight ()
{
csRef<iLight> l_sL = SCF_QUERY_INTERFACE (m_light, iLight);
m_engine->RemoveLight (l_sL);
//The mesh is removed from the engine upon destruction of
//his relative light.
m_engine->GetMeshes ()->Remove (m_ballMW);
SCF_DESTRUCT_EMBEDDED_IBASE (scfiKDraggable);
SCF_DESTRUCT_IBASE ();
}
iLight* KLight::GetLight ()
{
return m_light;
}
bool KLight::HasRelighted () const
{
return m_relighted;
}
bool KLight::Relight ()
{
/*if (!m_relighted)
{*/
m_engine->ForceRelight (m_light);
m_relighted = true;
return true;
/*}//if
return false;*/
}
void KLight::LinkToMesh ()
{
//
//Add the iObject interface of this object to the
//iObject of the mesh wrapper. This way it is possible to query the
//iKDraggable interface (ie drag it with the mouse).
m_ballMW->QueryObject ()->ObjAdd (&scfiKDraggable);
}
void KLight::UnlinkToMesh ()
{
m_ballMW->QueryObject ()->ObjRemove (&scfiKDraggable);
}
bool KLight::Create (const char* p_name, const csVector3& p_center,
float p_radius, const csColor& p_color, csLightDynamicType p_type,
csLightAttenuationMode p_attenuation)
{
//
//Create the graphical part (ie the ball!)
//
const char* classId = "crystalspace.mesh.object.ball";
csRef<iMeshFactoryWrapper> l_mesh_fact (m_engine->CreateMeshFactory
(classId, "ballFact"));
m_ballMW = m_engine->CreateMeshWrapper (l_mesh_fact,
"", g_ske->m_world, p_center);//??
m_ballMW->SetRenderPriority (m_engine->GetRenderPriority ("object"));
m_ballMW->SetZBufMode(CS_ZBUF_USE);
//attach to the mesh wrapper this KLight.
this->ObjAdd (m_ballMW->QueryObject ());
csVector3 l_meshRadius (0.15f, 0.15f, 0.15f);
csRef<iBallState> l_ballstate (SCF_QUERY_INTERFACE (m_ballMW->GetMeshObject(),
iBallState));
l_ballstate->SetRadius (l_meshRadius.x, l_meshRadius.y, l_meshRadius.z);
l_ballstate->SetShift (0, 0, 0);
l_ballstate->SetRimVertices (16);
l_ballstate->SetMaterialWrapper (SKE::CreateMaterial ("blue", 0.6f ,0.6f ,0.0f) );
//??ballstate->SetMixMode (CS_FX_SETALPHA(0.5));
l_ballstate->SetMixMode (CS_FX_COPY);
l_ballstate->SetReversed (false);
l_ballstate->SetTopOnly (false);
l_ballstate->SetLighting (true);
//
//some sort of initialization is below.
/*m_meshW->GetMovable ()->SetTransform (csMatrix3 ());
m_meshW->GetMovable ()->UpdateMove ();
m_state = SCF_QUERY_INTERFACE (m_meshW->GetMeshObject (), iSprite3DState);
m_state->SetLighting(true);
m_meshW->SetZBufMode (CS_ZBUF_USE);
m_meshW->SetRenderPriority (m_engine->GetObjectRenderPriority ());*/
//Store the arguments values.
SetName (p_name);
m_ballMW->QueryObject ()->SetName (p_name);
//
//Create the actual light!
//
m_light = m_engine->CreateLight (p_name, p_center, p_radius, p_color,
p_type);
g_ske->m_world->GetLights ()->Add (m_light);//???
m_light->SetAttenuationMode (p_attenuation);
LinkToMesh ();
//Init of some member vars.
m_constructedProperly = true;
return true;
}
bool KLight::IsConstructedProperly () const
{
return m_constructedProperly;
}
iMovable* KLight::GetMovable() const
{
return m_ballMW-> GetMovable ();
}
iMeshWrapper* KLight::GetMeshWrapper () const
{
return m_ballMW;
};
iMeshFactoryWrapper* KLight::GetMeshFactoryWrapper () const
{
return m_meshFactW;
};
void KLight::SetPosition (const csVector3& p_pos )
{
//Move the mesh
m_ballMW->GetMovable()->SetPosition(p_pos);
//And also move the light!
m_light->SetCenter (p_pos);
}
const csVector3& KLight::GetPosition ( ) const
{
return m_ballMW->GetMovable()->GetPosition();
}
void KLight::Transform (const csMatrix3& p_tr)
{
m_ballMW->GetMovable()-> Transform(p_tr);
}
void KLight::SetTransform (const csMatrix3& p_tr)
{
m_ballMW->GetMovable()-> SetTransform(p_tr);
}
const csMatrix3& KLight::GetTransform () const
{
return m_ballMW->GetMovable()-> GetTransform ().GetT2O ();
}
void KLight::LookAt (const csVector3& p_to, const csVector3& p_up )
{
m_ballMW->GetMovable()->GetTransform ().LookAt(p_to, p_up);
}
void KLight::GetBoundingBox (csBox3& p_bbox) const
{
csRef<iObjectModel> l_oM = m_ballMW->GetMeshObject ()->GetObjectModel ();
l_oM->GetObjectBoundingBox (p_bbox);
}
void KLight::SetVisible (bool p_visible)
{
m_visible = p_visible;
csFlags& l_flags = m_ballMW->GetFlags ();
if(!m_visible)
l_flags.Set(l_flags.Get () | CS_ENTITY_INVISIBLE);
else
l_flags.Reset(CS_ENTITY_INVISIBLE);
}
bool KLight::IsVisible () const
{
return m_visible;
}
//
//
void KLight::SetSector (iSector* p_sector)
{
//Set the sector on the mesh
m_ballMW->GetMovable ()->SetSector (p_sector);
//
//Set the sector on the light. Remove it from a lightlist, and
//add it to the new sector light's list.
m_light->GetSector ()->GetLights ()->Remove (m_light);
p_sector->GetLights ()->Add (m_light);
}
//
//
void KLight::UpdateMove ()
{
m_ballMW->GetMovable ()->UpdateMove ();
}
//
//
void KLight::SetZBufMode (csZBufMode p_zBufMode)
{
m_ballMW->SetZBufMode (p_zBufMode);
}
//
//
void KLight::SetRenderPriority (int p_rP)
{
m_ballMW->SetRenderPriority (p_rP);
}
//
//
void KLight::HardTransform (const csReversibleTransform& p_rT )
{
m_meshFactW->HardTransform (p_rT);
}
bool KLight::eiKDraggable::Supports (DRAG_TYPE p_dT) const
{
if (p_dT == ROTATION)
return false;
return true;
}
bool KLight::eiKDragHandler::HandleDrag (float p_x, float p_y)
{
switch (m_dragType)
{
case TRANSLATION:
{
csVector3 l_center = scfParent->m_light->GetCenter ();
csVector3 lInt;
float lDist;
g_ske->CalculateIntersection (p_x, p_y,
csPlane3 (0, 1, 0, -l_center.y), lInt, lDist);
scfParent->SetPosition (lInt);
scfParent->UpdateMove ();
return true;
}//case
}//switch
return false;
}
const csRefArray<iMeshWrapper> KLight::eiKDraggable::GetMeshWrappers () const
{
csRefArray<iMeshWrapper> l_mWL;
l_mWL.Push (scfParent->GetMeshWrapper ());
return l_mWL;
}
const char* KLight::GetName () const
{
char const* lName = m_light->QueryObject ()->GetName ();
return (lName ? lName : "null_name");
}
float KLight::GetDirectionalCutoffRadius () const
{
return m_light->GetDirectionalCutoffRadius ();
}
void KLight::SetDirectionalCutoffRadius (float pRadius)
{
m_light->SetDirectionalCutoffRadius (pRadius);
}
csLightAttenuationMode KLight::GetAttenuationMode () const
{
return m_light->GetAttenuationMode ();
}
void KLight::SetAttenuationMode (csLightAttenuationMode pAM)
{
m_light->SetAttenuationMode (pAM);
}
const csColor& KLight::GetColor () const
{
return m_light->GetColor ();
}
csLightDynamicType KLight::GetDynamicType () const
{
return m_light->GetDynamicType ();
}
bool KLight::Read (iDocumentNode* p_parent)
{
if (strcmpi (p_parent->GetValue (), "light"))
return false;
const char* l_name = p_parent->GetAttributeValue ("name");
csVector3 l_pos;
l_pos.x = p_parent->GetAttributeValueAsFloat ("x");
l_pos.y = p_parent->GetAttributeValueAsFloat ("y");
l_pos.z = p_parent->GetAttributeValueAsFloat ("z");
csLightAttenuationMode l_attenuation = (csLightAttenuationMode)
p_parent->GetAttributeValueAsInt ("attenuation");
csLightDynamicType l_type = (csLightDynamicType) p_parent->
GetAttributeValueAsInt ("type");
float l_radius = p_parent->GetAttributeValueAsFloat ("radius");
csColor l_color;
l_color.red = p_parent->GetAttributeValueAsFloat ("red");
l_color.green = p_parent->GetAttributeValueAsFloat ("green");
l_color.blue = p_parent->GetAttributeValueAsFloat ("blue");
Create (l_name, l_pos, l_radius, l_color, l_type, l_attenuation);
return true;
}
bool KLight::Write (iDocumentNode* p_parent) const
{
//Create a new light node.
csRef<iDocumentNode> l_lNode = p_parent->CreateNodeBefore
(CS_NODE_ELEMENT, 0);
l_lNode->SetValue ("light");
l_lNode->SetAttribute ("name", GetName ());
csVector3 l_pos = GetPosition ();
l_lNode->SetAttributeAsFloat ("x", l_pos.x);
l_lNode->SetAttributeAsFloat ("y", l_pos.y);
l_lNode->SetAttributeAsFloat ("z", l_pos.z);
float l_radius = GetDirectionalCutoffRadius ();
l_lNode->SetAttributeAsFloat ("radius", l_radius);
csColor l_color = GetColor ();
l_lNode->SetAttributeAsFloat ("red", l_color.red);
l_lNode->SetAttributeAsFloat ("green", l_color.green);
l_lNode->SetAttributeAsFloat ("blue", l_color.blue);
int l_attenuation = GetAttenuationMode ();
l_lNode->SetAttributeAsInt ("attenuation", l_attenuation);
int l_type = GetDynamicType ();
l_lNode->SetAttributeAsInt ("type", l_type);
return true;
}
See more files for this project here