absence_thelab

view 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 source
1 #ifndef _3DENGINE_H_
2 #define _3DENGINE_H_
4 // standard includes
5 #include <vector>
6 // system includes
7 #include "d3d8.h"
8 // includes from my codebase
9 #include "typedefs.h"
10 #include "linkedlist.h"
11 #include "color.h"
12 // 3d engine includes
13 #include "n3dmath.h"
14 #include "switches.h"
15 #include "3dengtypes.h"
16 #include "material.h"
17 #include "textureman.h"
18 #include "3dgeom.h"
20 enum DeviceType {DeviceHardware = D3DDEVTYPE_HAL, DeviceReference = D3DDEVTYPE_REF};
21 enum TnLMode {HardwareTnL = D3DCREATE_HARDWARE_VERTEXPROCESSING, SoftwareTnL = D3DCREATE_SOFTWARE_VERTEXPROCESSING};
22 enum BufferChainMode {DoubleBuffering = 1, TripleBuffering = 2};
23 enum UsageFlags {UsageStatic = 0, UsageDynamic = D3DUSAGE_DYNAMIC};
24 enum ShadeMode {FlatShading = D3DSHADE_FLAT, GouraudShading = D3DSHADE_GOURAUD};
25 enum FaceOrder {Clockwise = D3DCULL_CW, CounterClockwise = D3DCULL_CCW};
27 enum PhongComponent {Ambient, Diffuse, Specular};
29 enum PrimitiveType {
30 TriangleList = D3DPT_TRIANGLELIST,
31 TriangleStrip = D3DPT_TRIANGLESTRIP,
32 TriangleFan = D3DPT_TRIANGLEFAN,
33 LineList = D3DPT_LINELIST,
34 LineStrip = D3DPT_LINESTRIP,
35 PointList = D3DPT_POINTLIST
36 };
38 enum BlendingFactor {
39 BLEND_ZERO = D3DBLEND_ZERO,
40 BLEND_ONE = D3DBLEND_ONE,
41 BLEND_SRCCOLOR = D3DBLEND_SRCCOLOR,
42 BLEND_INVSRCCOLOR = D3DBLEND_INVSRCCOLOR,
43 BLEND_SRCALPHA = D3DBLEND_SRCALPHA,
44 BLEND_INVSRCALPHA = D3DBLEND_INVSRCALPHA,
45 BLEND_DESTCOLOR = D3DBLEND_DESTCOLOR,
46 BLEND_INVDESTCOLOR = D3DBLEND_INVDESTCOLOR,
47 BLEND_DESTALPHA = D3DBLEND_DESTALPHA,
48 BLEND_INVDESTALPHA = D3DBLEND_INVDESTALPHA,
49 BLEND_SRCALPHASAT = D3DBLEND_SRCALPHASAT
50 };
52 enum CmpFunc {
53 CMP_NEVER = D3DCMP_NEVER,
54 CMP_LESS = D3DCMP_LESS,
55 CMP_EQUAL = D3DCMP_EQUAL,
56 CMP_LEQUAL = D3DCMP_LESSEQUAL,
57 CMP_GREATER = D3DCMP_GREATER,
58 CMP_NOTEQUAL = D3DCMP_NOTEQUAL,
59 CMP_GEQUAL = D3DCMP_GREATEREQUAL,
60 CMP_ALWAYS = D3DCMP_ALWAYS
61 };
63 enum StencilOp {
64 SOP_KEEP = D3DSTENCILOP_KEEP,
65 SOP_ZERO = D3DSTENCILOP_ZERO,
66 SOP_REPLACE = D3DSTENCILOP_REPLACE,
67 SOP_INCSAT = D3DSTENCILOP_INCRSAT,
68 SOP_DECSAT = D3DSTENCILOP_DECRSAT,
69 SOP_INVERT = D3DSTENCILOP_INVERT,
70 SOP_INC = D3DSTENCILOP_INCR,
71 SOP_DEC = D3DSTENCILOP_DECR
72 };
74 enum TextureBlendFunction {
75 TexBlendSelectArg1 = D3DTOP_SELECTARG1, // S[rgba] = Arg1
76 TexBlendSelectArg2 = D3DTOP_SELECTARG2, // S[rgba] = Arg2
77 TexBlendAdd = D3DTOP_ADD, // S[rgba] = Arg1 + Arg2
78 TexBlendAddSigned = D3DTOP_ADDSIGNED, // S[rgba] = Arg1 + Arg2 - 0.5
79 TexBlendAddSigned2x = D3DTOP_ADDSIGNED2X, // S[rgba] = (Arg1 + Arg2 - 0.5) << 1
80 TexBlendSubtract = D3DTOP_SUBTRACT, // S[rgba] = Arg1 - Arg2
81 TexBlendAddSmooth = D3DTOP_ADDSMOOTH, // S[rgba] = (Arg1 + Arg2) - (Arg1 * Arg2)
82 TexBlendModulate = D3DTOP_MODULATE, // S[rgba] = Arg1 * Arg2
83 TexBlendModulate2x = D3DTOP_MODULATE2X, // S[rgba] = (Arg1 * Arg2) << 1
84 TexBlendModulate4x = D3DTOP_MODULATE4X, // S[rgba] = (Arg1 * Arg2) << 2
85 TexBlendVertexAlpha = D3DTOP_BLENDDIFFUSEALPHA, // S[rgba] = Arg1*Alpha + Arg2*(1-Alpha)
86 TexBlendTextureAlpha = D3DTOP_BLENDTEXTUREALPHA, // S[rgba] = Arg1*Alpha + Arg2*(1-Alpha)
87 TexBlendFactorAlpha = D3DTOP_BLENDFACTORALPHA, // S[rgba] = Arg1*Alpha + Arg2*(1-Alpha)
88 TexBlendPrevAlpha = D3DTOP_BLENDFACTORALPHA, // S[rgba] = Arg1*Alpha + Arg2*(1-Alpha)
89 TexBlendPreMulAlpha = D3DTOP_BLENDTEXTUREALPHAPM, // S[rgba] = Arg1 + Arg2*(1-Alpha)
90 TexBlendPreModulate = D3DTOP_PREMODULATE,
91 TexBlendModAlphaAddColor = D3DTOP_MODULATEALPHA_ADDCOLOR,// S[rgba] = Arg1[rgb] + Arg1[a] * Arg2[rgb]
92 TexBlendModulateAddAlpha = D3DTOP_MODULATECOLOR_ADDALPHA,// S[rgba] = Arg1[rgb] * Arg2[rgb] + Arg1[a]
93 TexBlendModInvAlphaAddColor = D3DTOP_MODULATEINVALPHA_ADDCOLOR, // S[rgba] = Arg1[rgb] + (1-Arg1[a]) * Arg2[rgb]
94 TexBlendModulateInvAddAlpha = D3DTOP_MODULATEINVCOLOR_ADDALPHA, // S[rgba] = (1-Arg1[rgb]) * Arg2[rgb] + Arg1[a]
95 TexBlendBumpEnv = D3DTOP_BUMPENVMAP, // bump mapping with next stage's env map
96 TexBlendBumpEnvLuminance = D3DTOP_BUMPENVMAPLUMINANCE, // bump mapping with next stage's env map with luminance
97 TexBlendDotProduct = D3DTOP_DOTPRODUCT3, // S[rgba] = Arg1[rgb] (dot) Arg2[rgb]
98 TexBlendMultiplyAdd = D3DTOP_MULTIPLYADD, // S[rgba] = Arg1 + Arg2 * Arg3
99 TexBlendLerp = D3DTOP_LERP // S[rgba] = Arg2 + (Arg3 - Arg2) * Arg1
100 };
102 enum TextureBlendArgument {
103 TexArgNone = 0, // valid only for arg3
104 TexArgCurrent = D3DTA_CURRENT, // the color from the previous stage output (diffuse for 1st)
105 TexArgDiffuseColor = D3DTA_DIFFUSE, // the diffuse interpolated color
106 TexArgSpecularColor = D3DTA_SPECULAR, // the specular interpolated color
107 TexArgTexture = D3DTA_TEXTURE, // the texture bound to this stage
108 TexArgFactor = D3DTA_TFACTOR, // a user defined factor
109 TexArgTemp = D3DTA_TEMP // temp register
110 };
112 enum TextureFilteringType {
113 PointSampling,
114 BilinearFiltering,
115 TrilinearFiltering,
116 AnisotropicFiltering
117 };
119 enum TextureAddressing {
120 TexAddrWrap = D3DTADDRESS_WRAP,
121 TexAddrMirror = D3DTADDRESS_MIRROR,
122 TexAddrClamp = D3DTADDRESS_CLAMP,
123 TexAddrBorder = D3DTADDRESS_BORDER,
124 TexAddrMirrorOnce = D3DTADDRESS_MIRRORONCE
125 };
127 enum TexTransformState {
128 TexTransformDisable = D3DTTFF_DISABLE,
129 TexTransform1D = D3DTTFF_COUNT1,
130 TexTransform2D = D3DTTFF_COUNT2,
131 TexTransform3D = D3DTTFF_COUNT3,
132 TexTransform4D = D3DTTFF_COUNT4,
133 TexTransformProjected = D3DTTFF_PROJECTED
134 };
136 // don't care flags for context creation
137 const unsigned short GCPDONTCARE_NONE = 0; // 0000000000000000
138 const unsigned short GCPDONTCARE_BPP = 1; // 0000000000000001
139 const unsigned short GCPDONTCARE_REFRESH = 2; // 0000000000000010
140 const unsigned short GCPDONTCARE_ALPHA = 4; // 0000000000000100
141 const unsigned short GCPDONTCARE_DEPTH = 8; // 0000000000001000
142 const unsigned short GCPDONTCARE_TNL = 16; // 0000000000010000
143 const unsigned short GCPDONTCARE_BUFFERS = 32; // 0000000000100000
144 const unsigned short GCPDONTCARE_AA = 64; // 0000000001000000
145 const unsigned short GCPDONTCARE_VSYNC = 128; // 0000000010000000
147 // fullscreen / windowed flags
148 const unsigned short GCCREATE_WINDOWED = 0;
149 const unsigned short GCCREATE_FULLSCREEN = 1;
153 class Vertex;
155 struct ColorDepth {
156 int bpp, colorbits, alpha;
158 ColorDepth(int bits=0, int c=0, int a=0) {colorbits = c; alpha = a; bpp = bits; }
159 };
161 struct DisplayMode {
162 unsigned int XRes, YRes, RefreshRate;
163 ColorDepth ColorFormat;
164 };
166 enum DisplayModeItem {ModeItemSize, ModeItemBpp, ModeItemAlpha, ModeItemRefresh};
168 struct Adapter {
169 char *Driver, *Description;
170 int64 DriverVersion;
171 dword VentorID, DeviceID, SubSysID, Revision;
172 GUID DeviceGUID;
174 unsigned int ModeCount;
175 DisplayMode *Modes;
176 D3DCAPS8 Capabilities;
177 };
179 struct RenderTarget {
180 Surface *ColorSurface, *DepthStencilSurface;
181 };
183 struct RenderParams {
184 ShadeMode Shading;
185 bool Billboarded;
186 dword VertexProgram;
187 dword PixelProgram;
188 bool ZWrite;
189 BlendingFactor SourceBlendFactor, DestBlendFactor;
190 };
192 struct ContextInitParameters {
193 int x, y;
194 int bpp;
195 int RefreshRate;
196 bool AlphaChannel;
197 int DepthBits;
198 DeviceType DevType;
199 bool HardwareTnL;
200 bool FullScreen;
201 bool Antialiasing;
202 bool BestAA;
203 bool VSync;
204 BufferChainMode Buffers;
205 unsigned short DontCareFlags;
206 };
209 class GraphicsContext {
210 private:
211 PrimitiveType ptype;
212 FaceOrder CullOrder;
213 bool BackfaceCulling;
214 bool MipMapEnabled;
215 bool BillBoardingEnabled;
216 dword MipFilter;
218 // cache of current transformation matrices
219 Matrix4x4 WorldMat[256], ViewMat, ProjMat, TexMat[8];
221 // disable copying contexts by making copy constructor and assignment private
222 GraphicsContext(const GraphicsContext &gc);
223 const GraphicsContext &operator =(const GraphicsContext &gc);
225 public:
226 HWND WindowHandle;
227 RenderTarget MainRenderTarget;
228 IDirect3DDevice8 *D3DDevice;
229 ContextInitParameters ContextParams;
230 D3DFORMAT ColorFormat, ZFormat;
231 int AASamples;
232 int MaxTextureStages;
234 TextureManager *texman; // texture manager
236 GraphicsContext();
238 void SetDefaultStates();
240 bool CreateVertexBuffer(uint32 VertexCount, UsageFlags usage, VertexBuffer **vb) const;
241 bool CreateIndexBuffer(uint32 IndexCount, UsageFlags usage, IndexBuffer **ib) const;
243 bool CreateSurface(uint32 Width, uint32 Height, Surface **surf) const;
244 bool CreateDepthStencil(uint32 Width, uint32 Height, Surface **zsurf) const;
246 void Clear(dword color) const;
247 void ClearZBuffer(float zval) const;
248 void ClearStencil(byte sval) const;
249 void ClearZBufferStencil(float zval, byte sval) const;
251 void Flip() const;
253 bool Draw(VertexBuffer *vb);
254 bool Draw(Vertex *varray, unsigned int VertexCount);
255 bool Draw(VertexBuffer *vb, IndexBuffer *ib);
256 bool Draw(Vertex *varray, Index *iarray, unsigned int VertexCount, unsigned int IndexCount);
257 bool Draw(Vertex *varray, Triangle *triarray, unsigned int VertexCount, unsigned int TriCount);
259 IDirect3DDevice8 *GetDevice() const;
260 int GetTextureStageNumber() const {return MaxTextureStages;}
262 ////// render states //////
263 void SetPrimitiveType(PrimitiveType pt);
264 void SetBackfaceCulling(bool enable);
265 void SetFrontFace(FaceOrder order);
266 void SetAutoNormalize(bool enable);
267 void SetBillboarding(bool enable);
268 void SetColorWrite(bool red, bool green, bool blue, bool alpha);
270 // blending states
271 void SetAlphaBlending(bool enable);
272 void SetBlendFunc(BlendingFactor src, BlendingFactor dest);
274 // zbuffer states
275 void SetZBuffering(bool enable);
276 void SetZWrite(bool enable);
277 void SetZFunc(CmpFunc func);
279 // set stencil buffer states
280 void SetStencilBuffering(bool enable);
281 void SetStencilPassOp(StencilOp sop);
282 void SetStencilFailOp(StencilOp sop);
283 void SetStencilPassZFailOp(StencilOp sop);
284 void SetStencilOp(StencilOp Fail, StencilOp StencilPassZFail, StencilOp Pass);
285 void SetStencilFunc(CmpFunc func);
286 void SetStencilReference(dword value);
288 // texture & material states
289 void SetTextureFiltering(TextureFilteringType texfilter, int TextureStage = 0xa11);
290 void SetTextureAddressing(TextureAddressing uaddr, TextureAddressing vaddr, int TextureStage = 0xa11);
291 void SetTextureBorderColor(dword color, int TextureStage = 0xa11);
292 void SetTexture(int index, Texture *tex);
293 void SetTextureFactor(dword factor);
294 void SetMipMapping(bool enable, int TextureStage = 0xa11);
295 void SetMaterial(const Material &mat);
297 void BlitTexture(const Texture *texture, RECT *rect, const Color &col = Color(1.0f));
299 // multitexturing interface
300 void EnableTextureStage(int stage);
301 void DisableTextureStage(int stage);
302 void SetTextureStageColor(int stage, TextureBlendFunction op, TextureBlendArgument arg1, TextureBlendArgument arg2, TextureBlendArgument arg3 = TexArgNone);
303 void SetTextureStageAlpha(int stage, TextureBlendFunction op, TextureBlendArgument arg1, TextureBlendArgument arg2, TextureBlendArgument arg3 = TexArgNone);
304 void SetTextureCoordIndex(int stage, int index);
305 void SetTextureTransformState(int stage, TexTransformState TexXForm);
306 //void SetTextureCoordGenerator(int stage, TexGen tgen);
308 // programmable interface
309 void SetVertexProgram(dword vs);
310 void SetPixelProgram(dword ps);
312 dword CreateVertexProgram(const char *fname);
313 void DestroyVertexProgram(dword vprog);
314 void SetVertexProgramConstant(dword creg, float val);
315 void SetVertexProgramConstant(dword creg, const Vector3 &val);
316 void SetVertexProgramConstant(dword creg, const Vector4 &val);
317 void SetVertexProgramConstant(dword creg, const Color &val);
318 void SetVertexProgramConstant(dword creg, const Matrix4x4 &val);
319 void SetVertexProgramConstant(dword creg, const void *data, dword size);
322 // lighting states
323 void SetLighting(bool enable);
324 void SetColorVertex(bool enable);
325 void SetAmbientLight(Color AmbientColor);
326 void SetShadingMode(ShadeMode mode);
327 void SetSpecular(bool enable);
329 // Transformation Matrices
330 void SetWorldMatrix(const Matrix4x4 &WorldMat, unsigned int BlendIndex = 0);
331 void SetViewMatrix(const Matrix4x4 &ViewMat);
332 void SetProjectionMatrix(const Matrix4x4 &ProjMat);
333 void SetTextureMatrix(const Matrix4x4 &TexMat, unsigned int TextureStage = 0);
334 void SetViewport(unsigned int x, unsigned int y, unsigned int xsize, unsigned int ysize, float MinZ = 0.0f, float MaxZ = 1.0f);
336 const Matrix4x4 &GetWorldMatrix(unsigned int BlendIndex = 0);
337 const Matrix4x4 &GetViewMatrix();
338 const Matrix4x4 &GetProjectionMatrix();
339 const Matrix4x4 &GetTextureMatrix(unsigned int TextureStage = 0);
341 // render target
342 void ResetRenderTarget();
343 void SetRenderTarget(RenderTarget &rtarg);
344 void SetRenderTarget(Texture *rtarg, Texture *ztarg);
345 void SetRenderTarget(Texture *rtarg, Surface *ztarg);
346 //RenderTarget GetRenderTarget() const;
347 };
351 class Engine3D {
352 private:
353 IDirect3D8 *d3d;
354 unsigned int AdapterCount;
355 Adapter *adapters; // array of adapters (filled in at the constructor)
356 std::vector<GraphicsContext*> GraphicsContexts; // a list of active graphics contexts
358 void RetrieveAdapterInfo();
359 LinkedList<DisplayMode> *CreateModesList(unsigned int AdapterID) const;
360 void NarrowModesList(LinkedList<DisplayMode> *list, DisplayModeItem item, long value, long value2=0) const;
361 DisplayMode ChooseBestMode(LinkedList<DisplayMode> *modes) const;
362 int MaxAntialiasingSamples() const;
364 public:
366 Engine3D();
367 ~Engine3D();
369 int GetAdapterCount() const;
370 const Adapter *GetAdapterInfo(int adapter) const;
372 GraphicsContext *CreateGraphicsContext(HWND WindowHandle, int x, int y, int bpp, word flags);
373 GraphicsContext *CreateGraphicsContext(HWND WindowHandle, unsigned int AdapterID, ContextInitParameters *GCParams);
374 ContextInitParameters LoadContextParamsConfigFile(const char *cfgfilename);
376 void DestroyGraphicsContext(GraphicsContext *gc);
377 };
379 class Light;
381 // helper functions
382 bool Lock(VertexBuffer *vb, Vertex **data);
383 bool Lock(IndexBuffer *ib, Index **data);
384 void Unlock(VertexBuffer *vb);
385 void Unlock(IndexBuffer *ib);
386 void CreateProjectionMatrix(Matrix4x4 *mat, float yFOV, float Aspect, float NearClip, float FarClip);
387 void NormalMapFromHeightField(Texture *tex);
388 void UpdateMipmapChain(Texture *tex);
389 TriMesh *CreateShadowVolume(const TriMesh &mesh, const Light *light, const Matrix4x4 &MeshXForm, bool WorldCoords = false);
391 #endif // _3DENGINE_H_