absence_thelab
diff src/3deng/3dengine.h @ 0:1cffe3409164
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 23 Oct 2014 01:46:07 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/3deng/3dengine.h Thu Oct 23 01:46:07 2014 +0300 1.3 @@ -0,0 +1,391 @@ 1.4 +#ifndef _3DENGINE_H_ 1.5 +#define _3DENGINE_H_ 1.6 + 1.7 +// standard includes 1.8 +#include <vector> 1.9 +// system includes 1.10 +#include "d3d8.h" 1.11 +// includes from my codebase 1.12 +#include "typedefs.h" 1.13 +#include "linkedlist.h" 1.14 +#include "color.h" 1.15 +// 3d engine includes 1.16 +#include "n3dmath.h" 1.17 +#include "switches.h" 1.18 +#include "3dengtypes.h" 1.19 +#include "material.h" 1.20 +#include "textureman.h" 1.21 +#include "3dgeom.h" 1.22 + 1.23 +enum DeviceType {DeviceHardware = D3DDEVTYPE_HAL, DeviceReference = D3DDEVTYPE_REF}; 1.24 +enum TnLMode {HardwareTnL = D3DCREATE_HARDWARE_VERTEXPROCESSING, SoftwareTnL = D3DCREATE_SOFTWARE_VERTEXPROCESSING}; 1.25 +enum BufferChainMode {DoubleBuffering = 1, TripleBuffering = 2}; 1.26 +enum UsageFlags {UsageStatic = 0, UsageDynamic = D3DUSAGE_DYNAMIC}; 1.27 +enum ShadeMode {FlatShading = D3DSHADE_FLAT, GouraudShading = D3DSHADE_GOURAUD}; 1.28 +enum FaceOrder {Clockwise = D3DCULL_CW, CounterClockwise = D3DCULL_CCW}; 1.29 + 1.30 +enum PhongComponent {Ambient, Diffuse, Specular}; 1.31 + 1.32 +enum PrimitiveType { 1.33 + TriangleList = D3DPT_TRIANGLELIST, 1.34 + TriangleStrip = D3DPT_TRIANGLESTRIP, 1.35 + TriangleFan = D3DPT_TRIANGLEFAN, 1.36 + LineList = D3DPT_LINELIST, 1.37 + LineStrip = D3DPT_LINESTRIP, 1.38 + PointList = D3DPT_POINTLIST 1.39 +}; 1.40 + 1.41 +enum BlendingFactor { 1.42 + BLEND_ZERO = D3DBLEND_ZERO, 1.43 + BLEND_ONE = D3DBLEND_ONE, 1.44 + BLEND_SRCCOLOR = D3DBLEND_SRCCOLOR, 1.45 + BLEND_INVSRCCOLOR = D3DBLEND_INVSRCCOLOR, 1.46 + BLEND_SRCALPHA = D3DBLEND_SRCALPHA, 1.47 + BLEND_INVSRCALPHA = D3DBLEND_INVSRCALPHA, 1.48 + BLEND_DESTCOLOR = D3DBLEND_DESTCOLOR, 1.49 + BLEND_INVDESTCOLOR = D3DBLEND_INVDESTCOLOR, 1.50 + BLEND_DESTALPHA = D3DBLEND_DESTALPHA, 1.51 + BLEND_INVDESTALPHA = D3DBLEND_INVDESTALPHA, 1.52 + BLEND_SRCALPHASAT = D3DBLEND_SRCALPHASAT 1.53 +}; 1.54 + 1.55 +enum CmpFunc { 1.56 + CMP_NEVER = D3DCMP_NEVER, 1.57 + CMP_LESS = D3DCMP_LESS, 1.58 + CMP_EQUAL = D3DCMP_EQUAL, 1.59 + CMP_LEQUAL = D3DCMP_LESSEQUAL, 1.60 + CMP_GREATER = D3DCMP_GREATER, 1.61 + CMP_NOTEQUAL = D3DCMP_NOTEQUAL, 1.62 + CMP_GEQUAL = D3DCMP_GREATEREQUAL, 1.63 + CMP_ALWAYS = D3DCMP_ALWAYS 1.64 +}; 1.65 + 1.66 +enum StencilOp { 1.67 + SOP_KEEP = D3DSTENCILOP_KEEP, 1.68 + SOP_ZERO = D3DSTENCILOP_ZERO, 1.69 + SOP_REPLACE = D3DSTENCILOP_REPLACE, 1.70 + SOP_INCSAT = D3DSTENCILOP_INCRSAT, 1.71 + SOP_DECSAT = D3DSTENCILOP_DECRSAT, 1.72 + SOP_INVERT = D3DSTENCILOP_INVERT, 1.73 + SOP_INC = D3DSTENCILOP_INCR, 1.74 + SOP_DEC = D3DSTENCILOP_DECR 1.75 +}; 1.76 + 1.77 +enum TextureBlendFunction { 1.78 + TexBlendSelectArg1 = D3DTOP_SELECTARG1, // S[rgba] = Arg1 1.79 + TexBlendSelectArg2 = D3DTOP_SELECTARG2, // S[rgba] = Arg2 1.80 + TexBlendAdd = D3DTOP_ADD, // S[rgba] = Arg1 + Arg2 1.81 + TexBlendAddSigned = D3DTOP_ADDSIGNED, // S[rgba] = Arg1 + Arg2 - 0.5 1.82 + TexBlendAddSigned2x = D3DTOP_ADDSIGNED2X, // S[rgba] = (Arg1 + Arg2 - 0.5) << 1 1.83 + TexBlendSubtract = D3DTOP_SUBTRACT, // S[rgba] = Arg1 - Arg2 1.84 + TexBlendAddSmooth = D3DTOP_ADDSMOOTH, // S[rgba] = (Arg1 + Arg2) - (Arg1 * Arg2) 1.85 + TexBlendModulate = D3DTOP_MODULATE, // S[rgba] = Arg1 * Arg2 1.86 + TexBlendModulate2x = D3DTOP_MODULATE2X, // S[rgba] = (Arg1 * Arg2) << 1 1.87 + TexBlendModulate4x = D3DTOP_MODULATE4X, // S[rgba] = (Arg1 * Arg2) << 2 1.88 + TexBlendVertexAlpha = D3DTOP_BLENDDIFFUSEALPHA, // S[rgba] = Arg1*Alpha + Arg2*(1-Alpha) 1.89 + TexBlendTextureAlpha = D3DTOP_BLENDTEXTUREALPHA, // S[rgba] = Arg1*Alpha + Arg2*(1-Alpha) 1.90 + TexBlendFactorAlpha = D3DTOP_BLENDFACTORALPHA, // S[rgba] = Arg1*Alpha + Arg2*(1-Alpha) 1.91 + TexBlendPrevAlpha = D3DTOP_BLENDFACTORALPHA, // S[rgba] = Arg1*Alpha + Arg2*(1-Alpha) 1.92 + TexBlendPreMulAlpha = D3DTOP_BLENDTEXTUREALPHAPM, // S[rgba] = Arg1 + Arg2*(1-Alpha) 1.93 + TexBlendPreModulate = D3DTOP_PREMODULATE, 1.94 + TexBlendModAlphaAddColor = D3DTOP_MODULATEALPHA_ADDCOLOR,// S[rgba] = Arg1[rgb] + Arg1[a] * Arg2[rgb] 1.95 + TexBlendModulateAddAlpha = D3DTOP_MODULATECOLOR_ADDALPHA,// S[rgba] = Arg1[rgb] * Arg2[rgb] + Arg1[a] 1.96 + TexBlendModInvAlphaAddColor = D3DTOP_MODULATEINVALPHA_ADDCOLOR, // S[rgba] = Arg1[rgb] + (1-Arg1[a]) * Arg2[rgb] 1.97 + TexBlendModulateInvAddAlpha = D3DTOP_MODULATEINVCOLOR_ADDALPHA, // S[rgba] = (1-Arg1[rgb]) * Arg2[rgb] + Arg1[a] 1.98 + TexBlendBumpEnv = D3DTOP_BUMPENVMAP, // bump mapping with next stage's env map 1.99 + TexBlendBumpEnvLuminance = D3DTOP_BUMPENVMAPLUMINANCE, // bump mapping with next stage's env map with luminance 1.100 + TexBlendDotProduct = D3DTOP_DOTPRODUCT3, // S[rgba] = Arg1[rgb] (dot) Arg2[rgb] 1.101 + TexBlendMultiplyAdd = D3DTOP_MULTIPLYADD, // S[rgba] = Arg1 + Arg2 * Arg3 1.102 + TexBlendLerp = D3DTOP_LERP // S[rgba] = Arg2 + (Arg3 - Arg2) * Arg1 1.103 +}; 1.104 + 1.105 +enum TextureBlendArgument { 1.106 + TexArgNone = 0, // valid only for arg3 1.107 + TexArgCurrent = D3DTA_CURRENT, // the color from the previous stage output (diffuse for 1st) 1.108 + TexArgDiffuseColor = D3DTA_DIFFUSE, // the diffuse interpolated color 1.109 + TexArgSpecularColor = D3DTA_SPECULAR, // the specular interpolated color 1.110 + TexArgTexture = D3DTA_TEXTURE, // the texture bound to this stage 1.111 + TexArgFactor = D3DTA_TFACTOR, // a user defined factor 1.112 + TexArgTemp = D3DTA_TEMP // temp register 1.113 +}; 1.114 + 1.115 +enum TextureFilteringType { 1.116 + PointSampling, 1.117 + BilinearFiltering, 1.118 + TrilinearFiltering, 1.119 + AnisotropicFiltering 1.120 +}; 1.121 + 1.122 +enum TextureAddressing { 1.123 + TexAddrWrap = D3DTADDRESS_WRAP, 1.124 + TexAddrMirror = D3DTADDRESS_MIRROR, 1.125 + TexAddrClamp = D3DTADDRESS_CLAMP, 1.126 + TexAddrBorder = D3DTADDRESS_BORDER, 1.127 + TexAddrMirrorOnce = D3DTADDRESS_MIRRORONCE 1.128 +}; 1.129 + 1.130 +enum TexTransformState { 1.131 + TexTransformDisable = D3DTTFF_DISABLE, 1.132 + TexTransform1D = D3DTTFF_COUNT1, 1.133 + TexTransform2D = D3DTTFF_COUNT2, 1.134 + TexTransform3D = D3DTTFF_COUNT3, 1.135 + TexTransform4D = D3DTTFF_COUNT4, 1.136 + TexTransformProjected = D3DTTFF_PROJECTED 1.137 +}; 1.138 + 1.139 +// don't care flags for context creation 1.140 +const unsigned short GCPDONTCARE_NONE = 0; // 0000000000000000 1.141 +const unsigned short GCPDONTCARE_BPP = 1; // 0000000000000001 1.142 +const unsigned short GCPDONTCARE_REFRESH = 2; // 0000000000000010 1.143 +const unsigned short GCPDONTCARE_ALPHA = 4; // 0000000000000100 1.144 +const unsigned short GCPDONTCARE_DEPTH = 8; // 0000000000001000 1.145 +const unsigned short GCPDONTCARE_TNL = 16; // 0000000000010000 1.146 +const unsigned short GCPDONTCARE_BUFFERS = 32; // 0000000000100000 1.147 +const unsigned short GCPDONTCARE_AA = 64; // 0000000001000000 1.148 +const unsigned short GCPDONTCARE_VSYNC = 128; // 0000000010000000 1.149 + 1.150 +// fullscreen / windowed flags 1.151 +const unsigned short GCCREATE_WINDOWED = 0; 1.152 +const unsigned short GCCREATE_FULLSCREEN = 1; 1.153 + 1.154 + 1.155 + 1.156 +class Vertex; 1.157 + 1.158 +struct ColorDepth { 1.159 + int bpp, colorbits, alpha; 1.160 + 1.161 + ColorDepth(int bits=0, int c=0, int a=0) {colorbits = c; alpha = a; bpp = bits; } 1.162 +}; 1.163 + 1.164 +struct DisplayMode { 1.165 + unsigned int XRes, YRes, RefreshRate; 1.166 + ColorDepth ColorFormat; 1.167 +}; 1.168 + 1.169 +enum DisplayModeItem {ModeItemSize, ModeItemBpp, ModeItemAlpha, ModeItemRefresh}; 1.170 + 1.171 +struct Adapter { 1.172 + char *Driver, *Description; 1.173 + int64 DriverVersion; 1.174 + dword VentorID, DeviceID, SubSysID, Revision; 1.175 + GUID DeviceGUID; 1.176 + 1.177 + unsigned int ModeCount; 1.178 + DisplayMode *Modes; 1.179 + D3DCAPS8 Capabilities; 1.180 +}; 1.181 + 1.182 +struct RenderTarget { 1.183 + Surface *ColorSurface, *DepthStencilSurface; 1.184 +}; 1.185 + 1.186 +struct RenderParams { 1.187 + ShadeMode Shading; 1.188 + bool Billboarded; 1.189 + dword VertexProgram; 1.190 + dword PixelProgram; 1.191 + bool ZWrite; 1.192 + BlendingFactor SourceBlendFactor, DestBlendFactor; 1.193 +}; 1.194 + 1.195 +struct ContextInitParameters { 1.196 + int x, y; 1.197 + int bpp; 1.198 + int RefreshRate; 1.199 + bool AlphaChannel; 1.200 + int DepthBits; 1.201 + DeviceType DevType; 1.202 + bool HardwareTnL; 1.203 + bool FullScreen; 1.204 + bool Antialiasing; 1.205 + bool BestAA; 1.206 + bool VSync; 1.207 + BufferChainMode Buffers; 1.208 + unsigned short DontCareFlags; 1.209 +}; 1.210 + 1.211 + 1.212 +class GraphicsContext { 1.213 +private: 1.214 + PrimitiveType ptype; 1.215 + FaceOrder CullOrder; 1.216 + bool BackfaceCulling; 1.217 + bool MipMapEnabled; 1.218 + bool BillBoardingEnabled; 1.219 + dword MipFilter; 1.220 + 1.221 + // cache of current transformation matrices 1.222 + Matrix4x4 WorldMat[256], ViewMat, ProjMat, TexMat[8]; 1.223 + 1.224 + // disable copying contexts by making copy constructor and assignment private 1.225 + GraphicsContext(const GraphicsContext &gc); 1.226 + const GraphicsContext &operator =(const GraphicsContext &gc); 1.227 + 1.228 +public: 1.229 + HWND WindowHandle; 1.230 + RenderTarget MainRenderTarget; 1.231 + IDirect3DDevice8 *D3DDevice; 1.232 + ContextInitParameters ContextParams; 1.233 + D3DFORMAT ColorFormat, ZFormat; 1.234 + int AASamples; 1.235 + int MaxTextureStages; 1.236 + 1.237 + TextureManager *texman; // texture manager 1.238 + 1.239 + GraphicsContext(); 1.240 + 1.241 + void SetDefaultStates(); 1.242 + 1.243 + bool CreateVertexBuffer(uint32 VertexCount, UsageFlags usage, VertexBuffer **vb) const; 1.244 + bool CreateIndexBuffer(uint32 IndexCount, UsageFlags usage, IndexBuffer **ib) const; 1.245 + 1.246 + bool CreateSurface(uint32 Width, uint32 Height, Surface **surf) const; 1.247 + bool CreateDepthStencil(uint32 Width, uint32 Height, Surface **zsurf) const; 1.248 + 1.249 + void Clear(dword color) const; 1.250 + void ClearZBuffer(float zval) const; 1.251 + void ClearStencil(byte sval) const; 1.252 + void ClearZBufferStencil(float zval, byte sval) const; 1.253 + 1.254 + void Flip() const; 1.255 + 1.256 + bool Draw(VertexBuffer *vb); 1.257 + bool Draw(Vertex *varray, unsigned int VertexCount); 1.258 + bool Draw(VertexBuffer *vb, IndexBuffer *ib); 1.259 + bool Draw(Vertex *varray, Index *iarray, unsigned int VertexCount, unsigned int IndexCount); 1.260 + bool Draw(Vertex *varray, Triangle *triarray, unsigned int VertexCount, unsigned int TriCount); 1.261 + 1.262 + IDirect3DDevice8 *GetDevice() const; 1.263 + int GetTextureStageNumber() const {return MaxTextureStages;} 1.264 + 1.265 + ////// render states ////// 1.266 + void SetPrimitiveType(PrimitiveType pt); 1.267 + void SetBackfaceCulling(bool enable); 1.268 + void SetFrontFace(FaceOrder order); 1.269 + void SetAutoNormalize(bool enable); 1.270 + void SetBillboarding(bool enable); 1.271 + void SetColorWrite(bool red, bool green, bool blue, bool alpha); 1.272 + 1.273 + // blending states 1.274 + void SetAlphaBlending(bool enable); 1.275 + void SetBlendFunc(BlendingFactor src, BlendingFactor dest); 1.276 + 1.277 + // zbuffer states 1.278 + void SetZBuffering(bool enable); 1.279 + void SetZWrite(bool enable); 1.280 + void SetZFunc(CmpFunc func); 1.281 + 1.282 + // set stencil buffer states 1.283 + void SetStencilBuffering(bool enable); 1.284 + void SetStencilPassOp(StencilOp sop); 1.285 + void SetStencilFailOp(StencilOp sop); 1.286 + void SetStencilPassZFailOp(StencilOp sop); 1.287 + void SetStencilOp(StencilOp Fail, StencilOp StencilPassZFail, StencilOp Pass); 1.288 + void SetStencilFunc(CmpFunc func); 1.289 + void SetStencilReference(dword value); 1.290 + 1.291 + // texture & material states 1.292 + void SetTextureFiltering(TextureFilteringType texfilter, int TextureStage = 0xa11); 1.293 + void SetTextureAddressing(TextureAddressing uaddr, TextureAddressing vaddr, int TextureStage = 0xa11); 1.294 + void SetTextureBorderColor(dword color, int TextureStage = 0xa11); 1.295 + void SetTexture(int index, Texture *tex); 1.296 + void SetTextureFactor(dword factor); 1.297 + void SetMipMapping(bool enable, int TextureStage = 0xa11); 1.298 + void SetMaterial(const Material &mat); 1.299 + 1.300 + void BlitTexture(const Texture *texture, RECT *rect, const Color &col = Color(1.0f)); 1.301 + 1.302 + // multitexturing interface 1.303 + void EnableTextureStage(int stage); 1.304 + void DisableTextureStage(int stage); 1.305 + void SetTextureStageColor(int stage, TextureBlendFunction op, TextureBlendArgument arg1, TextureBlendArgument arg2, TextureBlendArgument arg3 = TexArgNone); 1.306 + void SetTextureStageAlpha(int stage, TextureBlendFunction op, TextureBlendArgument arg1, TextureBlendArgument arg2, TextureBlendArgument arg3 = TexArgNone); 1.307 + void SetTextureCoordIndex(int stage, int index); 1.308 + void SetTextureTransformState(int stage, TexTransformState TexXForm); 1.309 + //void SetTextureCoordGenerator(int stage, TexGen tgen); 1.310 + 1.311 + // programmable interface 1.312 + void SetVertexProgram(dword vs); 1.313 + void SetPixelProgram(dword ps); 1.314 + 1.315 + dword CreateVertexProgram(const char *fname); 1.316 + void DestroyVertexProgram(dword vprog); 1.317 + void SetVertexProgramConstant(dword creg, float val); 1.318 + void SetVertexProgramConstant(dword creg, const Vector3 &val); 1.319 + void SetVertexProgramConstant(dword creg, const Vector4 &val); 1.320 + void SetVertexProgramConstant(dword creg, const Color &val); 1.321 + void SetVertexProgramConstant(dword creg, const Matrix4x4 &val); 1.322 + void SetVertexProgramConstant(dword creg, const void *data, dword size); 1.323 + 1.324 + 1.325 + // lighting states 1.326 + void SetLighting(bool enable); 1.327 + void SetColorVertex(bool enable); 1.328 + void SetAmbientLight(Color AmbientColor); 1.329 + void SetShadingMode(ShadeMode mode); 1.330 + void SetSpecular(bool enable); 1.331 + 1.332 + // Transformation Matrices 1.333 + void SetWorldMatrix(const Matrix4x4 &WorldMat, unsigned int BlendIndex = 0); 1.334 + void SetViewMatrix(const Matrix4x4 &ViewMat); 1.335 + void SetProjectionMatrix(const Matrix4x4 &ProjMat); 1.336 + void SetTextureMatrix(const Matrix4x4 &TexMat, unsigned int TextureStage = 0); 1.337 + void SetViewport(unsigned int x, unsigned int y, unsigned int xsize, unsigned int ysize, float MinZ = 0.0f, float MaxZ = 1.0f); 1.338 + 1.339 + const Matrix4x4 &GetWorldMatrix(unsigned int BlendIndex = 0); 1.340 + const Matrix4x4 &GetViewMatrix(); 1.341 + const Matrix4x4 &GetProjectionMatrix(); 1.342 + const Matrix4x4 &GetTextureMatrix(unsigned int TextureStage = 0); 1.343 + 1.344 + // render target 1.345 + void ResetRenderTarget(); 1.346 + void SetRenderTarget(RenderTarget &rtarg); 1.347 + void SetRenderTarget(Texture *rtarg, Texture *ztarg); 1.348 + void SetRenderTarget(Texture *rtarg, Surface *ztarg); 1.349 + //RenderTarget GetRenderTarget() const; 1.350 +}; 1.351 + 1.352 + 1.353 + 1.354 +class Engine3D { 1.355 +private: 1.356 + IDirect3D8 *d3d; 1.357 + unsigned int AdapterCount; 1.358 + Adapter *adapters; // array of adapters (filled in at the constructor) 1.359 + std::vector<GraphicsContext*> GraphicsContexts; // a list of active graphics contexts 1.360 + 1.361 + void RetrieveAdapterInfo(); 1.362 + LinkedList<DisplayMode> *CreateModesList(unsigned int AdapterID) const; 1.363 + void NarrowModesList(LinkedList<DisplayMode> *list, DisplayModeItem item, long value, long value2=0) const; 1.364 + DisplayMode ChooseBestMode(LinkedList<DisplayMode> *modes) const; 1.365 + int MaxAntialiasingSamples() const; 1.366 + 1.367 +public: 1.368 + 1.369 + Engine3D(); 1.370 + ~Engine3D(); 1.371 + 1.372 + int GetAdapterCount() const; 1.373 + const Adapter *GetAdapterInfo(int adapter) const; 1.374 + 1.375 + GraphicsContext *CreateGraphicsContext(HWND WindowHandle, int x, int y, int bpp, word flags); 1.376 + GraphicsContext *CreateGraphicsContext(HWND WindowHandle, unsigned int AdapterID, ContextInitParameters *GCParams); 1.377 + ContextInitParameters LoadContextParamsConfigFile(const char *cfgfilename); 1.378 + 1.379 + void DestroyGraphicsContext(GraphicsContext *gc); 1.380 +}; 1.381 + 1.382 +class Light; 1.383 + 1.384 +// helper functions 1.385 +bool Lock(VertexBuffer *vb, Vertex **data); 1.386 +bool Lock(IndexBuffer *ib, Index **data); 1.387 +void Unlock(VertexBuffer *vb); 1.388 +void Unlock(IndexBuffer *ib); 1.389 +void CreateProjectionMatrix(Matrix4x4 *mat, float yFOV, float Aspect, float NearClip, float FarClip); 1.390 +void NormalMapFromHeightField(Texture *tex); 1.391 +void UpdateMipmapChain(Texture *tex); 1.392 +TriMesh *CreateShadowVolume(const TriMesh &mesh, const Light *light, const Matrix4x4 &MeshXForm, bool WorldCoords = false); 1.393 + 1.394 +#endif // _3DENGINE_H_ 1.395 \ No newline at end of file