]> git.tdb.fi Git - libs/gl.git/blob - source/box.cpp
Make desertpillars compatible with the modernization changes
[libs/gl.git] / source / box.cpp
1 #include <cmath>
2 #include "box.h"
3 #include "primitivebuilder.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 BoxBuilder::BoxBuilder(float w, float h, float d):
11         origin(-w/2, -h/2, -d/2),
12         span(w, h, d)
13 { }
14
15 BoxBuilder::BoxBuilder(const Vector3 &o, const Vector3 &s):
16         origin(o),
17         span(s)
18 { }
19
20 void BoxBuilder::build(PrimitiveBuilder &builder) const
21 {
22         builder.normal(1, 0, 0);
23         build_face(builder, Vector3(origin.x+span.x, origin.y, origin.z), Vector3(0, span.y, 0), Vector3(0, 0, span.z));
24         builder.normal(0, 1, 0);
25         build_face(builder, Vector3(origin.x+span.x, origin.y+span.y, origin.z), Vector3(-span.x, 0, 0), Vector3(0, 0, span.z));
26         builder.normal(-1, 0, 0);
27         build_face(builder, Vector3(origin.x, origin.y+span.y, origin.z), Vector3(0, -span.y, 0), Vector3(0, 0, span.z));
28         builder.normal(0, -1, 0);
29         build_face(builder, origin, Vector3(span.x, 0, 0), Vector3(0, 0, span.z));
30         builder.normal(0, 0, 1);
31         build_face(builder, Vector3(origin.x, origin.y, origin.z+span.z), Vector3(span.x, 0, 0), Vector3(0, span.y, 0));
32         builder.normal(0, 0, -1);
33         build_face(builder, Vector3(origin.x+span.x, origin.y, origin.z), Vector3(-span.x, 0, 0), Vector3(0, span.y, 0));
34 }
35
36 void BoxBuilder::build_face(PrimitiveBuilder &builder, const Vector3 &o, const Vector3 &s1, const Vector3 &s2) const
37 {
38         float l1 = 1, l2 = 1;
39         if(generate_tbn || tex_fit!=STRETCH)
40         {
41                 l1 = s1.norm();
42                 l2 = s2.norm();
43         }
44
45         if(generate_tbn)
46         {
47                 builder.tangent(s1/l1);
48                 builder.binormal(s2/l2);
49         }
50
51         float u_size = 1;
52         float v_size = 1;
53         adjust_texture_scale(u_size, v_size, l1, l2);
54
55         builder.begin(TRIANGLE_STRIP);
56         builder.texcoord(0, v_size);
57         builder.vertex(o+s2);
58         builder.texcoord(0, 0);
59         builder.vertex(o);
60         builder.texcoord(u_size, v_size);
61         builder.vertex(o+s1+s2);
62         builder.texcoord(u_size, 0);
63         builder.vertex(o+s1);
64         builder.end();
65 }
66
67 } // namespace GL
68 } // namespace Msp