My Project
RenderTarget.h
1 #pragma once
2 #include "BaseObject.h"
3 #include "PaintEngine/PaintDevice.h"
4 
5 namespace ParaEngine
6 {
7  struct TextureEntity;
8  class ImageEntity;
9  class CPaintEngine;
10  class CPainter;
11 
16  class CRenderTarget : public CBaseObject, public CPaintDevice
17  {
18  public:
19  CRenderTarget();
20  virtual ~CRenderTarget();
21 
22  ATTRIBUTE_DEFINE_CLASS(CRenderTarget);
23  ATTRIBUTE_SUPPORT_CREATE_FACTORY(CRenderTarget);
24 
26  virtual int InstallFields(CAttributeClass* pClass, bool bOverride);
27 
28  ATTRIBUTE_METHOD1(CRenderTarget, GetClearColor_s, Vector3*) { *p1 = cls->GetClearColor().ToVector3(); return S_OK; }
29  ATTRIBUTE_METHOD1(CRenderTarget, SetClearColor_s, Vector3) { LinearColor c(p1.x, p1.y, p1.z, 1); cls->SetClearColor(c); return S_OK; }
30 
31  ATTRIBUTE_METHOD1(CRenderTarget, GetRenderTargetSize_s, Vector2*) { *p1 = cls->GetRenderTargetSize(); return S_OK; }
32  ATTRIBUTE_METHOD1(CRenderTarget, SetRenderTargetSize_s, Vector2) { cls->SetRenderTargetSize(p1); return S_OK; }
33 
34  ATTRIBUTE_METHOD1(CRenderTarget, IsActiveRenderingEnabled_s, bool*) { *p1 = cls->IsActiveRenderingEnabled(); return S_OK; }
35  ATTRIBUTE_METHOD1(CRenderTarget, EnableActiveRendering_s, bool) { cls->EnableActiveRendering(p1); return S_OK; }
36 
37  ATTRIBUTE_METHOD1(CRenderTarget, IsDirty_s, bool*) { *p1 = cls->IsDirty(); return S_OK; }
38  ATTRIBUTE_METHOD1(CRenderTarget, SetDirty_s, bool) { cls->SetDirty(p1); return S_OK; }
39 
40  ATTRIBUTE_METHOD1(CRenderTarget, IsPersistentRenderTarget_s, bool*) { *p1 = cls->IsPersistentRenderTarget(); return S_OK; }
41  ATTRIBUTE_METHOD1(CRenderTarget, SetPersistentRenderTarget_s, bool) { cls->SetPersistentRenderTarget(p1); return S_OK; }
42 
45 
46 
47  public:
48  virtual CPaintEngine * paintEngine() const;
49  virtual int metric(PaintDeviceMetric metric) const;
50 
52  bool InitWithWidthAndHeight(int width, int height, D3DFORMAT format = D3DFMT_A8R8G8B8, D3DFORMAT depthStencilFormat = D3DFMT_D16);
53 
54  virtual int PrepareRender(CBaseCamera* pCamera, SceneState* pSceneState);
56  virtual HRESULT Draw(SceneState * sceneState);
57 
59  virtual bool Begin();
60 
61  void CheckInit();
62 
64  virtual void End();
65 
67  virtual void Clear(const LinearColor& color, float depthValue = 1.f, int stencilValue = 0, DWORD flags = D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER);
68 
70  void EnableActiveRendering(bool bEnable);
71 
73  bool IsActiveRenderingEnabled() const;
74 
77  HRESULT InitDeviceObjects();
79  HRESULT RestoreDeviceObjects();
81  HRESULT InvalidateDeviceObjects();
83  HRESULT DeleteDeviceObjects();
85  virtual void Cleanup();
89  virtual void DoPaint(CPainter* painter = NULL);
90 
92  void SetClearColor(const LinearColor& bgColor);
95 
101  bool IsPersistentRenderTarget() const;
102  void SetPersistentRenderTarget(bool val);
103 
108  virtual TextureEntity* GetTexture();
110  virtual AssetEntity* GetPrimaryAsset();
111 
121  HRESULT SaveToFile(const char* filename, int width = 0, int height = 0, DWORD dwFormat = 3, UINT MipLevels = 0, int srcLeft = 0, int srcTop = 0, int srcWidth = 0, int srcHeight = 0);
122 
123  /* creates a new Image from with the texture's data.
124  Caller is responsible for releasing it by calling delete.
125  * @param colorKey: if 0 no color key is used. for black color key use 0xff000000
126  */
127  ImageEntity* NewImage(bool bFlipImage = true, Color colorKey = 0);
128 
134  void SetRenderTargetSize(int nWidth, int nHeight);
135  void GetRenderTargetSize(int* nWidth, int* nHeight);
136  void SetRenderTargetSize(const Vector2& size);
137  Vector2 GetRenderTargetSize();
138 
139  int GetTextureWidth() const { return m_nTextureWidth; }
140  int GetTextureHeight() const { return m_nTextureHeight; }
141 
143  bool IsDirty() const;
144  void SetDirty(bool val);
145 
146  int GetLifeTime() const;
147  void SetLifeTime(int val);
148 
149  virtual bool IsDead();
150  virtual void SetDead();
151 
152  const std::string& GetCanvasTextureName();
153  void SetCanvasTextureName(const std::string& sValue);
154  protected:
155  // whether device is created.
156  bool m_bInitialized;
157 
158  bool m_bPersistentRenderTarget;
159  int m_nTextureWidth;
160  int m_nTextureHeight;
161  /* -1 means alive forever. 0 means dead. any other value means number of frames to live before it is released. */
162  int m_nLifeTime;
163 
164  DWORD m_depthStencilFormat;
166  asset_ptr<TextureEntity> m_pCanvasTexture;
167 #ifdef USE_DIRECTX_RENDERER
168  LPDIRECT3DSURFACE9 m_pCanvasSurface;
169  LPDIRECT3DSURFACE9 m_pDepthStencilSurface;
170 
171  LPDIRECT3DSURFACE9 m_pOldRenderTarget;
172  LPDIRECT3DSURFACE9 m_pOldZBuffer;
173 #elif defined USE_OPENGL_RENDERER
174  GLuint _FBO;
175  GLuint _depthRenderBufffer;
176  GLint _oldFBO;
177  GLint _oldRBO;
178 #endif
179  D3DVIEWPORT9 m_oldViewport;
180  Vector3 m_vOldRenderOrigin;
181 
184 
185  /* whether we are currently inside begin() end() function pair. */
186  bool m_bIsBegin;
187 
193  std::string m_sCanvasTextureName;
194 
195  mutable CPaintEngine *engine;
196  };
197 
200  {
201  public:
202  ScopedPaintOnRenderTarget(CRenderTarget* pRendertarget) :m_pRenderTarget(pRendertarget){
203  if (m_pRenderTarget)
204  m_pRenderTarget->Begin();
205  }
207  if (m_pRenderTarget)
208  m_pRenderTarget->End();
209  };
210  public:
211  CRenderTarget* m_pRenderTarget;
212  };
213 }
214 
Which DXT Compression to Use? Obviously, there are some trade-offs between the different formats whic...
Definition: TextureEntity.h:29
std::string m_sCanvasTextureName
render target canvas name
Definition: RenderTarget.h:193
an attribute class is a collection of attribute fields.
Definition: AttributeClass.h:10
HRESULT RestoreDeviceObjects()
restore device object
Definition: RenderTarget.cpp:98
It&#39;s used as parameter to Draw method of each scene object.
Definition: SceneState.h:284
HRESULT InitDeviceObjects()
init device objects.
Definition: RenderTarget.cpp:91
void SetClearColor(const LinearColor &bgColor)
set the color of the scene ground when it is not enabled.When scene is enabled, the background color ...
Definition: RenderTarget.cpp:585
LinearColor GetClearColor()
Get the color of the scene ground when it is not enabled.When scene is enabled, the background color ...
Definition: RenderTarget.cpp:590
a render target scene object.
Definition: RenderTarget.h:16
different physics engine has different winding order.
Definition: EventBinding.h:32
DEFINE_SCRIPT_EVENT(CRenderTarget, Paint)
define the On_Paint script callback for painting in 2d space.
bool InitWithWidthAndHeight(int width, int height, D3DFORMAT format=D3DFMT_A8R8G8B8, D3DFORMAT depthStencilFormat=D3DFMT_D16)
initializes a RenderTexture object with width and height in Points and a pixel format( only RGB and R...
Definition: RenderTarget.cpp:472
bool IsDirty() const
whether render target is dirty and should be redraw on the next frame.
Definition: RenderTarget.cpp:662
virtual bool IsDead()
whether this object should be removed some time in the future.
Definition: RenderTarget.cpp:701
virtual HRESULT Draw(SceneState *sceneState)
only for drawable objects
Definition: RenderTarget.cpp:650
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
bool m_bActiveRendering
if true, contents will be drawn to the current render target each frame.
Definition: RenderTarget.h:189
virtual int InstallFields(CAttributeClass *pClass, bool bOverride)
this class should be implemented if one wants to add new attribute.
Definition: RenderTarget.cpp:721
bool IsPersistentRenderTarget() const
default to false, where the render target texture will be deleted when scene object is deleted...
Definition: RenderTarget.cpp:67
Definition: BaseCamera.h:70
Standard 2-dimensional vector.
Definition: ParaVector2.h:16
DWORD m_dwClearColor
used when clearing background color
Definition: RenderTarget.h:183
virtual void Cleanup()
clean up all resource objects
Definition: RenderTarget.cpp:203
helper class for painting on render target
Definition: RenderTarget.h:199
asset_ptr< TextureEntity > m_pCanvasTexture
render target
Definition: RenderTarget.h:166
base class for a surface to paint to.
Definition: Painter.h:38
Definition: RenderCoreOpenGL.h:140
virtual void End()
end rendering to texture, restore old render target
Definition: RenderTarget.cpp:550
virtual TextureEntity * GetTexture()
the canvas texture, which can be used as any other ordinary texture on 3D or 2D object.
Definition: RenderTarget.cpp:77
void EnableActiveRendering(bool bEnable)
if true, contents will be drawn to the current render target each frame.
Definition: RenderTarget.cpp:677
void SetRenderTargetSize(int nWidth, int nHeight)
set the canvas size in pixels
Definition: RenderTarget.cpp:427
The CPaintEngine class provides an abstract definition of how CPainter draws to a given device on a g...
Definition: PaintEngine.h:34
virtual void DoPaint(CPainter *painter=NULL)
invoke the On_Paint script event handler if any.
Definition: RenderTarget.cpp:622
bool m_bIsDirty
whether render target is dirty and should be redraw on the next frame.
Definition: RenderTarget.h:191
HRESULT DeleteDeviceObjects()
delete device objects
Definition: RenderTarget.cpp:190
Defines the base class of all scene elements:CBaseObject for Parallel World Engine.
Definition: BaseObject.h:230
HRESULT InvalidateDeviceObjects()
Invalid device object.
Definition: RenderTarget.cpp:168
virtual int PrepareRender(CBaseCamera *pCamera, SceneState *pSceneState)
derived class can override this function to place the object in to the render pipeline.
Definition: RenderTarget.cpp:639
base class for a surface to paint to.
Definition: PaintDevice.h:10
HRESULT SaveToFile(const char *filename, int width=0, int height=0, DWORD dwFormat=3, UINT MipLevels=0, int srcLeft=0, int srcTop=0, int srcWidth=0, int srcHeight=0)
save o a different texture file format and save with full mipmapping to disk.
Definition: RenderTarget.cpp:209
A linear, 32-bit/component floating point RGBA color.
Definition: ParaColor.h:12
Definition: ParaColor.h:275
virtual bool Begin()
starts rendering to texture.
Definition: RenderTarget.cpp:492
Unlike TextureEntity, ImageEntity is a device independent entity in memory.
Definition: ImageEntity.h:9
bool IsActiveRenderingEnabled() const
if true, contents will be drawn to the current render target each frame.
Definition: RenderTarget.cpp:672
virtual void Clear(const LinearColor &color, float depthValue=1.f, int stencilValue=0, DWORD flags=D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER)
this is usually the first function to call after calling begin().
Definition: RenderTarget.cpp:596
virtual AssetEntity * GetPrimaryAsset()
get the asset render target object.
Definition: RenderTarget.cpp:82
Base class for managed asset entity in ParaEngine.
Definition: AssetEntity.h:25
virtual CPaintEngine * paintEngine() const
Returns the paint engine.
Definition: RenderTarget.cpp:609