]> git.tdb.fi Git - libs/gl.git/blob - source/box.cpp
Improve smoothing in blender exporter
[libs/gl.git] / source / box.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <cmath>
9 #include "box.h"
10 #include "primitivebuilder.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 BoxBuilder::BoxBuilder(float w, float h, float d):
18         origin(-w/2, -h/2, -d/2),
19         span(w, h, d)
20 { }
21
22 BoxBuilder::BoxBuilder(const Vector3 &o, const Vector3 &s):
23         origin(o),
24         span(s)
25 { }
26
27 void BoxBuilder::build(PrimitiveBuilder &builder) const
28 {
29         builder.normal(1, 0, 0);
30         build_face(builder, Vector3(origin.x+span.x, origin.y, origin.z), Vector3(0, span.y, 0), Vector3(0, 0, span.z));
31         builder.normal(0, 1, 0);
32         build_face(builder, Vector3(origin.x+span.x, origin.y+span.y, origin.z), Vector3(-span.x, 0, 0), Vector3(0, 0, span.z));
33         builder.normal(-1, 0, 0);
34         build_face(builder, Vector3(origin.x, origin.y+span.y, origin.z), Vector3(0, -span.y, 0), Vector3(0, 0, span.z));
35         builder.normal(0, -1, 0);
36         build_face(builder, origin, Vector3(span.x, 0, 0), Vector3(0, 0, span.z));
37         builder.normal(0, 0, 1);
38         build_face(builder, Vector3(origin.x, origin.y, origin.z+span.z), Vector3(span.x, 0, 0), Vector3(0, span.y, 0));
39         builder.normal(0, 0, -1);
40         build_face(builder, Vector3(origin.x+span.x, origin.y, origin.z), Vector3(-span.x, 0, 0), Vector3(0, span.y, 0));
41 }
42
43 void BoxBuilder::build_face(PrimitiveBuilder &builder, const Vector3 &o, const Vector3 &s1, const Vector3 &s2) const
44 {
45         float l1 = 1, l2 = 1;
46         if(tangent_attr>=0 || tex_fit!=STRETCH)
47                 l1 = sqrt(s1.x*s1.x+s1.y*s1.y+s1.z*s1.z);
48         if(binormal_attr>=0 || tex_fit!=STRETCH)
49                 l2 = sqrt(s2.x*s2.x+s2.y*s2.y+s2.z*s2.z);
50
51         if(tangent_attr>=0)
52                 builder.attrib(tangent_attr, s1.x/l1, s1.y/l1, s1.z/l1);
53         if(binormal_attr>=0)
54                 builder.attrib(binormal_attr, s2.x/l2, s2.y/l2, s2.z/l2);
55
56         float u_size = 1;
57         float v_size = 1;
58         if(tex_fit!=STRETCH)
59         {
60                 if((l1<l2)==(tex_fit==CUT))
61                         u_size = l1/l2;
62                 else
63                         v_size = l2/l1;
64         }
65
66         builder.begin(TRIANGLE_STRIP);
67         builder.texcoord(0, v_size);
68         builder.vertex(o.x+s2.x, o.y+s2.y, o.z+s2.z);
69         builder.texcoord(0, 0);
70         builder.vertex(o.x, o.y, o.z);
71         builder.texcoord(u_size, v_size);
72         builder.vertex(o.x+s1.x+s2.x, o.y+s1.y+s2.y, o.z+s1.z+s2.z);
73         builder.texcoord(u_size, 0);
74         builder.vertex(o.x+s1.x, o.y+s1.y, o.z+s1.z);
75         builder.end();
76 }
77
78 } // namespace GL
79 } // namespace Msp