Code Search for Developers
 
 
  

ksprite3d.cpp from Spatial Knowledge Experiments at Krugle


Show ksprite3d.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 "csutil/flags.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 "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 "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 "ksprite3d.h"





SCF_IMPLEMENT_IBASE_EXT (KSprite3D)
  SCF_IMPLEMENTS_INTERFACE (KSprite3D)
  SCF_IMPLEMENTS_INTERFACE (iKXMLWriter)
  SCF_IMPLEMENTS_INTERFACE (iKXMLReader)
  SCF_IMPLEMENTS_EMBEDDED_INTERFACE (iKDraggable)
SCF_IMPLEMENT_IBASE_EXT_END

SCF_IMPLEMENT_EMBEDDED_IBASE (KSprite3D::eiKDraggable)
  SCF_IMPLEMENTS_INTERFACE (iKDraggable)
SCF_IMPLEMENT_EMBEDDED_IBASE_END

KSprite3D::KSprite3D ()
{
  SCF_CONSTRUCT_IBASE (0);
  SCF_CONSTRUCT_EMBEDDED_IBASE (scfiKDraggable);
  
  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_yRotation = 0;
  m_visible = true;
  m_constructedProperly = false;
}

KSprite3D::~KSprite3D ()
{
  SCF_DESTRUCT_IBASE ();
  SCF_DESTRUCT_EMBEDDED_IBASE (scfiKDraggable);
 
  //
  //Remove all the children we added to the mesh. Specifically
  //the children are added in the KLandmark::AddKSprite3D method.
  m_meshW->QueryObject ()->ObjRemoveAll ();

  //the mesh is removed from the engine upon destruction.
  m_engine->GetMeshes ()->Remove (m_meshW);
}












bool KSprite3D::Create (const char* p_name, const char* p_mFName,
  const char* p_materialName,
  float p_x, float p_y, float p_z, const csReversibleTransform* p_hardTransf)
{ 
  if (!LoadMeshFactory (p_mFName))
    return false;

  m_factState = SCF_QUERY_INTERFACE 
    (m_meshFactW->GetMeshObjectFactory (), iSprite3DFactoryState);

  if (!Create (p_name))
    return false;

  csBox3 l_box;
  GetBoundingBox (l_box);

  if (p_hardTransf != NULL)
    m_meshFactW->HardTransform (*p_hardTransf);

  if (!Resize (p_x, p_y, p_z))
    return false;

  //
  //Find and set the material.
  iMaterialWrapper* l_matW = m_engine->GetMaterialList ()->FindByName 
    (p_materialName);
  if (l_matW == 0)
    return false;
  SetMaterial (l_matW);



  //
  //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 ());
  
  SetBaseColor (csColor(0.2f, 0.2f, 0.2f));


  //store the arguments values.
  SetName (p_name);
  m_modelName.AttachNew (new scfString (p_mFName));
  m_materialName.AttachNew (new scfString (p_materialName));

  //init of some member vars.
  m_constructedProperly = true;

  return true;
}


bool KSprite3D::IsConstructedProperly  ()
{
  return m_constructedProperly;
}

bool KSprite3D::Resize  (float p_xSize, float p_ySize, float p_zSize)
{
  if (!CenterToOrigin ())
    return false;

  if (!Rescale (p_xSize, p_ySize, p_zSize))
    return false;

  OverTheFloor ();
  
  return true;
}

bool KSprite3D::LoadMeshFactory (const char* p_mFName)
{
  m_meshFactW = m_loader->LoadMeshObjectFactory(p_mFName);
  if(m_meshFactW == NULL)
  {
    SKE::Report (CS_REPORTER_SEVERITY_WARNING,
    		"Error loading mesh object factory!");
    return false;
  }//if

  return true;
}

iString* KSprite3D::GetModelName ()
{
  return m_modelName;
}
float KSprite3D::GetXScale ()
{
  return m_xScale;
}
float KSprite3D::GetYScale ()
{
  return m_yScale;
}
float KSprite3D::GetZScale ()
{
  return m_zScale;
}

//create the sprite.
bool KSprite3D::Create (const char* p_name)
{  
  m_meshW = m_engine->CreateMeshWrapper (m_meshFactW, p_name, 0, 0);

  //Attach to the KSprite3D its mesh wrapper.
  this->ObjAdd (m_meshW->QueryObject ());//???????

  //Set the default iKDraggable interface.
  csRef<iKDraggable> l_d = SCF_QUERY_INTERFACE (this, iKDraggable);
  SetDraggable (l_d);

  //??Is this needed?
  m_meshW->GetMeshObject ()->SetMeshWrapper (m_meshW);

  return true;
}

void KSprite3D::Release ()
{
  if (m_meshW)
    m_meshW->QueryObject ()->ObjRemoveAll ();
}

bool KSprite3D::SetDraggable (iKDraggable* p_draggable)
{
  //Remove the old draggable, if any.
  csRef<iKDraggable> l_d = CS_GET_CHILD_OBJECT
    (m_meshW->QueryObject(), iKDraggable);
  m_meshW->QueryObject ()->ObjRemove (l_d);

  //Attach the draggable object to the mesh wrapper.
  m_meshW->QueryObject ()->ObjAdd (p_draggable);
  
  return true;
}


iMovable& KSprite3D::GetMovable()
{
  return *(m_meshW-> GetMovable ());
}

void KSprite3D::SetBaseColor (const csColor& p_color)
{
  m_state->SetBaseColor (p_color);//set an ambient light for sprites!
}

void KSprite3D::SetAction (const char* p_actionName)
{
  m_state->SetAction (p_actionName);
}


iMeshWrapper* KSprite3D::GetMeshWrapper ()
{
  return m_meshW;
};

iMeshFactoryWrapper& KSprite3D::GetMeshFactoryWrapper ()
{
  return *m_meshFactW;
};

iSprite3DState& KSprite3D::GetState ()
{
  return *m_state;
};

void KSprite3D::SetPosition (const csVector3& p_pos )
{
  m_meshW->GetMovable()->SetPosition(p_pos);
}

void KSprite3D::SetYRotationPosition (float p_yRotation, const csVector3& p_pos )
{
  m_yRotation = p_yRotation;
  m_meshW->GetMovable()->SetPosition (p_pos);
  csYRotMatrix3 l_yRot (p_yRotation);
  m_meshW->GetMovable()-> SetTransform (l_yRot);
}

float KSprite3D::GetYRotation ()
{
  return m_yRotation;
}

void KSprite3D::SetYRotation (float p_yRotation)
{
  m_yRotation = p_yRotation;
  csYRotMatrix3 l_matY (m_yRotation);
  SetTransform (l_matY);
}

const csVector3& KSprite3D::GetPosition ( )
{
  return m_meshW->GetMovable()->GetPosition();
}

void KSprite3D::Transform (const csMatrix3& p_tr)
{
  m_meshW->GetMovable()-> Transform(p_tr);
}

void KSprite3D::SetTransform (const csMatrix3& p_tr)
{
  m_meshW->GetMovable()-> SetTransform(p_tr);
}

const csMatrix3& KSprite3D::GetTransform ()
{
  return m_meshW->GetMovable()-> GetTransform ().GetT2O ();
}

void KSprite3D::LookAt (const csVector3& p_to, const csVector3& p_up )
{
  m_meshW->GetMovable()->GetTransform ().LookAt(p_to, p_up);
}

void KSprite3D::GetBoundingBox (csBox3& p_bbox)
{
	csRef<iObjectModel> l_oM = m_meshW->GetMeshObject ()->GetObjectModel ();
	l_oM->GetObjectBoundingBox (p_bbox);
}

//rescale the object.
//p_x is the length should be the bounding box on the x axis.
bool KSprite3D::Rescale (float p_x, float p_y, float p_z)
{
  csBox3 l_bbox;
  GetBoundingBox (l_bbox);

  if ((p_x == 0) && (p_y == 0) && (p_z == 0))
    return true;

  float l_xCoeff = 1.0f;
  if (p_x != 0) 
    l_xCoeff = p_x / (l_bbox.MaxX() - l_bbox.MinX());

  float l_yCoeff = 1.0f;
  if (p_y != 0) 
    l_yCoeff = p_y / (l_bbox.MaxY() - l_bbox.MinY());
  
  float l_zCoeff = 1.0f;
  if (p_z != 0) 
    l_zCoeff = p_z / (l_bbox.MaxZ() - l_bbox.MinZ());

  csMatrix3 l_scaling =
    csMatrix3 (
    l_xCoeff, 0, 0,
	  0, l_yCoeff, 0,
    0, 0, l_zCoeff);
  csReversibleTransform l_rT;
  l_rT.SetT2O (l_scaling);
  l_rT.SetOrigin (csVector3 (0,0,0));
  m_meshFactW->HardTransform (l_rT);

  //
  //Store the new dimension values.
  m_xScale = p_x;
  m_yScale = p_y;
  m_zScale = p_z;

  return true;
}

//center to origin.
//?? this must be called before creating the actual instance of a sprite3d.
bool KSprite3D::CenterToOrigin ()
{  
  csBox3 l_bbox;
  GetBoundingBox (l_bbox);
  csVector3 l_center = l_bbox.GetCenter ();
  csMatrix3 m;
  csReversibleTransform l_rT (m, -l_center);
  m_meshFactW->HardTransform (l_rT);

  return true;
}

void KSprite3D::OverTheFloor ()
{
  csBox3 l_box;
  GetBoundingBox (l_box);
  csReversibleTransform l_rT;
  l_rT.SetT2O (csMatrix3());
  float l_yOffset = l_box.MinY ();
  l_rT.SetOrigin (csVector3 (0, -l_yOffset, 0));
  m_meshFactW->HardTransform (l_rT);
}


void KSprite3D::SetVisible (bool p_visible)
{
  m_visible = p_visible;
  csFlags& l_flags = m_meshW->GetFlags ();
  if(!m_visible)
    l_flags.Set(l_flags.Get () | CS_ENTITY_INVISIBLE);
  else
    l_flags.Reset(CS_ENTITY_INVISIBLE);
}

bool KSprite3D::IsVisible ()
{
  return m_visible;
}

//
//
void KSprite3D::SetSector (iSector* p_sector)
{
  m_meshW->GetMovable ()->SetSector (p_sector);
}

//
//
void KSprite3D::UpdateMove ()
{
  m_meshW->GetMovable ()->UpdateMove ();
}

//
//
void KSprite3D::SetZBufMode (csZBufMode p_zBufMode)
{
  m_meshW->SetZBufMode (p_zBufMode);
}

//
//
void KSprite3D::SetRenderPriority (int p_rP)
{
  m_meshW->SetRenderPriority (p_rP);
}

//
//
void KSprite3D::HardTransform (const csReversibleTransform& p_rT )
{
  m_meshFactW->HardTransform (p_rT);
}



bool KSprite3D::Write (iDocumentNode*) const
{
  //??? TO DO
  return true;
}

bool KSprite3D::Read (iDocumentNode*)
{
  //??? TO DO
  return true;
}


bool KSprite3D::SetMaterial (iMaterialWrapper* p_mat)
{
  m_factState-> SetMaterialWrapper (p_mat);

  return true;
}

iMaterialWrapper* KSprite3D::GetMaterial ()
{
  return m_factState-> GetMaterialWrapper ();
}


bool KSprite3D::SetMaterialName (const char* p_matName)
{
  iMaterialWrapper* l_matW = m_engine->GetMaterialList ()->FindByName 
    (p_matName);
  if (l_matW == NULL)
    return false;
  
  SetMaterial (l_matW);

  return true;
}

const char* KSprite3D::GetMaterialName ()
{
  return m_factState-> GetMaterialWrapper ()->QueryObject ()->GetName ();
}



const csRefArray<iMeshWrapper> KSprite3D::eiKDraggable::GetMeshWrappers () const
{
  csRefArray<iMeshWrapper> l_mWL;

  csRef<iMeshWrapper> l_mW = CS_GET_CHILD_OBJECT (scfParent, iMeshWrapper);
  l_mWL.Push (l_mW);

  return l_mWL;
}




See more files for this project here

Spatial Knowledge Experiments

A simulation of 3D virtual worlds for psychological experiments

Project homepage: http://sourceforge.net/projects/ske
Programming language(s): C,C++,Perl
License: other

  isense/
  joystick/
  Jamfile
  KImageCardinalDirection.h
  SKE.cpp
  SKE.h
  eulerangles.c
  eulerangles.h
  ikdraggable.cpp
  ikdraggable.h
  ikdraghandler.h
  ikmission.h
  ikxmlreader.h
  ikxmlwriter.h
  kappstate.cpp
  kappstate.h
  kbasedraggable.cpp
  kbasedraggable.h
  kbasedraghandler.cpp
  kbasedraghandler.h
  kblock.cpp
  kblock.h
  kblockId.h
  kblockid.cpp
  kcamera.cpp
  kcamera.h
  kcollision.cpp
  kcommandprocessor.cpp
  kcommandprocessor.h
  kconstant.cpp
  kconstant.h
  kcursor3d.cpp
  kcursor3d.h
  kdata.cpp
  kdata.h
  kdatalist.cpp
  kdatalist.h
  kdragmanager.cpp
  kdragmanager.h
  keventhandlermanager.cpp
  keventhandlermanager.h
  kflags.cpp
  kflags.h
  kimagecardinaldirection.cpp
  kkeys.cpp
  kkeys.h
  kkeystate.cpp
  kkeystate.h
  klandmark.cpp
  klandmark.h
  klandmarklist.cpp
  klandmarklist.h
  klight.cpp
  klight.h
  klightlist.cpp
  klightlist.h
  kmap.cpp
  kmap.h
  kmission.cpp
  kmission.h
  kmissiontype.cpp
  kmissiontype.h
  kmode.cpp
  kmode.h
  kmousemanager.cpp
  kmousemanager.h
  kobject3d.cpp
  kobject3d.h
  korientation.cpp
  korientation.h
  kpath.cpp
  kpath.h
  kpathfollower.cpp
  kpathfollower.h
  kplayer.cpp
  kplayer.h
  kposition.cpp
  kposition.h
  kquaternion.cpp
  kquaternion.h
  ksaveddatamanager.cpp
  ksaveddatamanager.h
  ksegment3.h
  ksign.cpp
  ksign.h
  ksprite3d.cpp
  ksprite3d.h
  ksprite3dlist.cpp
  ksprite3dlist.h
  kterrain.cpp
  kterrain.h
  kthing.cpp
  kthing.h
  kutil.cpp
  kutil.h
  kwireframe.cpp
  kwireframe.h
  kxmlhelper.h
  movement1d.h
  movement3d.h
  quattypes.h