00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __SP_DEFERRED_RENDERER_H__
00009 #define __SP_DEFERRED_RENDERER_H__
00010
00011
00012 #include "Base/spStandard.hpp"
00013
00014 #if defined(SP_COMPILE_WITH_DEFERREDRENDERER)
00015
00016
00017 #include "RenderSystem/DeferredRenderer/spGBuffer.hpp"
00018 #include "RenderSystem/DeferredRenderer/spShadowMapper.hpp"
00019 #include "RenderSystem/PostProcessing/spBloomEffect.hpp"
00020 #include "Base/spVertexFormatUniversal.hpp"
00021
00022 #include <boost/shared_ptr.hpp>
00023
00024
00025 namespace sp
00026 {
00027
00028 namespace scene
00029 {
00030 class SceneGraph;
00031 class Camera;
00032 }
00033
00034 namespace video
00035 {
00036
00037
00039 enum EDeferredRenderFlags
00040 {
00045 DEFERREDFLAG_USE_TEXTURE_MATRIX = 0x0001,
00050 DEFERREDFLAG_HAS_SPECULAR_MAP = 0x0002,
00056 DEFERREDFLAG_HAS_LIGHT_MAP = 0x0004,
00058 DEFERREDFLAG_ALLOW_OVERBLENDING = 0x0008,
00059
00064 DEFERREDFLAG_NORMAL_MAPPING = 0x0010,
00072 DEFERREDFLAG_PARALLAX_MAPPING = 0x0020,
00077 DEFERREDFLAG_NORMALMAP_XYZ_H = 0x0040,
00082 DEFERREDFLAG_SHADOW_MAPPING = 0x0080,
00090 DEFERREDFLAG_GLOBAL_ILLUMINATION = 0x0100,
00092 DEFERREDFLAG_BLOOM = 0x0200,
00093
00094 #if 0
00095
00099 DEFERREDFLAG_TESSELLATION = 0x0400,
00100 #endif
00101
00106 DEFERREDFLAG_DEBUG_GBUFFER = 0x1000,
00112 DEFERREDFLAG_DEBUG_GBUFFER_WORLDPOS = 0x2000,
00118 DEFERREDFLAG_DEBUG_GBUFFER_TEXCOORDS = 0x4000,
00125 DEFERREDFLAG_DEBUG_VIRTUALPOINTLIGHTS = 0x8000,
00126 };
00127
00128
00129 class ShaderClass;
00130
00131
00136 struct STextureLayerModel
00137 {
00138 STextureLayerModel() :
00139 DiffuseMap (TEXTURE_IGNORE),
00140 SpecularMap (TEXTURE_IGNORE),
00141 LightMap (TEXTURE_IGNORE),
00142 NormalMap (TEXTURE_IGNORE),
00143 HeightMap (TEXTURE_IGNORE)
00144 {
00145 }
00146 ~STextureLayerModel()
00147 {
00148 }
00149
00150
00151 void clear()
00152 {
00153 DiffuseMap = TEXTURE_IGNORE;
00154 SpecularMap = TEXTURE_IGNORE;
00155 LightMap = TEXTURE_IGNORE;
00156 NormalMap = TEXTURE_IGNORE;
00157 HeightMap = TEXTURE_IGNORE;
00158 }
00159
00160
00161 u8 DiffuseMap;
00162 u8 SpecularMap;
00163 u8 LightMap;
00164 u8 NormalMap;
00165
00170 u8 HeightMap;
00171 };
00172
00173
00178 class SP_EXPORT DeferredRenderer
00179 {
00180
00181 public:
00182
00183 DeferredRenderer();
00184 virtual ~DeferredRenderer();
00185
00186
00187
00199 virtual bool generateResources(
00200 s32 Flags, s32 ShadowTexSize, u32 MaxPointLightCount, u32 MaxSpotLightCount, s32 MultiSampling = 0
00201 );
00202
00219 virtual void renderScene(
00220 scene::SceneGraph* Graph, scene::Camera* ActiveCamera = 0,
00221 Texture* RenderTarget = 0, bool UseDefaultGBufferShader = true
00222 );
00223
00224
00225
00234 inline bool generateResources(s32 Flags = 0)
00235 {
00236 return generateResources(Flags, 256, 8, 8);
00237 }
00238
00240 inline const GBuffer* getGBuffer() const
00241 {
00242 return &GBuffer_;
00243 }
00245 inline GBuffer* getGBuffer()
00246 {
00247 return &GBuffer_;
00248 }
00249
00251 inline const ShadowMapper* getShadowMapper() const
00252 {
00253 return &ShadowMapper_;
00254 }
00256 inline ShadowMapper* getShadowMapper()
00257 {
00258 return &ShadowMapper_;
00259 }
00260
00262 inline const BloomEffect* getBloomEffect() const
00263 {
00264 return &BloomEffect_;
00265 }
00267 inline BloomEffect* getBloomEffect()
00268 {
00269 return &BloomEffect_;
00270 }
00271
00273 inline ShaderClass* getGBufferShader() const
00274 {
00275 return GBufferShader_;
00276 }
00278 inline ShaderClass* getDeferredShader() const
00279 {
00280 return DeferredShader_;
00281 }
00282
00284 inline const VertexFormatUniversal* getVertexFormat() const
00285 {
00286 return &VertexFormat_;
00287 }
00288
00294 inline const STextureLayerModel& getTextureLayerModel() const
00295 {
00296 return LayerModel_;
00297 }
00298
00303 inline void setAmbientColor(const dim::vector3df &ColorVec)
00304 {
00305 AmbientColor_ = ColorVec;
00306 }
00308 inline dim::vector3df getAmbientColor() const
00309 {
00310 return AmbientColor_;
00311 }
00312
00319 inline void setDebugVPL(bool Enable)
00320 {
00321 DebugVPL_.Enabled = Enable;
00322 }
00327 inline bool getDebugVPL() const
00328 {
00329 return DebugVPL_.Enabled;
00330 }
00331
00332 protected:
00333
00334
00335
00336 #if defined(_MSC_VER)
00337 # pragma pack(push, packing)
00338 # pragma pack(1)
00339 # define SP_PACK_STRUCT
00340 #elif defined(__GNUC__)
00341 # define SP_PACK_STRUCT __attribute__((packed))
00342 #else
00343 # define SP_PACK_STRUCT
00344 #endif
00345
00346 struct SP_EXPORT SLight
00347 {
00348 SLight();
00349 ~SLight();
00350
00351
00352 dim::vector3df Position;
00353 f32 Radius;
00354 dim::vector3df Color;
00355 s32 Type;
00356 s32 ShadowIndex;
00357 s32 UsedForLightmaps;
00358 SShaderConstant Constants[5];
00359 }
00360 SP_PACK_STRUCT;
00361
00362 struct SP_EXPORT SLightEx
00363 {
00364 SLightEx();
00365 ~SLightEx();
00366
00367
00368 dim::matrix4f ViewProjection;
00369 dim::vector3df Direction;
00370 f32 SpotTheta;
00371 f32 SpotPhiMinusTheta;
00372 dim::matrix4f InvViewProjection;
00373 SShaderConstant Constants[5];
00374 }
00375 SP_PACK_STRUCT;
00376
00377 #ifdef _MSC_VER
00378 # pragma pack(pop, packing)
00379 #endif
00380
00381 #undef SP_PACK_STRUCT
00382
00383 struct SP_EXPORT SLightDesc
00384 {
00385 SShaderConstant LightCountConstant;
00386 SShaderConstant LightExCountConstant;
00387 };
00388
00389 struct SP_EXPORT SDebugVPL
00390 {
00391 SDebugVPL();
00392 ~SDebugVPL();
00393
00394
00395 void load();
00396 void unload();
00397
00398
00399 ShaderClass* ShdClass;
00400 VertexFormatUniversal* VtxFormat;
00401 MeshBuffer Model;
00402 MaterialStates Material;
00403 bool Enabled;
00404 };
00405
00406
00407
00409 virtual void updateLightSources(scene::SceneGraph* Graph, scene::Camera* ActiveCamera);
00411 virtual void renderSceneIntoGBuffer(
00412 scene::SceneGraph* Graph, scene::Camera* ActiveCamera, bool UseDefaultGBufferShader
00413 );
00414 virtual void renderDeferredShading(Texture* RenderTarget);
00415
00416 void renderDebugVirtualPointLights(scene::Camera* ActiveCamera);
00417
00418 bool buildShader(
00419 const io::stringc &Name,
00420
00421 ShaderClass* &ShdClass,
00422 VertexFormat* VertFmt,
00423
00424 const std::list<io::stringc>* ShdBufferVertex,
00425 const std::list<io::stringc>* ShdBufferPixel,
00426
00427 const io::stringc &VertexMain = "VertexMain",
00428 const io::stringc &PixelMain = "PixelMain",
00429
00430 s32 Flags = SHADERBUILD_CG
00431 );
00432
00433 void deleteShaders();
00434 void createVertexFormats();
00435
00436 void setupCompilerOptions(
00437 std::list<io::stringc> &GBufferCompilerOp, std::list<io::stringc> &DeferredCompilerOp
00438 );
00439
00440 void setupGBufferSampler(Shader* ShaderObj);
00441 void setupDeferredSampler(Shader* ShaderObj);
00442 void setupDebugVPLSampler(Shader* ShaderObj);
00443
00444 void setupLightShaderConstants();
00445 void setupJitteredOffsets();
00446 void setupVPLOffsets(
00447 Shader* ShaderObj, const io::stringc &BufferName, u32 OffsetCount, s32 Rings = 5, s32 Rotations = 5
00448 );
00449
00450
00451
00452 GBuffer GBuffer_;
00453 ShadowMapper ShadowMapper_;
00454 BloomEffect BloomEffect_;
00455
00456 ShaderClass* GBufferShader_;
00457 ShaderClass* DeferredShader_;
00458 ShaderClass* ShadowShader_;
00459
00460 VertexFormatUniversal VertexFormat_;
00461 VertexFormatUniversal ImageVertexFormat_;
00462
00463 s32 Flags_;
00464 STextureLayerModel LayerModel_;
00465
00466 SLightDesc LightDesc_;
00467 std::vector<SLight> Lights_;
00468 std::vector<SLightEx> LightsEx_;
00469
00470 dim::vector3df AmbientColor_;
00471
00472 SDebugVPL DebugVPL_;
00473
00474
00475
00476 };
00477
00478
00479 }
00480
00481 }
00482
00483
00484 #endif
00485
00486 #endif
00487
00488
00489
00490