3 #include "primitivebuilder.h"
10 GridBuilder::GridBuilder(float w, float h, unsigned u, unsigned v):
11 GridBuilder(Vector3(-w/2, -h/2, 0), Vector3(w, 0, 0), Vector3(0, h, 0), u, v)
14 GridBuilder::GridBuilder(const Vector3 &o, const Vector3 &s, unsigned u, unsigned v):
15 GridBuilder(o, get_side(s, 0), get_side(s, 1), u, v)
18 GridBuilder::GridBuilder(const Vector3 &o, const Vector3 &s1, const Vector3 &s2, unsigned u, unsigned v):
25 norm = normalize(cross(side1, side2));
26 binorm = normalize(cross(norm, side1));
29 Vector3 GridBuilder::get_side(const Vector3 &size, unsigned index)
32 return Vector3(0, (index==0 ? size.y : 0), (index==1 ? size.z : 0));
34 return Vector3((index==0 ? size.x : 0), 0, (index==1 ? size.z : 0));
36 return Vector3((index==0 ? size.x : 0), (index==1 ? size.z : 0), 0);
39 void GridBuilder::build(PrimitiveBuilder &builder) const
41 float l1_sq = dot(side1, side1);
42 float l2 = dot(side2, binorm);
43 float u_scale = 1/l1_sq;
45 adjust_texture_scale(u_scale, v_scale, sqrt(l1_sq), l2);
47 builder.normal(norm.x, norm.y, norm.z);
49 builder.tangent(side1);
51 for(unsigned j=0; j<=v_div; ++j)
53 Vector3 row = side2*(j*1.0f/v_div);
54 float v = dot(row, binorm)*v_scale;
55 for(unsigned i=0; i<=u_div; ++i)
57 Vector3 p = row+side1*(i*1.0f/u_div);
58 float u = dot(p, side1)*u_scale;
59 builder.texcoord(u, v);
60 builder.vertex(origin+p);
64 for(unsigned j=0; j<v_div; ++j)
66 builder.begin(TRIANGLE_STRIP);
67 for(unsigned i=0; i<=u_div; ++i)
69 builder.element((j+1)*(u_div+1)+i);
70 builder.element(j*(u_div+1)+i);