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