00001
#include "StdAfx.h"
00002
#include "Material.h"
00003
00004 CMaterial::CMaterial()
00005 {
00006
SetEntityType(Entity_Material);
00007
m_fShininess = 0;
00008
m_Ambient =
m_Diffuse =
m_Specular =
m_Emissive = tRGBA(0, 0, 0, 0);
00009 }
00010
00011 CMaterial::~CMaterial()
00012 {
00013
Destroy();
00014 }
00015
00016 int CMaterial::LoadXML(
TiXmlNode *pDataNode, CString strFilename)
00017 {
00018
TiXmlElement* pXMLElement = NULL;
00019
const char *pcValue = NULL;
00020
00021
int iRetValue =
CXMLResource::LoadXML(pDataNode, strFilename);
00022
if(iRetValue !=1)
00023
return iRetValue;
00024
00025 pXMLElement = m_pXMLElement;
00026
00027
if(stricmp(pXMLElement->
pcValue(),
"Material") !=0)
00028
return 0;
00029
00030
int Color[3] = {0, 0, 0};
00031 pcValue = pXMLElement->
Attribute(
"Ambient");
00032
if(pcValue !=NULL)
00033 {
00034
00035 sscanf(pcValue,
"%d,%d,%d", &Color[0], &Color[1], &Color[2]);
00036
SetAmbient(tRGBA(Color[0], Color[1], Color[2], -1));
00037 pcValue = NULL;
00038 }
00039
00040 pcValue = pXMLElement->
Attribute(
"Diffuse");
00041
if(pcValue !=NULL)
00042 {
00043
int Color[3] = {0,0,0};
00044 sscanf(pcValue,
"%d,%d,%d", &Color[0], &Color[1], &Color[2]);
00045
SetDiffuse(tRGBA(Color[0], Color[1], Color[2], -1));
00046 pcValue = NULL;
00047 }
00048
00049 pcValue = pXMLElement->
Attribute(
"Emissive");
00050
if(pcValue !=NULL)
00051 {
00052
int Color[3] = {0,0,0};
00053 sscanf(pcValue,
"%d,%d,%d", &Color[0], &Color[1], &Color[2]);
00054
SetEmissive(tRGBA(Color[0], Color[1], Color[2], -1));
00055 pcValue = NULL;
00056 }
00057
00058 pcValue = pXMLElement->
Attribute(
"Specular");
00059
if(pcValue !=NULL)
00060 {
00061
int Color[3] = {0,0,0};
00062 sscanf(pcValue,
"%d,%d,%d", &Color[0], &Color[1], &Color[2]);
00063
SetSpecular(tRGBA(Color[0], Color[1], Color[2], -1));
00064 pcValue = NULL;
00065 }
00066
00067 pcValue = pXMLElement->
Attribute(
"Shininess");
00068
if(pcValue !=NULL)
00069 {
00070
int s = 0;
00071 sscanf(pcValue,
"%d", &s);
00072
SetShininess((
float)s);
00073 pcValue = NULL;
00074 }
00075
00076
return 1;
00077 }
00078
00079 int CMaterial::SaveXML(
TiXmlNode *pDataNode, CString strFilename)
00080 {
00081
TiXmlElement *pSaveElement = NULL;
00082
TiXmlDocument Doc;
00083
00084
if(pDataNode !=NULL)
00085 pSaveElement = pDataNode->
ToElement();
00086
else if(m_pXMLElement !=NULL)
00087 pSaveElement = m_pXMLElement;
00088
else
00089 pSaveElement =
new TiXmlElement(
"Material");
00090
00091
char pcValue[256] =
"";
00092
00093
if(
GetName() !=
"")
00094 pSaveElement->
SetAttribute(
"strName",
GetName().GetBuffer());
00095
00096 sprintf(pcValue,
"%d,%d,%d", (
int)
m_Ambient.R, (
int)
m_Ambient.G, (
int)
m_Ambient.B);
00097 pSaveElement->
SetAttribute(
"Ambient", pcValue);
00098 memset(pcValue, 0,
sizeof(pcValue));
00099
00100 sprintf(pcValue,
"%d,%d,%d", (
int)
m_Diffuse.R, (
int)
m_Diffuse.G, (
int)
m_Diffuse.B);
00101 pSaveElement->
SetAttribute(
"Diffuse", pcValue);
00102 memset(pcValue, 0,
sizeof(pcValue));
00103
00104 sprintf(pcValue,
"%d,%d,%d", (
int)
m_Specular.R, (
int)
m_Specular.G, (
int)
m_Specular.B);
00105 pSaveElement->
SetAttribute(
"Specular", pcValue);
00106 memset(pcValue, 0,
sizeof(pcValue));
00107
00108 sprintf(pcValue,
"%d,%d,%d", (
int)
m_Emissive.R, (
int)
m_Emissive.G, (
int)
m_Emissive.B);
00109 pSaveElement->
SetAttribute(
"Emissive", pcValue);
00110 memset(pcValue, 0,
sizeof(pcValue));
00111
00112 pSaveElement->
SetAttribute(
"Shininess",
m_fShininess);
00113
00114
if(pDataNode == NULL)
00115 {
00116
if(strFilename ==
"")
00117
return 0;
00118
00119 Doc.
InsertEndChild(*pSaveElement);
00120 SAFEDEL(pSaveElement)
00121
return Doc.
SaveFile(strFilename.GetBuffer());
00122 }
00123
else
00124 {
00125
if(strFilename ==
"")
00126
return 1;
00127
00128 Doc.
InsertEndChild(*pSaveElement);
00129
return Doc.
SaveFile(strFilename.GetBuffer());
00130 }
00131
00132
return 0;
00133 }
00134
00135 void CMaterial::SetAmbient(tRGBA ambient)
00136 {
00137
m_Ambient = ambient;
00138 }
00139
00140 void CMaterial::SetDiffuse(tRGBA diffuse)
00141 {
00142
m_Diffuse = diffuse;
00143 }
00144
00145 void CMaterial::SetEmissive(tRGBA emissive)
00146 {
00147
m_Emissive = emissive;
00148 }
00149
00150 void CMaterial::SetSpecular(tRGBA specular)
00151 {
00152
m_Specular = specular;
00153 }
00154
00155 void CMaterial::SetShininess(
float fShininess)
00156 {
00157
m_fShininess = fShininess;
00158 }
00159
00160 tRGBA&
CMaterial::GetAmbient()
00161 {
00162
return m_Ambient;
00163 }
00164
00165 tRGBA&
CMaterial::GetDiffuse()
00166 {
00167
return m_Diffuse;
00168 }
00169
00170 tRGBA&
CMaterial::GetSpecular()
00171 {
00172
return m_Specular;
00173 }
00174
00175 tRGBA&
CMaterial::GetEmissive()
00176 {
00177
return m_Emissive;
00178 }
00179
00180 float&
CMaterial::GetShininess()
00181 {
00182
return m_fShininess;
00183 }
00184
00185 void CMaterial::Bind()
00186 {
00187
00188 }
00189
00190 void CMaterial::Destroy()
00191 {
00192
00193 }
00194
00195 bool CMaterial::IsOfType(eEntityType eType)
00196 {
00197
if(eType == Entity_Material)
00198
return true;
00199
00200
return CXMLResource::IsOfType(eType);
00201 }