00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __SP_RENDERSYSTEM_DIRECT3D9_H__
00009 #define __SP_RENDERSYSTEM_DIRECT3D9_H__
00010
00011
00012 #include "Base/spStandard.hpp"
00013
00014 #if defined(SP_COMPILE_WITH_DIRECT3D9)
00015
00016
00017 #include "RenderSystem/spRenderSystem.hpp"
00018 #include "RenderSystem/Direct3D9/spDirect3D9Texture.hpp"
00019 #include "RenderSystem/Direct3D9/spDirect3D9Shader.hpp"
00020
00021 #include <d3d9.h>
00022 #include <d3dx9.h>
00023 #if defined(SP_COMPILER_VC)
00024 # include <dxerr.h>
00025 #else
00026 # include <dxerr9.h>
00027 #endif
00028
00029
00030 namespace sp
00031 {
00032 namespace video
00033 {
00034
00035
00036 extern const io::stringc d3dDllFileName;
00037
00038
00040 class SP_EXPORT Direct3D9RenderSystem : public RenderSystem
00041 {
00042
00043 public:
00044
00045 Direct3D9RenderSystem();
00046 ~Direct3D9RenderSystem();
00047
00048
00049
00050 void setupConfiguration();
00051
00052
00053
00054 io::stringc getRenderer() const;
00055 io::stringc getVersion() const;
00056 io::stringc getVendor() const;
00057 io::stringc getShaderVersion() const;
00058
00059 s32 getMultitexCount() const;
00060 s32 getMaxAnisotropicFilter() const;
00061 s32 getMaxLightCount() const;
00062
00063 bool queryVideoSupport(const EVideoFeatureQueries Query) const;
00064
00065
00066
00067 void clearBuffers(const s32 ClearFlags = BUFFER_COLOR | BUFFER_DEPTH);
00068
00069
00070
00071 void setShadeMode(const EShadeModeTypes ShadeMode);
00072
00073 void setClearColor(const color &Color);
00074 void setColorMask(bool isRed, bool isGreen, bool isBlue, bool isAlpha = true);
00075 void setDepthMask(bool isDepth);
00076 void setAntiAlias(bool isAntiAlias);
00077
00078 void setDepthRange(f32 Near, f32 Far);
00079 void setDepthClip(bool Enable);
00080
00081
00082
00083 void setStencilMask(u32 BitMask);
00084 void setStencilMethod(const ESizeComparisionTypes Method, s32 Reference = 0, u32 BitMask = ~0);
00085 void setStencilOperation(const EStencilOperations FailOp, const EStencilOperations ZFailOp, const EStencilOperations ZPassOp);
00086 void setClearStencil(s32 Stencil);
00087
00088
00089
00090 bool setupMaterialStates(const MaterialStates* Material, bool Forced = false);
00091
00092 void setupTextureLayer(
00093 u8 LayerIndex, const dim::matrix4f &TexMatrix, const ETextureEnvTypes EnvType,
00094 const EMappingGenTypes GenType, s32 MappingCoordsFlags
00095 );
00096
00097 void updateLight(
00098 u32 LightID, const scene::ELightModels LightType, bool isVolumetric,
00099 const dim::vector3df &Direction, f32 SpotInnerConeAngle, f32 SpotOuterConeAngle,
00100 f32 AttenuationConstant, f32 AttenuationLinear, f32 AttenuationQuadratic
00101 );
00102
00103
00104
00105 void createVertexBuffer(void* &BufferID);
00106 void createIndexBuffer(void* &BufferID);
00107
00108 void deleteVertexBuffer(void* &BufferID);
00109 void deleteIndexBuffer(void* &BufferID);
00110
00111 void updateVertexBuffer(
00112 void* BufferID, const dim::UniversalBuffer &BufferData, const VertexFormat* Format, const EHWBufferUsage Usage
00113 );
00114 void updateIndexBuffer(
00115 void* BufferID, const dim::UniversalBuffer &BufferData, const IndexFormat* Format, const EHWBufferUsage Usage
00116 );
00117
00118 void updateVertexBufferElement(void* BufferID, const dim::UniversalBuffer &BufferData, u32 Index);
00119 void updateIndexBufferElement(void* BufferID, const dim::UniversalBuffer &BufferData, u32 Index);
00120
00121 void drawMeshBuffer(const MeshBuffer* MeshBuffer);
00122
00123
00124
00125 void setRenderState(const video::ERenderStates Type, s32 State);
00126 s32 getRenderState(const video::ERenderStates Type) const;
00127
00128
00129
00130 void addDynamicLightSource(
00131 u32 LightID, scene::ELightModels Type,
00132 video::color &Diffuse, video::color &Ambient, video::color &Specular,
00133 f32 AttenuationConstant, f32 AttenuationLinear, f32 AttenuationQuadratic
00134 );
00135
00136 void setLightStatus(u32 LightID, bool Enable, bool UseAllRCs = false);
00137
00138 void setLightColor(
00139 u32 LightID,
00140 const video::color &Diffuse, const video::color &Ambient, const video::color &Specular,
00141 bool UseAllRCs = false
00142 );
00143
00144
00145
00146 void setFog(const EFogTypes Type);
00147 void setFogColor(const video::color &Color);
00148 void setFogRange(f32 Range, f32 NearPlane = 1.0f, f32 FarPlane = 1000.0f, const EFogModes Mode = FOG_PALE);
00149
00150
00151
00152 void setClipPlane(u32 Index, const dim::plane3df &Plane, bool Enable);
00153
00154
00155
00156 ShaderClass* createShaderClass(VertexFormat* VertexInputLayout = 0);
00157
00158 Shader* createShader(
00159 ShaderClass* ShaderClassObj, const EShaderTypes Type, const EShaderVersions Version,
00160 const std::list<io::stringc> &ShaderBuffer, const io::stringc &EntryPoint = ""
00161 );
00162
00163 Shader* createCgShader(
00164 ShaderClass* ShaderClassObj, const EShaderTypes Type, const EShaderVersions Version,
00165 const std::list<io::stringc> &ShaderBuffer, const io::stringc &EntryPoint = "",
00166 const c8** CompilerOptions = 0
00167 );
00168
00169 void unbindShaders();
00170
00171
00172
00173 void endSceneRendering();
00174
00175 void beginDrawing2D();
00176
00177 void setBlending(const EBlendingTypes SourceBlend, const EBlendingTypes DestBlend);
00178 void setClipping(bool Enable, const dim::point2di &Position, const dim::size2di &Dimension);
00179 void setViewport(const dim::point2di &Position, const dim::size2di &Dimension);
00180
00181 bool setRenderTarget(Texture* Target);
00182
00183 void setLineSize(s32 Size = 1);
00184 void setPointSize(s32 Size = 1);
00185
00186
00187
00188 void draw2DImage(
00189 const Texture* Tex, const dim::point2di &Position, const color &Color = color(255)
00190 );
00191
00192 void draw2DImage(
00193 const Texture* Tex, const dim::rect2di &Position,
00194 const dim::rect2df &Clipping = dim::rect2df(0.0f, 0.0f, 1.0f, 1.0f),
00195 const color &Color = color(255)
00196 );
00197
00198 void draw2DImage(
00199 const Texture* Tex, const dim::point2di &Position, f32 Rotation, f32 Radius, const color &Color = color(255)
00200 );
00201
00202 void draw2DImage(
00203 const Texture* Tex,
00204 const dim::point2di &lefttopPosition,
00205 const dim::point2di &righttopPosition,
00206 const dim::point2di &rightbottomPosition,
00207 const dim::point2di &leftbottomPosition,
00208 const dim::point2df &lefttopClipping = dim::point2df(0.0f, 0.0f),
00209 const dim::point2df &righttopClipping = dim::point2df(1.0f, 0.0f),
00210 const dim::point2df &rightbottomClipping = dim::point2df(1.0f, 1.0f),
00211 const dim::point2df &leftbottomClipping = dim::point2df(0.0f, 1.0f),
00212 const color &lefttopColor = color(255),
00213 const color &righttopColor = color(255),
00214 const color &rightbottomColor = color(255),
00215 const color &leftbottomColor = color(255)
00216 );
00217
00218
00219
00220 color getPixelColor(const dim::point2di &Position) const;
00221 f32 getPixelDepth(const dim::point2di &Position) const;
00222
00223 void draw2DPoint(const dim::point2di &Position, const color &Color = 255);
00224
00225 void draw2DLine(
00226 const dim::point2di &PositionA, const dim::point2di &PositionB, const color &Color = 255
00227 );
00228 void draw2DLine(
00229 const dim::point2di &PositionA, const dim::point2di &PositionB, const color &ColorA, const color &ColorB
00230 );
00231 void draw2DLine(
00232 const dim::point2di &PositionA, const dim::point2di &PositionB, const color &Color, s32 DotLength
00233 );
00234
00235 void draw2DRectangle(
00236 const dim::rect2di &Rect, const color &Color = 255, bool isSolid = true
00237 );
00238 void draw2DRectangle(
00239 const dim::rect2di &Rect, const color &lefttopColor, const color &righttopColor,
00240 const color &rightbottomColor, const color &leftbottomColor, bool isSolid = true
00241 );
00242
00243
00244
00245 void draw2DPolygon(
00246 const ERenderPrimitives Type, const scene::SPrimitiveVertex2D* VerticesList, u32 Count
00247 );
00248 void draw2DPolygonImage(
00249 const ERenderPrimitives Type, Texture* Tex, const scene::SPrimitiveVertex2D* VerticesList, u32 Count
00250 );
00251
00252
00253
00254 void draw3DPoint(
00255 const dim::vector3df &Position, const color &Color = 255
00256 );
00257 void draw3DLine(
00258 const dim::vector3df &PositionA, const dim::vector3df &PositionB, const color &Color = 255
00259 );
00260 void draw3DLine(
00261 const dim::vector3df &PositionA, const dim::vector3df &PositionB, const color &ColorA, const color &ColorB
00262 );
00263 void draw3DEllipse(
00264 const dim::vector3df &Position, const dim::vector3df &Rotation = 0.0f,
00265 const dim::size2df &Radius = 0.5f, const color &Color = 255
00266 );
00267 void draw3DTriangle(
00268 Texture* hTexture, const dim::triangle3df &Triangle, const color &Color = 255
00269 );
00270
00271
00272
00273 Texture* createTexture(const STextureCreationFlags &CreationFlags);
00274
00275 Texture* createScreenShot(const dim::point2di &Position = 0, dim::size2di Size = 0);
00276 void createScreenShot(Texture* Tex, const dim::point2di &Position = 0);
00277
00278
00279
00280 Font* createBitmapFont(const io::stringc &FontName = "", s32 FontSize = 0, s32 Flags = 0);
00281
00282
00283
00284 void updateModelviewMatrix();
00285
00286 void setProjectionMatrix(const dim::matrix4f &Matrix);
00287 void setViewMatrix (const dim::matrix4f &Matrix);
00288 void setWorldMatrix (const dim::matrix4f &Matrix);
00289 void setTextureMatrix (const dim::matrix4f &Matrix, u8 TextureLayer = 0);
00290 void setColorMatrix (const dim::matrix4f &Matrix);
00291
00292
00293
00294 inline IDirect3DDevice9* getDirect3DDevice() const
00295 {
00296 return D3DDevice_;
00297 }
00298
00299 private:
00300
00301 friend class Direct3D9Texture;
00302 friend class Direct3D9RenderContext;
00303
00304
00305
00306 void updatePrimitiveList(const SPrimitiveVertex* VertexList, u32 Size);
00307 void updatePrimitiveListFlexible(const SPrimitiveVertex* VertexList, u32 Count);
00308
00309 static void setupTextureFormats(
00310 const EPixelFormats Format, const EHWTextureFormats HWFormat, D3DFORMAT &D3DFormat, DWORD &Usage
00311 );
00312
00313 bool createRendererTexture(
00314 bool MipMaps, const ETextureDimensions Dimension, dim::vector3di Size, const EPixelFormats Format,
00315 const u8* ImageData, const EHWTextureFormats HWFormat = HWTEXFORMAT_UBYTE8, bool isRenderTarget = false
00316 );
00317
00318 bool setRenderTargetSurface(const s32 Index, Texture* Target);
00319
00320 void releaseFontObject(Font* FontObj);
00321
00322 void drawTexturedFont(const Font* FontObj, const dim::point2di &Position, const io::stringc &Text, const color &Color);
00323 void drawBitmapFont(const Font* FontObj, const dim::point2di &Position, const io::stringc &Text, const color &Color);
00324
00325 void bindDrawingColor(const video::color &Color);
00326 void unbindDrawingColor();
00327
00328
00329
00330 inline D3DCOLORVALUE getD3DColor(const video::color &Color)
00331 {
00332 D3DCOLORVALUE Result;
00333 Color.getFloatArray(&Result.r);
00334 return Result;
00335 }
00336
00337 template <class T> inline void releaseObject(T* &Object)
00338 {
00339 if (Object)
00340 {
00341 Object->Release();
00342 Object = 0;
00343 }
00344 }
00345
00346
00347
00348 IDirect3D9* D3DInstance_;
00349 IDirect3DDevice9* D3DDevice_;
00350
00351 IDirect3DVertexBuffer9* D3DDefVertexBuffer_;
00352 IDirect3DVertexBuffer9* D3DDefFlexibleVertexBuffer_;
00353
00354 D3DCAPS9 DevCaps_;
00355 D3DLIGHT9 D3DActiveLight_;
00356
00357 IDirect3DSurface9* LastRenderTarget_;
00358 s32 LastRTCount_;
00359
00360 IDirect3DTexture9* CurD3DTexture_;
00361 IDirect3DCubeTexture9* CurD3DCubeTexture_;
00362 IDirect3DVolumeTexture9* CurD3DVolumeTexture_;
00363
00364 video::color ClearColor_, ClearColorMask_;
00365 DWORD ClearStencil_;
00366
00367 };
00368
00369
00370 }
00371
00372 }
00373
00374
00375 #endif
00376
00377 #endif
00378
00379
00380
00381