1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "nel/3d/u_instance_material.h"
20 #include "nel/3d/async_texture_block.h"
21 #include "nel/3d/mesh_base_instance.h"
22 #include "nel/3d/driver.h"
23 #include "nel/3d/texture_mem.h"
24 #include "nel/3d/texture_file.h"
34 // ***************************************************************************
35 bool UInstanceMaterial::isTextureFile(uint stage
) const
37 if (stage
>= IDRV_MAT_MAXTEXTURES
)
39 nlwarning("UInstanceMaterialUser::isTextureFile : invalid stage");
42 return dynamic_cast<CTextureFile
*>(_Object
->getTexture(stage
)) != NULL
;
45 // ***************************************************************************
46 std::string
UInstanceMaterial::getTextureFileName(uint stage
) const
48 if (stage
>= IDRV_MAT_MAXTEXTURES
)
50 nlwarning("UInstanceMaterialUser::getTextureFileName : invalid stage");
55 if(_MBI
->getAsyncTextureMode())
57 nlassert(_AsyncTextureBlock
->isTextureFile(stage
));
58 // return name of the async one.
59 return _AsyncTextureBlock
->TextureNames
[stage
];
63 // return the name in the material
64 return NLMISC::safe_cast
<CTextureFile
*>(_Object
->getTexture(stage
))->getFileName();
68 // ***************************************************************************
69 void UInstanceMaterial::setTextureFileName(const std::string
&fileName
, uint stage
)
71 if (stage
>= IDRV_MAT_MAXTEXTURES
)
73 nlwarning("UInstanceMaterialUser::setTextureFileName : invalid stage");
78 if(_MBI
->getAsyncTextureMode())
80 if(!_AsyncTextureBlock
->isTextureFile(stage
))
82 nlwarning("UInstanceMaterialUser::setTextureFileName : the texture is not a texture file");
85 // replace the fileName
86 _AsyncTextureBlock
->TextureNames
[stage
]= fileName
;
88 _MBI
->setAsyncTextureDirty(true);
92 CTextureFile
*otherTex
= dynamic_cast<CTextureFile
*>(_Object
->getTexture(stage
));
95 nlwarning("UInstanceMaterialUser::setTextureFileName : the texture is not a texture file");
98 CTextureFile
*tf
= new CTextureFile(*otherTex
);
99 tf
->setFileName(fileName
);
100 NLMISC::CSmartPtr
<ITexture
> old
= _Object
->getTexture(stage
);
101 _Object
->setTexture(stage
, tf
);
105 // ***************************************************************************
106 void UInstanceMaterial::emptyTexture(uint stage
/*=0*/)
108 if (stage
>= IDRV_MAT_MAXTEXTURES
)
110 nlwarning("UInstanceMaterialUser::emptyTexture : invalid stage");
113 _Object
->setTexture(stage
, NULL
);
116 // ***************************************************************************
117 /*bool UInstanceMaterial::isSupportedByDriver(UDriver &drv, bool forceBaseCaps)
119 IDriver *idrv = NLMISC::safe_cast<CDriverUser *>(&drv)->getDriver();
120 return _Object->isSupportedByDriver(*idrv, forceBaseCaps);
123 // ***************************************************************************
124 void UInstanceMaterial::setTextureMem(uint stage
, uint8
*data
, uint32 length
, bool _delete
, bool isFile
/*=true*/, uint width
/*=0*/, uint height
/*=0*/, CBitmap::TType texType
/*=CBitmap::RGBA*/)
126 if (stage
>= IDRV_MAT_MAXTEXTURES
)
128 nlwarning("UInstanceMaterialUser::emptyTexture : invalid stage");
131 _Object
->setTexture((uint8
) stage
, new CTextureMem(data
, length
, _delete
, isFile
, width
, height
, texType
));
134 // ***************************************************************************
135 bool UInstanceMaterial::isLighted() const
137 return _Object
->isLighted();
140 // ***************************************************************************
141 void UInstanceMaterial::setLighting(bool active
,
142 CRGBA emissive
/*=CRGBA(0,0,0)*/,
143 CRGBA ambient
/*=CRGBA(0,0,0)*/,
144 CRGBA diffuse
/*=CRGBA(0,0,0)*/,
145 CRGBA specular
/*=CRGBA(0,0,0)*/,
146 float shininess
/*=10*/)
148 _Object
->setLighting(active
, emissive
, ambient
, diffuse
, specular
, shininess
);
151 // ***************************************************************************
153 bool UInstanceMaterial::isUserColor() const
155 return _Object
->getShader()==CMaterial::UserColor
;
158 // ***************************************************************************
160 void UInstanceMaterial::setEmissive( CRGBA emissive
)
162 _Object
->setEmissive(emissive
);
165 // ***************************************************************************
167 void UInstanceMaterial::setAmbient( CRGBA ambient
)
169 _Object
->setAmbient( ambient
);
172 // ***************************************************************************
174 void UInstanceMaterial::setDiffuse( CRGBA diffuse
)
176 _Object
->setDiffuse( diffuse
);
179 // ***************************************************************************
181 void UInstanceMaterial::setOpacity( uint8 opa
)
183 _Object
->setOpacity( opa
);
186 // ***************************************************************************
188 void UInstanceMaterial::setSpecular( CRGBA specular
)
190 _Object
->setSpecular( specular
);
193 // ***************************************************************************
195 void UInstanceMaterial::setShininess( float shininess
)
197 _Object
->setShininess( shininess
);
200 // ***************************************************************************
202 CRGBA
UInstanceMaterial::getEmissive() const
204 return _Object
->getEmissive();
207 // ***************************************************************************
209 CRGBA
UInstanceMaterial::getAmbient() const
211 return _Object
->getAmbient();
214 // ***************************************************************************
216 CRGBA
UInstanceMaterial::getDiffuse() const
218 return _Object
->getDiffuse();
221 // ***************************************************************************
223 uint8
UInstanceMaterial::getOpacity() const
225 return _Object
->getOpacity();
228 // ***************************************************************************
230 CRGBA
UInstanceMaterial::getSpecular() const
232 return _Object
->getSpecular();
235 // ***************************************************************************
237 float UInstanceMaterial::getShininess() const
239 return _Object
->getShininess();
242 // ***************************************************************************
244 void UInstanceMaterial::setColor(CRGBA rgba
)
246 _Object
->setColor(rgba
) ;
249 // ***************************************************************************
251 CRGBA
UInstanceMaterial::getColor(void) const
253 return _Object
->getColor();
256 // ***************************************************************************
258 void UInstanceMaterial::setUserColor(CRGBA userColor
)
261 _Object
->setUserColor(userColor
);
264 // ***************************************************************************
266 CRGBA
UInstanceMaterial::getUserColor() const
269 return _Object
->getUserColor();
271 return CRGBA(0,0,0,0);
274 // ***************************************************************************
276 void UInstanceMaterial::setConstantColor(uint stage
, NLMISC::CRGBA color
)
278 if (stage
>= IDRV_MAT_MAXTEXTURES
)
280 nlwarning("UInstanceMaterialUser::setConstantColor : invalid stage");
283 _Object
->texConstantColor(stage
, color
);
286 // ***************************************************************************
288 NLMISC::CRGBA
UInstanceMaterial::getConstantColor(uint stage
) const
290 if (stage
>= IDRV_MAT_MAXTEXTURES
)
292 nlwarning("UInstanceMaterialUser::getConstantColor : invalid stage");
293 return NLMISC::CRGBA::Black
;
295 return _Object
->getTexConstantColor(stage
);
299 // ***************************************************************************
301 sint
UInstanceMaterial::getLastTextureStage() const
304 for(uint k
= 0; k
< IDRV_MAT_MAXTEXTURES
; ++k
)
306 if (_Object
->getTexture(k
) != NULL
)
314 // ***************************************************************************
316 void UInstanceMaterial::setBlend(bool active
)
318 _Object
->setBlend(active
);
321 // ***************************************************************************
323 void UInstanceMaterial::setBlendFunc(TBlend src
, TBlend dst
)
325 _Object
->setBlendFunc((CMaterial::TBlend
)(uint32
)src
, (CMaterial::TBlend
)(uint32
)dst
);
328 // ***************************************************************************
330 void UInstanceMaterial::setSrcBlend(TBlend val
)
332 _Object
->setSrcBlend((CMaterial::TBlend
)(uint32
)val
);
335 // ***************************************************************************
337 void UInstanceMaterial::setDstBlend(TBlend val
)
339 _Object
->setDstBlend((CMaterial::TBlend
)(uint32
)val
);
342 // ***************************************************************************
344 void UInstanceMaterial::setAlphaTestThreshold(float at
)
346 _Object
->setAlphaTestThreshold(at
);
349 // ***************************************************************************
350 float UInstanceMaterial::getAlphaTestThreshold() const
352 return _Object
->getAlphaTestThreshold();
355 // ***************************************************************************
356 void UInstanceMaterial::setAlphaTest(bool active
)
358 _Object
->setAlphaTest(active
);
361 // ***************************************************************************
363 void UInstanceMaterial::setZWrite(bool active
)
365 _Object
->setZWrite(active
);
368 // ***************************************************************************
369 void UInstanceMaterial::setZFunc(ZFunc val
)
371 _Object
->setZFunc((CMaterial::ZFunc
) val
);
374 // ***************************************************************************
376 bool UInstanceMaterial::getBlend() const
378 return _Object
->getBlend();
381 // ***************************************************************************
383 UInstanceMaterial::TBlend
UInstanceMaterial::getSrcBlend(void) const
385 return (UInstanceMaterial::TBlend
)(uint32
)_Object
->getSrcBlend();
388 // ***************************************************************************
390 UInstanceMaterial::TBlend
UInstanceMaterial::getDstBlend(void) const
392 return (UInstanceMaterial::TBlend
)(uint32
)_Object
->getDstBlend();
395 // ***************************************************************************
397 void UInstanceMaterial::enableUserTexMat(uint stage
, bool enabled
)
399 if (stage
>= IDRV_MAT_MAXTEXTURES
)
401 nlwarning("UInstanceMaterial::enableUserTexMat : stage %d is invalid", stage
);
404 _Object
->enableUserTexMat(stage
, enabled
);
407 // ***************************************************************************
409 bool UInstanceMaterial::isUserTexMatEnabled(uint stage
) const
411 if (stage
>= IDRV_MAT_MAXTEXTURES
)
413 nlwarning("UInstanceMaterial::enableUserTexMat : stage %d is invalid", stage
);
416 return _Object
->isUserTexMatEnabled(stage
);
419 // ***************************************************************************
421 void UInstanceMaterial::setUserTexMat(uint stage
, const NLMISC::CMatrix
&m
)
423 if (stage
>= IDRV_MAT_MAXTEXTURES
)
425 nlwarning("UInstanceMaterial::enableUserTexMat : stage %d is invalid", stage
);
428 if (!_Object
->isUserTexMatEnabled(stage
))
430 nlwarning("UInstanceMaterial::setUserTexMat : texture stage %d has no user matrix.", stage
);
432 _Object
->setUserTexMat(stage
, m
);
435 // ***************************************************************************
437 const NLMISC::CMatrix
&UInstanceMaterial::getUserTexMat(uint stage
) const
439 if (stage
>= IDRV_MAT_MAXTEXTURES
)
441 nlwarning("UInstanceMaterial::enableUserTexMat : stage %d is invalid", stage
);
442 return CMatrix::Identity
;
444 if (!_Object
->isUserTexMatEnabled(stage
))
446 nlwarning("UInstanceMaterial::setUserTexMat : texture stage %d has no user matrix.", stage
);
447 return CMatrix::Identity
;
449 return _Object
->getUserTexMat(stage
);
452 // ***************************************************************************
454 void UInstanceMaterial::setWrapS(uint stage
, TWrapMode mode
)
456 if (stage
>= IDRV_MAT_MAXTEXTURES
|| _Object
->getTexture(stage
) == NULL
)
458 nlwarning("UInstanceMaterial::setWrapS : stage %d is invalid or there's no texture", stage
);
461 _Object
->getTexture(stage
)->setWrapS((ITexture::TWrapMode
) mode
);
464 // ***************************************************************************
466 void UInstanceMaterial::setWrapT(uint stage
, TWrapMode mode
)
468 if (stage
>= IDRV_MAT_MAXTEXTURES
|| _Object
->getTexture(stage
) == NULL
)
470 nlwarning("UInstanceMaterial::setWrapT : stage %d is invalid or there's no texture", stage
);
473 _Object
->getTexture(stage
)->setWrapT((ITexture::TWrapMode
) mode
);
476 // ***************************************************************************
478 UInstanceMaterial::TWrapMode
UInstanceMaterial::getWrapS(uint stage
) const
480 if (stage
>= IDRV_MAT_MAXTEXTURES
|| _Object
->getTexture(uint8(stage
)) == NULL
)
482 nlwarning("UInstanceMaterial::getWrapS : stage %d is invalid or there's no texture", stage
);
485 return (TWrapMode
) _Object
->getTexture(uint8(stage
))->getWrapS();
488 // ***************************************************************************
490 UInstanceMaterial::TWrapMode
UInstanceMaterial::getWrapT(uint stage
) const
492 if (stage
>= IDRV_MAT_MAXTEXTURES
|| _Object
->getTexture(uint8(stage
)) == NULL
)
494 nlwarning("UInstanceMaterial::getWrapT : stage %d is invalid or there's no texture", stage
);
497 return (TWrapMode
) _Object
->getTexture(uint8(stage
))->getWrapT();
500 // ***************************************************************************