]> git.tdb.fi Git - libs/gl.git/blob - source/box.cpp
Fix logic with appending Batches
[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(tangent_attr>=0 || tex_fit!=STRETCH)
40                 l1 = sqrt(s1.x*s1.x+s1.y*s1.y+s1.z*s1.z);
41         if(binormal_attr>=0 || tex_fit!=STRETCH)
42                 l2 = sqrt(s2.x*s2.x+s2.y*s2.y+s2.z*s2.z);
43
44         if(tangent_attr>=0)
45                 builder.attrib(tangent_attr, s1.x/l1, s1.y/l1, s1.z/l1);
46         if(binormal_attr>=0)
47                 builder.attrib(binormal_attr, s2.x/l2, s2.y/l2, s2.z/l2);
48
49         float u_size = 1;
50         float v_size = 1;
51         adjust_texture_scale(u_size, v_size, l1, l2);
52
53         builder.begin(TRIANGLE_STRIP);
54         builder.texcoord(0, v_size);
55         builder.vertex(o.x+s2.x, o.y+s2.y, o.z+s2.z);
56         builder.texcoord(0, 0);
57         builder.vertex(o.x, o.y, o.z);
58         builder.texcoord(u_size, v_size);
59         builder.vertex(o.x+s1.x+s2.x, o.y+s1.y+s2.y, o.z+s1.z+s2.z);
60         builder.texcoord(u_size, 0);
61         builder.vertex(o.x+s1.x, o.y+s1.y, o.z+s1.z);
62         builder.end();
63 }
64
65 } // namespace GL
66 } // namespace Msp