cameraComponent.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "T3D/components/camera/cameracomponent.h"
  23. #include "T3D/components/camera/cameraComponent_ScriptBinding.h"
  24. #include "platform/platform.h"
  25. #include "console/consoleTypes.h"
  26. #include "core/util/safeDelete.h"
  27. #include "core/resourceManager.h"
  28. #include "core/stream/fileStream.h"
  29. #include "console/consoleTypes.h"
  30. #include "console/consoleObject.h"
  31. #include "ts/tsShapeInstance.h"
  32. #include "core/stream/bitStream.h"
  33. #include "gfx/gfxTransformSaver.h"
  34. #include "console/engineAPI.h"
  35. #include "lighting/lightQuery.h"
  36. #include "T3D/gameBase/gameConnection.h"
  37. #include "T3D/gameFunctions.h"
  38. #include "math/mathUtils.h"
  39. #include "T3D/components/render/renderComponentInterface.h"
  40. IMPLEMENT_CALLBACK( CameraComponent, validateCameraFov, F32, (F32 fov), (fov),
  41. "@brief Called on the server when the client has requested a FOV change.\n\n"
  42. "When the client requests that its field of view should be changed (because "
  43. "they want to use a sniper scope, for example) this new FOV needs to be validated "
  44. "by the server. This method is called if it exists (it is optional) to validate "
  45. "the requested FOV, and modify it if necessary. This could be as simple as checking "
  46. "that the FOV falls within a correct range, to making sure that the FOV matches the "
  47. "capabilities of the current weapon.\n\n"
  48. "Following this method, ShapeBase ensures that the given FOV still falls within "
  49. "the datablock's mCameraMinFov and mCameraMaxFov. If that is good enough for your "
  50. "purposes, then you do not need to define the validateCameraFov() callback for "
  51. "your ShapeBase.\n\n"
  52. "@param fov The FOV that has been requested by the client.\n"
  53. "@return The FOV as validated by the server.\n\n"
  54. "@see ShapeBaseData\n\n");
  55. //////////////////////////////////////////////////////////////////////////
  56. // Constructor/Destructor
  57. //////////////////////////////////////////////////////////////////////////
  58. CameraComponent::CameraComponent() : Component()
  59. {
  60. mClientScreen = Point2F(1, 1);
  61. mCameraFov = mCameraDefaultFov = 80;
  62. mCameraMinFov = 5;
  63. mCameraMaxFov = 175;
  64. mTargetNodeIdx = -1;
  65. mPosOffset = Point3F(0, 0, 0);
  66. mRotOffset = EulerF(0, 0, 0);
  67. mTargetNode = "";
  68. mUseParentTransform = true;
  69. mFriendlyName = "Camera(Component)";
  70. }
  71. CameraComponent::~CameraComponent()
  72. {
  73. for(S32 i = 0;i < mFields.size();++i)
  74. {
  75. ComponentField &field = mFields[i];
  76. SAFE_DELETE_ARRAY(field.mFieldDescription);
  77. }
  78. SAFE_DELETE_ARRAY(mDescription);
  79. }
  80. IMPLEMENT_CO_NETOBJECT_V1(CameraComponent);
  81. bool CameraComponent::onAdd()
  82. {
  83. if(! Parent::onAdd())
  84. return false;
  85. return true;
  86. }
  87. void CameraComponent::onRemove()
  88. {
  89. Parent::onRemove();
  90. }
  91. void CameraComponent::initPersistFields()
  92. {
  93. Parent::initPersistFields();
  94. addProtectedField("FOV", TypeF32, Offset(mCameraFov, CameraComponent), &_setCameraFov, defaultProtectedGetFn, "");
  95. addField("MinFOV", TypeF32, Offset(mCameraMinFov, CameraComponent), "");
  96. addField("MaxFOV", TypeF32, Offset(mCameraMaxFov, CameraComponent), "");
  97. addField("ScreenAspect", TypePoint2I, Offset(mClientScreen, CameraComponent), "");
  98. addProtectedField("targetNode", TypeString, Offset(mTargetNode, CameraComponent), &_setNode, defaultProtectedGetFn, "");
  99. addProtectedField("positionOffset", TypePoint3F, Offset(mPosOffset, CameraComponent), &_setPosOffset, defaultProtectedGetFn, "");
  100. addProtectedField("rotationOffset", TypeRotationF, Offset(mRotOffset, CameraComponent), &_setRotOffset, defaultProtectedGetFn, "");
  101. addField("useParentTransform", TypeBool, Offset(mUseParentTransform, CameraComponent), "");
  102. }
  103. bool CameraComponent::_setNode(void *object, const char *index, const char *data)
  104. {
  105. CameraComponent *mcc = static_cast<CameraComponent*>(object);
  106. mcc->mTargetNode = StringTable->insert(data);
  107. mcc->setMaskBits(OffsetMask);
  108. return true;
  109. }
  110. bool CameraComponent::_setPosOffset(void *object, const char *index, const char *data)
  111. {
  112. CameraComponent *mcc = static_cast<CameraComponent*>(object);
  113. if (mcc)
  114. {
  115. Point3F pos;
  116. Con::setData(TypePoint3F, &pos, 0, 1, &data);
  117. mcc->mPosOffset = pos;
  118. mcc->setMaskBits(OffsetMask);
  119. return true;
  120. }
  121. return false;
  122. }
  123. bool CameraComponent::_setRotOffset(void *object, const char *index, const char *data)
  124. {
  125. CameraComponent *mcc = static_cast<CameraComponent*>(object);
  126. if (mcc)
  127. {
  128. RotationF rot;
  129. Con::setData(TypeRotationF, &rot, 0, 1, &data);
  130. mcc->mRotOffset = rot;
  131. mcc->setMaskBits(OffsetMask);
  132. return true;
  133. }
  134. return false;
  135. }
  136. bool CameraComponent::isValidCameraFov(F32 fov)
  137. {
  138. return((fov >= mCameraMinFov) && (fov <= mCameraMaxFov));
  139. }
  140. bool CameraComponent::_setCameraFov(void *object, const char *index, const char *data)
  141. {
  142. CameraComponent *cCI = static_cast<CameraComponent*>(object);
  143. cCI->setCameraFov(dAtof(data));
  144. return true;
  145. }
  146. void CameraComponent::setCameraFov(F32 fov)
  147. {
  148. mCameraFov = mClampF(fov, mCameraMinFov, mCameraMaxFov);
  149. if (isClientObject())
  150. GameSetCameraTargetFov(mCameraFov);
  151. if (isServerObject())
  152. setMaskBits(FOVMask);
  153. }
  154. void CameraComponent::onCameraScopeQuery(NetConnection *cr, CameraScopeQuery * query)
  155. {
  156. // update the camera query
  157. query->camera = this;
  158. if(GameConnection * con = dynamic_cast<GameConnection*>(cr))
  159. {
  160. // get the fov from the connection (in deg)
  161. F32 fov;
  162. if (con->getControlCameraFov(&fov))
  163. {
  164. query->fov = mDegToRad(fov/2);
  165. query->sinFov = mSin(query->fov);
  166. query->cosFov = mCos(query->fov);
  167. }
  168. else
  169. {
  170. query->fov = mDegToRad(mCameraFov/2);
  171. query->sinFov = mSin(query->fov);
  172. query->cosFov = mCos(query->fov);
  173. }
  174. }
  175. // use eye rather than camera transform (good enough and faster)
  176. MatrixF camTransform = mOwner->getTransform();
  177. camTransform.getColumn(3, &query->pos);
  178. camTransform.getColumn(1, &query->orientation);
  179. // Get the visible distance.
  180. if (mOwner->getSceneManager() != NULL)
  181. query->visibleDistance = mOwner->getSceneManager()->getVisibleDistance();
  182. }
  183. bool CameraComponent::getCameraTransform(F32* pos,MatrixF* mat)
  184. {
  185. // Returns camera to world space transform
  186. // Handles first person / third person camera position
  187. bool isServer = isServerObject();
  188. if (mTargetNodeIdx == -1)
  189. {
  190. if (mUseParentTransform)
  191. {
  192. MatrixF rMat = mOwner->getRenderTransform();
  193. rMat.mul(mRotOffset.asMatrixF());
  194. mat->set(rMat.toEuler(), rMat.getPosition() + mPosOffset);
  195. }
  196. else
  197. {
  198. mat->set(mRotOffset.asEulerF(), mPosOffset);
  199. }
  200. return true;
  201. }
  202. else
  203. {
  204. RenderComponentInterface *renderInterface = mOwner->getComponent<RenderComponentInterface>();
  205. if (!renderInterface)
  206. return false;
  207. if (mUseParentTransform)
  208. {
  209. MatrixF rMat = mOwner->getRenderTransform();
  210. Point3F position = rMat.getPosition();
  211. RotationF rot = mRotOffset;
  212. if (mTargetNodeIdx != -1)
  213. {
  214. Point3F nodPos;
  215. MatrixF nodeTrans = renderInterface->getNodeTransform(mTargetNodeIdx);
  216. nodeTrans.getColumn(3, &nodPos);
  217. // Scale the camera position before applying the transform
  218. const Point3F& scale = mOwner->getScale();
  219. nodPos.convolve(scale);
  220. mOwner->getRenderTransform().mulP(nodPos, &position);
  221. nodeTrans.mul(rMat);
  222. rot = nodeTrans;
  223. }
  224. position += mPosOffset;
  225. MatrixF rotMat = rot.asMatrixF();
  226. MatrixF rotOffsetMat = mRotOffset.asMatrixF();
  227. rotMat.mul(rotOffsetMat);
  228. rot = RotationF(rotMat);
  229. mat->set(rot.asEulerF(), position);
  230. }
  231. else
  232. {
  233. MatrixF rMat = mOwner->getRenderTransform();
  234. Point3F position = rMat.getPosition();
  235. RotationF rot = mRotOffset;
  236. if (mTargetNodeIdx != -1)
  237. {
  238. Point3F nodPos;
  239. MatrixF nodeTrans = renderInterface->getNodeTransform(mTargetNodeIdx);
  240. nodeTrans.getColumn(3, &nodPos);
  241. // Scale the camera position before applying the transform
  242. const Point3F& scale = mOwner->getScale();
  243. nodPos.convolve(scale);
  244. position = nodPos;
  245. }
  246. position += mPosOffset;
  247. mat->set(rot.asEulerF(), position);
  248. }
  249. return true;
  250. }
  251. }
  252. void CameraComponent::getCameraParameters(F32 *min, F32* max, Point3F* off, MatrixF* rot)
  253. {
  254. *min = 0.2f;
  255. *max = 0.f;
  256. off->set(0, 0, 0);
  257. rot->identity();
  258. }
  259. U32 CameraComponent::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
  260. {
  261. U32 retmask = Parent::packUpdate(con, mask, stream);
  262. if (stream->writeFlag(mask & FOVMask))
  263. {
  264. stream->write(mCameraFov);
  265. }
  266. if (stream->writeFlag(mask & OffsetMask))
  267. {
  268. RenderComponentInterface* renderInterface = getOwner()->getComponent<RenderComponentInterface>();
  269. if (renderInterface && renderInterface->getShape())
  270. {
  271. S32 nodeIndex = renderInterface->getShape()->findNode(mTargetNode);
  272. mTargetNodeIdx = nodeIndex;
  273. }
  274. stream->writeInt(mTargetNodeIdx, 32);
  275. //send offsets here
  276. stream->writeCompressedPoint(mPosOffset);
  277. stream->writeCompressedPoint(mRotOffset.asEulerF());
  278. stream->writeFlag(mUseParentTransform);
  279. }
  280. return retmask;
  281. }
  282. void CameraComponent::unpackUpdate(NetConnection *con, BitStream *stream)
  283. {
  284. Parent::unpackUpdate(con, stream);
  285. if (stream->readFlag())
  286. {
  287. F32 fov;
  288. stream->read(&fov);
  289. setCameraFov(fov);
  290. }
  291. if(stream->readFlag())
  292. {
  293. mTargetNodeIdx = stream->readInt(32);
  294. stream->readCompressedPoint(&mPosOffset);
  295. EulerF rot;
  296. stream->readCompressedPoint(&rot);
  297. mRotOffset = RotationF(rot);
  298. mUseParentTransform = stream->readFlag();
  299. }
  300. }
  301. void CameraComponent::setForwardVector(VectorF newForward, VectorF upVector)
  302. {
  303. MatrixF mat;
  304. F32 pos = 0;
  305. getCameraTransform(&pos, &mat);
  306. mPosOffset = mat.getPosition();
  307. VectorF up(0.0f, 0.0f, 1.0f);
  308. VectorF axisX;
  309. VectorF axisY = newForward;
  310. VectorF axisZ;
  311. if (upVector != VectorF::Zero)
  312. up = upVector;
  313. // Validate and normalize input:
  314. F32 lenSq;
  315. lenSq = axisY.lenSquared();
  316. if (lenSq < 0.000001f)
  317. {
  318. axisY.set(0.0f, 1.0f, 0.0f);
  319. Con::errorf("Entity::setForwardVector() - degenerate forward vector");
  320. }
  321. else
  322. {
  323. axisY /= mSqrt(lenSq);
  324. }
  325. lenSq = up.lenSquared();
  326. if (lenSq < 0.000001f)
  327. {
  328. up.set(0.0f, 0.0f, 1.0f);
  329. Con::errorf("SceneObject::setForwardVector() - degenerate up vector - too small");
  330. }
  331. else
  332. {
  333. up /= mSqrt(lenSq);
  334. }
  335. if (fabsf(mDot(up, axisY)) > 0.9999f)
  336. {
  337. Con::errorf("SceneObject::setForwardVector() - degenerate up vector - same as forward");
  338. // i haven't really tested this, but i think it generates something which should be not parallel to the previous vector:
  339. F32 tmp = up.x;
  340. up.x = -up.y;
  341. up.y = up.z;
  342. up.z = tmp;
  343. }
  344. // construct the remaining axes:
  345. mCross(axisY, up, &axisX);
  346. mCross(axisX, axisY, &axisZ);
  347. mat.setColumn(0, axisX);
  348. mat.setColumn(1, axisY);
  349. mat.setColumn(2, axisZ);
  350. mRotOffset = RotationF(mat.toEuler());
  351. mRotOffset.y = 0;
  352. setMaskBits(OffsetMask);
  353. }
  354. void CameraComponent::setPosition(Point3F newPos)
  355. {
  356. mPosOffset = newPos;
  357. setMaskBits(OffsetMask);
  358. }
  359. void CameraComponent::setRotation(RotationF newRot)
  360. {
  361. mRotOffset = newRot;
  362. setMaskBits(OffsetMask);
  363. }
  364. Frustum CameraComponent::getFrustum()
  365. {
  366. Frustum visFrustum;
  367. F32 left, right, top, bottom;
  368. F32 aspectRatio = mClientScreen.x / mClientScreen.y;
  369. visFrustum.set(false, mDegToRad(mCameraFov), aspectRatio, 0.1f, 1000, mOwner->getTransform());
  370. return visFrustum;
  371. }