1 // 3dsmodel.cpp
2 //
3 // Copyright (C) 2000, Chris Laurel <claurel@shatters.net>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 
10 #include "3dsmodel.h"
11 
12 using namespace std;
13 
14 
M3DColor()15 M3DColor::M3DColor() :
16     red(0), green(0), blue(0)
17 {
18 }
19 
M3DColor(float _red,float _green,float _blue)20 M3DColor::M3DColor(float _red, float _green, float _blue) :
21     red(_red), green(_green), blue(_blue)
22 {
23 }
24 
25 
M3DMaterial()26 M3DMaterial::M3DMaterial() :
27     ambient(0, 0, 0),
28     diffuse(0, 0, 0),
29     specular(0, 0, 0),
30     shininess(1.0f)
31 {
32 }
33 
getName() const34 string M3DMaterial::getName() const
35 {
36     return name;
37 }
38 
setName(string _name)39 void M3DMaterial::setName(string _name)
40 {
41     name = _name;
42 }
43 
getDiffuseColor() const44 M3DColor M3DMaterial::getDiffuseColor() const
45 {
46     return diffuse;
47 }
48 
setDiffuseColor(M3DColor color)49 void M3DMaterial::setDiffuseColor(M3DColor color)
50 {
51     diffuse = color;
52 }
53 
getAmbientColor() const54 M3DColor M3DMaterial::getAmbientColor() const
55 {
56     return ambient;
57 }
58 
setAmbientColor(M3DColor color)59 void M3DMaterial::setAmbientColor(M3DColor color)
60 {
61     ambient = color;
62 }
63 
getSpecularColor() const64 M3DColor M3DMaterial::getSpecularColor() const
65 {
66     return specular;
67 }
68 
setSpecularColor(M3DColor color)69 void M3DMaterial::setSpecularColor(M3DColor color)
70 {
71     specular = color;
72 }
73 
getShininess() const74 float M3DMaterial::getShininess() const
75 {
76     return shininess;
77 }
78 
setShininess(float _shininess)79 void M3DMaterial::setShininess(float _shininess)
80 {
81     shininess = _shininess;
82 }
83 
getOpacity() const84 float M3DMaterial::getOpacity() const
85 {
86     return opacity;
87 }
88 
setOpacity(float _opacity)89 void M3DMaterial::setOpacity(float _opacity)
90 {
91     opacity = _opacity;
92 }
93 
getTextureMap() const94 string M3DMaterial::getTextureMap() const
95 {
96     return texmap;
97 }
98 
setTextureMap(const string & _texmap)99 void M3DMaterial::setTextureMap(const string& _texmap)
100 {
101     texmap = _texmap;
102 }
103 
104 
M3DTriangleMesh()105 M3DTriangleMesh::M3DTriangleMesh()
106 {
107     matrix = Mat4f::identity();
108 }
109 
~M3DTriangleMesh()110 M3DTriangleMesh::~M3DTriangleMesh()
111 {
112 }
113 
getMatrix() const114 Mat4f M3DTriangleMesh::getMatrix() const
115 {
116     return matrix;
117 }
118 
setMatrix(const Mat4f & m)119 void M3DTriangleMesh::setMatrix(const Mat4f& m)
120 {
121     matrix = m;
122 }
123 
getVertex(uint16 n) const124 Point3f M3DTriangleMesh::getVertex(uint16 n) const
125 {
126     return points[n];
127 }
128 
getVertexCount() const129 uint16 M3DTriangleMesh::getVertexCount() const
130 {
131     return (uint16) (points.size());
132 }
133 
addVertex(Point3f p)134 void M3DTriangleMesh::addVertex(Point3f p)
135 {
136     points.insert(points.end(), p);
137 }
138 
getTexCoord(uint16 n) const139 Point2f M3DTriangleMesh::getTexCoord(uint16 n) const
140 {
141     return texCoords[n];
142 }
143 
getTexCoordCount() const144 uint16 M3DTriangleMesh::getTexCoordCount() const
145 {
146     return (uint16) (texCoords.size());
147 }
148 
addTexCoord(Point2f p)149 void M3DTriangleMesh::addTexCoord(Point2f p)
150 {
151     texCoords.insert(texCoords.end(), p);
152 }
153 
getFace(uint16 n,uint16 & v0,uint16 & v1,uint16 & v2) const154 void M3DTriangleMesh::getFace(uint16 n, uint16& v0, uint16& v1, uint16& v2) const
155 {
156     int m = (int) n * 3;
157     v0 = faces[m];
158     v1 = faces[m + 1];
159     v2 = faces[m + 2];
160 }
161 
getFaceCount() const162 uint16 M3DTriangleMesh::getFaceCount() const
163 {
164     return (uint16) (faces.size() / 3);
165 }
166 
addFace(uint16 v0,uint16 v1,uint16 v2)167 void M3DTriangleMesh::addFace(uint16 v0, uint16 v1, uint16 v2)
168 {
169     faces.insert(faces.end(), v0);
170     faces.insert(faces.end(), v1);
171     faces.insert(faces.end(), v2);
172 }
173 
getSmoothingGroups(uint16 face) const174 uint32 M3DTriangleMesh::getSmoothingGroups(uint16 face) const
175 {
176     if (face < smoothingGroups.size())
177         return smoothingGroups[face];
178     else
179         return 0;
180 }
181 
addSmoothingGroups(uint32 smGroups)182 void M3DTriangleMesh::addSmoothingGroups(uint32 smGroups)
183 {
184     smoothingGroups.insert(smoothingGroups.end(), smGroups);
185 }
186 
getSmoothingGroupCount() const187 uint16 M3DTriangleMesh::getSmoothingGroupCount() const
188 {
189     return (uint16) (smoothingGroups.size());
190 }
191 
getMaterialName() const192 string M3DTriangleMesh::getMaterialName() const
193 {
194     return materialName;
195 }
196 
setMaterialName(string _materialName)197 void M3DTriangleMesh::setMaterialName(string _materialName)
198 {
199     materialName = _materialName;
200 }
201 
202 
203 
M3DModel()204 M3DModel::M3DModel()
205 {
206 }
207 
~M3DModel()208 M3DModel::~M3DModel()
209 {
210     for (unsigned int i = 0; i < triMeshes.size(); i++)
211         if (triMeshes[i] != NULL)
212             delete triMeshes[i];
213 }
214 
getTriMesh(uint32 n)215 M3DTriangleMesh* M3DModel::getTriMesh(uint32 n)
216 {
217     if (n < triMeshes.size())
218         return triMeshes[n];
219     else
220         return NULL;
221 }
222 
getTriMeshCount()223 uint32 M3DModel::getTriMeshCount()
224 {
225     return triMeshes.size();
226 }
227 
addTriMesh(M3DTriangleMesh * triMesh)228 void M3DModel::addTriMesh(M3DTriangleMesh* triMesh)
229 {
230     triMeshes.insert(triMeshes.end(), triMesh);
231 }
232 
setName(const string _name)233 void M3DModel::setName(const string _name)
234 {
235     name = _name;
236 }
237 
getName() const238 const string M3DModel::getName() const
239 {
240     return name;
241 }
242 
243 
M3DScene()244 M3DScene::M3DScene()
245 {
246 }
247 
~M3DScene()248 M3DScene::~M3DScene()
249 {
250     unsigned int i;
251     for (i = 0; i < models.size(); i++)
252         if (models[i] != NULL)
253             delete models[i];
254     for (i = 0; i < materials.size(); i++)
255         if (materials[i] != NULL)
256             delete materials[i];
257 }
258 
getModel(uint32 n) const259 M3DModel* M3DScene::getModel(uint32 n) const
260 {
261     if (n < models.size())
262         return models[n];
263     else
264         return NULL;
265 }
266 
getModelCount() const267 uint32 M3DScene::getModelCount() const
268 {
269     return models.size();
270 }
271 
addModel(M3DModel * model)272 void M3DScene::addModel(M3DModel* model)
273 {
274     models.insert(models.end(), model);
275 }
276 
getMaterial(uint32 n) const277 M3DMaterial* M3DScene::getMaterial(uint32 n) const
278 {
279     if (n < materials.size())
280         return materials[n];
281     else
282         return NULL;
283 }
284 
getMaterialCount() const285 uint32 M3DScene::getMaterialCount() const
286 {
287     return materials.size();
288 }
289 
addMaterial(M3DMaterial * material)290 void M3DScene::addMaterial(M3DMaterial* material)
291 {
292     materials.insert(materials.end(), material);
293 }
294 
getBackgroundColor() const295 M3DColor M3DScene::getBackgroundColor() const
296 {
297     return backgroundColor;
298 }
299 
setBackgroundColor(M3DColor color)300 void M3DScene::setBackgroundColor(M3DColor color)
301 {
302     backgroundColor = color;
303 }
304