From: Mikko Rasa Date: Wed, 25 Nov 2015 11:53:24 +0000 (+0200) Subject: Add geometry builder for sphere X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=093ed86110ebbcd1bc09f693a89bac09ec8198e4 Add geometry builder for sphere --- diff --git a/source/sphere.cpp b/source/sphere.cpp new file mode 100644 index 00000000..0bedf150 --- /dev/null +++ b/source/sphere.cpp @@ -0,0 +1,81 @@ +#include +#include "primitivebuilder.h" +#include "sphere.h" + +namespace Msp { +namespace GL { + +UvSphereBuilder::UvSphereBuilder(float r, unsigned s, unsigned n): + radius(r), + segments(s), + rings(n) +{ + if(segments<3) + segments = 3; + + if(rings==0) + rings = (segments+1)/2; + else if(rings<2) + rings = 2; +} + +void UvSphereBuilder::build(PrimitiveBuilder &builder) const +{ + float u_scale = 1.0f/segments; + float v_scale = 1.0f/rings; + adjust_texture_scale(u_scale, v_scale, radius*M_PI*2, radius*M_PI); + + for(unsigned i=0; i<=rings; ++i) + { + float av = i*M_PI/rings-M_PI/2; + float cv = cos(av); + float sv = sin(av); + + for(unsigned j=0; j<=segments; ++j) + { + float au = j*M_PI*2/segments; + float cu = cos(au); + float su = sin(au); + + builder.normal(cv*cu, cv*su, sv); + builder.texcoord(j*u_scale, i*v_scale); + if(generate_tbn) + { + builder.tangent(-su, cu, 0); + builder.binormal(-sv*cu, -sv*su, cv); + } + builder.vertex(cv*cu*radius, cv*su*radius, sv*radius); + } + } + + builder.begin(TRIANGLES); + for(unsigned j=0; j