-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMesh.cpp
856 lines (748 loc) · 24.9 KB
/
Mesh.cpp
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
// @Begin License@
// This file is part of Coldest.
//
// Coldest is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Coldest is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Coldest. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2008-2012 Ben Nemec
// @End License@
#include "Mesh.h"
#include "util.h"
#include "ProceduralTree.h"
Mesh::Mesh(NTreeReader reader, ResourceManager& rm) : render(true), dynamic(true), collide(true), terrain(false), reverseanim(false), dist(0.f), impdist(0.f),
impostorfbo(0), updatedelay(0), drawdistmult(1.f), meshdata(rm),
next(0), size(0.f), height(0.f), width(0.f), scale(1.f), trismoved(false),
trischanged(true), boundschanged(true), updatevbo(true), animtime(0), currkeyframe(0), animspeed(1.f), curranimation(0), nextanimation(0)
{
ImpTimer.start();
animtimer.start();
Load(reader);
}
void Mesh::Load(const NTreeReader& reader)
{
string material;
string type("");
reader.Read(type, "Type");
reader.Read(name, "Name");
reader.Read(position.x, "Position", 0);
reader.Read(position.y, "Position", 1);
reader.Read(position.z, "Position", 2);
reader.Read(rotation.x, "Rotations", 0);
reader.Read(rotation.y, "Rotations", 1);
reader.Read(rotation.z, "Rotations", 2);
reader.Read(size, "Size");
reader.Read(impdist, "ImpostorDistance");
reader.Read(scale, "Scale");
reader.Read(animspeed, "AnimSpeed");
if (type == "External")
{
string currfile, basepath;
numframes = intvec(10, 0);
startframe = intvec(10, 0);
reader.Read(basefile, "BaseFile");
int numkeyframes = 0;
if (basefile != "")
{
basepath = basefile;
NTreeReader base(basefile);
for (size_t i = 0; i < 10; ++i)
{
base.Read(numframes[i], "NumFrames", i);
startframe[i] = numkeyframes;
numkeyframes += numframes[i];
if (numframes[i] == 0)
break;
}
}
else
{
basepath = reader.GetPath();
basefile = basepath;
for (size_t i = 0; i < 10; ++i)
{
reader.Read(numframes[i], "NumFrames", i);
startframe[i] = numkeyframes;
numkeyframes += numframes[i];
if (numframes[i] == 0)
break;
}
}
basepath = basepath.substr(0, basepath.length() - 5);
//logout << "Loading " << basepath << endl;
map<size_t, size_t> vertmap;
for (int i = 0; i < numkeyframes; ++i)
{
currfile = basepath + "/frame" + PadNum(i, 4);
NTreeReader currframe(currfile);
string currver("");
currframe.Read(currver, "Version");
if (currver != objectfilever)
{
logout << "Object file version mismatch for file: " << currfile << endl;
logout << currver << endl;
return;
}
int currft = 0;
currframe.Read(currft, "TimeToNextFrame");
frametime.push_back(currft);
string currsound;
currframe.Read(currsound, "Sound");
framesound.push_back(currsound);
MeshNodeMap nodes;
for (int j = 0; j < currframe.GetItemIndex("Triangles"); ++j)
{
NTreeReader currcon = currframe.GetItem(j);
MeshNodePtr newnode(new MeshNode());
currcon.Read(newnode->id, "ID");
currcon.Read(newnode->parentid, "ParentID");
currcon.Read(newnode->rot1.x, "Rot1", 0);
currcon.Read(newnode->rot1.y, "Rot1", 1);
currcon.Read(newnode->rot1.z, "Rot1", 2);
currcon.Read(newnode->rot2.x, "Rot2", 0);
currcon.Read(newnode->rot2.y, "Rot2", 1);
currcon.Read(newnode->rot2.z, "Rot2", 2);
currcon.Read(newnode->trans.x, "Trans", 0);
currcon.Read(newnode->trans.y, "Trans", 1);
currcon.Read(newnode->trans.z, "Trans", 2);
newnode->trans *= scale;
// Read vertices
for (size_t k = 0; k < currcon.NumChildren(); ++k)
{
const NTreeReader& currvert = currcon(k);
Vertex newv;
currvert.Read(newv.id, "ID");
currvert.Read(newv.pos.x, "Pos", 0);
currvert.Read(newv.pos.y, "Pos", 1);
currvert.Read(newv.pos.z, "Pos", 2);
currvert.Read(newv.norm.x, "Norm", 0);
currvert.Read(newv.norm.y, "Norm", 1);
currvert.Read(newv.norm.z, "Norm", 2);
newv.pos *= scale;
for (int m = 0; m < 8; ++m)
{
currvert.Read(newv.texcoords[m][0], "TC", m * 2);
currvert.Read(newv.texcoords[m][1], "TC", m * 2 + 1);
}
int intermediate; // These are uchars, so they will only read a single digit if read directly
for (size_t m = 0; m < 4; ++m)
{
intermediate = newv.color[m];
currvert.Read(intermediate, "Color", m);
newv.color[m] = intermediate;
}
newnode->vertices.push_back(newv);
if (!i)
{
meshdata.vertices.push_back(VertexPtr(new Vertex(newv)));
vertmap[newv.id] = meshdata.vertices.size() - 1;
meshdata.vertices.back()->id = meshdata.vertices.size() - 1;
}
newnode->vertices.back().id = vertmap[newv.id];
}
currcon.Read(newnode->facing, "Facing");
currcon.Read(newnode->name, "Name");
nodes[newnode->id] = newnode;
}
// Now that the nodes are loaded, rebuild the tree to get their proper positions
MeshNodeMap::iterator it;
for (it = nodes.begin(); it != nodes.end(); ++it)
{
if (it->second->parentid != -1)
{
if (nodes.find(it->second->parentid) != nodes.end())
{
nodes[it->second->parentid]->children.push_back(it->second);
}
else
{
logout << "Error building tree for: " << currfile << endl;
}
}
else
{
meshdata.frameroot.push_back(it->second);
}
}
// Load triangles
if (!i)
{
string matname;
size_t vid;
const NTreeReader& readtris = currframe.GetItemByName("Triangles");
for (size_t j = 0; j < readtris.NumChildren(); ++j)
{
const NTreeReader& curr = readtris(j);
TrianglePtr newtri(new Triangle());
for (int k = 0; k < 3; ++k)
{
curr.Read(vid, "Verts", k);
newtri->v[k] = meshdata.vertices[vertmap[vid]];
string tempid;
curr.Read(tempid, "ID");
}
curr.Read(newtri->matname, "Material");
curr.Read(newtri->collide, "Collide");
curr.Read(newtri->id, "ID");
meshdata.AddTriangle(*newtri);
}
}
// Generate tangents for triangles
size_t currf = meshdata.frameroot.size() - 1;
GraphicMatrix m;
meshdata.frameroot[currf]->Transform(meshdata.frameroot[currf], 0.f, meshdata.vertices, m, Vector3());
for (size_t k = 0; k < meshdata.NumTris(); ++k)
{
Triangle& newtri = meshdata.Tri(k);
Vector3 one = newtri.v[1]->pos - newtri.v[0]->pos;
Vector3 two = newtri.v[2]->pos - newtri.v[0]->pos;
float tcone = newtri.v[1]->texcoords[0][1] - newtri.v[0]->texcoords[0][1];
float tctwo = newtri.v[2]->texcoords[0][1] - newtri.v[0]->texcoords[0][1];
Vector3 tangent = one * -tctwo + two * tcone;
for (size_t j = 0; j < 3; ++j)
newtri.v[j]->tangent += tangent;
// Apply tangents to nodes' vertices
for (size_t j = 0; j < 3; ++j)
{
newtri.v[j]->tangent.normalize();
meshdata.frameroot[currf]->SetTangent(newtri.v[j]->id, newtri.v[j]->tangent);
}
}
}
}
else if (type == "proctree")
{
ProceduralTree t;
string barkmat, leafmat;
t.ReadParams(reader);
reader.Read(barkmat, "Materials", 0);
reader.Read(leafmat, "Materials", 1);
size_t save = t.GenTree(this, &meshdata.resman.LoadMaterial(barkmat), &meshdata.resman.LoadMaterial(leafmat));
collide = true;
logout << "Tree primitives: " << save << endl;
}
else if (type == "Terrain" || type == "Empty"){} // No-op to avoid bogus warnings
else
{
logout << "Warning: Attempted to load unknown object type " << type;
logout << " from file " << reader.GetPath() << endl;
}
Update();
}
void Mesh::Move(const Vector3& v, const bool movetris)
{
if (movetris)
{
Vector3 move = v - position;
for (VertexPtrvec::iterator i = meshdata.vertices.begin(); i != meshdata.vertices.end(); ++i)
{
(*i)->pos += move;
}
updatevbo = true;
}
ResetTriMaxDims();
position = v;
trismoved = true;
}
void Mesh::Rotate(const Vector3& v, const bool movetris)
{
if (movetris)
{
Vector3 pos = GetPosition();
GraphicMatrix m;
m.translate(-pos);
m.rotatez(-rotation.z);
m.rotatey(-rotation.y);
m.rotatex(-rotation.x);
m.rotatex(v.x);
m.rotatey(v.y);
m.rotatez(v.z);
m.translate(pos);
for (VertexPtrvec::iterator i = meshdata.vertices.begin(); i != meshdata.vertices.end(); ++i)
{
(*i)->pos.transform(m);
}
updatevbo = true;
}
ResetTriMaxDims();
rotation = v;
trismoved = true;
boundschanged = true;
}
void Mesh::Scale(const float& sval)
{
for (size_t i = 0; i < meshdata.frameroot.size(); ++i)
meshdata.frameroot[i]->Scale(sval);
ResetTriMaxDims();
scale *= sval;
trismoved = true;
boundschanged = true;
}
void Mesh::ScaleZ(const float& sval)
{
for (size_t i = 0; i < meshdata.frameroot.size(); ++i)
meshdata.frameroot[i]->ScaleZ(sval);
ResetTriMaxDims();
trismoved = true;
boundschanged = true;
}
// Most of what goes on in Update should maybe be moved to MeshData, but for the moment I'm leaving it alone.
// Something to think about in the future though.
void Mesh::Update(const Vector3& campos, bool noanimation)
{
if (!noanimation)
AdvanceAnimation();
if (trismoved || trischanged)
UpdateTris(campos);
if (boundschanged)
CalcBounds();
}
void Mesh::AdvanceAnimation()
{
if (meshdata.frameroot.size() < 2)
return;
animtime += static_cast<int>(fabs(animspeed) * static_cast<float>(animtimer.elapsed()));
animtimer.start();
while (animtime > frametime[currkeyframe])
{
animtime -= frametime[currkeyframe];
currkeyframe = NextKeyFrame();
if (framesound[currkeyframe] != "")
currentsound = framesound[currkeyframe];
}
trismoved = true;
boundschanged = true;
}
int Mesh::NextKeyFrame()
{
int retval = currkeyframe;
if (curranimation != nextanimation)
{
curranimation = nextanimation;
return startframe[curranimation];
}
else
{
if (reverseanim)
--retval;
else
++retval;
}
if (reverseanim)
{
if (retval < startframe[curranimation])
retval = startframe[curranimation] + numframes[curranimation] - 1;
}
else
{
if (retval > startframe[curranimation] + numframes[curranimation] - 1)
retval = startframe[curranimation];
}
return retval;
}
void Mesh::UpdateTris(const Vector3& campos)
{
if (!meshdata.frameroot.size() || (currkeyframe < startframe[curranimation] || currkeyframe >= startframe[curranimation] + numframes[curranimation]))
return;
float interpval;
if (frametime.size() > 0)
interpval = float(animtime) / float(frametime[currkeyframe]);
else
interpval = 0.f;
GraphicMatrix m;
m.rotatex(rotation.x);
m.rotatey(rotation.y);
m.rotatez(rotation.z);
m.translate(position);
if (curranimation != nextanimation)
{
meshdata.frameroot[currkeyframe]->Transform(meshdata.frameroot[startframe[nextanimation]], interpval, meshdata.vertices, m, campos);
}
else
{
if (numframes[curranimation] == 1) // Unanimated meshes
{
meshdata.frameroot[currkeyframe]->Transform(meshdata.frameroot[currkeyframe], interpval, meshdata.vertices, m, campos);
}
else if (reverseanim)
{
meshdata.frameroot[currkeyframe]->Transform(meshdata.frameroot[NextKeyFrame()], interpval, meshdata.vertices, m, campos);
}
else
{
meshdata.frameroot[currkeyframe]->Transform(meshdata.frameroot[NextKeyFrame()], interpval, meshdata.vertices, m, campos);
}
}
trismoved = false;
updatevbo = true; // Let the render code know that what it has is out of date
}
// Note that UpdateTris needs to be called before this so we're not working with old tris
void Mesh::CalcBounds()
{
// If we just translated the mesh then we don't need to do this.
size = 0.f;
float dist = 0.f;
float temp;
Vector3 localpos = GetPosition();
Vector3 localwpos, localhpos;
size_t tsize = meshdata.NumTris();
height = 0;
width = 0;
for (size_t i = 0; i < tsize; ++i)
{
for (int j = 0; j < 3; ++j)
{
Triangle& currtri = meshdata.Tri(i);
dist = currtri.v[j]->pos.distance(localpos) + currtri.radmod;
if (dist > size) size = dist;
localwpos = currtri.v[j]->pos;
localwpos.y = localpos.y;
temp = localwpos.distance(localpos) + currtri.radmod;
if (temp > width) width = temp;
localhpos = currtri.v[j]->pos;
localhpos.x = localpos.x;
localhpos.z = localpos.z;
temp = localhpos.distance(localpos) + currtri.radmod;
if (temp > height) height = temp;
}
}
height *= 2.f;
width *= 2.f;
boundschanged = false;
}
// Note: This needs to be done even if tris just moved without changing size because this also triggers
// the collision detection code to regenerate the midpoint of the tri for culling calculations
void Mesh::ResetTriMaxDims()
{
size_t s = meshdata.NumTris();
for (size_t i = 0; i < s; ++i)
meshdata.Tri(i).maxdim = -1.f;
}
// Passing in overridemat causes this function to use every attribute of that material
// except the texture in unit 0. This is primarily useful for shadowmap renders where
// we don't care about anything but the right shape so we use a very basic material
void Mesh::Render(Material* overridemat)
{
#ifndef DEDICATED
if (!render || !meshdata.NumTris())
return;
EnsureMaterials();
if (updatevbo)
GenVbo();
vbo.Bind();
size_t currindex = 0;
if (overridemat)
overridemat->Use();
for (size_t i = 0; i < vbosteps.size(); ++i)
{
if (meshdata.Tri(currindex).material)
{
if (!overridemat)
meshdata.Tri(currindex).material->Use();
else meshdata.Tri(currindex).material->UseTextureOnly();
}
else
{
logout << "NULL material? " << &meshdata.Tri(currindex) << " " << vbosteps[i] << endl;
}
BindAttribs();
void* offset = offsets[i];
glDrawRangeElements(GL_TRIANGLES, minindex[i], maxindex[i], vbosteps[i] * 3, GL_UNSIGNED_SHORT, offset);
UnbindAttribs();
currindex += vbosteps[i];
}
#endif
}
void Mesh::GenVbo()
{
#ifndef DEDICATED
if (meshdata.NumTris())
{
GenVboData();
if (trischanged)
GenIboData();
vbo.UploadVBO(dynamic || meshdata.frameroot.size() > 1);
}
updatevbo = false;
#endif
}
void Mesh::GenVboData()
{
#ifndef DEDICATED
size_t currindex = 0;
size_t numverts = meshdata.vertices.size();
if (vbo.vbodata.size() != numverts)
{
vbo.vbodata.resize(numverts);
}
VertexPtrvec::iterator vend = meshdata.vertices.end();
// Build VBO
for (VertexPtrvec::iterator i = meshdata.vertices.begin(); i != vend; ++i)
{
Vertex& currv = **i;
if (currv.index != currindex)
{
currv.index = currindex;
trischanged = true;
}
currv.GetVboData(vbo.vbodata[currindex], dynamic);
++currindex;
}
#endif
}
void Mesh::GenIboData()
{
#ifndef DEDICATED
// Build IBO
meshdata.SortTris();
vbo.indexdata.clear();
vbosteps.clear();
offsets.clear();
minindex.clear();
maxindex.clear();
int counter = 0;
unsigned short currmin = 0, currmax = 0;
size_t last = 0;
size_t tend = meshdata.NumTris();
offsets.push_back((void*)0);
for (size_t i = 0; i < tend; ++i)
{
if (meshdata.Tri(last).material != meshdata.Tri(i).material)
{
vbosteps.push_back(counter);
ptrdiff_t current = ptrdiff_t(&vbo.indexdata[vbo.indexdata.size() - 1]) + ptrdiff_t(sizeof(vbo.indexdata[0]));
ptrdiff_t start = ptrdiff_t(&vbo.indexdata[0]);
offsets.push_back( (void*)(current - start) );
minindex.push_back(currmin);
maxindex.push_back(currmax);
counter = 0;
currmin = 0;
currmax = 0;
}
ushortvec ind = meshdata.Tri(i).GetIndices();
vbo.indexdata.insert(vbo.indexdata.end(), ind.begin(), ind.end());
last = i;
++counter;
for (size_t j = 0; j < ind.size(); ++j)
{
currmin = std::min(currmin, ind[j]);
currmax = std::max(currmax, ind[j]);
}
}
vbosteps.push_back(counter);
minindex.push_back(currmin);
maxindex.push_back(currmax);
trischanged = false;
#endif
}
void Mesh::BindAttribs()
{
#ifndef DEDICATED
if (meshdata.resman.shaderman.CurrentShader() == 0) return;
VBOData dummy;
int location;
location = meshdata.resman.shaderman.GetAttribLocation(meshdata.resman.shaderman.CurrentShader(), "terrainwt");
if (location >= 0)
{
glEnableVertexAttribArrayARB(location);
glVertexAttribPointerARB(location, 3, GL_FLOAT, GL_FALSE, sizeof(VBOData), (void*)((ptrdiff_t)&(dummy.terrainwt[0]) - (ptrdiff_t)&dummy));
}
location = meshdata.resman.shaderman.GetAttribLocation(meshdata.resman.shaderman.CurrentShader(), "terrainwt1");
if (location >= 0)
{
glEnableVertexAttribArrayARB(location);
glVertexAttribPointerARB(location, 3, GL_FLOAT, GL_FALSE, sizeof(VBOData), (void*)((ptrdiff_t)&(dummy.terrainwt1[0]) - (ptrdiff_t)&dummy));
}
location = meshdata.resman.shaderman.GetAttribLocation(meshdata.resman.shaderman.CurrentShader(), "tangent");
if (location >= 0)
{
glEnableVertexAttribArrayARB(location);
glVertexAttribPointerARB(location, 3, GL_FLOAT, GL_FALSE, sizeof(VBOData), (void*)((ptrdiff_t)&(dummy.tx) - (ptrdiff_t)&dummy));
}
#endif
}
void Mesh::UnbindAttribs()
{
#ifndef DEDICATED
if (meshdata.resman.shaderman.CurrentShader() == 0) return;
int location = meshdata.resman.shaderman.GetAttribLocation(meshdata.resman.shaderman.CurrentShader(), "terrainwt");
if (location >= 0)
{
glDisableVertexAttribArrayARB(location);
}
location = meshdata.resman.shaderman.GetAttribLocation(meshdata.resman.shaderman.CurrentShader(), "terrainwt1");
if (location >= 0)
{
glDisableVertexAttribArrayARB(location);
}
location = meshdata.resman.shaderman.GetAttribLocation(meshdata.resman.shaderman.CurrentShader(), "tangent");
if (location >= 0)
{
glDisableVertexAttribArrayARB(location);
}
#endif
}
void Mesh::RenderImpostor(Mesh& rendermesh, FBO& impfbo, const Vector3& campos)
{
#ifndef DEDICATED
if (!meshdata.impmat)
{
meshdata.impmat = MaterialPtr(new Material("materials/impostor", meshdata.resman.texman, meshdata.resman.shaderman));
}
if (!meshdata.impostor)
{
meshdata.impostor = MeshPtr(new Mesh(NTreeReader("models/impostor/base"), meshdata.resman));
Triangle& first = meshdata.impostor->meshdata.Tri(0);
Triangle& second = meshdata.impostor->meshdata.Tri(1);
first.material = meshdata.impmat.get();
second.material = meshdata.impmat.get();
}
meshdata.impmat->SetTexture(0, impfbo.GetTexture());
float width2 = width / 2.f;
float height2 = height / 2.f;
meshdata.impostor->meshdata.frameroot[0]->vertices[0].pos = Vector3(-width2, height2, 0);
meshdata.impostor->meshdata.frameroot[0]->vertices[1].pos = Vector3(-width2, -height2, 0);
meshdata.impostor->meshdata.frameroot[0]->vertices[2].pos = Vector3(width2, height2, 0);
meshdata.impostor->meshdata.frameroot[0]->vertices[3].pos = Vector3(width2, -height2, 0);
Vector3 moveto = position;
meshdata.impostor->Move(moveto);
meshdata.impostor->Update(campos);
rendermesh.Add(*meshdata.impostor);
#endif
}
void Mesh::Add(Triangle& triangle)
{
meshdata.AddTriangle(triangle);
AddVertices(meshdata.Tri(meshdata.NumTris() - 1));
}
void Mesh::Add(TrianglePtr triangle)
{
meshdata.AddTriangle(triangle);
AddVertices(meshdata.Tri(meshdata.NumTris() - 1));
}
void Mesh::AddVertices(Triangle& triangle)
{
for (size_t i = 0; i < 3; ++i)
{
if (std::find(meshdata.vertices.begin(), meshdata.vertices.end(), triangle.v[i]) == meshdata.vertices.end())
{
meshdata.vertices.push_back(triangle.v[i]);
triangle.v[i]->id = meshdata.vertices.size() - 1;
}
}
boundschanged = true;
trischanged = true;
updatevbo = true;
}
void Mesh::Add(Quad& quad)
{
Add(*quad.First());
Add(*quad.Second());
}
void Mesh::AddNoCopy(Quad& quad)
{
Add(quad.First());
Add(quad.Second());
}
void Mesh::Add(Mesh &mesh)
{
for (size_t i = 0; i < mesh.meshdata.NumTris(); ++i)
{
// It's likely that it would be better to add pointers here, but at the moment there's no way to do that
Add(mesh.meshdata.Tri(i));
}
}
void Mesh::Clear(const bool cleartris)
{
// Not clearing the tris allows the GUI to update triangles directly through pointers and signal the mesh that they were changed
if (cleartris)
{
meshdata.ClearTris();
meshdata.vertices.resize(0);
trischanged = true;
boundschanged = true;
}
updatevbo = true;
}
// Only for meshes that are built out of raw triangles - meshes loaded from a file should already have this done
void Mesh::GenTangents()
{
for (size_t k = 0; k < meshdata.NumTris(); ++k)
{
Triangle& newtri = meshdata.Tri(k);
Vector3 one = newtri.v[1]->pos - newtri.v[0]->pos;
Vector3 two = newtri.v[2]->pos - newtri.v[0]->pos;
float tcone = newtri.v[1]->texcoords[0][1] - newtri.v[0]->texcoords[0][1];
float tctwo = newtri.v[2]->texcoords[0][1] - newtri.v[0]->texcoords[0][1];
Vector3 tangent = one * -tctwo + two * tcone;
for (size_t j = 0; j < 3; ++j)
newtri.v[j]->tangent += tangent;
}
for (size_t i = 0; i < meshdata.vertices.size(); ++i)
{
meshdata.vertices[i]->tangent.normalize();
}
}
void Mesh::EnsureMaterials()
{
if (trischanged)
{
meshdata.EnsureMaterials();
// This also means we need color, normal, etc. to be calculated by the MeshNodes
for (size_t i = 0; i < meshdata.frameroot.size(); ++i)
meshdata.frameroot[i]->SetGL(true);
}
}
// There's a good chance animspeed doesn't actually need to be part of the state, but it doesn't really hurt to have it either.
void Mesh::SetState(const Vector3& pos, const Vector3& rot, const int keyframe, const int atime, const float aspeed)
{
if (meshdata.frameroot.size() < 1) return;
rotation = rot;
currkeyframe = keyframe;
animtime = atime;
SetAnimSpeed(aspeed);
position = pos;
Update(Vector3(), true); // We don't want the animation code to run because it would alter the state
}
void Mesh::ReadState(Vector3& pos, Vector3& rot, int& keyframe, int& atime, float& aspeed, float& getsize)
{
pos = GetPosition();
rot = rotation;
keyframe = currkeyframe;
atime = animtime;
aspeed = animspeed;
getsize = size;
}
void Mesh::SetAnimSpeed(const float newas)
{
animspeed = newas;
}
void Mesh::SetAnimation(const int newanim)
{
if (newanim < 10 && numframes[newanim] != 0)
nextanimation = newanim;
}
void Mesh::debug()
{
if (basefile == "models/sighthit/base")
{
logout << "debug" << endl;
for (size_t i = 0; i < meshdata.NumTris(); ++i)
{
for (size_t j = 0; j < 3; ++j)
{
logout << meshdata.Tri(i).v[j]->texcoords[0][0] << endl;
logout << meshdata.Tri(i).v[j]->texcoords[0][1] << endl;
}
}
}
}