coverPoint.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 Daniel Buckmaster
  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 "coverPoint.h"
  23. #include "math/mathIO.h"
  24. #include "scene/sceneRenderState.h"
  25. #include "core/stream/bitStream.h"
  26. #include "materials/sceneData.h"
  27. #include "gfx/gfxDebugEvent.h"
  28. #include "gfx/gfxTransformSaver.h"
  29. #include "gfx/gfxDrawUtil.h"
  30. #include "renderInstance/renderPassManager.h"
  31. #include "console/engineAPI.h"
  32. extern bool gEditingMission;
  33. IMPLEMENT_CO_NETOBJECT_V1(CoverPoint);
  34. ConsoleDocClass(CoverPoint,
  35. "@brief A type of marker that designates a location AI characters can take cover.\n\n"
  36. );
  37. ImplementEnumType(CoverPointSize,
  38. "The size of a cover point.\n")
  39. { CoverPoint::Prone, "Prone", "Only provides cover when prone.\n" },
  40. { CoverPoint::Crouch, "Crouch", "Only provides cover when crouching.\n" },
  41. { CoverPoint::Stand, "Stand", "Provides cover when standing.\n" },
  42. EndImplementEnumType;
  43. //-----------------------------------------------------------------------------
  44. // Object setup and teardown
  45. //-----------------------------------------------------------------------------
  46. CoverPoint::CoverPoint()
  47. {
  48. mNetFlags.clear(Ghostable);
  49. mTypeMask |= MarkerObjectType;
  50. mSize = Stand;
  51. mQuality = 1.0f;
  52. mOccupied = false;
  53. mPeekLeft = false;
  54. mPeekRight = false;
  55. mPeekOver = false;
  56. }
  57. CoverPoint::~CoverPoint()
  58. {
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Object Editing
  62. //-----------------------------------------------------------------------------
  63. void CoverPoint::initPersistFields()
  64. {
  65. docsURL;
  66. addGroup("CoverPoint");
  67. addField("size", TYPEID<CoverPointSize>(), Offset(mSize, CoverPoint),
  68. "The size of this cover point.");
  69. addField("quality", TypeF32, Offset(mQuality, CoverPoint),
  70. "Reliability of this point as solid cover. (0...1)");
  71. addField("peekLeft", TypeBool, Offset(mPeekLeft, CoverPoint),
  72. "Can characters look left around this cover point?");
  73. addField("peekRight", TypeBool, Offset(mPeekRight, CoverPoint),
  74. "Can characters look right around this cover point?");
  75. addField("peekOver", TypeBool, Offset(mPeekOver, CoverPoint),
  76. "Can characters look over the top of this cover point?");
  77. endGroup("CoverPoint");
  78. Parent::initPersistFields();
  79. }
  80. bool CoverPoint::onAdd()
  81. {
  82. if(!Parent::onAdd())
  83. return false;
  84. // Set up a 1x1x1 bounding box
  85. mObjBox.set(Point3F(-0.5f, -0.5f, -0.5f),
  86. Point3F( 0.5f, 0.5f, 0.5f));
  87. resetWorldBox();
  88. if(gEditingMission)
  89. onEditorEnable();
  90. addToScene();
  91. return true;
  92. }
  93. void CoverPoint::onRemove()
  94. {
  95. if(gEditingMission)
  96. onEditorDisable();
  97. removeFromScene();
  98. Parent::onRemove();
  99. for(U32 i = 0; i < NumSizes; i++)
  100. smVertexBuffer[i] = NULL;
  101. }
  102. void CoverPoint::setTransform(const MatrixF & mat)
  103. {
  104. Parent::setTransform(mat);
  105. setMaskBits(TransformMask);
  106. }
  107. void CoverPoint::onEditorEnable()
  108. {
  109. mNetFlags.set(Ghostable);
  110. }
  111. void CoverPoint::onEditorDisable()
  112. {
  113. mNetFlags.clear(Ghostable);
  114. }
  115. void CoverPoint::inspectPostApply()
  116. {
  117. setMaskBits(TransformMask);
  118. }
  119. U32 CoverPoint::packUpdate(NetConnection *conn, U32 mask, BitStream *stream)
  120. {
  121. U32 retMask = Parent::packUpdate(conn, mask, stream);
  122. stream->writeInt(mSize, 4);
  123. stream->writeFlag(mOccupied);
  124. stream->writeFlag(peekLeft());
  125. stream->writeFlag(peekRight());
  126. stream->writeFlag(peekOver());
  127. // Write our transform information
  128. if(stream->writeFlag(mask & TransformMask))
  129. {
  130. mathWrite(*stream, getTransform());
  131. mathWrite(*stream, getScale());
  132. }
  133. return retMask;
  134. }
  135. void CoverPoint::unpackUpdate(NetConnection *conn, BitStream *stream)
  136. {
  137. Parent::unpackUpdate(conn, stream);
  138. mSize = (Size)stream->readInt(4);
  139. setOccupied(stream->readFlag());
  140. mPeekLeft = stream->readFlag();
  141. mPeekRight = stream->readFlag();
  142. mPeekOver = stream->readFlag();
  143. if(stream->readFlag()) // TransformMask
  144. {
  145. mathRead(*stream, &mObjToWorld);
  146. mathRead(*stream, &mObjScale);
  147. setTransform(mObjToWorld);
  148. }
  149. }
  150. //-----------------------------------------------------------------------------
  151. // Functionality
  152. //-----------------------------------------------------------------------------
  153. Point3F CoverPoint::getNormal() const
  154. {
  155. return getTransform().getForwardVector();
  156. }
  157. DefineEngineMethod(CoverPoint, isOccupied, bool, (),,
  158. "@brief Returns true if someone is already using this cover point.")
  159. {
  160. return object->isOccupied();
  161. }
  162. //-----------------------------------------------------------------------------
  163. // Object Rendering
  164. //-----------------------------------------------------------------------------
  165. GFXStateBlockRef CoverPoint::smNormalSB;
  166. GFXVertexBufferHandle<CoverPoint::VertexType> CoverPoint::smVertexBuffer[CoverPoint::NumSizes];
  167. void CoverPoint::initGFXResources()
  168. {
  169. if(smVertexBuffer[0] != NULL)
  170. return;
  171. static const Point3F planePoints[4] =
  172. {
  173. Point3F(-1.0f, 0.0f, 0.0f), Point3F(-1.0f, 0.0f, 2.0f),
  174. Point3F( 1.0f, 0.0f, 0.0f), Point3F( 1.0f, 0.0f, 2.0f),
  175. };
  176. static const U32 planeFaces[6] =
  177. {
  178. 0, 1, 2,
  179. 1, 2, 3
  180. };
  181. static const Point3F scales[NumSizes] =
  182. {
  183. Point3F(1.0f, 1.0f, 0.5f), // Prone
  184. Point3F(1.0f, 1.0f, 1.0f), // Crouch
  185. Point3F(1.0f, 1.0f, 2.0f) // Stand
  186. };
  187. static const ColorI colours[NumSizes] =
  188. {
  189. ColorI(180, 0, 0, 128), // Prone
  190. ColorI(250, 200, 90, 128), // Crouch
  191. ColorI( 80, 190, 20, 128) // Stand
  192. };
  193. for(U32 i = 0; i < NumSizes; i++)
  194. {
  195. // Fill the vertex buffer
  196. VertexType *pVert = NULL;
  197. smVertexBuffer[i].set(GFX, 6, GFXBufferTypeStatic);
  198. pVert = smVertexBuffer[i].lock();
  199. for(U32 j = 0; j < 6; j++)
  200. {
  201. pVert[j].point = planePoints[planeFaces[j]] * scales[i] * 0.5f;
  202. pVert[j].normal = Point3F(0.0f, -1.0f, 0.0f);
  203. pVert[j].color = colours[i];
  204. }
  205. smVertexBuffer[i].unlock();
  206. }
  207. // Set up our StateBlock
  208. GFXStateBlockDesc desc;
  209. desc.cullDefined = true;
  210. desc.cullMode = GFXCullNone;
  211. desc.setBlend(true);
  212. smNormalSB = GFX->createStateBlock(desc);
  213. }
  214. void CoverPoint::prepRenderImage(SceneRenderState *state)
  215. {
  216. // Allocate an ObjectRenderInst so that we can submit it to the RenderPassManager
  217. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  218. // Now bind our rendering function so that it will get called
  219. ri->renderDelegate.bind(this, &CoverPoint::render);
  220. // Set our RenderInst as a standard object render
  221. ri->type = RenderPassManager::RIT_Editor;
  222. // Set our sorting keys to a default value
  223. ri->defaultKey = 0;
  224. ri->defaultKey2 = 0;
  225. // Submit our RenderInst to the RenderPassManager
  226. state->getRenderPass()->addInst(ri);
  227. }
  228. void CoverPoint::render(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat)
  229. {
  230. initGFXResources();
  231. if(overrideMat)
  232. return;
  233. if(smVertexBuffer[mSize].isNull())
  234. return;
  235. PROFILE_SCOPE(CoverPoint_Render);
  236. // Set up a GFX debug event (this helps with debugging rendering events in external tools)
  237. GFXDEBUGEVENT_SCOPE(CoverPoint_Render, ColorI::RED);
  238. // GFXTransformSaver is a handy helper class that restores
  239. // the current GFX matrices to their original values when
  240. // it goes out of scope at the end of the function
  241. GFXTransformSaver saver;
  242. // Calculate our object to world transform matrix
  243. MatrixF objectToWorld = getRenderTransform();
  244. objectToWorld.scale(getScale());
  245. // Apply our object transform
  246. GFX->multWorld(objectToWorld);
  247. // Set the state block
  248. GFX->setStateBlock(smNormalSB);
  249. // Set up the "generic" shaders
  250. // These handle rendering on GFX layers that don't support
  251. // fixed function. Otherwise they disable shaders.
  252. GFX->setupGenericShaders(GFXDevice::GSModColorTexture);
  253. // Set the vertex buffer
  254. GFX->setVertexBuffer(smVertexBuffer[mSize]);
  255. // Draw our triangles
  256. GFX->drawPrimitive(GFXTriangleList, 0, 2);
  257. // Data for decorations.
  258. GFXStateBlockDesc desc;
  259. F32 height = (float)(mSize + 1) / NumSizes * 2.0f;
  260. // Draw an X if we're occupied.
  261. if(isOccupied())
  262. {
  263. GFX->getDrawUtil()->drawArrow(desc, Point3F(-0.5, 0, 0), Point3F(0.5, 0, height), ColorI::RED);
  264. GFX->getDrawUtil()->drawArrow(desc, Point3F(0.5, 0, 0), Point3F(-0.5, 0, height), ColorI::RED);
  265. }
  266. // Draw arrows to represent peek directions.
  267. if(peekLeft())
  268. GFX->getDrawUtil()->drawArrow(desc, Point3F(0, 0, height * 0.5), Point3F(-0.5, 0, height * 0.5), ColorI::GREEN);
  269. if(peekRight())
  270. GFX->getDrawUtil()->drawArrow(desc, Point3F(0, 0, height * 0.5), Point3F(0.5, 0, height * 0.5), ColorI::GREEN);
  271. if(peekOver())
  272. GFX->getDrawUtil()->drawArrow(desc, Point3F(0, 0, height * 0.5), Point3F(0, 0, height), ColorI::GREEN);
  273. }