/CS/migrated/branches/NEW_SHADERSYS/plugins/engine/iso/isomater.cpp

# · C++ · 257 lines · 196 code · 35 blank · 26 comment · 11 complexity · 5810c82d0269db90ea286d9e3653acec MD5 · raw file

  1. /*
  2. Copyright (C) 2001 by Jorrit Tyberghein
  3. Copyright (C) 2000 by W.C.A. Wijngaards
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free
  14. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "cssysdef.h"
  17. #include "isomater.h"
  18. #include "ivideo/txtmgr.h"
  19. #include "ivideo/texture.h"
  20. SCF_IMPLEMENT_IBASE (csIsoMaterial)
  21. SCF_IMPLEMENTS_INTERFACE (iMaterial)
  22. SCF_IMPLEMENT_IBASE_END
  23. csIsoMaterial::csIsoMaterial () :
  24. diffuse(CS_DEFMAT_DIFFUSE),
  25. ambient(CS_DEFMAT_AMBIENT),
  26. reflection(CS_DEFMAT_REFLECTION)
  27. {
  28. SCF_CONSTRUCT_IBASE (0);
  29. flat_color.Set (255, 255, 255); // Default state is white, flat-shaded.
  30. effect = 0;
  31. }
  32. csIsoMaterial::csIsoMaterial (iTextureHandle* w) :
  33. diffuse(CS_DEFMAT_DIFFUSE),
  34. ambient(CS_DEFMAT_AMBIENT),
  35. reflection(CS_DEFMAT_REFLECTION)
  36. {
  37. SCF_CONSTRUCT_IBASE (0);
  38. flat_color.Set (255, 255, 255); // Default state is white, flat-shaded.
  39. texture = w;
  40. effect = 0;
  41. }
  42. csIsoMaterial::~csIsoMaterial ()
  43. {
  44. SCF_DESTRUCT_IBASE();
  45. }
  46. void csIsoMaterial::SetEffect (iEffectDefinition *ed)
  47. {
  48. effect = ed;
  49. }
  50. iEffectDefinition *csIsoMaterial::GetEffect ()
  51. {
  52. return effect;
  53. }
  54. iTextureHandle *csIsoMaterial::GetTexture ()
  55. {
  56. return texture;
  57. }
  58. iTextureHandle *csIsoMaterial::GetTexture (csStringID)
  59. {
  60. return texture;
  61. }
  62. void csIsoMaterial::GetFlatColor (csRGBpixel &oColor, bool useTextureMean)
  63. {
  64. oColor = flat_color;
  65. if (texture && useTextureMean)
  66. {
  67. iTextureHandle *th = texture;
  68. if (th) th->GetMeanColor (oColor.red, oColor.green, oColor.blue);
  69. }
  70. }
  71. void csIsoMaterial::GetReflection (float &oDiffuse, float &oAmbient,
  72. float &oReflection)
  73. {
  74. oDiffuse = diffuse;
  75. oAmbient = ambient;
  76. oReflection = reflection;
  77. }
  78. //---------------------------------------------------------------------------
  79. SCF_IMPLEMENT_IBASE_EXT (csIsoMaterialWrapper)
  80. SCF_IMPLEMENTS_EMBEDDED_INTERFACE (iMaterialWrapper)
  81. SCF_IMPLEMENTS_EMBEDDED_INTERFACE (iIsoMaterialWrapperIndex)
  82. SCF_IMPLEMENT_IBASE_EXT_END
  83. SCF_IMPLEMENT_EMBEDDED_IBASE (csIsoMaterialWrapper::MaterialWrapper)
  84. SCF_IMPLEMENTS_INTERFACE (iMaterialWrapper)
  85. SCF_IMPLEMENT_EMBEDDED_IBASE_END
  86. SCF_IMPLEMENT_EMBEDDED_IBASE (csIsoMaterialWrapper::IsoMaterialWrapperIndex)
  87. SCF_IMPLEMENTS_INTERFACE (iIsoMaterialWrapperIndex)
  88. SCF_IMPLEMENT_EMBEDDED_IBASE_END
  89. csIsoMaterialWrapper::csIsoMaterialWrapper (iMaterial* material) :
  90. csObject ()
  91. {
  92. SCF_CONSTRUCT_EMBEDDED_IBASE (scfiMaterialWrapper);
  93. SCF_CONSTRUCT_EMBEDDED_IBASE (scfiIsoMaterialWrapperIndex);
  94. csIsoMaterialWrapper::material = material;
  95. index = 0;
  96. }
  97. csIsoMaterialWrapper::csIsoMaterialWrapper (csIsoMaterialWrapper &th) :
  98. csObject ()
  99. {
  100. SCF_CONSTRUCT_EMBEDDED_IBASE (scfiMaterialWrapper);
  101. SCF_CONSTRUCT_EMBEDDED_IBASE (scfiIsoMaterialWrapperIndex);
  102. material = th.material;
  103. handle = th.GetMaterialHandle ();
  104. SetName (th.GetName ());
  105. index = th.index;
  106. }
  107. csIsoMaterialWrapper::csIsoMaterialWrapper (iMaterialHandle *ith) :
  108. csObject ()
  109. {
  110. SCF_CONSTRUCT_EMBEDDED_IBASE (scfiMaterialWrapper);
  111. SCF_CONSTRUCT_EMBEDDED_IBASE (scfiIsoMaterialWrapperIndex);
  112. handle = ith;
  113. index = 0;
  114. }
  115. csIsoMaterialWrapper::~csIsoMaterialWrapper ()
  116. {
  117. SCF_DESTRUCT_EMBEDDED_IBASE (scfiIsoMaterialWrapperIndex);
  118. SCF_DESTRUCT_EMBEDDED_IBASE (scfiMaterialWrapper);
  119. }
  120. void csIsoMaterialWrapper::SetMaterialHandle (iMaterialHandle *m)
  121. {
  122. material = 0;
  123. handle = m;
  124. }
  125. void csIsoMaterialWrapper::SetMaterial (iMaterial *material)
  126. {
  127. csIsoMaterialWrapper::material = material;
  128. }
  129. void csIsoMaterialWrapper::Register (iTextureManager *txtmgr)
  130. {
  131. handle = txtmgr->RegisterMaterial (material);
  132. }
  133. void csIsoMaterialWrapper::Visit ()
  134. {
  135. // @@@ This is not very clean! We shouldn't cast from iMaterial to csMaterial.
  136. //csMaterial* mat = (csMaterial*)material;
  137. //if (mat && mat->GetTextureWrapper ())
  138. //mat->GetTextureWrapper ()->Visit ();
  139. }
  140. //------------------------------------------------------ csMaterialList -----//
  141. SCF_IMPLEMENT_IBASE (csIsoMaterialList)
  142. SCF_IMPLEMENTS_EMBEDDED_INTERFACE (iMaterialList)
  143. SCF_IMPLEMENT_IBASE_END
  144. SCF_IMPLEMENT_EMBEDDED_IBASE (csIsoMaterialList::MaterialList)
  145. SCF_IMPLEMENTS_INTERFACE (iMaterialList)
  146. SCF_IMPLEMENT_EMBEDDED_IBASE_END
  147. csIsoMaterialList::csIsoMaterialList () :
  148. csRefArray<csIsoMaterialWrapper> (16, 16)
  149. {
  150. SCF_CONSTRUCT_IBASE (0);
  151. SCF_CONSTRUCT_EMBEDDED_IBASE (scfiMaterialList);
  152. lastindex = 0;
  153. }
  154. csIsoMaterialList::~csIsoMaterialList ()
  155. {
  156. DeleteAll ();
  157. SCF_DESTRUCT_EMBEDDED_IBASE (scfiMaterialList);
  158. SCF_DESTRUCT_IBASE();
  159. }
  160. int csIsoMaterialList::GetNewIndex()
  161. {
  162. int i = lastindex;
  163. while(i < Length())
  164. {
  165. if(Get(i)==0)
  166. {
  167. lastindex = i+1;
  168. return i;
  169. }
  170. i++;
  171. }
  172. /// no indices free
  173. Push(0);
  174. lastindex = Length();
  175. return lastindex-1;
  176. }
  177. void csIsoMaterialList::RemoveIndex(int i)
  178. {
  179. CS_ASSERT (i >= 0);
  180. if(i>=Length()) return;
  181. (*this)[i] = 0;
  182. if(i==Length()-1)
  183. {
  184. csRef<csIsoMaterialWrapper> w = Pop(); // pop last element from the list
  185. if(Length()<lastindex) lastindex = Length();
  186. return;
  187. }
  188. /// remove from middle of list
  189. if(i<lastindex) lastindex = i;
  190. }
  191. csIsoMaterialWrapper* csIsoMaterialList::NewMaterial (iMaterial* material)
  192. {
  193. csIsoMaterialWrapper *tm = new csIsoMaterialWrapper (material);
  194. int i = GetNewIndex();
  195. (*this)[i] = tm;
  196. tm->SetIndex(i);
  197. return tm;
  198. }
  199. csIsoMaterialWrapper* csIsoMaterialList::FindByName (const char* name)
  200. {
  201. int i;
  202. for (i = 0 ; i < Length () ; i++)
  203. {
  204. csIsoMaterialWrapper* w = (*this)[i];
  205. if (!strcmp (w->GetName (), name))
  206. {
  207. return w;
  208. }
  209. }
  210. return 0;
  211. }
  212. csIsoMaterialWrapper* csIsoMaterialList::NewMaterial (iMaterialHandle *ith)
  213. {
  214. csIsoMaterialWrapper *tm = new csIsoMaterialWrapper (ith);
  215. int i = GetNewIndex();
  216. (*this)[i] = tm;
  217. tm->SetIndex(i);
  218. return tm;
  219. }