renderObjectExample.h
Engine/source/T3D/examples/renderObjectExample.h
Classes:
class
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _RENDEROBJECTEXAMPLE_H_ 25#define _RENDEROBJECTEXAMPLE_H_ 26 27#ifndef _SCENEOBJECT_H_ 28#include "scene/sceneObject.h" 29#endif 30#ifndef _GFXSTATEBLOCK_H_ 31#include "gfx/gfxStateBlock.h" 32#endif 33#ifndef _GFXVERTEXBUFFER_H_ 34#include "gfx/gfxVertexBuffer.h" 35#endif 36#ifndef _GFXPRIMITIVEBUFFER_H_ 37#include "gfx/gfxPrimitiveBuffer.h" 38#endif 39 40class BaseMatInstance; 41 42 43//----------------------------------------------------------------------------- 44// This class implements a basic SceneObject that can exist in the world at a 45// 3D position and render itself. Note that RenderObjectExample handles its own 46// rendering by submitting itself as an ObjectRenderInst (see 47// renderInstance\renderPassmanager.h) along with a delegate for its render() 48// function. However, the preffered rendering method in the engine is to submit 49// a MeshRenderInst along with a Material, vertex buffer, primitive buffer, and 50// transform and allow the RenderMeshMgr handle the actual rendering. You can 51// see this implemented in RenderMeshExample. 52//----------------------------------------------------------------------------- 53 54class RenderObjectExample : public SceneObject 55{ 56 typedef SceneObject Parent; 57 58 // Networking masks 59 // We need to implement at least one of these to allow 60 // the client version of the object to receive updates 61 // from the server version (like if it has been moved 62 // or edited) 63 enum MaskBits 64 { 65 TransformMask = Parent::NextFreeMask << 0, 66 NextFreeMask = Parent::NextFreeMask << 1 67 }; 68 69 //-------------------------------------------------------------------------- 70 // Rendering variables 71 //-------------------------------------------------------------------------- 72 // Define our vertex format here so we don't have to 73 // change it in multiple spots later 74 typedef GFXVertexPCN VertexType; 75 76 // The handles for our StateBlocks 77 GFXStateBlockRef mNormalSB; 78 GFXStateBlockRef mReflectSB; 79 80 // The GFX vertex and primitive buffers 81 GFXVertexBufferHandle< VertexType> mVertexBuffer; 82 83public: 84 RenderObjectExample(); 85 virtual ~RenderObjectExample(); 86 87 // Declare this object as a ConsoleObject so that we can 88 // instantiate it into the world and network it 89 DECLARE_CONOBJECT(RenderObjectExample); 90 91 //-------------------------------------------------------------------------- 92 // Object Editing 93 // Since there is always a server and a client object in Torque and we 94 // actually edit the server object we need to implement some basic 95 // networking functions 96 //-------------------------------------------------------------------------- 97 // Set up any fields that we want to be editable (like position) 98 static void initPersistFields(); 99 100 // Handle when we are added to the scene and removed from the scene 101 bool onAdd(); 102 void onRemove(); 103 104 // Override this so that we can dirty the network flag when it is called 105 void setTransform( const MatrixF &mat ); 106 107 // This function handles sending the relevant data from the server 108 // object to the client object 109 U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream ); 110 // This function handles receiving relevant data from the server 111 // object and applying it to the client object 112 void unpackUpdate( NetConnection *conn, BitStream *stream ); 113 114 //-------------------------------------------------------------------------- 115 // Object Rendering 116 // Torque utilizes a "batch" rendering system. This means that it builds a 117 // list of objects that need to render (via RenderInst's) and then renders 118 // them all in one batch. This allows it to optimized on things like 119 // minimizing texture, state, and shader switching by grouping objects that 120 // use the same Materials. For this example, however, we are just going to 121 // get this object added to the list of objects that handle their own 122 // rendering. 123 //-------------------------------------------------------------------------- 124 // Create the geometry for rendering 125 void createGeometry(); 126 127 // This is the function that allows this object to submit itself for rendering 128 void prepRenderImage( SceneRenderState *state ); 129 130 // This is the function that actually gets called to do the rendering 131 // Note that there is no longer a predefined name for this function. 132 // Instead, when we submit our ObjectRenderInst in prepRenderImage(), 133 // we bind this function as our rendering delegate function 134 void render( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat ); 135}; 136 137#endif // _RENDEROBJECTEXAMPLE_H_ 138