]> git.tdb.fi Git - libs/gl.git/blob - source/cylinder.cpp
Drop Id tags and copyright notices from files
[libs/gl.git] / source / cylinder.cpp
1 #include <cmath>
2 #include "cylinder.h"
3 #include "primitivebuilder.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 CylinderBuilder::CylinderBuilder(float r, float l, unsigned s):
11         radius(r),
12         length(l),
13         segments(s)
14 {
15         if(segments<3)
16                 segments = 3;
17 }
18
19 void CylinderBuilder::build(PrimitiveBuilder &builder) const
20 {
21         if(binormal_attr>=0)
22                 builder.attrib(binormal_attr, 0, 1, 0);
23         for(unsigned i=0; i<2; ++i)
24         {
25                 float z = (i-0.5)*length;
26                 builder.normal(0, 0, i*2.0-1.0);
27                 builder.texcoord(0.5, 0.5);
28                 if(tangent_attr>=0)
29                         builder.attrib(tangent_attr, (i ? 1 : -1), 0, 0);
30                 builder.vertex(0, 0, z);
31                 for(unsigned j=0; j<segments; ++j)
32                 {
33                         float a = j*M_PI*2/segments;
34                         float c = cos(a);
35                         float s = sin(a);
36                         builder.texcoord(0.5+(i ? 0.5 : -0.5)*c, 0.5+0.5*s);
37                         builder.vertex(radius*c, radius*s, z);
38                 }
39         }
40
41         float u_scale = 1.0/segments;
42         float v_scale = 1;
43         adjust_texture_scale(u_scale, v_scale, radius*M_PI*2, length);
44
45         if(binormal_attr>=0)
46                 builder.attrib(binormal_attr, 0, 0, 1);
47         for(unsigned i=0; i<2; ++i)
48         {
49                 float z = (i-0.5)*length;
50                 for(unsigned j=0; j<=segments; ++j)
51                 {
52                         float a = j*M_PI*2/segments;
53                         float c = cos(a);
54                         float s = sin(a);
55                         builder.normal(c, s, 0);
56                         builder.texcoord(j*u_scale, i*v_scale);
57                         if(tangent_attr>=0)
58                                 builder.attrib(tangent_attr, -s, c, 0);
59                         builder.vertex(radius*c, radius*s, z);
60                 }
61         }
62
63         unsigned base = 0;
64         for(unsigned i=0; i<2; ++i)
65         {
66                 builder.begin(TRIANGLE_FAN);
67                 builder.element(base);
68                 for(unsigned j=0; j<=segments; ++j)
69                         builder.element(base+1+j%segments);
70                 builder.end();
71
72                 base += segments+1;
73         }
74
75         builder.begin(TRIANGLE_STRIP);
76         for(unsigned j=0; j<=segments; ++j)
77         {
78                 builder.element(base+segments+1+j);
79                 builder.element(base+j);
80         }
81         builder.end();
82 }
83
84 } // namespace GL
85 } // namespace Msp