00001
00002
00003 #ifndef _RENDER_MANAGER_H_
00004 #define _RENDER_MANAGER_H_
00005
00006 #include <EngineConfig.h>
00007 #include <core/Types.h>
00008 #include <core/Singleton.h>
00009 #include <render/Color.h>
00010 #include <render/RenderOperation.h>
00011 #include <render/Material.h>
00012 #include <render/ShaderParamData.h>
00013 #include <engine/Object.h>
00014
00015 #include <string>
00016 #include <list>
00017 #include <vector>
00018 #include <map>
00019
00020 namespace core
00021 {
00022 class matrix4;
00023 }
00024
00025 namespace platform
00026 {
00027 class Timer;
00028 }
00029
00030 namespace resource
00031 {
00032 class Resource;
00033 class Serializer;
00034 enum BufferUsage;
00035 enum PixelFormat;
00036 }
00037
00038 namespace scene
00039 {
00040 class Node;
00041 }
00042
00043 namespace render
00044 {
00045
00046 class Frustum;
00047 class Light;
00048 class Camera;
00049 class Renderable;
00050 class Model;
00051 class Overlay;
00052 class PanelOverlay;
00053 class TextOverlay;
00054 class Font;
00055 class MeshData;
00056 class Material;
00057 class Viewport;
00058 class RenderWindow;
00059 class RenderDriver;
00060 class FrameEventReceiver;
00061 struct FrameEvent;
00062 class Shader;
00063 class VertexBuffer;
00064 class IndexBuffer;
00065 class PixelBuffer;
00066 class VertexDeclaration;
00067 class VertexBufferBinding;
00068 class FontFactory;
00069 class MaterialFactory;
00070 class MeshDataFactory;
00071 enum IndexType;
00072 enum ShaderType;
00073
00079 class ENGINE_PUBLIC_EXPORT RenderManager: public engine::Object, public core::Singleton<RenderManager>
00080 {
00081 public:
00082
00083
00084 RenderManager();
00085
00086 ~RenderManager();
00087
00088 RenderWindow* createRenderWindow(s32 width, s32 height, s32 colorDepth, bool fullScreen, s32 left = 0, s32 top = 0, bool depthBuffer = true, void* windowId = NULL);
00089
00091 void setMainWindow(RenderWindow* window);
00092
00094 RenderWindow* getMainWindow();
00095
00097 RenderWindow* getRenderWindow(const u32& id);
00098
00100 void removeRenderWindow(const u32& id);
00101
00103 void removeAllRenderWindows();
00104
00105 void start();
00106
00107 void stop();
00108
00109 void addFrameEventReceiver(FrameEventReceiver* newEventReceiver);
00110 void removeFrameEventReceiver(FrameEventReceiver* oldEventReceiver);
00111
00113 s32 getViewportHeight() const;
00114
00116 s32 getViewportWidth() const;
00117
00119 Light* createLight(scene::Node* parent = NULL);
00120 Light* createLight(const std::string& name, scene::Node* parent = NULL);
00122 Light* getLight(const u32& id);
00123
00125 u32 getNumberOfLights() const;
00126
00128 void removeLight(Light *lt);
00130 void removeLight(const u32& id);
00132 void removeAllLights();
00133
00135 Camera* createCamera(scene::Node* parent = NULL);
00136 Camera* createCamera(const std::string& name, scene::Node* parent = NULL);
00137
00139 Camera* getCamera(const u32& id);
00140
00142 u32 getNumberOfCameras() const;
00143
00145 void removeCamera(Camera *cam);
00147 void removeCamera(const u32& id);
00149 void removeAllCameras();
00150
00152 Renderable* createRenderable(scene::Node* parent = NULL);
00153 Renderable* createRenderable(const std::string& name, scene::Node* parent = NULL);
00154
00156 Renderable* getRenderable(const u32& id);
00157
00159 u32 getNumberOfRenderables() const;
00160
00162 void removeRenderable(Renderable *renderable);
00164 void removeRenderable(const u32& id);
00166 void removeAllRenderables();
00167
00169 Model* createModel(const std::string& meshFilename, scene::Node* parent = NULL);
00170 Model* createModel(const std::string& name, const std::string& meshFilename, scene::Node* parent = NULL);
00171
00173 Model* createModel(MeshData* meshData, scene::Node* parent = NULL);
00174 Model* createModel(const std::string& name, MeshData* meshData, scene::Node* parent = NULL);
00175
00177 Model* getModel(const u32& id);
00178
00180 void removeModel(Model *model);
00182 void removeModel(const u32& id);
00183
00185 PanelOverlay* createPanelOverlay();
00186 PanelOverlay* createPanelOverlay(const std::string& name);
00187
00189 TextOverlay* createTextOverlay();
00190 TextOverlay* createTextOverlay(const std::string& name);
00191
00193 Overlay* getOverlay(const u32& id);
00194
00196 u32 getNumberOfOverlays() const;
00197
00199 void removeOverlay(Overlay *overlay);
00201 void removeOverlay(const u32& id);
00203 void removeAllOverlays();
00204
00206 Font* createFont(const std::string& fontFilename);
00207
00209 Font* getFont(const u32& id);
00210
00212 u32 getNumberOfFonts() const;
00213
00215 void removeFont(Font *fnt);
00217 void removeFont(const u32& id);
00219 void removeAllFonts();
00220
00222 Shader* createShader(const std::string& shaderFilename, const ShaderType& type);
00223
00225 Shader* getShader(const u32& id);
00226
00228 u32 getNumberOfShaders() const;
00229
00231 void removeShader(Shader* shader);
00233 void removeShader(const u32& id);
00235 void removeAllShaders();
00236
00238 VertexBuffer* createVertexBuffer(u32 vertexSize, u32 numVertices, resource::BufferUsage usage, bool useShadowBuffer = false);
00240 void removeVertexBuffer(VertexBuffer* buf);
00242 void removeAllVertexBuffers();
00243
00245 IndexBuffer* createIndexBuffer(IndexType idxType, u32 numIndexes, resource::BufferUsage usage, bool useShadowBuffer = false);
00247 void removeIndexBuffer(IndexBuffer* buf);
00249 void removeAllIndexBuffers();
00250
00252 PixelBuffer* createPixelBuffer(u32 width, u32 height, u32 depth, resource::PixelFormat format, resource::BufferUsage usage, bool useShadowBuffer = false);
00254 void removePixelBuffer(PixelBuffer* buf);
00256 void removeAllPixelBuffer();
00257
00259 VertexDeclaration* createVertexDeclaration();
00261 void removeVertexDeclaration(VertexDeclaration* decl);
00263 void removeAllVertexDeclarations();
00264
00266 VertexBufferBinding* createVertexBufferBinding();
00268 void removeVertexBufferBinding(VertexBufferBinding* binding);
00270 void removeAllVertexBufferBindings();
00271
00272 void setAmbientLight(const Color& ambient = Color::White);
00273
00274 void setFog(FogMode mode = FM_NONE, const Color& color = Color::White, f32 density = 0.001f, f32 start = 0.0f, f32 end = 1.0f);
00275
00277 void convertProjectionMatrix(const core::matrix4& matrix, core::matrix4& dest);
00278
00280 f32 getMinimumDepthInputValue();
00282 f32 getMaximumDepthInputValue();
00283
00285 f32 getHorizontalTexelOffset();
00287 f32 getVerticalTexelOffset();
00288
00289 void registerDefaultFactories();
00290 void removeDefaultFactories();
00291
00292 void setRenderDriver(RenderDriver* driver);
00293 void removeRenderDriver();
00294
00295 static RenderManager* getInstance();
00296
00297 private:
00298
00300 static void ENGINE_CALLBACK releaseFontImpl(resource::Resource* resource);
00302 static void ENGINE_CALLBACK releaseMeshDataImpl(resource::Resource* resource);
00304 static void ENGINE_CALLBACK releaseMaterialImpl(resource::Resource* resource);
00305
00306 protected:
00307
00308 void initializeImpl();
00309 void uninitializeImpl();
00310 void updateImpl(f32 elapsedTime);
00311
00312 FontFactory* mDefaultFontFactory;
00313 MaterialFactory* mDefaultMaterialFactory;
00314 MeshDataFactory* mDefaultMeshDataFactory;
00315
00316 RenderDriver* mRenderDriver;
00317
00318 static std::list<FrameEventReceiver*> mFrameEventReceivers;
00319
00320
00321 std::map<u32, RenderWindow*> mRenderWindows;
00322 RenderWindow* mMainWindow;
00323
00325 std::map<u32, Light*> mLights;
00326
00328 std::map<u32, Renderable*> mRenderables;
00329
00331 std::map<u32, Overlay*> mOverlays;
00332
00334 std::map<u32, Font*> mFonts;
00335
00337 std::map<u32, Camera*> mCameras;
00338
00340 std::map<u32, Shader*> mShaders;
00341
00342 std::list<VertexDeclaration*> mVertexDeclarations;
00343 std::list<VertexBufferBinding*> mVertexBufferBindings;
00344 std::list<VertexBuffer*> mVertexBuffers;
00345 std::list<IndexBuffer*> mIndexBuffers;
00346 std::list<PixelBuffer*> mPixelBuffers;
00347
00348 struct SolidRenderable
00349 {
00350 SolidRenderable()
00351 {
00352 renderable = NULL;
00353 materialID = 0;
00354 }
00355
00356 bool operator < (const SolidRenderable& other) const
00357 {
00358 return (materialID < other.materialID);
00359 }
00360
00361 Renderable* renderable;
00362 u32 materialID;
00363 };
00364
00365 struct TransparentRenderable
00366 {
00367 TransparentRenderable()
00368 {
00369 renderable = NULL;
00370 distance = 0;
00371 }
00372
00373 bool operator < (const TransparentRenderable& other) const
00374 {
00375 return (distance > other.distance);
00376 }
00377
00378 Renderable* renderable;
00379 f32 distance;
00380 };
00381
00382 struct DistanceLight
00383 {
00384 DistanceLight()
00385 {
00386 light = NULL;
00387 distance = 0;
00388 }
00389
00390 bool operator < (const DistanceLight& other) const
00391 {
00392 return (distance < other.distance);
00393 }
00394
00395 Light* light;
00396 f32 distance;
00397 };
00398
00399 std::vector<SolidRenderable> mSolidRenderables;
00400 std::vector<TransparentRenderable> mTransparentRenderables;
00401 std::vector<DistanceLight> mDistanceLights;
00402
00403 std::list<Light*> mLightsAffectingFrustum;
00404
00406 bool mUpdateRender;
00407
00409 Color mAmbientLight;
00410
00412 ShaderParamData mShaderParamData;
00413
00415 Material* mDefaultMaterial;
00416
00418 Viewport* mCurrentViewport;
00419
00420 Frustum* mFrustum;
00421
00422 RenderOperation mRenderOperation;
00423
00424
00425 FogMode mFogMode;
00426 Color mFogColor;
00427 f32 mFogDensity;
00428 f32 mFogStart;
00429 f32 mFogEnd;
00430
00431
00432 platform::Timer* mTimer;
00433
00434 u32 mLastStartTime;
00435 u32 mLastEndTime;
00436
00437 s32 mLastViewportWidth;
00438 s32 mLastViewportHeight;
00439
00440 void fireFrameStarted();
00441
00442 void fireFrameEnded();
00443
00444 void render(Camera* cam, Viewport *vp);
00445
00446 void setCurrentViewport(Viewport *vp);
00447
00448 void beginFrame(Viewport *vp);
00449
00450 void findLightsAffectingFrustum(Camera* cam);
00451
00452 void findLightsAffectingRenderables(Renderable* renderable);
00453
00454 void findVisibleRenderables(Camera* cam);
00455
00456 void renderVisibleRenderables();
00457
00458 void renderVisibleOverlays(Camera* cam, Viewport *vp);
00459 void renderSingleOverlay(Overlay* overlay, Camera* cam, Viewport *vp);
00460
00461 void renderSingleRenderable(Renderable* renderable);
00462
00463 void setMaterial(Material* mat);
00464
00465 void endFrame();
00466 };
00467
00468 }
00469
00470 #endif