cameraComponent.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. mNetworked = true;
  70. mFriendlyName = "Camera(Component)";
  71. }
  72. CameraComponent::~CameraComponent()
  73. {
  74. for(S32 i = 0;i < mFields.size();++i)
  75. {
  76. ComponentField &field = mFields[i];
  77. SAFE_DELETE_ARRAY(field.mFieldDescription);
  78. }
  79. SAFE_DELETE_ARRAY(mDescription);
  80. }
  81. IMPLEMENT_CO_NETOBJECT_V1(CameraComponent);
  82. bool CameraComponent::onAdd()
  83. {
  84. if(! Parent::onAdd())
  85. return false;
  86. return true;
  87. }
  88. void CameraComponent::onRemove()
  89. {
  90. Parent::onRemove();
  91. }
  92. void CameraComponent::initPersistFields()
  93. {
  94. Parent::initPersistFields();
  95. addProtectedField("FOV", TypeF32, Offset(mCameraFov, CameraComponent), &_setCameraFov, defaultProtectedGetFn, "");
  96. addField("MinFOV", TypeF32, Offset(mCameraMinFov, CameraComponent), "");
  97. addField("MaxFOV", TypeF32, Offset(mCameraMaxFov, CameraComponent), "");
  98. addField("ScreenAspect", TypePoint2I, Offset(mClientScreen, CameraComponent), "");
  99. addProtectedField("targetNode", TypeString, Offset(mTargetNode, CameraComponent), &_setNode, defaultProtectedGetFn, "");
  100. addProtectedField("positionOffset", TypePoint3F, Offset(mPosOffset, CameraComponent), &_setPosOffset, defaultProtectedGetFn, "");
  101. addProtectedField("rotationOffset", TypeRotationF, Offset(mRotOffset, CameraComponent), &_setRotOffset, defaultProtectedGetFn, "");
  102. addField("useParentTransform", TypeBool, Offset(mUseParentTransform, CameraComponent), "");
  103. }
  104. bool CameraComponent::_setNode(void *object, const char *index, const char *data)
  105. {
  106. CameraComponent *mcc = static_cast<CameraComponent*>(object);
  107. mcc->mTargetNode = StringTable->insert(data);
  108. mcc->setMaskBits(OffsetMask);
  109. return true;
  110. }
  111. bool CameraComponent::_setPosOffset(void *object, const char *index, const char *data)
  112. {
  113. CameraComponent *mcc = static_cast<CameraComponent*>(object);
  114. if (mcc)
  115. {
  116. Point3F pos;
  117. Con::setData(TypePoint3F, &pos, 0, 1, &data);
  118. mcc->mPosOffset = pos;
  119. mcc->setMaskBits(OffsetMask);
  120. return true;
  121. }
  122. return false;
  123. }
  124. bool CameraComponent::_setRotOffset(void *object, const char *index, const char *data)
  125. {
  126. CameraComponent *mcc = static_cast<CameraComponent*>(object);
  127. if (mcc)
  128. {
  129. RotationF rot;
  130. Con::setData(TypeRotationF, &rot, 0, 1, &data);
  131. mcc->mRotOffset = rot;
  132. mcc->setMaskBits(OffsetMask);
  133. return true;
  134. }
  135. return false;
  136. }
  137. bool CameraComponent::isValidCameraFov(F32 fov)
  138. {
  139. return((fov >= mCameraMinFov) && (fov <= mCameraMaxFov));
  140. }
  141. bool CameraComponent::_setCameraFov(void *object, const char *index, const char *data)
  142. {
  143. CameraComponent *cCI = static_cast<CameraComponent*>(object);
  144. cCI->setCameraFov(dAtof(data));
  145. return true;
  146. }
  147. void CameraComponent::setCameraFov(F32 fov)
  148. {
  149. mCameraFov = mClampF(fov, mCameraMinFov, mCameraMaxFov);
  150. if (isClientObject())
  151. GameSetCameraTargetFov(mCameraFov);
  152. if (isServerObject())
  153. setMaskBits(FOVMask);
  154. }
  155. void CameraComponent::onCameraScopeQuery(NetConnection *cr, CameraScopeQuery * query)
  156. {
  157. // update the camera query
  158. query->camera = mOwner;//this;
  159. if(GameConnection * con = dynamic_cast<GameConnection*>(cr))
  160. {
  161. // get the fov from the connection (in deg)
  162. F32 fov;
  163. if (con->getControlCameraFov(&fov))
  164. {
  165. query->fov = mDegToRad(fov/2);
  166. query->sinFov = mSin(query->fov);
  167. query->cosFov = mCos(query->fov);
  168. }
  169. else
  170. {
  171. query->fov = mDegToRad(mCameraFov/2);
  172. query->sinFov = mSin(query->fov);
  173. query->cosFov = mCos(query->fov);
  174. }
  175. }
  176. // use eye rather than camera transform (good enough and faster)
  177. MatrixF camTransform = mOwner->getTransform();
  178. camTransform.getColumn(3, &query->pos);
  179. camTransform.getColumn(1, &query->orientation);
  180. // Get the visible distance.
  181. if (mOwner->getSceneManager() != NULL)
  182. query->visibleDistance = mOwner->getSceneManager()->getVisibleDistance();
  183. }
  184. bool CameraComponent::getCameraTransform(F32* pos,MatrixF* mat)
  185. {
  186. // Returns camera to world space transform
  187. // Handles first person / third person camera position
  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. if(stream->writeFlag(mTargetNodeIdx > -1))
  275. stream->writeInt(mTargetNodeIdx, 32);
  276. //send offsets here
  277. stream->writeCompressedPoint(mPosOffset);
  278. stream->writeCompressedPoint(mRotOffset.asEulerF());
  279. stream->writeFlag(mUseParentTransform);
  280. }
  281. return retmask;
  282. }
  283. void CameraComponent::unpackUpdate(NetConnection *con, BitStream *stream)
  284. {
  285. Parent::unpackUpdate(con, stream);
  286. if (stream->readFlag())
  287. {
  288. F32 fov;
  289. stream->read(&fov);
  290. setCameraFov(fov);
  291. }
  292. if(stream->readFlag())
  293. {
  294. if (stream->readFlag())
  295. mTargetNodeIdx = stream->readInt(32);
  296. else
  297. mTargetNodeIdx = -1;
  298. stream->readCompressedPoint(&mPosOffset);
  299. EulerF rot;
  300. stream->readCompressedPoint(&rot);
  301. mRotOffset = RotationF(rot);
  302. mUseParentTransform = stream->readFlag();
  303. }
  304. }
  305. void CameraComponent::setForwardVector(VectorF newForward, VectorF upVector)
  306. {
  307. MatrixF mat;
  308. F32 pos = 0;
  309. getCameraTransform(&pos, &mat);
  310. mPosOffset = mat.getPosition();
  311. VectorF up(0.0f, 0.0f, 1.0f);
  312. VectorF axisX;
  313. VectorF axisY = newForward;
  314. VectorF axisZ;
  315. if (upVector != VectorF::Zero)
  316. up = upVector;
  317. // Validate and normalize input:
  318. F32 lenSq;
  319. lenSq = axisY.lenSquared();
  320. if (lenSq < 0.000001f)
  321. {
  322. axisY.set(0.0f, 1.0f, 0.0f);
  323. Con::errorf("Entity::setForwardVector() - degenerate forward vector");
  324. }
  325. else
  326. {
  327. axisY /= mSqrt(lenSq);
  328. }
  329. lenSq = up.lenSquared();
  330. if (lenSq < 0.000001f)
  331. {
  332. up.set(0.0f, 0.0f, 1.0f);
  333. Con::errorf("SceneObject::setForwardVector() - degenerate up vector - too small");
  334. }
  335. else
  336. {
  337. up /= mSqrt(lenSq);
  338. }
  339. if (fabsf(mDot(up, axisY)) > 0.9999f)
  340. {
  341. Con::errorf("SceneObject::setForwardVector() - degenerate up vector - same as forward");
  342. // i haven't really tested this, but i think it generates something which should be not parallel to the previous vector:
  343. F32 tmp = up.x;
  344. up.x = -up.y;
  345. up.y = up.z;
  346. up.z = tmp;
  347. }
  348. // construct the remaining axes:
  349. mCross(axisY, up, &axisX);
  350. mCross(axisX, axisY, &axisZ);
  351. mat.setColumn(0, axisX);
  352. mat.setColumn(1, axisY);
  353. mat.setColumn(2, axisZ);
  354. mRotOffset = RotationF(mat.toEuler());
  355. mRotOffset.y = 0;
  356. setMaskBits(OffsetMask);
  357. }
  358. void CameraComponent::setPosition(Point3F newPos)
  359. {
  360. mPosOffset = newPos;
  361. setMaskBits(OffsetMask);
  362. }
  363. void CameraComponent::setRotation(RotationF newRot)
  364. {
  365. mRotOffset = newRot;
  366. setMaskBits(OffsetMask);
  367. }
  368. Frustum CameraComponent::getFrustum()
  369. {
  370. Frustum visFrustum;
  371. F32 aspectRatio = mClientScreen.x / mClientScreen.y;
  372. visFrustum.set(false, mDegToRad(mCameraFov), aspectRatio, 0.1f, 1000, mOwner->getTransform());
  373. return visFrustum;
  374. }