goat3d
view src/mesh.cc @ 39:0e48907847ad
slugishly progressing with the blender exporter
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 09 Oct 2013 16:40:59 +0300 |
parents | b35427826b60 |
children | a5c5cec3cb88 |
line source
1 #include "goat3d_impl.h"
2 #include "mesh.h"
3 #include "openctm.h"
4 #include "log.h"
6 Mesh::Mesh()
7 {
8 material = 0;
9 }
11 bool Mesh::load(const char *fname)
12 {
13 CTMcontext ctm = ctmNewContext(CTM_IMPORT);
15 ctmLoad(ctm, fname);
16 if(ctmGetError(ctm) != CTM_NONE) {
17 logmsg(LOG_ERROR, "failed to load ctm mesh: %s\n", fname);
18 ctmFreeContext(ctm);
19 return false;
20 }
22 int vnum = ctmGetInteger(ctm, CTM_VERTEX_COUNT);
23 int fnum = ctmGetInteger(ctm, CTM_TRIANGLE_COUNT);
25 const CTMfloat *vertices = ctmGetFloatArray(ctm, CTM_VERTICES);
26 if(!vertices) {
27 logmsg(LOG_ERROR, "failed to load ctm mesh: %s: no vertices found!\n", fname);
28 ctmFreeContext(ctm);
29 return false;
30 }
32 const CTMuint *indices = ctmGetIntegerArray(ctm, CTM_INDICES);
33 if(!indices) {
34 logmsg(LOG_ERROR, "failed to load ctm mesh: %s: no faces found!\n", fname);
35 ctmFreeContext(ctm);
36 return false;
37 }
39 const CTMfloat *normals = ctmGetFloatArray(ctm, CTM_NORMALS);
40 const CTMfloat *texcoords = ctmGetFloatArray(ctm, CTM_UV_MAP_1);
42 CTMenum tangent_id = ctmGetNamedAttribMap(ctm, "tangent");
43 const CTMfloat *tangents = tangent_id ? ctmGetFloatArray(ctm, tangent_id) : 0;
45 CTMenum skinweight_id = ctmGetNamedAttribMap(ctm, "skin_weight");
46 const CTMfloat *skinweights = skinweight_id ? ctmGetFloatArray(ctm, skinweight_id) : 0;
48 CTMenum skinmat_id = ctmGetNamedAttribMap(ctm, "skin_matrix");
49 const CTMuint *skinmats = skinmat_id ? ctmGetIntegerArray(ctm, skinmat_id) : 0;
51 CTMenum color_id = ctmGetNamedAttribMap(ctm, "color");
52 const CTMfloat *colors = color_id ? ctmGetFloatArray(ctm, color_id) : 0;
54 // now put everything we found into our vectors
55 this->vertices = VECDATA(Vector3, vertices, vnum);
57 if(texcoords) {
58 this->texcoords = VECDATA(Vector2, texcoords, vnum);
59 }
60 if(normals) {
61 this->normals = VECDATA(Vector3, normals, vnum);
62 }
63 if(skinweights) {
64 this->skin_weights = VECDATA(Vector4, skinweights, vnum);
65 }
66 if(colors) {
67 this->colors = VECDATA(Vector4, colors, vnum);
68 }
70 // the rest need converting
71 if(tangents) {
72 this->tangents.clear();
73 this->tangents.resize(vnum);
75 for(int i=0; i<vnum; i++) {
76 for(int j=0; j<3; j++) {
77 this->tangents[i][j] = tangents[j];
78 }
79 tangents += 4;
80 }
81 }
82 if(skinmats) {
83 this->skin_matrices.clear();
84 this->skin_matrices.resize(vnum);
86 for(int i=0; i<vnum; i++) {
87 this->skin_matrices[i].x = skinmats[0];
88 this->skin_matrices[i].y = skinmats[1];
89 this->skin_matrices[i].z = skinmats[2];
90 this->skin_matrices[i].w = skinmats[3];
91 }
92 }
94 // grab the face data
95 this->faces.clear();
96 this->faces.resize(fnum);
98 for(int i=0; i<fnum; i++) {
99 for(int j=0; j<3; j++) {
100 this->faces[i].v[j] = indices[j];
101 }
102 indices += 3;
103 }
106 ctmFreeContext(ctm);
107 return true;
108 }
110 bool Mesh::save(const char *fname) const
111 {
112 int vnum = (int)vertices.size();
113 int fnum = (int)faces.size();
115 if(!vnum || !fnum) {
116 return false;
117 }
119 CTMcontext ctm = ctmNewContext(CTM_EXPORT);
121 // vertices, normals, and face-vertex indices
122 ctmDefineMesh(ctm, &vertices[0].x, vnum, (CTMuint*)faces[0].v, fnum,
123 normals.empty() ? 0 : &normals[0].x);
125 // texture coordinates
126 if(!texcoords.empty()) {
127 ctmAddUVMap(ctm, &texcoords[0].x, "texcoord", 0);
128 }
130 // vertex colors
131 if(!colors.empty()) {
132 ctmAddAttribMap(ctm, &colors[0].x, "color");
133 }
135 // skin weights
136 if(!skin_weights.empty()) {
137 ctmAddAttribMap(ctm, &skin_weights[0].x, "skin_weight");
138 }
140 // if either of the non-float4 attributes are present we need to make a tmp array
141 CTMfloat *attr_array = 0;
142 if(!tangents.empty() || !skin_matrices.empty()) {
143 attr_array = new CTMfloat[vnum * 4 * sizeof *attr_array];
144 }
146 // tangents
147 if(!tangents.empty()) {
148 CTMfloat *ptr = attr_array;
150 for(int i=0; i<vnum; i++) {
151 *ptr++ = tangents[i].x;
152 *ptr++ = tangents[i].y;
153 *ptr++ = tangents[i].z;
154 *ptr++ = 1.0;
155 }
156 ctmAddAttribMap(ctm, attr_array, "tangent");
157 }
159 // skin matrix indices (4 per vertex)
160 if(!skin_matrices.empty()) {
161 CTMfloat *ptr = attr_array;
163 for(int i=0; i<vnum; i++) {
164 *ptr++ = (float)skin_matrices[i].x;
165 *ptr++ = (float)skin_matrices[i].y;
166 *ptr++ = (float)skin_matrices[i].z;
167 *ptr++ = (float)skin_matrices[i].w;
168 }
169 ctmAddAttribMap(ctm, attr_array, "skin_matrix");
170 }
172 delete [] attr_array;
174 /* TODO find a way to specify the nodes participating in the skinning of this mesh
175 * probably in the comment field?
176 */
178 logmsg(LOG_INFO, "saving CTM mesh file: %s\n", fname);
179 ctmSave(ctm, fname);
181 ctmFreeContext(ctm);
182 return true;
183 }
185 void Mesh::set_material(Material *mat)
186 {
187 material = mat;
188 }
190 Material *Mesh::get_material()
191 {
192 return material;
193 }
195 const Material *Mesh::get_material() const
196 {
197 return material;
198 }