]> git.tdb.fi Git - libs/gl.git/blob - source/builders/grid.cpp
Include only tangent in mesh data and calculate binormal on the fly
[libs/gl.git] / source / builders / grid.cpp
1 #include <cmath>
2 #include "grid.h"
3 #include "primitivebuilder.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 GridBuilder::GridBuilder(float w, float h, unsigned u, unsigned v):
11         origin(-w/2, -h/2, 0),
12         side1(w, 0, 0),
13         side2(0, h, 0),
14         norm(0, 0, 1),
15         binorm(0, 1, 0),
16         u_div(u),
17         v_div(v)
18 {
19         init(false);
20 }
21
22 GridBuilder::GridBuilder(const Vector3 &o, const Vector3 &s, unsigned u, unsigned v):
23         origin(o),
24         u_div(u),
25         v_div(v)
26 {
27         if(abs(s.z)<abs(s.x) && abs(s.z)<abs(s.y))
28         {
29                 side1 = Vector3(s.x, 0, 0);
30                 side2 = Vector3(0, s.y, 0);
31         }
32         else if(abs(s.y)<abs(s.x))
33         {
34                 side1 = Vector3(0, 0, s.z);
35                 side2 = Vector3(s.x, 0, 0);
36         }
37         else
38         {
39                 side1 = Vector3(0, s.y, 0);
40                 side2 = Vector3(0, 0, s.z);
41         }
42         init(true);
43 }
44
45 GridBuilder::GridBuilder(const Vector3 &o, const Vector3 &s1, const Vector3 &s2, unsigned u, unsigned v):
46         origin(o),
47         side1(s1),
48         side2(s2),
49         u_div(u),
50         v_div(v)
51 {
52         init(true);
53 }
54
55 void GridBuilder::init(bool compute_normals)
56 {
57         if(u_div<1)
58                 u_div = 1;
59         if(v_div<1)
60                 v_div = 1;
61
62         if(compute_normals)
63         {
64                 norm = normalize(cross(side1, side2));
65                 binorm = normalize(cross(norm, side1));
66         }
67 }
68
69 void GridBuilder::build(PrimitiveBuilder &builder) const
70 {
71         float l1_sq = dot(side1, side1);
72         float l2 = dot(side2, binorm);
73         float u_scale = 1/l1_sq;
74         float v_scale = 1/l2;
75         adjust_texture_scale(u_scale, v_scale, sqrt(l1_sq), l2);
76
77         builder.normal(norm.x, norm.y, norm.z);
78         if(generate_tan)
79                 builder.tangent(side1);
80
81         for(unsigned j=0; j<=v_div; ++j)
82         {
83                 Vector3 row = side2*(j*1.0f/v_div);
84                 float v = dot(row, binorm)*v_scale;
85                 for(unsigned i=0; i<=u_div; ++i)
86                 {
87                         Vector3 p = row+side1*(i*1.0f/u_div);
88                         float u = dot(p, side1)*u_scale;
89                         builder.texcoord(u, v);
90                         builder.vertex(origin+p);
91                 }
92         }
93
94         for(unsigned j=0; j<v_div; ++j)
95         {
96                 builder.begin(TRIANGLE_STRIP);
97                 for(unsigned i=0; i<=u_div; ++i)
98                 {
99                         builder.element((j+1)*(u_div+1)+i);
100                         builder.element(j*(u_div+1)+i);
101                 }
102                 builder.end();
103         }
104 }
105
106 } // namespace GL
107 } // namespace Msp