renderObjectExample.h 5.7 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 _RENDEROBJECTEXAMPLE_H_
  23. #define _RENDEROBJECTEXAMPLE_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _GFXSTATEBLOCK_H_
  28. #include "gfx/gfxStateBlock.h"
  29. #endif
  30. #ifndef _GFXVERTEXBUFFER_H_
  31. #include "gfx/gfxVertexBuffer.h"
  32. #endif
  33. #ifndef _GFXPRIMITIVEBUFFER_H_
  34. #include "gfx/gfxPrimitiveBuffer.h"
  35. #endif
  36. class BaseMatInstance;
  37. //-----------------------------------------------------------------------------
  38. // This class implements a basic SceneObject that can exist in the world at a
  39. // 3D position and render itself. Note that RenderObjectExample handles its own
  40. // rendering by submitting itself as an ObjectRenderInst (see
  41. // renderInstance\renderPassmanager.h) along with a delegate for its render()
  42. // function. However, the preffered rendering method in the engine is to submit
  43. // a MeshRenderInst along with a Material, vertex buffer, primitive buffer, and
  44. // transform and allow the RenderMeshMgr handle the actual rendering. You can
  45. // see this implemented in RenderMeshExample.
  46. //-----------------------------------------------------------------------------
  47. class RenderObjectExample : public SceneObject
  48. {
  49. typedef SceneObject Parent;
  50. // Networking masks
  51. // We need to implement at least one of these to allow
  52. // the client version of the object to receive updates
  53. // from the server version (like if it has been moved
  54. // or edited)
  55. enum MaskBits
  56. {
  57. TransformMask = Parent::NextFreeMask << 0,
  58. NextFreeMask = Parent::NextFreeMask << 1
  59. };
  60. //--------------------------------------------------------------------------
  61. // Rendering variables
  62. //--------------------------------------------------------------------------
  63. // Define our vertex format here so we don't have to
  64. // change it in multiple spots later
  65. typedef GFXVertexPCN VertexType;
  66. // The handles for our StateBlocks
  67. GFXStateBlockRef mNormalSB;
  68. GFXStateBlockRef mReflectSB;
  69. // The GFX vertex and primitive buffers
  70. GFXVertexBufferHandle< VertexType > mVertexBuffer;
  71. public:
  72. RenderObjectExample();
  73. virtual ~RenderObjectExample();
  74. // Declare this object as a ConsoleObject so that we can
  75. // instantiate it into the world and network it
  76. DECLARE_CONOBJECT(RenderObjectExample);
  77. //--------------------------------------------------------------------------
  78. // Object Editing
  79. // Since there is always a server and a client object in Torque and we
  80. // actually edit the server object we need to implement some basic
  81. // networking functions
  82. //--------------------------------------------------------------------------
  83. // Set up any fields that we want to be editable (like position)
  84. static void initPersistFields();
  85. // Handle when we are added to the scene and removed from the scene
  86. bool onAdd();
  87. void onRemove();
  88. // Override this so that we can dirty the network flag when it is called
  89. void setTransform( const MatrixF &mat );
  90. // This function handles sending the relevant data from the server
  91. // object to the client object
  92. U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream );
  93. // This function handles receiving relevant data from the server
  94. // object and applying it to the client object
  95. void unpackUpdate( NetConnection *conn, BitStream *stream );
  96. //--------------------------------------------------------------------------
  97. // Object Rendering
  98. // Torque utilizes a "batch" rendering system. This means that it builds a
  99. // list of objects that need to render (via RenderInst's) and then renders
  100. // them all in one batch. This allows it to optimized on things like
  101. // minimizing texture, state, and shader switching by grouping objects that
  102. // use the same Materials. For this example, however, we are just going to
  103. // get this object added to the list of objects that handle their own
  104. // rendering.
  105. //--------------------------------------------------------------------------
  106. // Create the geometry for rendering
  107. void createGeometry();
  108. // This is the function that allows this object to submit itself for rendering
  109. void prepRenderImage( SceneRenderState *state );
  110. // This is the function that actually gets called to do the rendering
  111. // Note that there is no longer a predefined name for this function.
  112. // Instead, when we submit our ObjectRenderInst in prepRenderImage(),
  113. // we bind this function as our rendering delegate function
  114. void render( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  115. };
  116. #endif // _RENDEROBJECTEXAMPLE_H_