]> git.tdb.fi Git - libs/gl.git/blob - source/grid.cpp
Initialize Texture::use_srgb_format
[libs/gl.git] / source / 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_tbn)
79         {
80                 builder.tangent(side1);
81                 builder.binormal(binorm);
82         }
83
84         for(unsigned j=0; j<=v_div; ++j)
85         {
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)
89                 {
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);
94                 }
95         }
96
97         for(unsigned j=0; j<v_div; ++j)
98         {
99                 builder.begin(TRIANGLE_STRIP);
100                 for(unsigned i=0; i<=u_div; ++i)
101                 {
102                         builder.element((j+1)*(u_div+1)+i);
103                         builder.element(j*(u_div+1)+i);
104                 }
105                 builder.end();
106         }
107 }
108
109 } // namespace GL
110 } // namespace Msp