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);
47 builder.tangent(-ss, sc, 0);
48 builder.texcoord(j*u_scale, v);
49 builder.vertex(rs*sc*radius, rs*ss*radius, cz-rc*radius);
52 builder.normal(0, 0, 1);
53 builder.texcoord(0.5, (length+radius*M_PI)*v_scale);
54 builder.vertex(0, 0, length/2+radius);
56 for(unsigned i=0; i<segments; ++i)
58 builder.begin(TRIANGLE_STRIP);
60 for(unsigned j=0; j+1<rings; ++j)
62 builder.element(1+j*(segments+1)+i+1);
63 builder.element(1+j*(segments+1)+i);
65 builder.element((segments+1)*(rings-1)+1);