00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef MATERIAL_H_
00026 #define MATERIAL_H_
00027
00028 #include <Graphics/Scene/SceneObject.h>
00029 #include <Graphics/System/GraphicsDeviceObjectHolder.h>
00030 #include <Core/Container/ArrayList.h>
00031
00032 namespace Lamp{
00033
00034 class Mesh;
00035 class Texture;
00036 class DrawRequest;
00037 class BasicMaterial;
00038
00039
00040
00041
00042
00043 class Material : public SceneObject , public GraphicsDeviceObjectHolder{
00044 friend class SceneObjectManagerTemplate<Material>;
00045 friend class MaterialManager;
00046 friend class Mesh;
00047 friend class Renderer;
00048 public:
00049
00050
00051
00052
00053
00054 virtual int getReferenceCount() const{ return parents_.getCount(); }
00055
00056
00057
00058
00059
00060
00061
00062 virtual Material* copy(u_int copyMask = 0) const = 0;
00063
00064
00065
00066
00067
00068
00069 static int recursiveDestroy(Material* material);
00070
00071
00072
00073
00074
00075 virtual void stateChanged(){ hasStateChanged_ = true; }
00076
00077
00078
00079
00080
00081 virtual bool hasStateChanged() const{ return hasStateChanged_; }
00082
00083
00084
00085
00086
00087
00088 virtual int getParentCount() const{ return parents_.getCount(); }
00089
00090
00091
00092
00093
00094
00095 virtual Mesh* getParent(int index) const{
00096 Assert(index >= 0);
00097 Assert(index < getParentCount());
00098 return parents_.get(index);
00099 }
00100
00101
00102
00103
00104
00105
00106 virtual bool useLight() const{ return true; }
00107
00108
00109
00110
00111
00112
00113 virtual void setPriority(int priority){
00114 priority_ = priority;
00115 stateChanged();
00116 }
00117
00118
00119
00120
00121
00122 virtual int getPriority() const{ return priority_; }
00123
00124
00125
00126
00127
00128 enum BlendMode{
00129 blendModeDisable = 0,
00130 blendModeAdd,
00131 blendModeSubtract,
00132 blendModeInverseSubtract,
00133 blendModeMinimum,
00134 blendModeMaximum,
00135 blendModeMax,
00136 };
00137
00138
00139
00140
00141
00142
00143 static const String& blendModeToString(BlendMode blendMode);
00144
00145
00146
00147
00148
00149
00150 static BlendMode blendModeFromString(const String& blendModeString);
00151
00152
00153
00154
00155
00156
00157 virtual void setBlendMode(BlendMode blendMode){
00158 blendMode_ = blendMode;
00159 stateChanged();
00160 }
00161
00162
00163
00164
00165
00166 virtual BlendMode getBlendMode() const{ return blendMode_; }
00167
00168
00169
00170
00171
00172 virtual bool isBlendEnabled() const{
00173 return (blendMode_ != blendModeDisable);
00174 }
00175
00176
00177
00178
00179
00180
00181 virtual void setAlpha(float alpha){
00182 alpha_ = alpha;
00183 stateChanged();
00184 }
00185
00186
00187
00188
00189
00190 virtual float getAlpha() const{ return alpha_; }
00191
00192
00193
00194 enum BlendState{
00195 blendStateZero = 0,
00196 blendStateOne,
00197 blendStateSourceColor,
00198 blendStateInverseSourceColor,
00199 blendStateSourceAlpha,
00200 blendStateInverseSourceAlpha,
00201 blendStateSourceAlphaSaturate,
00202 blendStateDestinationColor,
00203 blendStateInverseDestinationColor,
00204 blendStateDestinationAlpha,
00205 blendStateInverseDestinationAlpha,
00206 blendStateMax,
00207 };
00208
00209
00210
00211
00212
00213
00214 static const String& blendStateToString(BlendState blendState);
00215
00216
00217
00218
00219
00220
00221 static BlendState blendStateFromString(const String& blendStateString);
00222
00223
00224
00225
00226
00227
00228 virtual void setBlendSource(BlendState blendSource){
00229 Assert(blendSource >= 0);
00230 Assert(blendSource < blendStateMax);
00231 blendSource_ = blendSource;
00232 stateChanged();
00233 }
00234
00235
00236
00237
00238
00239 virtual BlendState getBlendSource() const{ return blendSource_; }
00240
00241
00242
00243
00244
00245
00246 virtual void setBlendDestination(BlendState blendDestination){
00247 Assert(blendDestination_ >= 0);
00248 Assert(blendDestination_ < blendStateMax);
00249 blendDestination_ = blendDestination;
00250 stateChanged();
00251 }
00252
00253
00254
00255
00256
00257 virtual BlendState getBlendDestination() const{
00258 return blendDestination_;
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268 virtual void setZWrite(bool zWrite){
00269 zWrite_ = zWrite;
00270 stateChanged();
00271 }
00272
00273
00274
00275
00276
00277 virtual bool useZWrite() const{ return zWrite_; }
00278
00279
00280
00281
00282
00283
00284 virtual void setZTest(bool zTest){
00285 zTest_ = zTest;
00286 stateChanged();
00287 }
00288
00289
00290
00291
00292
00293 virtual bool useZTest() const{ return zTest_; }
00294
00295
00296
00297
00298
00299 enum FogOption{
00300 fogOptionNone = 0,
00301 fogOptionDisable,
00302 fogOptionBlack,
00303 fogOptionMax,
00304 };
00305
00306
00307
00308
00309
00310
00311 static const String& fogOptionToString(FogOption fogOption);
00312
00313
00314
00315
00316
00317
00318 static FogOption fogOptionFromString(const String& fogOptionString);
00319
00320
00321
00322
00323
00324
00325 virtual void setFogOption(FogOption fogOption){
00326 Assert(fogOption >= 0);
00327 Assert(fogOption < fogOptionMax);
00328 fogOption_ = fogOption;
00329 stateChanged();
00330 }
00331
00332
00333
00334
00335
00336 virtual FogOption getFogOption() const{ return fogOption_; }
00337
00338
00339
00340
00341
00342
00343
00344
00345 virtual void setLightMask(u_int lightMask){ lightMask_ = lightMask; }
00346
00347
00348
00349
00350
00351 virtual u_int getLightMask() const{ return lightMask_; }
00352
00353
00354
00355
00356
00357 enum PipelineMode{
00358 pipelineModeNone = 0,
00359 pipelineModeFixed,
00360 pipelineModeProgrammableShader2,
00361 };
00362
00363
00364
00365
00366
00367 virtual PipelineMode getPipelineMode() const{
00368 Assert(pipelineMode_ != pipelineModeNone);
00369 return pipelineMode_;
00370 }
00371
00372
00373
00374
00375
00376
00377
00378
00379 virtual bool initializeGraphicsDeviceObjects(){ return true; }
00380
00381
00382
00383
00384 virtual void deleteGraphicsDeviceObjects(){}
00385
00386
00387
00388
00389
00390 virtual bool restoreGraphicsDeviceObjects(){
00391 buildStateBlock(&startStateBlock_, &endStateBlock_);
00392 return true;
00393 }
00394
00395
00396
00397
00398 virtual void invalidateGraphicsDeviceObjects(){ releaseStateBlock(); }
00399
00400
00401
00402
00403
00404
00405
00406
00407 virtual bool isMaterial() const{ return true; }
00408
00409
00410
00411
00412
00413
00414 virtual bool isBasicMaterial() const{ return false; }
00415
00416
00417
00418
00419
00420 virtual BasicMaterial* castBasicMaterial() const{
00421 if(isBasicMaterial()){ return (BasicMaterial*)this; }
00422 return NULL;
00423 }
00424
00425
00426 protected:
00427
00428
00429
00430
00431
00432 Material(const String& name, Scene* scene);
00433
00434
00435
00436
00437 virtual ~Material();
00438
00439
00440
00441
00442
00443 virtual void copyMaterialValue(Material* destination) const;
00444
00445
00446
00447
00448
00449 virtual int destroyChildren() = 0;
00450
00451
00452
00453
00454
00455
00456
00457
00458 virtual void drawSetup(DrawRequest* request);
00459
00460
00461
00462
00463
00464 virtual void draw(DrawRequest* request) = 0;
00465
00466
00467
00468
00469
00470
00471
00472 virtual void buildStateBlock(
00473 Direct3DStateBlock** startBlock, Direct3DStateBlock** endBlock) = 0;
00474
00475
00476
00477
00478 virtual void releaseStateBlock(){
00479 SafeRelease(endStateBlock_);
00480 SafeRelease(startStateBlock_);
00481 }
00482
00483
00484
00485
00486
00487 virtual void setPipelineMode(PipelineMode pipelineMode){
00488 Assert(pipelineMode != pipelineModeNone);
00489 pipelineMode_ = pipelineMode;
00490 }
00491
00492
00493
00494
00495
00496
00497
00498 virtual int addReference(Mesh* parent){
00499 parents_.add(parent);
00500 return getParentCount();
00501 }
00502
00503
00504
00505
00506
00507
00508 virtual int removeReference(Mesh* parent){
00509 parents_.removeByValue(parent);
00510 return getParentCount();
00511 }
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522 virtual Texture* setTextureReferense(
00523 Texture* nowTexture, Texture* newTexture);
00524
00525 private:
00526
00527
00528
00529
00530
00531
00532 virtual void drawStart();
00533
00534
00535
00536
00537 virtual void drawEnd();
00538
00539
00540
00541 ArrayList<Mesh*> parents_;
00542
00543 Direct3DStateBlock* startStateBlock_;
00544
00545 Direct3DStateBlock* endStateBlock_;
00546
00547 int priority_;
00548
00549 BlendMode blendMode_;
00550
00551 BlendState blendSource_;
00552
00553 BlendState blendDestination_;
00554
00555 float alpha_;
00556
00557 FogOption fogOption_;
00558
00559 u_int lightMask_;
00560
00561 PipelineMode pipelineMode_;
00562
00563 bool alphaBlend_;
00564
00565 bool zWrite_;
00566
00567 bool zTest_;
00568
00569 bool hasStateChanged_;
00570
00571
00572 static const String blendModeStringTable[];
00573
00574 static const String blendStateStringTable[];
00575
00576 static const String fogOptionStringTable[];
00577
00578 };
00579
00580
00581 }
00582 #endif // End of MATERIAL_H_
00583