1 | #include "OgreMaxMaterialExport.h" |
---|
2 | #include "max.h" |
---|
3 | #include "plugapi.h" |
---|
4 | #include "stdmat.h" |
---|
5 | #include "impexp.h" |
---|
6 | #include "IGame/IGame.h" |
---|
7 | |
---|
8 | namespace OgreMax { |
---|
9 | |
---|
10 | MaterialExporter::MaterialExporter(const Config& config, MaterialMap& map) : m_materialMap(map), OgreMaxExporter(config) |
---|
11 | { |
---|
12 | } |
---|
13 | |
---|
14 | MaterialExporter::~MaterialExporter() |
---|
15 | { |
---|
16 | } |
---|
17 | |
---|
18 | |
---|
19 | bool MaterialExporter::buildMaterial(std::string& output) { |
---|
20 | |
---|
21 | std::stringstream of; |
---|
22 | of.precision(6); |
---|
23 | of << std::fixed; |
---|
24 | output = ""; |
---|
25 | |
---|
26 | if (streamMaterial(of)) |
---|
27 | output = of.str(); |
---|
28 | else |
---|
29 | return false; |
---|
30 | |
---|
31 | return true; |
---|
32 | } |
---|
33 | |
---|
34 | //bool MaterialExporter::streamPass(std::ostream &of, Mtl *mtl) { |
---|
35 | bool MaterialExporter::streamPass(std::ostream &of, IGameMaterial *mtl) { |
---|
36 | of << "\t\tpass" << std::endl; |
---|
37 | of << "\t\t{" << std::endl; |
---|
38 | |
---|
39 | int subMtlCt = mtl->GetSubMaterialCount(); |
---|
40 | |
---|
41 | Point4 val4; |
---|
42 | Point3 val3; |
---|
43 | PropType pt; |
---|
44 | IGameProperty* p = mtl->GetAmbientData(); |
---|
45 | |
---|
46 | if (p) { |
---|
47 | pt = p->GetType(); |
---|
48 | |
---|
49 | if (pt == IGAME_POINT3_PROP) { |
---|
50 | p->GetPropertyValue(val3); |
---|
51 | of << "\t\t\tambient " << val3.x << " " << val3.y << " " << val3.z << " " << std::endl; |
---|
52 | } |
---|
53 | |
---|
54 | if (pt == IGAME_POINT4_PROP) { |
---|
55 | p->GetPropertyValue(val4); |
---|
56 | of << "\t\t\tambient " << val4.x << " " << val4.y << " " << val4.z << " " << val4.w << " " << std::endl; |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | p = mtl->GetDiffuseData(); |
---|
61 | if (p) { |
---|
62 | pt = p->GetType(); |
---|
63 | |
---|
64 | if (pt == IGAME_POINT3_PROP) { |
---|
65 | p->GetPropertyValue(val3); |
---|
66 | of << "\t\t\tdiffuse " << val3.x << " " << val3.y << " " << val3.z << " " << std::endl; |
---|
67 | } |
---|
68 | |
---|
69 | if (pt == IGAME_POINT4_PROP) { |
---|
70 | p->GetPropertyValue(val4); |
---|
71 | of << "\t\t\tdiffuse " << val4.x << " " << val4.y << " " << val4.z << " " << val4.w << " " << std::endl; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | p = mtl->GetSpecularData(); |
---|
76 | if (p) { |
---|
77 | pt = p->GetType(); |
---|
78 | |
---|
79 | if (pt == IGAME_POINT3_PROP) { |
---|
80 | p->GetPropertyValue(val3); |
---|
81 | of << "\t\t\tspecular " << val3.x << " " << val3.y << " " << val3.z << " " << std::endl; |
---|
82 | } |
---|
83 | |
---|
84 | if (pt == IGAME_POINT4_PROP) { |
---|
85 | p->GetPropertyValue(val4); |
---|
86 | of << "\t\t\tspecular " << val4.x << " " << val4.y << " " << val4.z << " " << val4.w << " " << std::endl; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | p = mtl->GetEmissiveData(); |
---|
91 | if (p) { |
---|
92 | pt = p->GetType(); |
---|
93 | |
---|
94 | if (pt == IGAME_POINT3_PROP) { |
---|
95 | p->GetPropertyValue(val3); |
---|
96 | of << "\t\t\temissive " << val3.x << " " << val3.y << " " << val3.z << " " << std::endl; |
---|
97 | } |
---|
98 | |
---|
99 | if (pt == IGAME_POINT4_PROP) { |
---|
100 | p->GetPropertyValue(val4); |
---|
101 | of << "\t\t\temissive " << val4.x << " " << val4.y << " " << val4.z << " " << val4.w << " " << std::endl; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | int numTexMaps = mtl->GetNumberOfTextureMaps(); |
---|
106 | if (numTexMaps > 0) { |
---|
107 | |
---|
108 | for (int texMapIdx = 0; texMapIdx < numTexMaps; texMapIdx++) { |
---|
109 | IGameTextureMap* tmap = mtl->GetIGameTextureMap(texMapIdx); |
---|
110 | if (tmap) { |
---|
111 | of << "\n\t\t\ttexture_unit " << std::endl; |
---|
112 | of << "\t\t\t{" << std::endl; |
---|
113 | |
---|
114 | std::string bmap(tmap->GetBitmapFileName()); |
---|
115 | bmap = bmap.substr(bmap.find_last_of('\\') + 1); |
---|
116 | of << "\t\t\t\ttexture " << bmap << std::endl; |
---|
117 | of << "\t\t\t}" << std::endl; |
---|
118 | } |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | of << "\t\t}" << std::endl; |
---|
123 | |
---|
124 | return true; |
---|
125 | } |
---|
126 | |
---|
127 | bool MaterialExporter::buildMaterial(IGameMaterial *material, const std::string& matName, std::string &script) { |
---|
128 | |
---|
129 | std::stringstream of; |
---|
130 | |
---|
131 | of << "material " << matName << std::endl; |
---|
132 | of << std::showpoint; |
---|
133 | of << "{" << std::endl; |
---|
134 | |
---|
135 | of << "\ttechnique" << std::endl; |
---|
136 | of << "\t{" << std::endl; |
---|
137 | |
---|
138 | int numSubMtl = 0; |
---|
139 | |
---|
140 | if (material != NULL) { |
---|
141 | numSubMtl = material->GetSubMaterialCount(); |
---|
142 | |
---|
143 | if (numSubMtl > 0) { |
---|
144 | int i; |
---|
145 | for (i=0; i<numSubMtl; i++) { |
---|
146 | streamPass(of, material->GetSubMaterial(i)); |
---|
147 | } |
---|
148 | } |
---|
149 | else |
---|
150 | streamPass(of, material); |
---|
151 | } |
---|
152 | else { |
---|
153 | streamPass(of, material); |
---|
154 | } |
---|
155 | |
---|
156 | of << "\t}" << std::endl; |
---|
157 | of << "}" << std::endl; |
---|
158 | |
---|
159 | script = of.str(); |
---|
160 | |
---|
161 | return true; |
---|
162 | } |
---|
163 | |
---|
164 | bool MaterialExporter::streamMaterial(std::ostream &of) { |
---|
165 | |
---|
166 | // serialize this information to the material file |
---|
167 | MaterialMap::iterator it = m_materialMap.begin(); |
---|
168 | |
---|
169 | while (it != m_materialMap.end()) { |
---|
170 | std::string matName(it->first); |
---|
171 | IGameMaterial *mtl = it->second; |
---|
172 | |
---|
173 | of << "material " << matName << std::endl; |
---|
174 | of << std::showpoint; |
---|
175 | of << "{" << std::endl; |
---|
176 | |
---|
177 | of << "\ttechnique" << std::endl; |
---|
178 | of << "\t{" << std::endl; |
---|
179 | |
---|
180 | int numSubMtl = 0; |
---|
181 | |
---|
182 | if (mtl != NULL) { |
---|
183 | numSubMtl = mtl->GetSubMaterialCount(); |
---|
184 | |
---|
185 | if (numSubMtl > 0) { |
---|
186 | int i; |
---|
187 | for (i=0; i<numSubMtl; i++) { |
---|
188 | streamPass(of, mtl->GetSubMaterial(i)); |
---|
189 | } |
---|
190 | } |
---|
191 | else |
---|
192 | streamPass(of, mtl); |
---|
193 | } |
---|
194 | else { |
---|
195 | streamPass(of, mtl); |
---|
196 | } |
---|
197 | |
---|
198 | of << "\t}" << std::endl; |
---|
199 | of << "}" << std::endl; |
---|
200 | |
---|
201 | it++; |
---|
202 | } |
---|
203 | |
---|
204 | m_materialMap.clear(); |
---|
205 | |
---|
206 | return true; |
---|
207 | } |
---|
208 | |
---|
209 | } |
---|