3 This file is part of libmspgl
4 Copyright © 2011 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #include "primitivebuilder.h"
17 CapsuleBuilder::CapsuleBuilder(float r, float l, unsigned s, unsigned n):
30 void CapsuleBuilder::build(PrimitiveBuilder &builder) const
32 float u_scale = 1.0/segments;
33 float v_scale = 1/(length+radius*M_PI);
36 float l1 = radius*M_PI*2;
37 float l2 = length+radius*M_PI;
38 if((l1<l2)==(tex_fit==CUT))
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)
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);
54 for(unsigned j=0; j<=segments; ++j)
56 float sa = j*M_PI*2/segments;
59 builder.normal(rs*sc, rs*ss, -rc);
62 builder.attrib(tangent_attr, -ss, sc, 0);
63 builder.attrib(binormal_attr, rc*sc, rc*ss, rs);
65 builder.texcoord(j*u_scale, v);
66 builder.vertex(rs*sc*radius, rs*ss*radius, cz-rc*radius);
69 builder.normal(0, 0, 1);
70 builder.texcoord(0.5, (length+radius*M_PI)*v_scale);
71 builder.vertex(0, 0, length/2-radius);
73 for(unsigned i=0; i<segments; ++i)
75 builder.begin(GL::TRIANGLE_STRIP);
77 for(unsigned j=0; j+1<rings; ++j)
79 builder.element(1+j*(segments+1)+i+1);
80 builder.element(1+j*(segments+1)+i);
82 builder.element((segments+1)*(rings-1)+1);