+/* $Id$
+
+This file is part of libmspgl
+Copyright © 2011 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include <cmath>
+#include "box.h"
+#include "primitivebuilder.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GL {
+
+BoxBuilder::BoxBuilder(float w, float h, float d):
+ origin(-w/2, -h/2, -d/2),
+ span(w, h, d)
+{ }
+
+BoxBuilder::BoxBuilder(const Vector3 &o, const Vector3 &s):
+ origin(o),
+ span(s)
+{ }
+
+void BoxBuilder::build(PrimitiveBuilder &builder) const
+{
+ builder.normal(1, 0, 0);
+ build_face(builder, Vector3(origin.x+span.x, origin.y, origin.z), Vector3(0, span.y, 0), Vector3(0, 0, span.z));
+ builder.normal(0, 1, 0);
+ build_face(builder, Vector3(origin.x+span.x, origin.y+span.y, origin.z), Vector3(-span.x, 0, 0), Vector3(0, 0, span.z));
+ builder.normal(-1, 0, 0);
+ build_face(builder, Vector3(origin.x, origin.y+span.y, origin.z), Vector3(0, -span.y, 0), Vector3(0, 0, span.z));
+ builder.normal(0, -1, 0);
+ build_face(builder, origin, Vector3(span.x, 0, 0), Vector3(0, 0, span.z));
+ builder.normal(0, 0, 1);
+ build_face(builder, Vector3(origin.x, origin.y, origin.z+span.z), Vector3(span.x, 0, 0), Vector3(0, span.y, 0));
+ builder.normal(0, 0, -1);
+ build_face(builder, Vector3(origin.x+span.x, origin.y, origin.z), Vector3(-span.x, 0, 0), Vector3(0, span.y, 0));
+}
+
+void BoxBuilder::build_face(PrimitiveBuilder &builder, const Vector3 &o, const Vector3 &s1, const Vector3 &s2) const
+{
+ if(tangent_attr>=0)
+ {
+ builder.attrib(tangent_attr, s1.x, s1.y, s1.z);
+ builder.attrib(binormal_attr, s2.x, s2.y, s2.z);
+ }
+ float u_size = 1;
+ float v_size = 1;
+ if(tex_fit!=STRETCH)
+ {
+ float l1 = sqrt(s1.x*s1.x+s1.y*s1.y+s1.z*s1.z);
+ float l2 = sqrt(s2.x*s2.x+s2.y*s2.y+s2.z*s2.z);
+ if((l1<l2)==(tex_fit==CUT))
+ u_size = l1/l2;
+ else
+ v_size = l2/l1;
+ }
+ builder.begin(TRIANGLE_STRIP);
+ builder.texcoord(0, v_size);
+ builder.vertex(o.x+s2.x, o.y+s2.y, o.z+s2.z);
+ builder.texcoord(0, 0);
+ builder.vertex(o.x, o.y, o.z);
+ builder.texcoord(u_size, v_size);
+ builder.vertex(o.x+s1.x+s2.x, o.y+s1.y+s2.y, o.z+s1.z+s2.z);
+ builder.texcoord(u_size, 0);
+ builder.vertex(o.x+s1.x, o.y+s1.y, o.z+s1.z);
+ builder.end();
+}
+
+} // namespace GL
+} // namespace Msp