1 #define _USE_MATH_DEFINES
4 #include "primitivebuilder.h"
11 CapsuleBuilder::CapsuleBuilder(float r, float l, unsigned s, unsigned n):
24 void CapsuleBuilder::build(PrimitiveBuilder &builder) const
26 float u_scale = 1.0/segments;
27 float v_scale = 1/(length+radius*M_PI);
28 adjust_texture_scale(u_scale, v_scale, radius*M_PI*2, length+radius*M_PI);
30 builder.normal(0, 0, -1);
31 builder.texcoord(0.5, 0);
32 builder.vertex(0, 0, -length/2-radius);
33 for(unsigned i=1; i<rings; ++i)
35 float cz = length*(i>rings/2 ? 0.5 : -0.5);
36 float v = ((i>rings/2 ? i-1 : i)*radius*M_PI/(rings-1)+(i>rings/2 ? length : 0))*v_scale;
37 float ra = (i>rings/2 ? i-1 : i)*M_PI/(rings-1);
40 for(unsigned j=0; j<=segments; ++j)
42 float sa = j*M_PI*2/segments;
45 builder.normal(rs*sc, rs*ss, -rc);
48 builder.tangent(-ss, sc, 0);
49 builder.binormal(rc*sc, rc*ss, rs);
51 builder.texcoord(j*u_scale, v);
52 builder.vertex(rs*sc*radius, rs*ss*radius, cz-rc*radius);
55 builder.normal(0, 0, 1);
56 builder.texcoord(0.5, (length+radius*M_PI)*v_scale);
57 builder.vertex(0, 0, length/2+radius);
59 for(unsigned i=0; i<segments; ++i)
61 builder.begin(TRIANGLE_STRIP);
63 for(unsigned j=0; j+1<rings; ++j)
65 builder.element(1+j*(segments+1)+i+1);
66 builder.element(1+j*(segments+1)+i);
68 builder.element((segments+1)*(rings-1)+1);