]> git.tdb.fi Git - libs/gl.git/blob - source/capsule.cpp
Add capsule and grid builders
[libs/gl.git] / source / capsule.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <cmath>
9 #include "capsule.h"
10 #include "primitivebuilder.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 CapsuleBuilder::CapsuleBuilder(float r, float l, unsigned s, unsigned n):
18         radius(r),
19         length(l),
20         segments(s),
21         rings(n)
22 {
23         if(segments<3)
24                 segments = 3;
25         rings |= 1;
26         if(rings<3)
27                 rings = 3;
28 }
29
30 void CapsuleBuilder::build(PrimitiveBuilder &builder) const
31 {
32         float u_scale = 1.0/segments;
33         float v_scale = 1/(length+radius*M_PI);
34         if(tex_fit!=STRETCH)
35         {
36                 float l1 = radius*M_PI*2;
37                 float l2 = length+radius*M_PI;
38                 if((l1<l2)==(tex_fit==CUT))
39                         u_scale *= l1/l2;
40                 else
41                         v_scale *= l2/l1;
42         }
43
44         builder.normal(0, 0, -1);
45         builder.texcoord(0.5, 0);
46         builder.vertex(0, 0, -length/2+radius);
47         for(unsigned i=1; i<rings; ++i)
48         {
49                 float cz = length*(i>rings/2 ? 0.5 : -0.5);
50                 float v = (i*radius*M_PI/rings+(i>rings/2 ? length : 0))*v_scale;
51                 float ra = (i>rings/2 ? i-1 : i)*M_PI/(rings-1);
52                 float rc = cos(ra);
53                 float rs = sin(ra);
54                 for(unsigned j=0; j<=segments; ++j)
55                 {
56                         float sa = j*M_PI*2/segments;
57                         float sc = cos(sa);
58                         float ss = sin(sa);
59                         builder.normal(rs*sc, rs*ss, -rc);
60                         if(tangent_attr>=0)
61                                 builder.attrib(tangent_attr, -ss, sc, 0);
62                         if(binormal_attr>=0)
63                                 builder.attrib(binormal_attr, rc*sc, rc*ss, rs);
64                         builder.texcoord(j*u_scale, v);
65                         builder.vertex(rs*sc*radius, rs*ss*radius, cz-rc*radius);
66                 }
67         }
68         builder.normal(0, 0, 1);
69         builder.texcoord(0.5, (length+radius*M_PI)*v_scale);
70         builder.vertex(0, 0, length/2-radius);
71
72         for(unsigned i=0; i<segments; ++i)
73         {
74                 builder.begin(GL::TRIANGLE_STRIP);
75                 builder.element(0);
76                 for(unsigned j=0; j+1<rings; ++j)
77                 {
78                         builder.element(1+j*(segments+1)+i+1);
79                         builder.element(1+j*(segments+1)+i);
80                 }
81                 builder.element((segments+1)*(rings-1)+1);
82                 builder.end();
83         }
84 }
85
86 } // namespace GL
87 } // namespace Msp