Adds Component, the various main component classes and their interfaces.

This commit is contained in:
Areloch 2016-05-14 00:00:02 -05:00
parent 2e339bafba
commit fa78a2f354
37 changed files with 9736 additions and 0 deletions

View file

@ -0,0 +1,483 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "T3D/Components/camera/CameraComponent.h"
#include "T3D/Components/Camera/CameraComponent_ScriptBinding.h"
#include "platform/platform.h"
#include "console/consoleTypes.h"
#include "core/util/safeDelete.h"
#include "core/resourceManager.h"
#include "core/stream/fileStream.h"
#include "console/consoleTypes.h"
#include "console/consoleObject.h"
#include "ts/tsShapeInstance.h"
#include "core/stream/bitStream.h"
#include "gfx/gfxTransformSaver.h"
#include "console/engineAPI.h"
#include "lighting/lightQuery.h"
#include "T3D/gameBase/gameConnection.h"
#include "T3D/gameFunctions.h"
#include "math/mathUtils.h"
#include "T3D/Components/render/renderComponentInterface.h"
IMPLEMENT_CALLBACK( CameraComponent, validateCameraFov, F32, (F32 fov), (fov),
"@brief Called on the server when the client has requested a FOV change.\n\n"
"When the client requests that its field of view should be changed (because "
"they want to use a sniper scope, for example) this new FOV needs to be validated "
"by the server. This method is called if it exists (it is optional) to validate "
"the requested FOV, and modify it if necessary. This could be as simple as checking "
"that the FOV falls within a correct range, to making sure that the FOV matches the "
"capabilities of the current weapon.\n\n"
"Following this method, ShapeBase ensures that the given FOV still falls within "
"the datablock's mCameraMinFov and mCameraMaxFov. If that is good enough for your "
"purposes, then you do not need to define the validateCameraFov() callback for "
"your ShapeBase.\n\n"
"@param fov The FOV that has been requested by the client.\n"
"@return The FOV as validated by the server.\n\n"
"@see ShapeBaseData\n\n");
//////////////////////////////////////////////////////////////////////////
// Constructor/Destructor
//////////////////////////////////////////////////////////////////////////
CameraComponent::CameraComponent() : Component()
{
mClientScreen = Point2F(1, 1);
mCameraFov = mCameraDefaultFov = 80;
mCameraMinFov = 5;
mCameraMaxFov = 175;
mTargetNodeIdx = -1;
mPosOffset = Point3F(0, 0, 0);
mRotOffset = EulerF(0, 0, 0);
mTargetNode = "";
mUseParentTransform = true;
mFriendlyName = "Camera(Component)";
}
CameraComponent::~CameraComponent()
{
for(S32 i = 0;i < mFields.size();++i)
{
ComponentField &field = mFields[i];
SAFE_DELETE_ARRAY(field.mFieldDescription);
}
SAFE_DELETE_ARRAY(mDescription);
}
IMPLEMENT_CO_NETOBJECT_V1(CameraComponent);
bool CameraComponent::onAdd()
{
if(! Parent::onAdd())
return false;
return true;
}
void CameraComponent::onRemove()
{
Parent::onRemove();
}
void CameraComponent::initPersistFields()
{
Parent::initPersistFields();
addProtectedField("FOV", TypeF32, Offset(mCameraFov, CameraComponent), &_setCameraFov, defaultProtectedGetFn, "");
addField("MinFOV", TypeF32, Offset(mCameraMinFov, CameraComponent), "");
addField("MaxFOV", TypeF32, Offset(mCameraMaxFov, CameraComponent), "");
addField("ScreenAspect", TypePoint2I, Offset(mClientScreen, CameraComponent), "");
addProtectedField("targetNode", TypeString, Offset(mTargetNode, CameraComponent), &_setNode, defaultProtectedGetFn, "");
addProtectedField("positionOffset", TypePoint3F, Offset(mPosOffset, CameraComponent), &_setPosOffset, defaultProtectedGetFn, "");
addProtectedField("rotationOffset", TypeRotationF, Offset(mRotOffset, CameraComponent), &_setRotOffset, defaultProtectedGetFn, "");
addField("useParentTransform", TypeBool, Offset(mUseParentTransform, CameraComponent), "");
}
bool CameraComponent::_setNode(void *object, const char *index, const char *data)
{
CameraComponent *mcc = static_cast<CameraComponent*>(object);
mcc->mTargetNode = StringTable->insert(data);
mcc->setMaskBits(OffsetMask);
return true;
}
bool CameraComponent::_setPosOffset(void *object, const char *index, const char *data)
{
CameraComponent *mcc = static_cast<CameraComponent*>(object);
if (mcc)
{
Point3F pos;
Con::setData(TypePoint3F, &pos, 0, 1, &data);
mcc->mPosOffset = pos;
mcc->setMaskBits(OffsetMask);
return true;
}
return false;
}
bool CameraComponent::_setRotOffset(void *object, const char *index, const char *data)
{
CameraComponent *mcc = static_cast<CameraComponent*>(object);
if (mcc)
{
RotationF rot;
Con::setData(TypeRotationF, &rot, 0, 1, &data);
mcc->mRotOffset = rot;
mcc->setMaskBits(OffsetMask);
return true;
}
return false;
}
bool CameraComponent::isValidCameraFov(F32 fov)
{
return((fov >= mCameraMinFov) && (fov <= mCameraMaxFov));
}
bool CameraComponent::_setCameraFov(void *object, const char *index, const char *data)
{
CameraComponent *cCI = static_cast<CameraComponent*>(object);
cCI->setCameraFov(dAtof(data));
return true;
}
void CameraComponent::setCameraFov(F32 fov)
{
mCameraFov = mClampF(fov, mCameraMinFov, mCameraMaxFov);
if (isClientObject())
GameSetCameraTargetFov(mCameraFov);
if (isServerObject())
setMaskBits(FOVMask);
}
void CameraComponent::onCameraScopeQuery(NetConnection *cr, CameraScopeQuery * query)
{
// update the camera query
query->camera = this;
if(GameConnection * con = dynamic_cast<GameConnection*>(cr))
{
// get the fov from the connection (in deg)
F32 fov;
if (con->getControlCameraFov(&fov))
{
query->fov = mDegToRad(fov/2);
query->sinFov = mSin(query->fov);
query->cosFov = mCos(query->fov);
}
else
{
query->fov = mDegToRad(mCameraFov/2);
query->sinFov = mSin(query->fov);
query->cosFov = mCos(query->fov);
}
}
// use eye rather than camera transform (good enough and faster)
MatrixF camTransform = mOwner->getTransform();
camTransform.getColumn(3, &query->pos);
camTransform.getColumn(1, &query->orientation);
// Get the visible distance.
if (mOwner->getSceneManager() != NULL)
query->visibleDistance = mOwner->getSceneManager()->getVisibleDistance();
}
bool CameraComponent::getCameraTransform(F32* pos,MatrixF* mat)
{
// Returns camera to world space transform
// Handles first person / third person camera position
bool isServer = isServerObject();
if (mTargetNodeIdx == -1)
{
if (mUseParentTransform)
{
MatrixF rMat = mOwner->getRenderTransform();
rMat.mul(mRotOffset.asMatrixF());
mat->set(rMat.toEuler(), rMat.getPosition() + mPosOffset);
}
else
{
mat->set(mRotOffset.asEulerF(), mPosOffset);
}
return true;
}
else
{
RenderComponentInterface *renderInterface = mOwner->getComponent<RenderComponentInterface>();
if (!renderInterface)
return false;
if (mUseParentTransform)
{
MatrixF rMat = mOwner->getRenderTransform();
Point3F position = rMat.getPosition();
RotationF rot = mRotOffset;
if (mTargetNodeIdx != -1)
{
Point3F nodPos;
MatrixF nodeTrans = renderInterface->getNodeTransform(mTargetNodeIdx);
nodeTrans.getColumn(3, &nodPos);
// Scale the camera position before applying the transform
const Point3F& scale = mOwner->getScale();
nodPos.convolve(scale);
mOwner->getRenderTransform().mulP(nodPos, &position);
nodeTrans.mul(rMat);
rot = nodeTrans;
}
position += mPosOffset;
MatrixF rotMat = rot.asMatrixF();
MatrixF rotOffsetMat = mRotOffset.asMatrixF();
rotMat.mul(rotOffsetMat);
rot = RotationF(rotMat);
mat->set(rot.asEulerF(), position);
}
else
{
MatrixF rMat = mOwner->getRenderTransform();
Point3F position = rMat.getPosition();
RotationF rot = mRotOffset;
if (mTargetNodeIdx != -1)
{
Point3F nodPos;
MatrixF nodeTrans = renderInterface->getNodeTransform(mTargetNodeIdx);
nodeTrans.getColumn(3, &nodPos);
// Scale the camera position before applying the transform
const Point3F& scale = mOwner->getScale();
nodPos.convolve(scale);
position = nodPos;
}
position += mPosOffset;
mat->set(rot.asEulerF(), position);
}
return true;
}
}
void CameraComponent::getCameraParameters(F32 *min, F32* max, Point3F* off, MatrixF* rot)
{
*min = 0.2f;
*max = 0.f;
off->set(0, 0, 0);
rot->identity();
}
U32 CameraComponent::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
{
U32 retmask = Parent::packUpdate(con, mask, stream);
if (stream->writeFlag(mask & FOVMask))
{
stream->write(mCameraFov);
}
if (stream->writeFlag(mask & OffsetMask))
{
RenderComponentInterface* renderInterface = getOwner()->getComponent<RenderComponentInterface>();
if (renderInterface && renderInterface->getShape())
{
S32 nodeIndex = renderInterface->getShape()->findNode(mTargetNode);
mTargetNodeIdx = nodeIndex;
}
stream->writeInt(mTargetNodeIdx, 32);
//send offsets here
stream->writeCompressedPoint(mPosOffset);
stream->writeCompressedPoint(mRotOffset.asEulerF());
stream->writeFlag(mUseParentTransform);
}
return retmask;
}
void CameraComponent::unpackUpdate(NetConnection *con, BitStream *stream)
{
Parent::unpackUpdate(con, stream);
if (stream->readFlag())
{
F32 fov;
stream->read(&fov);
setCameraFov(fov);
}
if(stream->readFlag())
{
mTargetNodeIdx = stream->readInt(32);
stream->readCompressedPoint(&mPosOffset);
EulerF rot;
stream->readCompressedPoint(&rot);
mRotOffset = RotationF(rot);
mUseParentTransform = stream->readFlag();
}
}
void CameraComponent::setForwardVector(VectorF newForward, VectorF upVector)
{
MatrixF mat;
F32 pos = 0;
getCameraTransform(&pos, &mat);
mPosOffset = mat.getPosition();
VectorF up(0.0f, 0.0f, 1.0f);
VectorF axisX;
VectorF axisY = newForward;
VectorF axisZ;
if (upVector != VectorF::Zero)
up = upVector;
// Validate and normalize input:
F32 lenSq;
lenSq = axisY.lenSquared();
if (lenSq < 0.000001f)
{
axisY.set(0.0f, 1.0f, 0.0f);
Con::errorf("Entity::setForwardVector() - degenerate forward vector");
}
else
{
axisY /= mSqrt(lenSq);
}
lenSq = up.lenSquared();
if (lenSq < 0.000001f)
{
up.set(0.0f, 0.0f, 1.0f);
Con::errorf("SceneObject::setForwardVector() - degenerate up vector - too small");
}
else
{
up /= mSqrt(lenSq);
}
if (fabsf(mDot(up, axisY)) > 0.9999f)
{
Con::errorf("SceneObject::setForwardVector() - degenerate up vector - same as forward");
// i haven't really tested this, but i think it generates something which should be not parallel to the previous vector:
F32 tmp = up.x;
up.x = -up.y;
up.y = up.z;
up.z = tmp;
}
// construct the remaining axes:
mCross(axisY, up, &axisX);
mCross(axisX, axisY, &axisZ);
mat.setColumn(0, axisX);
mat.setColumn(1, axisY);
mat.setColumn(2, axisZ);
mRotOffset = RotationF(mat.toEuler());
mRotOffset.y = 0;
setMaskBits(OffsetMask);
}
void CameraComponent::setPosition(Point3F newPos)
{
mPosOffset = newPos;
setMaskBits(OffsetMask);
}
void CameraComponent::setRotation(RotationF newRot)
{
mRotOffset = newRot;
setMaskBits(OffsetMask);
}
Frustum CameraComponent::getFrustum()
{
Frustum visFrustum;
F32 left, right, top, bottom;
F32 aspectRatio = mClientScreen.x / mClientScreen.y;
visFrustum.set(false, mDegToRad(mCameraFov), aspectRatio, 0.1f, 1000, mOwner->getTransform());
return visFrustum;
}

View file

@ -0,0 +1,159 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef CAMERA_COMPONENT_H
#define CAMERA_COMPONENT_H
#ifndef COMPONENT_H
#include "T3D/Components/Component.h"
#endif
#ifndef _SCENERENDERSTATE_H_
#include "scene/sceneRenderState.h"
#endif
#ifndef _MBOX_H_
#include "math/mBox.h"
#endif
#ifndef ENTITY_H
#include "T3D/Entity.h"
#endif
#ifndef CORE_INTERFACES_H
#include "T3D/Components/coreInterfaces.h"
#endif
class SceneRenderState;
struct CameraScopeQuery;
//////////////////////////////////////////////////////////////////////////
///
///
//////////////////////////////////////////////////////////////////////////
class CameraComponent : public Component, public CameraInterface
{
typedef Component Parent;
F32 mCameraFov; ///< The camera vertical FOV in degrees.
Point2F mClientScreen; ///< The dimensions of the client's screen. Used to calculate the aspect ratio.
F32 mCameraDefaultFov; ///< Default vertical FOV in degrees.
F32 mCameraMinFov; ///< Min vertical FOV allowed in degrees.
F32 mCameraMaxFov; ///< Max vertical FOV allowed in degrees.
protected:
Point3F mPosOffset;
RotationF mRotOffset;
StringTableEntry mTargetNode;
S32 mTargetNodeIdx;
bool mUseParentTransform;
enum
{
FOVMask = Parent::NextFreeMask,
OffsetMask = Parent::NextFreeMask << 1,
NextFreeMask = Parent::NextFreeMask << 2,
};
public:
CameraComponent();
virtual ~CameraComponent();
DECLARE_CONOBJECT(CameraComponent);
virtual bool onAdd();
virtual void onRemove();
static void initPersistFields();
static bool _setCameraFov(void *object, const char *index, const char *data);
virtual U32 packUpdate(NetConnection *con, U32 mask, BitStream *stream);
virtual void unpackUpdate(NetConnection *con, BitStream *stream);
static bool _setNode(void *object, const char *index, const char *data);
static bool _setPosOffset(void *object, const char *index, const char *data);
static bool _setRotOffset(void *object, const char *index, const char *data);
void setRotOffset(RotationF rot)
{
mRotOffset = rot;
setMaskBits(OffsetMask);
}
RotationF getRotOffset()
{
return mRotOffset;
}
Point3F getPosOffset()
{
return mPosOffset;
}
/// Gets the minimum viewing distance, maximum viewing distance, camera offsetand rotation
/// for this object, if the world were to be viewed through its eyes
/// @param min Minimum viewing distance
/// @param max Maximum viewing distance
/// @param offset Offset of the camera from the origin in local space
/// @param rot Rotation matrix
virtual void getCameraParameters(F32 *min, F32* max, Point3F* offset, MatrixF* rot);
/// Gets the camera to world space transform matrix
/// @todo Find out what pos does
/// @param pos TODO: Find out what this does
/// @param mat Camera transform (out)
virtual bool getCameraTransform(F32* pos, MatrixF* mat);
/// Returns the vertical field of view in degrees for
/// this object if used as a camera.
virtual F32 getCameraFov() { return mCameraFov; }
/// Returns the default vertical field of view in degrees
/// if this object is used as a camera.
virtual F32 getDefaultCameraFov() { return mCameraDefaultFov; }
/// Sets the vertical field of view in degrees for this
/// object if used as a camera.
/// @param yfov The vertical FOV in degrees to test.
virtual void setCameraFov(F32 fov);
/// Returns true if the vertical FOV in degrees is within
/// allowable parameters of the datablock.
/// @param yfov The vertical FOV in degrees to test.
/// @see ShapeBaseData::cameraMinFov
/// @see ShapeBaseData::cameraMaxFov
virtual bool isValidCameraFov(F32 fov);
/// @}
virtual Frustum getFrustum();
/// Control object scoping
void onCameraScopeQuery(NetConnection *cr, CameraScopeQuery *camInfo);
void setForwardVector(VectorF newForward, VectorF upVector = VectorF::Zero);
void setPosition(Point3F newPos);
void setRotation(RotationF newRot);
protected:
DECLARE_CALLBACK(F32, validateCameraFov, (F32 fov));
};
#endif // CAMERA_BEHAVIOR_H

View file

@ -0,0 +1,91 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "console/engineAPI.h"
#include "T3D/Components/Camera/CameraComponent.h"
//Basically, this only exists for backwards compatibility for parts of the editors
ConsoleMethod(CameraComponent, getMode, const char*, 2, 2, "() - We get the first behavior of the requested type on our owner object.\n"
"@return (string name) The type of the behavior we're requesting")
{
return "fly";
}
DefineConsoleMethod(CameraComponent, getForwardVector, VectorF, (), ,
"Get the number of static fields on the object.\n"
"@return The number of static fields defined on the object.")
{
F32 pos = 0;
MatrixF cameraMat;
object->getCameraTransform(&pos, &cameraMat);
VectorF returnVec = cameraMat.getForwardVector();
returnVec = VectorF(mRadToDeg(returnVec.x), mRadToDeg(returnVec.y), mRadToDeg(returnVec.z));
returnVec.normalize();
return returnVec;
}
DefineConsoleMethod(CameraComponent, getRightVector, VectorF, (), ,
"Get the number of static fields on the object.\n"
"@return The number of static fields defined on the object.")
{
F32 pos = 0;
MatrixF cameraMat;
object->getCameraTransform(&pos, &cameraMat);
VectorF returnVec = cameraMat.getRightVector();
returnVec = VectorF(mRadToDeg(returnVec.x), mRadToDeg(returnVec.y), mRadToDeg(returnVec.z));
returnVec.normalize();
return returnVec;
}
DefineConsoleMethod(CameraComponent, getUpVector, VectorF, (), ,
"Get the number of static fields on the object.\n"
"@return The number of static fields defined on the object.")
{
F32 pos = 0;
MatrixF cameraMat;
object->getCameraTransform(&pos, &cameraMat);
VectorF returnVec = cameraMat.getUpVector();
returnVec = VectorF(mRadToDeg(returnVec.x), mRadToDeg(returnVec.y), mRadToDeg(returnVec.z));
returnVec.normalize();
return returnVec;
}
DefineConsoleMethod(CameraComponent, setForwardVector, void, (VectorF newForward), (VectorF(0, 0, 0)),
"Get the number of static fields on the object.\n"
"@return The number of static fields defined on the object.")
{
object->setForwardVector(newForward);
}
DefineConsoleMethod(CameraComponent, getWorldPosition, Point3F, (), ,
"Get the number of static fields on the object.\n"
"@return The number of static fields defined on the object.")
{
F32 pos = 0;
MatrixF mat;
object->getCameraTransform(&pos, &mat);
return mat.getPosition();
}

View file

@ -0,0 +1,146 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "T3D/Components/Camera/CameraOrbiterComponent.h"
#include "core/util/safeDelete.h"
#include "console/consoleTypes.h"
#include "console/consoleObject.h"
#include "core/stream/bitStream.h"
#include "console/engineAPI.h"
#include "sim/netConnection.h"
#include "math/mathUtils.h"
//////////////////////////////////////////////////////////////////////////
// Constructor/Destructor
//////////////////////////////////////////////////////////////////////////
CameraOrbiterComponent::CameraOrbiterComponent() : Component()
{
mMinOrbitDist = 0.0f;
mMaxOrbitDist = 0.0f;
mCurOrbitDist = 8.0f;
mPosition.set(0.0f, 0.0f, 0.0f);
mMaxPitchAngle = 70;
mMinPitchAngle = -10;
mRotation.set(0, 0, 0);
mCamera = NULL;
}
CameraOrbiterComponent::~CameraOrbiterComponent()
{
}
IMPLEMENT_CO_NETOBJECT_V1(CameraOrbiterComponent);
bool CameraOrbiterComponent::onAdd()
{
if (!Parent::onAdd())
return false;
return true;
}
void CameraOrbiterComponent::onRemove()
{
Parent::onRemove();
}
void CameraOrbiterComponent::initPersistFields()
{
Parent::initPersistFields();
addField("orbitDistance", TypeF32, Offset(mCurOrbitDist, CameraOrbiterComponent), "Object world orientation.");
addField("Rotation", TypeRotationF, Offset(mRotation, CameraOrbiterComponent), "Object world orientation.");
addField("maxPitchAngle", TypeF32, Offset(mMaxPitchAngle, CameraOrbiterComponent), "Object world orientation.");
addField("minPitchAngle", TypeF32, Offset(mMinPitchAngle, CameraOrbiterComponent), "Object world orientation.");
}
//This is mostly a catch for situations where the behavior is re-added to the object and the like and we may need to force an update to the behavior
void CameraOrbiterComponent::onComponentAdd()
{
Parent::onComponentAdd();
CameraComponent *cam = mOwner->getComponent<CameraComponent>();
if (cam)
{
mCamera = cam;
}
}
void CameraOrbiterComponent::onComponentRemove()
{
Parent::onComponentRemove();
}
U32 CameraOrbiterComponent::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
{
U32 retMask = Parent::packUpdate(con, mask, stream);
return retMask;
}
void CameraOrbiterComponent::unpackUpdate(NetConnection *con, BitStream *stream)
{
Parent::unpackUpdate(con, stream);
}
void CameraOrbiterComponent::processTick()
{
Parent::processTick();
if (!mOwner)
return;
if (mCamera)
{
//Clamp our pitch to whatever range we allow, first.
mRotation.x = mClampF(mRotation.x, mDegToRad(mMinPitchAngle), mDegToRad(mMaxPitchAngle));
MatrixF ownerTrans = mOwner->getRenderTransform();
Point3F ownerPos = ownerTrans.getPosition();
Point3F pos;
pos.x = mCurOrbitDist * mSin(mRotation.x + M_HALFPI_F) * mCos(-1.0f * (mRotation.z + M_HALFPI_F));
pos.y = mCurOrbitDist * mSin(mRotation.x + M_HALFPI_F) * mSin(-1.0f * (mRotation.z + M_HALFPI_F));
pos.z = mCurOrbitDist * mSin(mRotation.x);
//orient the camera towards the owner
VectorF ownerVec = ownerPos - pos;
ownerVec.normalize();
MatrixF xRot, zRot, cameraMatrix;
xRot.set(EulerF(mRotation.x, 0.0f, 0.0f));
zRot.set(EulerF(0.0f, 0.0f, mRotation.z));
cameraMatrix.mul(zRot, xRot);
cameraMatrix.getColumn(1, &ownerVec);
cameraMatrix.setColumn(3, pos - ownerVec * pos);
RotationF camRot = RotationF(cameraMatrix);
if (camRot != mCamera->getRotOffset())
mCamera->setRotation(camRot);
if (pos != mCamera->getPosOffset())
mCamera->setPosition(pos);
}
}

View file

@ -0,0 +1,71 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef CAMERA_ORBITER_COMPONENT_H
#define CAMERA_ORBITER_COMPONENT_H
#ifndef COMPONENT_H
#include "T3D/Components/Component.h"
#endif
#ifndef CAMERA_COMPONENT_H
#include "T3D/Components/camera/cameraComponent.h"
#endif
//////////////////////////////////////////////////////////////////////////
///
///
//////////////////////////////////////////////////////////////////////////
class CameraOrbiterComponent : public Component
{
typedef Component Parent;
F32 mMinOrbitDist;
F32 mMaxOrbitDist;
F32 mCurOrbitDist;
Point3F mPosition;
F32 mMaxPitchAngle;
F32 mMinPitchAngle;
RotationF mRotation;
CameraComponent* mCamera;
public:
CameraOrbiterComponent();
virtual ~CameraOrbiterComponent();
DECLARE_CONOBJECT(CameraOrbiterComponent);
virtual bool onAdd();
virtual void onRemove();
static void initPersistFields();
virtual void onComponentAdd();
virtual void onComponentRemove();
virtual U32 packUpdate(NetConnection *con, U32 mask, BitStream *stream);
virtual void unpackUpdate(NetConnection *con, BitStream *stream);
virtual void processTick();
};
#endif // EXAMPLEBEHAVIOR_H