00001
00007 #include "MilkShape.h"
00008
00009 MS_Material::MS_Material() {
00010 }
00011
00012 MS_Material::~MS_Material() {
00013 clear();
00014 }
00015
00016 void MS_Material::clear() {
00017 app->renderer->deleteTexture(this->texture);
00018 }
00019
00020 void MS_Material::reloadTexture() {
00021 if (strlen(this->diffuse_name) > 0) {
00022 this->texture = app->renderer->addTexture(this->diffuse_name);
00023 }
00024 else {
00025 this->texture = TEXTURE_NONE;
00026 }
00027 }
00028
00029 bool MS_Material::loadFromAsciiSegment(const char *path, FILE *file) {
00030 char szLine[256] = {0};
00031
00032
00033
00034
00035 if (!fgets(szLine, 256, file))
00036 return false;
00037 if (sscanf(szLine, "\"%[^\"]\"", this->name) != 1)
00038 return false;
00039
00040
00041
00042
00043 if (!fgets(szLine, 256, file))
00044 return false;
00045 if (sscanf(szLine, "%f %f %f %f", &this->ambient[0], &this->ambient[1], &this->ambient[2], &this->ambient[3]) != 4)
00046 return false;
00047
00048
00049
00050
00051 if (!fgets(szLine, 256, file))
00052 return false;
00053 if (sscanf(szLine, "%f %f %f %f", &this->diffuse[0], &this->diffuse[1], &this->diffuse[2], &this->diffuse[3]) != 4)
00054 return false;
00055
00056
00057
00058
00059 if (!fgets(szLine, 256, file))
00060 return false;
00061 if (sscanf(szLine, "%f %f %f %f", &this->specular[0], &this->diffuse[1], &this->diffuse[2], &this->diffuse[3]) != 4)
00062 return false;
00063
00064
00065
00066
00067 if (!fgets(szLine, 256, file))
00068 return false;
00069 if (sscanf(szLine, "%f %f %f %f", &this->emissive[0], &this->emissive[1], &this->emissive[2], &this->emissive[3]) != 4)
00070 return false;
00071
00072
00073
00074
00075 if (!fgets(szLine, 256, file))
00076 return false;
00077 if (sscanf(szLine, "%f", &this->shininess) != 1)
00078 return false;
00079
00080
00081
00082
00083 if (!fgets(szLine, 256, file))
00084 return false;
00085 if (sscanf(szLine, "%f", &this->transparency) != 1)
00086 return false;
00087
00088
00089
00090
00091 if (!fgets(szLine, 256, file))
00092 return false;
00093
00094 strcpy(this->diffuse_name, "");
00095 char name[MAX_PATH] = {0};
00096 sscanf(szLine, "\"%[^\"]\"", name);
00097 sprintf(this->diffuse_name, "%s%s", path, name);
00098
00099
00100
00101
00102 if (!fgets(szLine, 256, file))
00103 return false;
00104
00105 strcpy(this->alpha_name, "");
00106 sscanf(szLine, "\"%[^\"]\"", this->alpha_name);
00107
00108 this->reloadTexture();
00109
00110 return true;
00111 }
00112
00113
00114
00115
00116 void MS_Material::activate() {
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 app->renderer->setTextures(this->texture);
00134 app->renderer->apply();
00135 }