renderMeshExample.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #ifndef _RENDERMESHEXAMPLE_H_
  23. #define _RENDERMESHEXAMPLE_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _GFXVERTEXBUFFER_H_
  28. #include "gfx/gfxVertexBuffer.h"
  29. #endif
  30. #ifndef _GFXPRIMITIVEBUFFER_H_
  31. #include "gfx/gfxPrimitiveBuffer.h"
  32. #endif
  33. #include "T3D/assets/MaterialAsset.h"
  34. class BaseMatInstance;
  35. //-----------------------------------------------------------------------------
  36. // This class implements a basic SceneObject that can exist in the world at a
  37. // 3D position and render itself. There are several valid ways to render an
  38. // object in Torque. This class implements the preferred rendering method which
  39. // is to submit a MeshRenderInst along with a Material, vertex buffer,
  40. // primitive buffer, and transform and allow the RenderMeshMgr handle the
  41. // actual setup and rendering for you.
  42. //-----------------------------------------------------------------------------
  43. class RenderMeshExample : public SceneObject
  44. {
  45. typedef SceneObject Parent;
  46. // Networking masks
  47. // We need to implement a mask specifically to handle
  48. // updating our transform from the server object to its
  49. // client-side "ghost". We also need to implement a
  50. // maks for handling editor updates to our properties
  51. // (like material).
  52. enum MaskBits
  53. {
  54. TransformMask = Parent::NextFreeMask << 0,
  55. UpdateMask = Parent::NextFreeMask << 1,
  56. NextFreeMask = Parent::NextFreeMask << 2
  57. };
  58. // Define our vertex format here so we don't have to
  59. // change it in multiple spots later
  60. typedef GFXVertexPNT VertexType;
  61. //--------------------------------------------------------------------------
  62. // Rendering variables
  63. //--------------------------------------------------------------------------
  64. BaseMatInstance* mMaterialInst;
  65. DECLARE_MATERIALASSET(RenderMeshExample, Material);
  66. DECLARE_ASSET_NET_SETGET(RenderMeshExample, Material, UpdateMask);
  67. // The GFX vertex and primitive buffers
  68. GFXVertexBufferHandle< VertexType > mVertexBuffer;
  69. GFXPrimitiveBufferHandle mPrimitiveBuffer;
  70. public:
  71. RenderMeshExample();
  72. virtual ~RenderMeshExample();
  73. // Declare this object as a ConsoleObject so that we can
  74. // instantiate it into the world and network it
  75. DECLARE_CONOBJECT(RenderMeshExample);
  76. //--------------------------------------------------------------------------
  77. // Object Editing
  78. // Since there is always a server and a client object in Torque and we
  79. // actually edit the server object we need to implement some basic
  80. // networking functions
  81. //--------------------------------------------------------------------------
  82. // Set up any fields that we want to be editable (like position)
  83. static void initPersistFields();
  84. // Allows the object to update its editable settings
  85. // from the server object to the client
  86. virtual void inspectPostApply();
  87. // Handle when we are added to the scene and removed from the scene
  88. bool onAdd();
  89. void onRemove();
  90. // Override this so that we can dirty the network flag when it is called
  91. void setTransform( const MatrixF &mat );
  92. // This function handles sending the relevant data from the server
  93. // object to the client object
  94. U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream );
  95. // This function handles receiving relevant data from the server
  96. // object and applying it to the client object
  97. void unpackUpdate( NetConnection *conn, BitStream *stream );
  98. //--------------------------------------------------------------------------
  99. // Object Rendering
  100. // Torque utilizes a "batch" rendering system. This means that it builds a
  101. // list of objects that need to render (via RenderInst's) and then renders
  102. // them all in one batch. This allows it to optimized on things like
  103. // minimizing texture, state, and shader switching by grouping objects that
  104. // use the same Materials.
  105. //--------------------------------------------------------------------------
  106. // Create the geometry for rendering
  107. void createGeometry();
  108. // Get the Material instance
  109. void updateMaterial();
  110. // This is the function that allows this object to submit itself for rendering
  111. void prepRenderImage( SceneRenderState *state );
  112. };
  113. #endif // _RENDERMESHEXAMPLE_H_