My Project
BlockWorldClient.h
1 #pragma once
2 #include "BlockWorld.h"
3 #include "effect_file.h"
4 
5 namespace ParaEngine
6 {
7  class BlockRegion;
8  class RenderableChunk;
9  class BlockRenderTask;
10  struct BlockHeightValue;
11  class CShadowMap;
12  class CMultiFrameBlockWorldRenderer;
13 
16  {
17  public:
19  virtual int GetAttributeClassID(){return ATTRIBUTE_CLASSID_BlockWorldClient;}
21  virtual const char* GetAttributeClassName(){static const char name[] = "BlockWorldClient"; return name;}
23  virtual const char* GetAttributeClassDescription(){static const char desc[] = ""; return desc;}
25  virtual int InstallFields(CAttributeClass* pClass, bool bOverride);
26 
28  virtual IAttributeFields* GetChildAttributeObject(const std::string& sName);
30  virtual int GetChildAttributeObjectCount(int nColumnIndex = 0);
32  virtual int GetChildAttributeColumnCount();
33  virtual IAttributeFields* GetChildAttributeObject(int nRowIndex, int nColumnIndex = 0);
34 
35  //ATTRIBUTE_METHOD1(BlockWorldClient, GetTimeOfDay_s, float*) {*p1 = cls->GetTimeOfDay(); return S_OK;}
36  //ATTRIBUTE_METHOD1(BlockWorldClient, SetTimeOfDay_s, float) {cls->SetTimeOfDay(p1); return S_OK;}
37 
38 
39  ATTRIBUTE_METHOD1(BlockWorldClient, GetBlockLightColor_s, Vector3*) {*p1 = cls->GetBlockLightColor().ToVector3(); return S_OK;}
40  ATTRIBUTE_METHOD1(BlockWorldClient, SetBlockLightColor_s, Vector3) {LinearColor c(p1.x, p1.y,p1.z,1); cls->SetBlockLightColor(c); return S_OK;}
41 
42  ATTRIBUTE_METHOD1(BlockWorldClient, SetPostProcessingScript_s, const char*) {cls->SetPostProcessingScript(p1); return S_OK;}
43 
44  ATTRIBUTE_METHOD1(BlockWorldClient, SetPostProcessingAlphaScript_s, const char*) { cls->SetPostProcessingAlphaScript(p1); return S_OK; }
45 
46  ATTRIBUTE_METHOD1(BlockWorldClient, GetUseSunlightShadowMap_s, bool*) {*p1 = cls->GetUseSunlightShadowMap(); return S_OK;}
47  ATTRIBUTE_METHOD1(BlockWorldClient, SetUseSunlightShadowMap_s, bool) {cls->SetUseSunlightShadowMap((BlockRenderMethod)p1); return S_OK;}
48 
49  ATTRIBUTE_METHOD1(BlockWorldClient, HasSunlightShadowMap_s, bool*) { *p1 = cls->HasSunlightShadowMap(); return S_OK; }
50 
51 
52  ATTRIBUTE_METHOD1(BlockWorldClient, GetUseWaterReflection_s, bool*) {*p1 = cls->GetUseWaterReflection(); return S_OK;}
53  ATTRIBUTE_METHOD1(BlockWorldClient, SetUseWaterReflection_s, bool) {cls->SetUseWaterReflection((BlockRenderMethod)p1); return S_OK;}
54 
55  ATTRIBUTE_METHOD1(BlockWorldClient, CanUseAdvancedShading_s, bool*) {*p1 = cls->CanUseAdvancedShading(); return S_OK;}
56 
57  ATTRIBUTE_METHOD1(BlockWorldClient, IsMovieOutputMode_s, bool*) { *p1 = cls->IsMovieOutputMode(); return S_OK; }
58  ATTRIBUTE_METHOD1(BlockWorldClient, EnableMovieOutputMode_s, bool) { cls->EnableMovieOutputMode((BlockRenderMethod)p1); return S_OK; }
59 
60  ATTRIBUTE_METHOD1(BlockWorldClient, GetVertexBufferSizeLimit_s, int*) { *p1 = cls->GetVertexBufferSizeLimit(); return S_OK; }
61  ATTRIBUTE_METHOD1(BlockWorldClient, SetVertexBufferSizeLimit_s, int) { cls->SetVertexBufferSizeLimit(p1); return S_OK; }
62 
63  ATTRIBUTE_METHOD1(BlockWorldClient, IsAsyncChunkMode_s, bool*) { *p1 = cls->IsAsyncChunkMode(); return S_OK; }
64  ATTRIBUTE_METHOD1(BlockWorldClient, SetAsyncChunkMode_s, bool) { cls->SetAsyncChunkMode(p1); return S_OK; }
65 
66 
67  ATTRIBUTE_METHOD1(BlockWorldClient, GetAlwaysInVertexBufferChunkRadius_s, int*) { *p1 = cls->GetAlwaysInVertexBufferChunkRadius(); return S_OK; }
68  ATTRIBUTE_METHOD1(BlockWorldClient, SetAlwaysInVertexBufferChunkRadius_s, int) { cls->SetAlwaysInVertexBufferChunkRadius(p1); return S_OK; }
69 
70  ATTRIBUTE_METHOD1(BlockWorldClient, GetMaxVisibleVertexBufferBytes_s, int*) { *p1 = cls->GetMaxVisibleVertexBufferBytes(); return S_OK; }
71  ATTRIBUTE_METHOD1(BlockWorldClient, SetMaxVisibleVertexBufferBytes_s, int) { cls->SetMaxVisibleVertexBufferBytes(p1); return S_OK; }
72 
73 
74  ATTRIBUTE_METHOD1(BlockWorldClient, GetNearCameraChunkDist_s, int*) { *p1 = cls->GetNearCameraChunkDist(); return S_OK; }
75  ATTRIBUTE_METHOD1(BlockWorldClient, SetNearCameraChunkDist_s, int) { cls->SetNearCameraChunkDist(p1); return S_OK; }
76 
77  ATTRIBUTE_METHOD1(BlockWorldClient, GetMaxBufferRebuildPerTick_s, int*) { *p1 = cls->GetMaxBufferRebuildPerTick(); return S_OK; }
78  ATTRIBUTE_METHOD1(BlockWorldClient, SetMaxBufferRebuildPerTick_s, int) { cls->SetMaxBufferRebuildPerTick(p1); return S_OK; }
79 
80  ATTRIBUTE_METHOD1(BlockWorldClient, GetMaxBufferRebuildPerTick_FarChunk_s, int*) { *p1 = cls->GetMaxBufferRebuildPerTick_FarChunk(); return S_OK; }
81  ATTRIBUTE_METHOD1(BlockWorldClient, SetMaxBufferRebuildPerTick_FarChunk_s, int) { cls->SetMaxBufferRebuildPerTick_FarChunk(p1); return S_OK; }
82 
83  ATTRIBUTE_METHOD1(BlockWorldClient, GetUsePointTextureFiltering_s, bool*) { *p1 = cls->GetUsePointTextureFiltering(); return S_OK; }
84  ATTRIBUTE_METHOD1(BlockWorldClient, SetUsePointTextureFiltering_s, bool) { cls->SetUsePointTextureFiltering(p1); return S_OK; }
85 
87  //static functions
89  static BlockWorldClient* GetInstance();
90 
92  //member functions
96 
97  //init block world
98  virtual void EnterWorld(const string& sWorldDir, float x,float y,float z);
99 
100  //call this function when leave the block world
101  virtual void LeaveWorld();
102 
103  //perform culling and fill up m_visibleChunks
104  virtual void UpdateVisibleChunks(bool bIsShadowPass = false);
105 
107  virtual void ClearBlockRenderCache();
108 
109  void DeleteAllBlocks();
110 
112  //render related function
113 
115  virtual void SetBlockRenderMethod(BlockRenderMethod method);
116 
117  //perform culling and prepare render group,this function should be called
118  //before Render
119  void PreRender(bool bIsShadowPass = false);
120 
121  void CheckRebuildVisibleChunks(bool bAsyncMode = true, bool bIsShadowPass = false);
122 
128  void Render(BlockRenderPass nRenderPass = BlockRenderPass_Opaque, std::vector<BlockRenderTask*>* pCurRenderQueue = NULL, int nRenderMethod = -1);
129 
131  void RenderDeferredLights();
132 
133  std::vector<BlockRenderTask*>* GetRenderQueueByPass(BlockRenderPass nRenderPass);
134 
136  void RenderShadowMap();
137 
138  //add render task to render queueGe
139  virtual void AddRenderTask(BlockRenderTask* pRenderTask);
140 
141  //this function can be used to render a damaged block
142  void SetDamagedBlock(uint16_t wx,uint16_t wy,uint16_t wz);
143 
144  void SetDamagedBlockDegree(float degree);
145 
149  bool IsPointUnderWater(const Vector3& vPos);
150 
152  void SetBlockLightColor(const LinearColor& color);
153 
156 
157 
161  void SetSelectionTexture(const char* textureName);
162 
163  std::string GetSelectionTexture();
164 
166  void SetDamageTexture(const char* textureName);
167  std::string GetDamageTexture();
168 
169 
174  float GetWaterLevel(float x, float y, float z, int nRayLength=2);
175 
177  void InitDeviceObjects();
178  void RestoreDeviceObjects();
180  void DeleteDeviceObjects();
183  void RendererRecreated();
184 
186  void DoPostRenderingProcessing(BlockRenderPass nRenderPass = BlockRenderPass_Opaque);
187 
188  void RestoreNormalShader();
189 
190  void Cleanup();
191 
193  bool PrepareAllRenderTargets(bool bSetRenderTarget = true);
194 
197 
200 
202  void SetPostProcessingScript(const char* sCallbackScript);
203  void SetPostProcessingAlphaScript(const char* sCallbackScript);
204 
206  bool CanUseAdvancedShading();
207 
209  void SetUseSunlightShadowMap(bool bEnable);
210  bool GetUseSunlightShadowMap();
211 
212 
214  void SetUseWaterReflection(bool bEnable);
215  bool GetUseWaterReflection();
216 
218  void PrepareShadowCasters( CShadowMap* pShadowMap );
219 
221  void RenderDynamicBlocks();
222 
223  //get highest y values of current and neighbor blocks above terrain.
224  //return terrain height if no block above ground.
225  //@param result: int16_t[6] array contains the result height of {center,x,-x,z,-z,mask}
226  // each bit of mask indicate return value is block height or terrain height,
227  virtual void GetMaxBlockHeightWatchingSky(uint16_t blockX_ws, uint16_t blockZ_ws, ChunkMaxHeight* pResult);
228 
232  bool IsMovieOutputMode() const;
233  void EnableMovieOutputMode(bool val);
234 
236  int GetVertexBufferSizeLimit() const;
237  void SetVertexBufferSizeLimit(int val);
238 
241  void SetAlwaysInVertexBufferChunkRadius(int val);
242 
244  int GetMaxVisibleVertexBufferBytes() const;
245  void SetMaxVisibleVertexBufferBytes(int val);
246 
249  bool IsAsyncChunkMode() const;
250  void SetAsyncChunkMode(bool val);
251 
253  int GetNearCameraChunkDist() const;
254  void SetNearCameraChunkDist(int val);
255 
259  int GetMaxBufferRebuildPerTick() const;
260  void SetMaxBufferRebuildPerTick(int val);
261 
266  void SetMaxBufferRebuildPerTick_FarChunk(int val);
267 
268  //helper function to compare render order
269  static bool CompareRenderOrder(BlockRenderTask* v0, BlockRenderTask* v1);
270 
273  void SetUsePointTextureFiltering(bool bUse);
274  protected:
275  virtual void UpdateActiveChunk();
276 
277  private:
278  //Get shared index buffer which will be reused for all render task
279  IndexBufferDevicePtr_type GetIndexBuffer();
280  VertexDeclarationPtr GetVertexLayout();
281  VertexDeclarationPtr GetSelectBlockVertexLayout();
282 
283  void BuildSelectionBlockBuffer();
284 
285  void RenderSelectionBlock(int nSelectionIndex = BLOCK_GROUP_ID_HIGHLIGHT, float fScaledBlockSize = 1.01f, LinearColor* pColor = NULL, BOOL bColorAnimate = TRUE);
286 
287  void RenderWireFrameBlock(int nSelectionIndex = BLOCK_GROUP_ID_WIREFRAME, float fScaledBlockSize = 1.01f, LinearColor* pLineColor = NULL);
288 
289  void RenderDamagedBlock();
290 
291  int FillSelectBlockVertice(std::map<int64_t, Uint16x3>* pSelectedBlockMap, uint16_t x, uint16_t y, uint16_t z, SelectBlockVertex* blockModel, const Vector3& vOffset, const float fBlockSize, float fUV_Y_Offset = 0.f, float fUV_Y_Size = 1.f);
292 
293  int FillSelectBlockInvert(std::map<int64_t, Uint16x3>& selectedBlockMap, uint16_t x, uint16_t y, uint16_t z, SelectBlockVertex* blockModel, const Vector3& vOffset, const float fBlockSize, float fUV_Y_Offset = 0.f, float fUV_Y_Size = 1.f);
294 
295  bool HasSunlightShadowMap();
296 
297  void AddToVisibleChunk(RenderableChunk &chunk, int nViewDist, int nRenderFrameCount);
298  int ClearActiveChunksToMemLimit(bool bIsShadowPass = false);
299 
300  void ClearVisibleChunksToByteLimit(bool bIsShadowPass);
301 
302  private:
303  static BlockWorldClient* g_pInstance;
304 
305  static int32_t g_TexPass;
306  static int32_t g_twoTexPass;
307  static int32_t g_TexNormalMapPass;
308  static int32_t g_twoTexNormalMapPass;
309  static int32_t g_transparentBlockPass;
310  static int32_t g_waterBlockPass;
311  static int32_t g_selectBlockPass;
312  static int32_t g_bumpyBlockPass;
313 
314 
315  bool m_isUnderLiquid;
316 
318  LinearColor m_vBlockLightColor;
319 
320  std::vector<BlockRenderTask*> m_solidRenderTasks;
321  std::vector<BlockRenderTask*> m_alphaTestRenderTasks;
322  std::vector<BlockRenderTask*> m_alphaBlendRenderTasks;
323  std::vector<BlockRenderTask*> m_reflectedWaterRenderTasks;
324 
325 
326  std::vector<float> m_selectBlockInstData;
327  int32_t m_maxSelectBlockPerBatch;
328 
329  asset_ptr<TextureEntity> m_highLightTextures[BLOCK_GROUP_ID_MAX];
330  std::vector<SelectBlockVertex> m_select_block_vertices;
331  std::vector<uint16_t> m_select_block_indices;
332 
333  ParaIndexBuffer m_sharedIndexBuffer;
334 
335 #ifdef USE_DIRECTX_RENDERER
336 
337  asset_ptr<TextureEntity> m_render_target_color;
339  asset_ptr<TextureEntity> m_render_target_color_hdr;
341  asset_ptr<TextureEntity> m_render_target_block_info;
342  LPDIRECT3DSURFACE9 m_render_target_block_info_surface;
344  asset_ptr<TextureEntity> m_render_target_depth_tex;
345  LPDIRECT3DSURFACE9 m_render_target_depth_tex_surface;
347  asset_ptr<TextureEntity> m_render_target_normal;
348  LPDIRECT3DSURFACE9 m_render_target_normal_surface;
350  LPDIRECT3DSURFACE9 m_pDepthStencilSurface;
351  LPDIRECT3DSURFACE9 m_pOldRenderTarget;
352  LPDIRECT3DSURFACE9 m_pOldZBuffer;
353 
355  asset_ptr<CEffectFile> m_block_effect_fancy;
356  asset_ptr<CEffectFile> m_normal_mesh_effect_fancy;
357  asset_ptr<CEffectFile> m_bmax_model_effect_fancy;
358  asset_ptr<CEffectFile> m_terrain_fancy;
359 
361  asset_ptr<CEffectFile> m_lightgeometry_effects[3];
362 #endif
363  CMultiFrameBlockWorldRenderer* m_pMultiFrameRenderer;
364 
365  Uint16x3 m_damagedBlockId;
366  asset_ptr<TextureEntity> m_damangeTexture;
367  float m_damageDegree;
368 
371  std::string m_sPostProcessorCallbackScript;
373  std::string m_sPostProcessorAlphaCallbackScript;
374 
375  int m_nMaxBufferRebuildPerTick;
376  int m_nNearCameraChunkDist;
377  int m_nMaxBufferRebuildPerTick_FarChunk;
378 
380  int m_nVertexBufferSizeLimit;
382  int m_nAlwaysInVertexBufferChunkRadius;
384  int m_nMaxVisibleVertexBufferBytes;
385 
387  int m_nBufferRebuildCountThisTick;
388 
390  bool m_bUseSunlightShadowMap;
391 
393  bool m_bUseWaterReflection;
394 
398  bool m_bMovieOutputMode;
399 
402  bool m_bAsyncChunkMode;
403 
405  bool m_bUsePointTextureFiltering;
406  };
407 }
408 
409 
410 
411 
412 
413 
void PrepareShadowCasters(CShadowMap *pShadowMap)
prepare shadow casters
Definition: BlockWorldClient.cpp:2413
virtual const char * GetAttributeClassDescription()
a static string, describing the attribute class object
Definition: BlockWorldClient.h:23
an attribute class is a collection of attribute fields.
Definition: AttributeClass.h:10
void DoPostRenderingProcessing(BlockRenderPass nRenderPass=BlockRenderPass_Opaque)
do post rendering processing
Definition: BlockWorldClient.cpp:2204
Definition: BlockModel.h:163
bool CanUseAdvancedShading()
check device capabilities for deferred shading.
Definition: BlockWorldClient.cpp:2365
different physics engine has different winding order.
Definition: EventBinding.h:32
int GetMaxBufferRebuildPerTick_FarChunk() const
how many chunks to upload to video memory for far aways chunks per tick.
Definition: BlockWorldClient.cpp:3098
virtual void SetBlockRenderMethod(BlockRenderMethod method)
how blocks are preferred to be rendered.
Definition: BlockWorldClient.cpp:2022
virtual void ClearBlockRenderCache()
clear block render cache
Definition: BlockWorldClient.cpp:2109
int GetMaxBufferRebuildPerTick() const
how many chunks to upload to video memory per tick for near camera chunks.
Definition: BlockWorldClient.cpp:3078
basic block world coordinate
Definition: BlockCoordinate.h:72
virtual int GetChildAttributeColumnCount()
we support multi-dimensional child object.
Definition: BlockWorldClient.cpp:3013
Definition: RenderableChunk.h:17
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
virtual IAttributeFields * GetChildAttributeObject(const std::string &sName)
get attribute by child object.
Definition: BlockWorldClient.cpp:2995
bool IsAsyncChunkMode() const
if true, if we want to use a separate thread to fill the chunk buffer.
Definition: BlockWorldClient.cpp:3061
virtual int InstallFields(CAttributeClass *pClass, bool bOverride)
this class should be implemented if one wants to add new attribute.
Definition: BlockWorldClient.cpp:3183
void RenderDeferredLights()
render light geometry.
Definition: BlockWorldClient.cpp:3116
void SetDamageTexture(const char *textureName)
used to render damaged texture.
Definition: BlockWorldClient.cpp:1932
int GetAlwaysInVertexBufferChunkRadius() const
we will ensure all chunks in this buffer range are also in memory, and not counted in m_nVertexBuffer...
Definition: BlockWorldClient.cpp:3041
int GetVertexBufferSizeLimit() const
for 32 bits version, we need to set the vertex buffer size limit to something like 700MB to prevent r...
Definition: BlockWorldClient.cpp:2985
base class for an instance of block world
Definition: BlockWorld.h:35
virtual int GetAttributeClassID()
attribute class ID should be identical, unless one knows how overriding rules work.
Definition: BlockWorldClient.h:19
bool GetUsePointTextureFiltering()
whether to use point texture filtering for all ui images rendered.
Definition: BlockWorldClient.cpp:2146
int GetNearCameraChunkDist() const
near camera chunk is always rendered and cached in vertex buffer.
Definition: BlockWorldClient.cpp:3088
vertex element
Definition: VertexDeclarationOpenGL.h:21
in OpenGL, there is no different between vertex and index buffer.
Definition: ParaVertexBuffer.h:78
bool PrepareAllRenderTargets(bool bSetRenderTarget=true)
call this function to set up all render targets for fancy graphics if any.
Definition: BlockWorldClient.cpp:2225
A common interface for all classes implementing IAttributeFields By implementing this class&#39;s virtual...
Definition: IAttributeFields.h:59
virtual int GetChildAttributeObjectCount(int nColumnIndex=0)
get the number of child objects (row count) in the given column.
Definition: BlockWorldClient.cpp:3032
bool IsMovieOutputMode() const
whenever in movie output mode, we will cache all terrain and lighting information.
Definition: BlockWorldClient.cpp:2930
void SetUseSunlightShadowMap(bool bEnable)
whether to use sun light shadow map
Definition: BlockWorldClient.cpp:2378
void SetUseWaterReflection(bool bEnable)
whether to use sun light shadow map
Definition: BlockWorldClient.cpp:2403
void InvalidateDeviceObjects()
must call this function when device is invalid.
Definition: BlockWorldClient.cpp:2166
int GetMaxVisibleVertexBufferBytes() const
we will only draw when there are less than this number of vertex visible at the current frame...
Definition: BlockWorldClient.cpp:3051
some functions about shadow map
Definition: ShadowMap.h:8
bool DrawMultiFrameBlockWorldOnSky()
render the image to the screen, with z test enabled and z-write disabled.
Definition: BlockWorldClient.cpp:3110
float GetWaterLevel(float x, float y, float z, int nRayLength=2)
get the water block level just nRayLength blocks under the given positions it will also check global ...
Definition: BlockWorldClient.cpp:1983
this is a singleton client side block world instance.
Definition: BlockWorldClient.h:15
void RenderDynamicBlocks()
render selection and damaged blocks
Definition: BlockWorldClient.cpp:2431
virtual const char * GetAttributeClassName()
a static string, describing the attribute class object&#39;s name
Definition: BlockWorldClient.h:21
this is used for rendering a super large block world across several frames.
Definition: MultiFrameBlockWorldRenderer.h:19
A linear, 32-bit/component floating point RGBA color.
Definition: ParaColor.h:12
Definition: BlockRenderTask.h:13
void RenderShadowMap()
render shadow map pass
Definition: BlockWorldClient.cpp:259
void SetBlockLightColor(const LinearColor &color)
default block emissive light color.
Definition: BlockWorldClient.cpp:2131
void DeleteDeviceObjects()
must call this function when device is lost.
Definition: BlockWorldClient.cpp:1860
bool DrawMultiFrameBlockWorld()
render multi frame block world is enabled.
Definition: BlockWorldClient.cpp:2348
LinearColor GetBlockLightColor()
default block emissive light color.
Definition: BlockWorldClient.cpp:2136
void SetPostProcessingScript(const char *sCallbackScript)
this is usually set for fancy graphics&#39; composite shader in deferred shading.
Definition: BlockWorldClient.cpp:2355
bool IsPointUnderWater(const Vector3 &vPos)
check whether a specified point is under the water surface.
Definition: BlockWorldClient.cpp:1953
void Render(BlockRenderPass nRenderPass=BlockRenderPass_Opaque, std::vector< BlockRenderTask *> *pCurRenderQueue=NULL, int nRenderMethod=-1)
render all blocks in render queue using the given pass
Definition: BlockWorldClient.cpp:440
void InitDeviceObjects()
this function currently does nothing
Definition: BlockWorldClient.cpp:2156
chunk column&#39;s height map data
Definition: ChunkMaxHeight.h:6
void SetSelectionTexture(const char *textureName)
used to render selection texture.
Definition: BlockWorldClient.cpp:1898