3 This file is part of libmspgl
4 Copyright © 2011 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #include "primitivebuilder.h"
17 BoxBuilder::BoxBuilder(float w, float h, float d):
18 origin(-w/2, -h/2, -d/2),
22 BoxBuilder::BoxBuilder(const Vector3 &o, const Vector3 &s):
27 void BoxBuilder::build(PrimitiveBuilder &builder) const
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));
43 void BoxBuilder::build_face(PrimitiveBuilder &builder, const Vector3 &o, const Vector3 &s1, const Vector3 &s2) const
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);
52 builder.attrib(tangent_attr, s1.x/l1, s1.y/l1, s1.z/l1);
54 builder.attrib(binormal_attr, s2.x/l2, s2.y/l2, s2.z/l2);
60 if((l1<l2)==(tex_fit==CUT))
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);