2
0

coverPoint.cpp 9.8 KB

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