-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathobj_load.cpp
More file actions
1110 lines (820 loc) · 26.6 KB
/
obj_load.cpp
File metadata and controls
1110 lines (820 loc) · 26.6 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
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "obj_load.hpp"
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <string.h>
#include "triangle.hpp"
#include "texture.hpp"
#include <math.h>
#include <list>
#include "vec.hpp"
#include "logging.hpp"
#include <vec/vec.hpp>
#include "util.hpp"
bool file_exists(const std::string& fname)
{
std::ifstream file;
file.open(fname.c_str());
return file.good();
}
bool is_samemtl(const std::string& str, const std::string newmtl_name)
{
return (str.substr(0, 7) == "newmtl ") && str.substr(str.find_last_of(" ")+1, newmtl_name.size()) == newmtl_name;
}
///get diffuse name
std::string retrieve_diffuse_new(const std::vector<std::string>& file, const std::string& name)
{
bool found = false;
for(unsigned int i=0; i<file.size(); i++)
{
if(is_samemtl(file[i], name))
{
found = true;
}
if(found && strncmp(file[i].c_str(), "map_Kd ", 7)==0)
{
return file[i].substr(file[i].find_last_of(' ')+1, std::string::npos);
}
}
return std::string("");
}
///get bumpmap name
std::string retrieve_bumpmap(const std::vector<std::string>& file, const std::string& name)
{
bool found = false;
for(unsigned int i=0; i<file.size(); i++)
{
///found newmtl + name of material
if(is_samemtl(file[i], name))
{
found = true;
continue;
}
if(found && strncmp(file[i].c_str(), "map_Bump ", 9)==0)
{
return file[i].substr(file[i].find_last_of(' ')+1, std::string::npos);
}
if(found && strncmp(file[i].c_str(), "newmtl ", 7)==0)
{
return std::string("None");
}
}
return std::string("None");
}
///eg
///Kd 0.298039 0.529412 0.427451
vec3f get_Kd(const std::vector<std::string>& file, const std::string name, bool& success)
{
bool found_mtl = false;
for(const std::string& line : file)
{
if(is_samemtl(line, name))
{
found_mtl = true;
continue;
}
if(!found_mtl)
continue;
if(line.substr(0, 2) == "Kd")
{
std::vector<std::string> splitted = split(line, ' ');
assert(splitted.size() > 3);
vec3f pos;
pos.x() = atof(splitted[1].c_str());
pos.y() = atof(splitted[2].c_str());
pos.z() = atof(splitted[3].c_str());
success = true;
return pos;
}
}
success = false;
return {0,0,0};
}
///vertex, texture coordinate, normal
///remember, offset by one for faces
///gets attributes
template <typename T>
void decompose_attribute(const std::string &str, T a[], int n)
{
size_t pos = str.find('.');
int s[n+1]; ///probably fix using varargs
///initialise first element to be initial position
s[0] = str.find(' '); ///
for(int i=1; i<n+1; i++)
{
s[i] = str.find(' ', s[i-1]+1); ///implicitly finds end, so correct for n despite no /
if(pos == std::string::npos)
{
a[i-1] = atoi(str.c_str() + s[i-1]+1);
}
else
{
a[i-1] = atof(str.c_str() + s[i-1]+1);
}
}
}
///decompose face into vertex ids, texture coordinate ids, and normal ids
void decompose_face(const std::string &str, int v[3], int vt[3], int vn[3])
{
///assume valid str because there is no sensible fail case where this isn't a bug
int start = 2;
for(int i=0; i<3; i++)
{
int s1 = str.find('/', start);
int s2 = str.find('/', s1+1);
int s3 = str.find(' ', s2+1);
v[i] = atoi(str.c_str() + start) - 1;
vt[i] = atoi(str.c_str() + s1+1) - 1;
vn[i] = atoi(str.c_str() + s2+1) - 1;
start = s3 + 1;
}
}
struct component
{
float x, y, z;
};
struct indices
{
int v[3];
int vt[3];
int vn[3];
};
///hastily and poorly written object loader
///requires triangulated faces, and explicit texture coordinates, normals, usemtl statements, and a diffuse texture specified
void obj_load(objects_container* pobj)
{
if(!pobj)
{
lg::log("something has gone horribly wrong in obj_load.cpp");
exit(0xDEAD);
}
sf::Clock clk;
std::string filename = pobj->file;
std::string mtlname;
int tp = filename.find_last_of('.');
mtlname = filename.substr(0, tp) + std::string(".mtl");
///get mtlname
size_t lslash = filename.find_last_of('/');
std::string dir = filename.substr(0, lslash);
if(lslash == std::string::npos)
{
dir = ".";
}
std::ifstream file;
std::ifstream mtlfile;
file.open(filename.c_str());
mtlfile.open(mtlname.c_str());
std::vector<std::string> file_contents;
std::vector<std::string> mtlf_contents;
int vc=0, vnc=0, fc=0, vtc=0;
if(!file.is_open())
{
lg::log(filename, " could not be opened in obj_load (file)");
return;
}
if(!mtlfile.is_open())
{
lg::log(mtlname, " could not be found in obj_load (mtlfile)");
}
///load .obj file contents
while(file.good())
{
std::string str;
std::getline(file, str);
file_contents.push_back(str);
}
///load mtl file contents
while(mtlfile.good())
{
std::string str;
std::getline(mtlfile, str);
mtlf_contents.push_back(str);
}
///find number of different types of things - vertices, faces, uv coordinates, normals
for(size_t i=0; i<file_contents.size(); i++)
{
const std::string& ln = file_contents[i];
if(ln.size() < 2)
{
continue;
}
if(ln[0] == 'v' && ln[1] != ' ')
{
vc++;
}
else if(ln[0] == 'v' && ln[1] == 'n')
{
vnc++;
}
else if(ln[0] == 'v' && ln[1] == 't')
{
vtc++;
}
else if(ln[0] == 'f' && ln[1] == ' ')
{
fc++;
}
}
///need to find object groups as well
std::vector<int> usemtl_pos;
std::vector<std::string> usemtl_name;
std::vector<int> group_pos;
std::vector<std::string> group_name;
std::vector<cl_float4> vl, vnl;
std::vector<cl_float2> vtl;
std::vector<indices> fl;
vl.reserve(vc);
vnl.reserve(vnc);
vtl.reserve(vtc);
fl.reserve(fc);
int usefc=0;
//sf::Clock clk2;
for(size_t i=0; i<file_contents.size(); i++)
{
///I must have been braindamaged when i wrote this code
if(file_contents[i][0] == 'f' && file_contents[i][1] == ' ')
{
usefc++;
int v[3];
int vt[3];
int vn[3];
decompose_face(file_contents[i], v, vt, vn);
indices f;
for(int j=0; j<3; j++)
{
f.v[j] = v[j];
f.vt[j] = vt[j];
f.vn[j] = vn[j];
}
fl.push_back(f);
continue;
}
///if == n then push normals etc
else if(file_contents[i][0] == 'v' && file_contents[i][1] == ' ')
{
float v[3];
decompose_attribute(file_contents[i], v, 3);
cl_float4 t;
t = {v[0], v[1], v[2], 0};
vl.push_back(t);
continue;
}
else if(file_contents[i][0] == 'v' && file_contents[i][1] == 't' && file_contents[i][2] == ' ')
{
float vt[3];
decompose_attribute(file_contents[i], vt, 2);
cl_float2 t = {vt[0], vt[1]};
vtl.push_back(t);
continue;
}
else if(file_contents[i][0] == 'v' && file_contents[i][1] == 'n' && file_contents[i][2] == ' ')
{
float vn[3];
decompose_attribute(file_contents[i], vn, 3);
cl_float4 t;
t = {vn[0], vn[1], vn[2], 0};
vnl.push_back(t);
continue;
}
//dont bother with the rest of it
else if(file_contents[i][0] == 'u' && file_contents[i][1] == 's' && file_contents[i][2] == 'e')
{
usemtl_pos.push_back(usefc);
usemtl_name.push_back(file_contents[i].substr(file_contents[i].find_last_of(" ")+1, std::string::npos));
continue;
}
else if(file_contents[i].substr(0, 2) == "g ")
{
group_pos.push_back(usefc);
group_name.push_back(file_contents[i].substr(file_contents[i].find_last_of(" ")+1, std::string::npos));
continue;
}
}
//std::cout << clk2.getElapsedTime().asSeconds() << std::endl;
file_contents.clear();
///now, resolve
std::vector<triangle> tris;
tris.reserve(fc);
for(size_t i=0; i<fl.size(); i++)
{
vertex vert[3];
indices index;
index = fl[i];
for(int j=0; j<3; j++)
{
cl_float4 v, vn;
cl_float2 vt;
v = vl [index.v [j]];
vt = vtl[index.vt[j]];
vn = vnl[index.vn[j]];
///implement scale here for performance reasons
vert[j].set_pos(mult(v, pobj->requested_scale));
vert[j].set_vt(vt);
vert[j].set_normal(vn);
}
triangle t;
t.vertices[0] = vert[0];
t.vertices[1] = vert[1];
t.vertices[2] = vert[2];
tris.push_back(t);
}
vl.clear();
vtl.clear();
vnl.clear();
fl.clear();
bool using_groups = usemtl_pos.size() == group_pos.size();
objects_container *c = pobj;
usemtl_pos.push_back(tris.size());
texture_context* tex_ctx = &c->parent->tex_ctx;
for(unsigned int i=0; i<usemtl_pos.size()-1; i++)///?
{
std::string texture_name = retrieve_diffuse_new(mtlf_contents, usemtl_name[i]);
std::string bumpmap_name = retrieve_bumpmap (mtlf_contents, usemtl_name[i]);
std::string full = dir + "/" + texture_name;
std::string object_group_name;
if(using_groups)
object_group_name = group_name[i];
std::string cache_name = full;
bool file_to_load_exists = file_exists(full);
if(!file_to_load_exists && texture_name != "")
{
lg::log("Warning file does not exist: ", full);
full = dir + "/" + get_file_name(texture_name);
file_to_load_exists = file_exists(full);
if(!file_to_load_exists)
lg::log("Warning recovery alternate name does not exist: ", full);
}
texture* tex;
bool is_real_file = texture_name != "" && file_to_load_exists;
if(is_real_file)
{
if(pobj->textures_are_unique)
{
tex = tex_ctx->make_new();
}
else
{
tex = tex_ctx->make_new_cached(cache_name);
}
tex->set_location(full);
}
else
{
bool success = false;
vec3f kd;
kd = get_Kd(mtlf_contents, usemtl_name[i], success) * 255.f;
if(success)
{
if(pobj->textures_are_unique)
{
tex = tex_ctx->make_new();
}
else
{
tex = tex_ctx->make_new_cached(col2cachename({kd.x(), kd.y(), kd.z(), 255.f}));
}
lg::log("kd ", success, " ", kd.x(), kd.y(), kd.z());
tex->set_create_colour(sf::Color(kd.x(), kd.y(), kd.z()), 32, 32);
}
else
{
tex = tex_ctx->make_new();
tex->set_create_colour(sf::Color(255, 0, 255), 32, 32);
lg::log("Warning, all attempts to get a reasonable texture in obj_load have failed");
}
}
bool isbump = false;
cl_uint b_id = -1;
if(bumpmap_name != std::string("None"))
{
isbump = true;
std::string bump_full = dir + "/" + bumpmap_name;
texture* bumpmap = tex_ctx->make_new_cached(bump_full);
bumpmap->type = 1;
bumpmap->set_location(bump_full);
b_id = bumpmap->id;
}
object obj;
obj.tri_list.reserve(usemtl_pos[i+1]-usemtl_pos[i]);
for(int j=usemtl_pos[i]; j<usemtl_pos[i+1]; j++)
{
obj.tri_list.push_back(tris[j]);
}
obj.object_name = object_group_name;
//memcpy(&obj.tri_list[0], &tris[usemtl_pos[i]], sizeof(triangle) * (usemtl_pos[i+1] - usemtl_pos[i]));
obj.tri_num = obj.tri_list.size(); ///needs to be removed
obj.tid = tex->id;
obj.bid = b_id;
obj.has_bump = isbump;
cl_uint normal_id = -1;
if(pobj->normal_map != "")
{
texture* normal;
normal = tex_ctx->make_new_cached(pobj->normal_map);
normal->set_location(pobj->normal_map);
normal_id = normal->id;
}
obj.rid = normal_id;
///doesn't this perform a double offset?
obj.pos = c->pos;
obj.rot = c->rot;
obj.isloaded = true;
c->objs.push_back(obj); ///does this copy get eliminated? ///timing this says yes
}
pobj->current_scale = pobj->requested_scale;
pobj->requested_scale = 1.f;
c->isloaded = true;
lg::log("Loaded object with id ", c->id);
//std::cout << "Object load time " << clk.getElapsedTime().asSeconds() << std::endl;
}
cl_float4 to_norm(cl_float4 p0, cl_float4 p1, cl_float4 p2)
{
return normalise(neg(cross(sub(p1, p0), sub(p2, p0))));
}
void obj_rect(objects_container* pobj, texture& tex, cl_float2 dim)
{
object obj;
obj.isloaded = true;
cl_float2 hdim = {dim.x/2.f, dim.y/2.f};
/*quad q;
q.p1 = {-hdim.x,-hdim.y, 0};
q.p2 = {-hdim.x, hdim.y, 0};
q.p3 = {hdim.x, hdim.y, 0};
q.p4 = {hdim.x, hdim.y, 0};
std::array<6, cl_float4> decomp = q.decompose();*/
struct triangle tri1, tri2;
tri1.vertices[0].set_pos({-hdim.x, -hdim.y, 0});
tri1.vertices[1].set_pos({-hdim.x, hdim.y, 0});
tri1.vertices[2].set_pos({ hdim.x, hdim.y, 0});
tri2.vertices[0].set_pos({-hdim.x, -hdim.y, 0});
tri2.vertices[1].set_pos({ hdim.x, hdim.y, 0});
tri2.vertices[2].set_pos({ hdim.x, -hdim.y, 0});
cl_float4 normal = to_norm(tri1.vertices[0].get_pos(), tri1.vertices[1].get_pos(), tri1.vertices[2].get_pos());
for(int i=0; i<3; i++)
tri1.vertices[i].set_normal(normal);
for(int i=0; i<3; i++)
tri2.vertices[i].set_normal(normal);
tri1.vertices[0].set_vt({0, 0});
tri1.vertices[1].set_vt({0, 1});
tri1.vertices[2].set_vt({1, 1});
tri2.vertices[0].set_vt({0, 0});
tri2.vertices[1].set_vt({1, 1});
tri2.vertices[2].set_vt({1, 0});
obj.tri_list.push_back(tri1);
obj.tri_list.push_back(tri2);
obj.tri_num = obj.tri_list.size();
obj.tid = tex.id;
pobj->objs.push_back(obj);
pobj->isloaded = true;
}
void obj_rect_tessellated(objects_container* pobj, texture& tex, cl_float2 dim, float tessellate_dim)
{
object obj;
obj.isloaded = true;
cl_float2 hdim = {dim.x/2, dim.y/2};
///1 + this many of points
///how many of length tessellate dim fit into dim
float xnum = dim.x / tessellate_dim;
float ynum = dim.y / tessellate_dim;
int xp = xnum;
int yp = ynum;
float start_x = -dim.x/2;
float start_y = -dim.y/2;
for(int y = 0; y < yp; y++)
{
for(int x = 0; x < xp; x++)
{
std::array<cl_float4, 6> tris;
tris[0] = {start_x + tessellate_dim, start_y, 0};
tris[1] = {start_x, start_y, 0};
tris[2] = {start_x, start_y + tessellate_dim, 0};
tris[3] = {start_x + tessellate_dim, start_y, 0};
tris[4] = {start_x, start_y + tessellate_dim, 0};
tris[5] = {start_x + tessellate_dim, start_y + tessellate_dim, 0};
triangle t1, t2;
t1.vertices[0].set_pos(tris[0]);
t1.vertices[1].set_pos(tris[1]);
t1.vertices[2].set_pos(tris[2]);
t2.vertices[0].set_pos(tris[3]);
t2.vertices[1].set_pos(tris[4]);
t2.vertices[2].set_pos(tris[5]);
cl_float4 normal = {0, 0, -1};
t1.vertices[0].set_normal(normal);
t1.vertices[1].set_normal(normal);
t1.vertices[2].set_normal(normal);
t2.vertices[0].set_normal(normal);
t2.vertices[1].set_normal(normal);
t2.vertices[2].set_normal(normal);
for(int i=0; i<3; i++)
{
float vx = (t1.vertices[i].get_pos().x + dim.x/2) / dim.x;
float vy = (t1.vertices[i].get_pos().y + dim.y/2) / dim.y;
t1.vertices[i].set_vt({vx, vy});
}
for(int i=0; i<3; i++)
{
float vx = (t2.vertices[i].get_pos().x + dim.x/2) / dim.x;
float vy = (t2.vertices[i].get_pos().y + dim.y/2) / dim.y;
t2.vertices[i].set_vt({vx, vy});
}
obj.tri_list.push_back(t1);
obj.tri_list.push_back(t2);
start_x += tessellate_dim;
}
start_y += tessellate_dim;
start_x = -dim.x/2;
}
obj.tri_num = obj.tri_list.size();
obj.tid = tex.id;
pobj->objs.push_back(obj);
pobj->isloaded = true;
}
struct ttri
{
cl_float4 pos[3];
};
///dont bother with correct normals or anything for the moment
void obj_cube_by_extents(objects_container* pobj, texture& tex, cl_float4 dim)
{
//struct triangle t1, t2, t3, t4, t5, t6, t7, t8, t9, t10. t11, t12;
std::vector<cl_float4> pos;
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
for(int k=0; k<2; k++)
{
cl_float4 r = mult({i,j,k,0}, dim);
pos.push_back(r);
}
}
}
/*std::vector<ttri> locs;
for(int i=0; i<pos.size(); i += 2)
{
if(i % 4 == 0)
{
locs.push_back({pos[i], pos[i+1], pos[i+2]});
}
else
{
locs.push_back({pos[i+1], pos[i], pos[i-1]});
}
if(i >= 4)
{
std::swap(locs[i/2].pos[0], locs[i/2].pos[1]);
}
}*/
quad q[6];
q[0].p1 = pos[0];
q[0].p2 = pos[1];
q[0].p3 = pos[2];
q[0].p4 = pos[3];
q[1].p1 = pos[4];
q[1].p2 = pos[5];
q[1].p3 = pos[6];
q[1].p4 = pos[7];
q[2].p1 = pos[0];
q[2].p2 = pos[1];
q[2].p3 = pos[4];
q[2].p4 = pos[5];
q[3].p1 = pos[2];
q[3].p2 = pos[3];
q[3].p3 = pos[6];
q[3].p4 = pos[7];
q[4].p1 = pos[0];
q[4].p2 = pos[2];
q[4].p3 = pos[4];
q[4].p4 = pos[6];
q[5].p1 = pos[1];
q[5].p2 = pos[3];
q[5].p3 = pos[5];
q[5].p4 = pos[7];
std::vector<ttri> locs;
for(auto& i : q)
{
std::array<cl_float4, 6> p = i.decompose();
locs.push_back({p[0], p[1], p[2]});
locs.push_back({p[3], p[4], p[5]});
}
/*for(auto& i : locs)
{
cl_float4 centre = div(dim, 2);
}*/
object obj;
obj.isloaded = true;
for(auto& i : locs)
{
triangle tri;
tri.vertices[0].set_pos(i.pos[0]);
tri.vertices[1].set_pos(i.pos[1]);
tri.vertices[2].set_pos(i.pos[2]);
for(int j=0; j<3; j++)
{
tri.vertices[j].set_vt({0,0});
tri.vertices[j].set_normal({0,1,0});
}
obj.tri_list.push_back(tri);
}
obj.tri_num = obj.tri_list.size();
obj.tid = tex.id;
pobj->objs.push_back(obj);
pobj->isloaded = true;
pobj->set_two_sided(true);
}
void obj_polygon(objects_container* pobj, texture& tex, struct triangle (*f)(int), int num)
{
object obj;
obj.isloaded = true;
for(int i=0; i<num; i++)
{
obj.tri_list.push_back(f(i));
}
obj.tri_num = obj.tri_list.size();
obj.tid = tex.id;
pobj->objs.push_back(obj);
pobj->isloaded = true;
}
#include <vec/vec.hpp>
std::vector<triangle> subdivide_tris(const std::vector<triangle>& in)
{
std::vector<triangle> tris = in;
std::vector<triangle> outv;
outv.reserve(tris.size() * 4);
for(auto& i : tris)
{
triangle tri = i;
cl_float4 p[3];
for(int j=0; j<3; j++)
{
p[j] = i.vertices[j].get_pos();
}
cl_float4 im[3];
for(int j=0; j<3; j++)
{
im[j] = div(add(p[j], p[(j+1) % 3]), 2.f);
}
triangle out[4];
for(auto& j : out)
j = tri;
out[0].vertices[0].set_pos(p[0]);
out[0].vertices[1].set_pos(im[0]);
out[0].vertices[2].set_pos(im[2]);
out[1].vertices[0].set_pos(p[1]);
out[1].vertices[1].set_pos(im[0]);
out[1].vertices[2].set_pos(im[1]);
out[2].vertices[0].set_pos(p[2]);
out[2].vertices[1].set_pos(im[2]);
out[2].vertices[2].set_pos(im[1]);
out[3].vertices[0].set_pos(im[0]);
out[3].vertices[1].set_pos(im[1]);
out[3].vertices[2].set_pos(im[2]);
for(auto& j : out)
{
outv.push_back(j);
}
}
return outv;
}
void rect_to_tris(std::array<cl_float4, 4> p, cl_float4 out[2][3])
{
out[0][0] = p[0];
out[0][1] = p[1];
out[0][2] = p[3];
out[1][0] = p[1];
out[1][1] = p[2];
out[1][2] = p[3];
}
triangle points_to_tri(cl_float4 in[3], float size, float len, int which_side) ///0 top and bottom, 1 front/back, 2 left right
{
triangle t;
for(int i=0; i<3; i++)
t.vertices[i].set_pos(in[i]);
///should probs fix this being slightly wrong, could eliminate edge boundaries
///and then just use the twice sized texture
for(int i=0; i<3; i++)
{
float mx = 0;
float mz = 0;
if(which_side == 0)
{
mx = in[i].x / (size * 2);
mz = in[i].z / (size * 2);
mx += 0.5f;
mz += 0.5f;
}
else if(which_side == 1)
{
mx = in[i].x / (size * 2);
mz = in[i].y / (len);
mx += 0.5f;
}
else if(which_side == 2)
{
mx = in[i].z / (size * 2);
mz = in[i].y / (len);
mx += 0.5f;
}
t.vertices[i].set_vt({mx, mz});
}
for(int i=0; i<3; i++)
t.vertices[i].set_normal(cross(sub(in[1], in[0]), sub(in[2], in[0])));
return t;
}
///fin - start is the object direction
void load_object_cube(objects_container* pobj, vec3f start, vec3f fin, float size, std::string tex_name, bool center)
{
texture_context* tex_ctx = &pobj->parent->tex_ctx;
///remember to set location if its just making new
texture* tex = tex_ctx->make_new_cached(tex_name.c_str());
return load_object_cube_tex(pobj, start, fin, size, *tex, center);
}
///fin - start is the object direction
void load_object_cube_tex(objects_container* pobj, vec3f start, vec3f fin, float size, texture& tex, bool center)
{
texture_context* tex_ctx = &pobj->parent->tex_ctx;
start = start + xyz_to_vec(pobj->pos);
fin = fin + xyz_to_vec(pobj->pos);
float len = (fin - start).length();
///make it point straight up