-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExporter.h
More file actions
385 lines (351 loc) · 9.91 KB
/
Exporter.h
File metadata and controls
385 lines (351 loc) · 9.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#ifndef _EXPORTER_H
#define _EXPORTER_H
#include <fstream>
#include <iostream>
#include <vector>
#include "Point3d.h"
#include "Face.h"
using namespace std;
// OBJ Model Exporter
class Exporter {
Parameters *params;
ofstream file;
vector<Point3d> v;//vertex coordinates
vector<Point2d> vt;//texture coordinates
vector<Face> faces;
int vOffset;
int vtOffset;
void writeVertices() {
for(unsigned int i=0; i<v.size(); i++) {
if(params->xp->invert)
v[i].rotate90x();
file<<"v "<<v[i].x<<" "<<v[i].y<<" "<<v[i].z<<endl;
}
}
void writeTextures() {
for(unsigned int i=0; i<vt.size(); i++)
file<<"vt "<<vt[i].x<<" "<<vt[i].y<<endl;
}
void writeFaces() {
//#define OBJGEOMETRYONLY
for(unsigned int i=0; i<faces.size(); i++) {
file<<"f ";
for(unsigned int j=0; j<3; j++)
{
// assert(faces[i].vt[j]<=4);
#ifdef OBJGEOMETRYONLY
file<<faces[i].v[j]<<" ";
#else
file<<faces[i].v[j]<<"/"<<faces[i].vt[j]<<" ";
#endif
}
file<<endl;
}
}
int findPoint(Point3d *a) { //O(1)
int found=0;
unsigned int prevmax=params->tp->circlePoints*4;
unsigned int start=MAX(v.size()-prevmax,0);
for(unsigned int i=start; i<v.size(); i++)
if(a->x==v[i].x&&a->y==v[i].y&&a->z==v[i].z)
{
found=i+1;//zapisz id znalezionego powiekszone o 1 bo .obj numeruje od 1
break;
}
return found;
}
int findTexture(Point2d *a) {
int found=0;
if(abs(0-a->x)<0.01) a->x=0;
if(abs(0-a->y)<0.01) a->y=0;
if(abs(1-a->x)<0.01) a->x=1;
if(abs(1-a->y)<0.01) a->y=1;
for(unsigned int i=0; i<vt.size(); i++)
if(a->x==vt[i].x&&a->y==vt[i].y) {
found=i+1;
break;
}
return found;
}
//Dodaje trojkat do listy faces bez normalnych i wsp. tekstury
void addTriangleVertex(Point3d &a,Point3d &b,Point3d &c,Face &f) {
//dla kazdego pkt trojkata sprawdz czy istnieje juz na liscie v
//bo obj nie lubi powtorzen wierzcholkow
Point3d* p[3]= {&a,&b,&c};
int ids[3];
for(unsigned int i=0; i<3; i++)
ids[i]=findPoint(p[i]);
for(unsigned int i=0; i<3; i++) {
if(ids[i]==0) { //not found
switch(i) {
case 0:
v.push_back(a);
break;
case 1:
v.push_back(b);
break;
case 2:
v.push_back(c);
break;
}
f.v[i]=v.size()+vOffset;
} else f.v[i]=ids[i]+vOffset;
}
}
void addTriangleTexture(Point2d &at,Point2d &bt,Point2d &ct,Face &f) {
Point2d *p[3]= {&at,&bt,&ct};
int ids[3];
for(unsigned int i=0; i<3; i++)
ids[i]=findTexture(p[i]);
for(unsigned int i=0; i<3; i++) {
if(ids[i]==0) { //not found
switch(i) {
case 0:
vt.push_back(at);
break;
case 1:
vt.push_back(bt);
break;
case 2:
vt.push_back(ct);
break;
}
f.vt[i]=vt.size()+vtOffset;
} else f.vt[i]=ids[i]+vtOffset;
}
}
void addTriangle(Point3d a,Point3d b,Point3d c,Point2d at,Point2d bt,Point2d ct) {
Face f;
addTriangleVertex(a,b,c,f);
addTriangleTexture(at,bt,ct,f);
faces.push_back(f);
}
void exportBranch(BranchModel *bm)
{
TrunkParameters *tp=params->tp;
float kx=tp->kx;//3;//ile razy ma sie owinac tekstura
float ky=tp->ky;//0.5;//ile jednostek tekstury na 1 jednostke dlugosci pnia
int nodeModelListLen = bm->nodeModelList.size();
static vector<Point2d> prevCoords[2];
prevCoords[0].clear();
prevCoords[1].clear();
// i - numer okregu w galezi
for(int i=0; i<nodeModelListLen-1; i++)
{
NodeModel *root= bm->nodeModelList.at(i);
NodeModel *child = bm->nodeModelList.at(i+1);
int index = child->segment->index;
// Obliczanie wspolrzednych tekstury na pniu
if(i==0) {
float dx=root->segment->circlePts[0]->getDistance(root->segment->circlePts[1]);
float D=tp->circlePoints*dx;//oblicz obwod pnia
for(int k=0; k<=tp->circlePoints; k++) {
float txcoord=(k)*dx*kx/D;
Point2d tcoord(txcoord,0);
prevCoords[1].push_back(tcoord);
}
}
prevCoords[i&1].clear();
float dx=child->segment->circlePts[0]->getDistance(child->segment->circlePts[1]);
float D=tp->circlePoints*dx;
for(int k=0; k<=tp->circlePoints; k++) {
float txcoord=(k*dx)*kx/D;
float tycoord=ky*(i+1);
Point2d tcoord(txcoord,tycoord);
prevCoords[i&1].push_back(tcoord);
}
#define GV(point,tpoint) point.x+tpoint.x,point.y+tpoint.y,point.z+tpoint.z
for (int i0 = 0; i0 < tp->circlePoints; i0++) {
int j0 = (index + i0) % tp->circlePoints;
int i1 = (i0+1)%tp->circlePoints;
int j1 = (j0+1)%tp->circlePoints;
int tj1= (j0+1);
int ti1= (i0+1);
Point3d rootAbs, childAbs;
rootAbs = bm->getAbsoluteNodePosition(root);
childAbs = bm->getAbsoluteNodePosition(child);
Point2d at(prevCoords[!(i&1)][i0].x,prevCoords[!(i&1)][i0].y);
//Point2d at(0,0);
Point3d a(GV(rootAbs, (*root->segment->circlePts[i0]) ));
Point2d bt(prevCoords[i&1][j0].x,prevCoords[i&1][j0].y);
//Point2d bt(0,1);
Point3d b(GV(childAbs, (*child->segment->circlePts[j0])));
Point2d ct(prevCoords[i&1][tj1].x,prevCoords[i&1][tj1].y);
//Point2d ct(1,1);
Point3d c(GV(childAbs, (*child->segment->circlePts[j1])));
//addTriangle(a,b,c);
addTriangle(a,b,c,at,bt,ct);
Point2d dt(prevCoords[!(i&1)][ti1].x,prevCoords[!(i&1)][ti1].y);
//Point2d dt(1,0);
Point3d d(GV(rootAbs, (*root->segment->circlePts[i1])));
Point2d et(prevCoords[(i&1)][tj1].x,prevCoords[(i&1)][tj1].y);
//Point2d et(1,1);
Point3d e(GV(childAbs, (*child->segment->circlePts[j1])));
Point2d ft(prevCoords[!(i&1)][i0].x,prevCoords[!(i&1)][i0].y);
//Point2d ft(0,0);
Point3d f(GV(rootAbs, (*root->segment->circlePts[i0])));
//addTriangle(d,e,f);
addTriangle(d,e,f,dt,et,ft);
}
#undef GV
}
}
void exportLeaf(Point3d *p, Vector3d *dir, Vector3d *norm,Parameters *params,unsigned int cnt) {
unsigned int lastTypeId=cnt;
//float size=params->lp->leaves[0].leafSize;
if(params->lp->types.size()==0) return;
unsigned int type=params->lp->types[lastTypeId].first;
//float size=params->lp->types[lastTypeId].second;
Point2d petiole = params->lp->leaves[type].petiole;
Vector3d a, b, *tmp1, *tmp2;
tmp1 = dir->crossProduct(norm);
tmp1->normalize();
tmp1->mul(petiole.x);
tmp2 = dir->crossProduct(norm);
tmp2->normalize();
tmp2->mul(petiole.x-1);
a.add(dir);
a.add(tmp1);
b.add(dir);
b.add(tmp2);
#define FT(va,s,t,u) Point3d va(p->x+s,p->y+t,p->z+u);
#define TT(va,u,v) Point2d va(u,v);
TT(at,petiole.x, 1.0f);
FT(av,0,0,0);
TT(bt,1.0f, 0.0f);
FT(bv,a.d[0],a.d[1],a.d[2]);
TT(ct,0.0f, 0.0f);
FT(cv,b.d[0],b.d[1],b.d[2]);
TT(dt,petiole.x, 1.0f);
FT(dv,0,0,0);
TT(et,0.0f, 0.0f);
FT(ev,b.d[0],b.d[1],b.d[2]);
TT(ft,0.0f, 1.0f);
FT(fv,tmp2->d[0],tmp2->d[1],tmp2->d[2]);
TT(gt,petiole.x, 1.0f);
FT(gv,0,0,0);
TT(ht,1.0f, 1.0f);
FT(hv,tmp1->d[0],tmp1->d[1],tmp1->d[2]);
TT(it,1.0f, 0.0f);
FT(iv,a.d[0],a.d[1],a.d[2]);
#undef FT
#undef TT
addTriangle(av,bv,cv,at,bt,ct);
addTriangle(dv,ev,fv,dt,et,ft);
addTriangle(gv,hv,iv,gt,ht,it);
}
void clean() {
v.clear();
vt.clear();
faces.clear();
}
void waitForGui() {
while (gtk_events_pending()) {
gtk_main_iteration_do(FALSE);
}
}
public:
void init(const char *filename,Parameters *params) {
this->params=params;
vtOffset=vOffset=0;
file.open(filename);
printf("saving...");
if(file.is_open()==false) {
printf("Exporter error file is not opened");
return;
}
file<<"# This model was generated by Treemaker O&O."<<endl;
file<<"mtllib tree0.mtl"<<endl;
//file<<"o Tree"<<endl;
}
void exportTrunk(Model3d *model)
{
this->params=params;
for(unsigned int i=0; i<model->branches.size(); i++) {
waitForGui();
exportBranch(model->branches[i]);
}
}
void exportLeaves(BranchModel *bm, unsigned int cnt)
{
if(!bm) return;
waitForGui();
for(unsigned int i=0; i<bm->nodeModelList.size(); i++)
if(bm->nodeModelList[i]->leaf)
{
NodeModel *node = bm->nodeModelList[i];
Point3d leafPoint = bm->getAbsoluteNodePosition(node);
exportLeaf(&leafPoint, node->leaf->dir, node->leaf->norm,params,cnt++);
if(cnt >= params->lp->types.size()) cnt=0;
}
for(unsigned int i = 0; i< bm->childBranches.size(); i++)
{
exportLeaves(bm->childBranches.at(i),cnt);
}
}
void exportMaterials() {
ofstream mtlfile;
string fname=params->xp->filename+".mtl";
mtlfile.open(fname.c_str());
mtlfile<<"# Leaves"<<endl;
for(unsigned int i=0; i<params->lp->leaves.size(); i++) {
char *name=strrchr(params->lp->leaves[i].leafTexPath,'/')+1;
if(name==(void*)1) name=params->lp->leaves[i].leafTexPath;
mtlfile<<"newmtl "<<name<<endl;
mtlfile<<"Ka 1.0 0.0 0.0"<<endl;
mtlfile<<"Ka 1.0 0.0 0.0"<<endl;
mtlfile<<"Kd 1.0000 0.0000 0.0000"<<endl;
mtlfile<<"Ks 1.0000 1.0000 1.0000"<<endl;
mtlfile<<"d 1"<<endl;
mtlfile<<"Ns 0"<<endl;
mtlfile<<"map_Kd "<<name<<endl;
mtlfile<<endl;
}
mtlfile<<"# Bark"<<endl;
char *name=strrchr(params->tp->barkPath,'/')+1;
if(name==(void*)1) name=params->tp->barkPath;
mtlfile<<"newmtl "<<name<<endl;
mtlfile<<"Ka 1.0 0.0 0.0"<<endl;
mtlfile<<"Ka 1.0 0.0 0.0"<<endl;
mtlfile<<"Kd 1.0000 0.0000 0.0000"<<endl;
mtlfile<<"Ks 1.0000 1.0000 1.0000"<<endl;
mtlfile<<"d 1"<<endl;
mtlfile<<"Ns 0"<<endl;
mtlfile<<"map_Kd "<<name<<endl;
mtlfile<<endl;
mtlfile.close();
}
void saveTrunk() {
waitForGui();
file<<"usemtl bark0"<<endl;
file<<"o Trunk"<<endl;
file<<"# List of Vertices, with (x,y,z) coordinates"<<endl;
writeVertices();
file<<"# Texture coordinates, with (x,y) coordinates"<<endl;
writeTextures();
file<<"# Face Definitions (v1-id v2-id v3-id)"<<endl;
writeFaces();
vOffset+=v.size();
vtOffset+=vt.size();
clean();
}
void saveLeaves() {
waitForGui();
file<<"usemtl leaf0"<<endl;
file<<"o Leaves"<<endl;
file<<"# List of Vertices, with (x,y,z) coordinates"<<endl;
writeVertices();
file<<"# Texture coordinates, with (x,y) coordinates"<<endl;
writeTextures();
file<<"# Face Definitions (v1-id v2-id v3-id)"<<endl;
writeFaces();
clean();
}
void close() {
file<<"# End of file"<<endl;
file.close();
clean();
printf("done\n");
}
};
#endif