PageRenderTime 36ms CodeModel.GetById 8ms app.highlight 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Irrlicht/CD3D8MaterialRenderer.h

http://myjeh.googlecode.com/
C++ Header | 588 lines | 444 code | 118 blank | 26 comment | 61 complexity | 0dcddbfd4fa1feb09c75f6f3f567d555 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0
  1// Copyright (C) 2002-2010 Nikolaus Gebhardt
  2// This file is part of the "Irrlicht Engine".
  3// For conditions of distribution and use, see copyright notice in irrlicht.h
  4
  5#ifndef __C_D3D8_MATERIAL_RENDERER_H_INCLUDED__
  6#define __C_D3D8_MATERIAL_RENDERER_H_INCLUDED__
  7
  8#include "IrrCompileConfig.h"
  9#ifdef _IRR_WINDOWS_API_
 10
 11#ifdef _IRR_COMPILE_WITH_DIRECT3D_8_
 12#include <d3d8.h>
 13
 14#include "IMaterialRenderer.h"
 15
 16namespace irr
 17{
 18namespace video
 19{
 20
 21D3DMATRIX UnitMatrixD3D8;
 22D3DMATRIX SphereMapMatrixD3D8;
 23
 24//! Base class for all internal D3D8 material renderers
 25class CD3D8MaterialRenderer : public IMaterialRenderer
 26{
 27public:
 28
 29	//! Constructor
 30	CD3D8MaterialRenderer(IDirect3DDevice8* d3ddev, video::IVideoDriver* driver)
 31		: pID3DDevice(d3ddev), Driver(driver)
 32	{
 33	}
 34
 35protected:
 36
 37	IDirect3DDevice8* pID3DDevice;
 38	video::IVideoDriver* Driver;
 39};
 40
 41
 42//! Solid material renderer
 43class CD3D8MaterialRenderer_SOLID : public CD3D8MaterialRenderer
 44{
 45public:
 46
 47	CD3D8MaterialRenderer_SOLID(IDirect3DDevice8* p, video::IVideoDriver* d)
 48		: CD3D8MaterialRenderer(p, d) {}
 49
 50	virtual void OnSetMaterial(const SMaterial& material, const SMaterial& lastMaterial,
 51		bool resetAllRenderstates, IMaterialRendererServices* services)
 52	{
 53		if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
 54		{
 55			pID3DDevice->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_MODULATE);
 56			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
 57			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
 58			pID3DDevice->SetTextureStageState (0, D3DTSS_ALPHAOP,  D3DTOP_DISABLE);
 59		}
 60
 61		pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
 62		pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
 63
 64		services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
 65	}
 66};
 67
 68//! Generic Texture Blend
 69class CD3D8MaterialRenderer_ONETEXTURE_BLEND : public CD3D8MaterialRenderer
 70{
 71public:
 72
 73	CD3D8MaterialRenderer_ONETEXTURE_BLEND(IDirect3DDevice8* p, video::IVideoDriver* d)
 74		: CD3D8MaterialRenderer(p, d) {}
 75
 76	virtual void OnSetMaterial(const SMaterial& material, const SMaterial& lastMaterial,
 77		bool resetAllRenderstates, IMaterialRendererServices* services)
 78	{
 79		if (material.MaterialType != lastMaterial.MaterialType ||
 80			material.MaterialTypeParam != lastMaterial.MaterialTypeParam ||
 81			resetAllRenderstates)
 82		{
 83
 84			E_BLEND_FACTOR srcFact,dstFact;
 85			E_MODULATE_FUNC modulate;
 86			u32 alphaSource;
 87			unpack_texureBlendFunc ( srcFact, dstFact, modulate, alphaSource, material.MaterialTypeParam );
 88
 89			if (srcFact == EBF_SRC_COLOR && dstFact == EBF_ZERO)
 90				pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
 91			else
 92			{
 93				pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
 94				pID3DDevice->SetRenderState(D3DRS_SRCBLEND, getD3DBlend ( srcFact ) );
 95				pID3DDevice->SetRenderState(D3DRS_DESTBLEND, getD3DBlend ( dstFact ) );
 96			}
 97
 98			pID3DDevice->SetTextureStageState (0, D3DTSS_COLOROP, getD3DModulate ( modulate ) );
 99			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
100			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
101
102			if ( alphaSource && (textureBlendFunc_hasAlpha ( srcFact ) || textureBlendFunc_hasAlpha ( dstFact ) ))
103			{
104				if (alphaSource==EAS_VERTEX_COLOR)
105				{
106					pID3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
107					pID3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );
108				}
109				else if (alphaSource==EAS_TEXTURE)
110				{
111					pID3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
112					pID3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
113				}
114				else
115				{
116					pID3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
117					pID3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
118					pID3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
119				}
120			}
121			else
122			{
123				pID3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
124			}
125
126			pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
127
128		}
129
130		services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
131
132	}
133
134	//! Returns if the material is transparent.
135	virtual bool isTransparent() const
136	{
137		return true;
138	}
139
140	private:
141
142		u32 getD3DBlend ( E_BLEND_FACTOR factor ) const
143		{
144			u32 r = 0;
145		switch ( factor )
146			{
147				case EBF_ZERO:					r = D3DBLEND_ZERO; break;
148				case EBF_ONE:					r = D3DBLEND_ONE; break;
149				case EBF_DST_COLOR:				r = D3DBLEND_DESTCOLOR; break;
150				case EBF_ONE_MINUS_DST_COLOR:	r = D3DBLEND_INVDESTCOLOR; break;
151				case EBF_SRC_COLOR:				r = D3DBLEND_SRCCOLOR; break;
152				case EBF_ONE_MINUS_SRC_COLOR:	r = D3DBLEND_INVSRCCOLOR; break;
153				case EBF_SRC_ALPHA:				r = D3DBLEND_SRCALPHA; break;
154				case EBF_ONE_MINUS_SRC_ALPHA:	r = D3DBLEND_INVSRCALPHA; break;
155				case EBF_DST_ALPHA:				r = D3DBLEND_DESTALPHA; break;
156				case EBF_ONE_MINUS_DST_ALPHA:	r = D3DBLEND_INVDESTALPHA; break;
157				case EBF_SRC_ALPHA_SATURATE:	r = D3DBLEND_SRCALPHASAT; break;
158			}
159			return r;
160		}
161
162		u32 getD3DModulate ( E_MODULATE_FUNC func ) const
163		{
164			u32 r = D3DTOP_MODULATE;
165			switch ( func )
166			{
167				case EMFN_MODULATE_1X: r = D3DTOP_MODULATE; break;
168				case EMFN_MODULATE_2X: r = D3DTOP_MODULATE2X; break;
169				case EMFN_MODULATE_4X: r = D3DTOP_MODULATE4X; break;
170			}
171			return r;
172		}
173
174};
175
176
177//! Solid 2 layer material renderer
178class CD3D8MaterialRenderer_SOLID_2_LAYER : public CD3D8MaterialRenderer
179{
180public:
181
182	CD3D8MaterialRenderer_SOLID_2_LAYER(IDirect3DDevice8* p, video::IVideoDriver* d)
183		: CD3D8MaterialRenderer(p, d) {}
184
185	virtual void OnSetMaterial(const SMaterial& material, const SMaterial& lastMaterial,
186		bool resetAllRenderstates, IMaterialRendererServices* services)
187	{
188		if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
189		{
190			pID3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
191			pID3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
192			pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP,  D3DTOP_DISABLE);
193
194			pID3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);
195			pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_BLENDDIFFUSEALPHA);
196
197			pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
198		}
199
200		services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
201	}
202};
203
204
205//! Transparent add color material renderer
206class CD3D8MaterialRenderer_TRANSPARENT_ADD_COLOR : public CD3D8MaterialRenderer
207{
208public:
209
210	CD3D8MaterialRenderer_TRANSPARENT_ADD_COLOR(IDirect3DDevice8* p, video::IVideoDriver* d)
211		: CD3D8MaterialRenderer(p, d) {}
212
213	virtual void OnSetMaterial(const SMaterial& material, const SMaterial& lastMaterial,
214		bool resetAllRenderstates, IMaterialRendererServices* services)
215	{
216		if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
217		{
218			pID3DDevice->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_MODULATE);
219			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
220			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
221			pID3DDevice->SetTextureStageState (0, D3DTSS_ALPHAOP,  D3DTOP_DISABLE);
222
223			pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
224			pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
225			pID3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
226			pID3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCCOLOR);
227		}
228
229		services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
230	}
231
232	//! Returns if the material is transparent.
233	virtual bool isTransparent() const
234	{
235		return true;
236	}
237};
238
239
240//! Transparent vertex alpha material renderer
241class CD3D8MaterialRenderer_TRANSPARENT_VERTEX_ALPHA : public CD3D8MaterialRenderer
242{
243public:
244
245	CD3D8MaterialRenderer_TRANSPARENT_VERTEX_ALPHA(IDirect3DDevice8* p, video::IVideoDriver* d)
246		: CD3D8MaterialRenderer(p, d) {}
247
248	virtual void OnSetMaterial(const SMaterial& material, const SMaterial& lastMaterial,
249		bool resetAllRenderstates, IMaterialRendererServices* services)
250	{
251		if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
252		{
253			pID3DDevice->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_MODULATE);
254			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
255			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
256			pID3DDevice->SetTextureStageState (0, D3DTSS_ALPHAOP,  D3DTOP_SELECTARG1);
257			pID3DDevice->SetTextureStageState (0, D3DTSS_ALPHAARG1,  D3DTA_DIFFUSE);
258
259			pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
260			pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
261			pID3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
262			pID3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
263		}
264
265		services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
266	}
267
268	//! Returns if the material is transparent.
269	virtual bool isTransparent() const
270	{
271		return true;
272	}
273};
274
275
276//! Transparent alpha channel material renderer
277class CD3D8MaterialRenderer_TRANSPARENT_ALPHA_CHANNEL : public CD3D8MaterialRenderer
278{
279public:
280
281	CD3D8MaterialRenderer_TRANSPARENT_ALPHA_CHANNEL(IDirect3DDevice8* p, video::IVideoDriver* d)
282		: CD3D8MaterialRenderer(p, d) {}
283
284	virtual void OnSetMaterial(const SMaterial& material, const SMaterial& lastMaterial,
285		bool resetAllRenderstates, IMaterialRendererServices* services)
286	{
287		if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates
288			|| material.MaterialTypeParam != lastMaterial.MaterialTypeParam )
289		{
290			pID3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
291			pID3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
292			pID3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_CURRENT );
293			pID3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
294			pID3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
295
296			pID3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_DISABLE );
297			pID3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
298
299			pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
300			pID3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
301			pID3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
302
303			pID3DDevice->SetRenderState(D3DRS_ALPHAREF, core::floor32(material.MaterialTypeParam * 255.f));
304			pID3DDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
305			pID3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
306		}
307
308		services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
309	}
310
311	virtual void OnUnsetMaterial()
312	{
313		pID3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
314	}
315
316	//! Returns if the material is transparent.
317	virtual bool isTransparent() const
318	{
319		return true;
320	}
321};
322
323
324//! Transparent alpha channel material renderer
325class CD3D8MaterialRenderer_TRANSPARENT_ALPHA_CHANNEL_REF : public CD3D8MaterialRenderer
326{
327public:
328
329	CD3D8MaterialRenderer_TRANSPARENT_ALPHA_CHANNEL_REF(IDirect3DDevice8* p, video::IVideoDriver* d)
330		: CD3D8MaterialRenderer(p, d) {}
331
332	virtual void OnSetMaterial(const SMaterial& material, const SMaterial& lastMaterial,
333		bool resetAllRenderstates, IMaterialRendererServices* services)
334	{
335		if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
336		{
337			pID3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
338			pID3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
339			pID3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_CURRENT );
340			pID3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
341			pID3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
342
343			pID3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
344			pID3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
345
346			pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
347
348			// 127 is required by EMT_TRANSPARENT_ALPHA_CHANNEL_REF
349			pID3DDevice->SetRenderState(D3DRS_ALPHAREF, 127);
350			pID3DDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
351			pID3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
352		}
353
354		services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
355	}
356
357	virtual void OnUnsetMaterial()
358	{
359		pID3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
360	}
361
362	//! Returns if the material is transparent. The scene managment needs to know this
363	//! for being able to sort the materials by opaque and transparent.
364	virtual bool isTransparent() const
365	{
366		return false; // this material is not really transparent because it does no blending.
367	}
368};
369
370
371
372//! material renderer for all kinds of linghtmaps
373class CD3D8MaterialRenderer_LIGHTMAP : public CD3D8MaterialRenderer
374{
375public:
376
377	CD3D8MaterialRenderer_LIGHTMAP(IDirect3DDevice8* p, video::IVideoDriver* d)
378		: CD3D8MaterialRenderer(p, d) {}
379
380	virtual void OnSetMaterial(const SMaterial& material, const SMaterial& lastMaterial,
381		bool resetAllRenderstates, IMaterialRendererServices* services)
382	{
383		if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
384		{
385			if (material.MaterialType >= EMT_LIGHTMAP_LIGHTING)
386			{
387				// with lighting
388				pID3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
389				pID3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
390				pID3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
391			}
392			else
393			{
394				pID3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
395				pID3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
396			}
397
398			pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP,  D3DTOP_DISABLE);
399			pID3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);
400
401			if (material.MaterialType == EMT_LIGHTMAP_ADD)
402				pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADD);
403			else
404			if (material.MaterialType == EMT_LIGHTMAP_M4 || material.MaterialType == EMT_LIGHTMAP_LIGHTING_M4)
405				pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE4X);
406			else
407			if (material.MaterialType == EMT_LIGHTMAP_M2 || material.MaterialType == EMT_LIGHTMAP_LIGHTING_M2)
408				pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE2X);
409			else
410				pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
411
412			pID3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
413			pID3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
414			pID3DDevice->SetTextureStageState (1, D3DTSS_ALPHAOP,  D3DTOP_DISABLE);
415
416			pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
417		}
418
419		services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
420	}
421};
422
423//! material renderer for detail maps
424class CD3D8MaterialRenderer_DETAIL_MAP : public CD3D8MaterialRenderer
425{
426public:
427
428	CD3D8MaterialRenderer_DETAIL_MAP(IDirect3DDevice8* p, video::IVideoDriver* d)
429		: CD3D8MaterialRenderer(p, d) {}
430
431	virtual void OnSetMaterial(const SMaterial& material, const SMaterial& lastMaterial,
432		bool resetAllRenderstates, IMaterialRendererServices* services)
433	{
434		if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
435		{
436			pID3DDevice->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_MODULATE);
437			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
438			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
439			pID3DDevice->SetTextureStageState (0, D3DTSS_ALPHAOP,  D3DTOP_DISABLE);
440
441			pID3DDevice->SetTextureStageState (1, D3DTSS_COLOROP, D3DTOP_ADDSIGNED);
442			pID3DDevice->SetTextureStageState (1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
443			pID3DDevice->SetTextureStageState (1, D3DTSS_COLORARG2, D3DTA_CURRENT);
444			pID3DDevice->SetTextureStageState (1, D3DTSS_ALPHAOP,  D3DTOP_DISABLE);
445
446			pID3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);
447			pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
448		}
449
450		services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
451	}
452};
453
454
455//! sphere map material renderer
456class CD3D8MaterialRenderer_SPHERE_MAP : public CD3D8MaterialRenderer
457{
458public:
459
460	CD3D8MaterialRenderer_SPHERE_MAP(IDirect3DDevice8* p, video::IVideoDriver* d)
461		: CD3D8MaterialRenderer(p, d) {}
462
463	virtual void OnSetMaterial(const SMaterial& material, const SMaterial& lastMaterial,
464		bool resetAllRenderstates, IMaterialRendererServices* services)
465	{
466		if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
467		{
468			pID3DDevice->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_MODULATE);
469			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
470			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
471			pID3DDevice->SetTextureStageState (0, D3DTSS_ALPHAOP,  D3DTOP_DISABLE);
472
473			pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
474			pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
475
476			pID3DDevice->SetTransform( D3DTS_TEXTURE0, &SphereMapMatrixD3D8 );
477			pID3DDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );
478			pID3DDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACENORMAL );
479		}
480
481		services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
482	}
483
484	virtual void OnUnsetMaterial()
485	{
486		pID3DDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
487		pID3DDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0);
488		pID3DDevice->SetTransform( D3DTS_TEXTURE0, &UnitMatrixD3D8 );
489	}
490};
491
492
493//! reflection 2 layer material renderer
494class CD3D8MaterialRenderer_REFLECTION_2_LAYER : public CD3D8MaterialRenderer
495{
496public:
497
498	CD3D8MaterialRenderer_REFLECTION_2_LAYER(IDirect3DDevice8* p, video::IVideoDriver* d)
499		: CD3D8MaterialRenderer(p, d) {}
500
501	virtual void OnSetMaterial(const SMaterial& material, const SMaterial& lastMaterial,
502		bool resetAllRenderstates, IMaterialRendererServices* services)
503	{
504		if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
505		{
506			pID3DDevice->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_MODULATE);
507			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
508			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
509			pID3DDevice->SetTextureStageState (0, D3DTSS_ALPHAOP,  D3DTOP_DISABLE);
510
511			pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
512			pID3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
513			pID3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
514
515			pID3DDevice->SetTransform( D3DTS_TEXTURE1, &SphereMapMatrixD3D8 );
516			pID3DDevice->SetTextureStageState( 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );
517			pID3DDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR);
518			pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
519		}
520
521		services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
522	}
523
524	virtual void OnUnsetMaterial()
525	{
526		pID3DDevice->SetTextureStageState( 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
527		pID3DDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 1);
528		pID3DDevice->SetTransform( D3DTS_TEXTURE1, &UnitMatrixD3D8 );
529	}
530};
531
532
533//! reflection 2 layer material renderer
534class CD3D8MaterialRenderer_TRANSPARENT_REFLECTION_2_LAYER : public CD3D8MaterialRenderer
535{
536public:
537
538	CD3D8MaterialRenderer_TRANSPARENT_REFLECTION_2_LAYER(IDirect3DDevice8* p, video::IVideoDriver* d)
539		: CD3D8MaterialRenderer(p, d) {}
540
541	virtual void OnSetMaterial(const SMaterial& material, const SMaterial& lastMaterial,
542		bool resetAllRenderstates, IMaterialRendererServices* services)
543	{
544		if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
545		{
546			pID3DDevice->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_MODULATE);
547			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
548			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
549			pID3DDevice->SetTextureStageState (0, D3DTSS_ALPHAOP,  D3DTOP_DISABLE);
550
551			pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
552			pID3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
553			pID3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
554
555			pID3DDevice->SetTransform( D3DTS_TEXTURE1, &SphereMapMatrixD3D8 );
556			pID3DDevice->SetTextureStageState( 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );
557			pID3DDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR);
558			pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
559
560			pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
561			pID3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
562			pID3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
563		}
564
565		services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
566	}
567
568	virtual void OnUnsetMaterial()
569	{
570		pID3DDevice->SetTextureStageState( 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
571		pID3DDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 1);
572		pID3DDevice->SetTransform( D3DTS_TEXTURE1, &UnitMatrixD3D8 );
573	}
574
575	//! Returns if the material is transparent.
576	virtual bool isTransparent() const
577	{
578		return true;
579	}
580};
581
582} // end namespace video
583} // end namespace irr
584
585#endif
586#endif
587#endif
588