3 #include "primitivebuilder.h"
10 GridBuilder::GridBuilder(float w, float h, unsigned u, unsigned v):
11 origin(-w/2, -h/2, 0),
22 GridBuilder::GridBuilder(const Vector3 &o, const Vector3 &s, unsigned u, unsigned v):
27 if(abs(s.z)<abs(s.x) && abs(s.z)<abs(s.y))
29 side1 = Vector3(s.x, 0, 0);
30 side2 = Vector3(0, s.y, 0);
32 else if(abs(s.y)<abs(s.x))
34 side1 = Vector3(0, 0, s.z);
35 side2 = Vector3(s.x, 0, 0);
39 side1 = Vector3(0, s.y, 0);
40 side2 = Vector3(0, 0, s.z);
45 GridBuilder::GridBuilder(const Vector3 &o, const Vector3 &s1, const Vector3 &s2, unsigned u, unsigned v):
55 void GridBuilder::init(bool compute_normals)
64 norm = normalize(cross(side1, side2));
65 binorm = normalize(cross(norm, side1));
69 void GridBuilder::build(PrimitiveBuilder &builder) const
71 float l1_sq = dot(side1, side1);
72 float l2 = dot(side2, binorm);
73 float u_scale = 1/l1_sq;
75 adjust_texture_scale(u_scale, v_scale, sqrt(l1_sq), l2);
77 builder.normal(norm.x, norm.y, norm.z);
80 builder.tangent(side1);
81 builder.binormal(binorm);
84 for(unsigned j=0; j<=v_div; ++j)
86 Vector3 row = side2*(j*1.0f/v_div);
87 float v = dot(row, binorm)*v_scale;
88 for(unsigned i=0; i<=u_div; ++i)
90 Vector3 p = row+side1*(i*1.0f/u_div);
91 float u = dot(p, side1)*u_scale;
92 builder.texcoord(u, v);
93 builder.vertex(origin+p);
97 for(unsigned j=0; j<v_div; ++j)
99 builder.begin(TRIANGLE_STRIP);
100 for(unsigned i=0; i<=u_div; ++i)
102 builder.element((j+1)*(u_div+1)+i);
103 builder.element(j*(u_div+1)+i);