renderstack::shapes::sphere Class Reference

#include <polymesh.hpp>

Inherits renderstack::shapes::polymesh.

Public Member Functions

 sphere (float radius, int stack_division, int slice_count)
mesh3d::pointmake_point (double rel_slice, double rel_stack, double radius)

Detailed Description

Definition at line 81 of file polymesh.hpp.


Constructor & Destructor Documentation

renderstack::shapes::sphere::sphere ( float  radius,
int  stack_division,
int  slice_count 
)

Definition at line 873 of file polymesh.cpp.

00874 {
00875     int stack_count = (2 * stack_division) + 1;
00876 
00877     int slice;
00878     int stack;
00879 
00880     std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::point*,   mesh3d::vec3> > point_locations;
00881     std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::point*,   mesh3d::vec3> > point_normals;
00882     std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::polygon*, mesh3d::vec3> > polygon_centroids;
00883     std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::polygon*, mesh3d::vec3> > polygon_normals;
00884 
00885     mesh3d::geometry &g = m_geometry;
00886     point_locations   = g.point_attributes  ().find_or_create<mesh3d::vec3>("point_locations");
00887     point_normals     = g.point_attributes  ().find_or_create<mesh3d::vec3>("point_normals");
00888     polygon_centroids = g.polygon_attributes().find_or_create<mesh3d::vec3>("polygon_centroids");
00889     polygon_normals   = g.polygon_attributes().find_or_create<mesh3d::vec3>("polygon_normals");
00890 
00891     /*  Bottom and top vertices  */ 
00892     mesh3d::point *bottom = make_point(0.5f, -1.0f, radius);
00893     mesh3d::point *top    = make_point(0.5f, 1.0f, radius);
00894 
00895     /*  Other vertices  */ 
00896     std::vector<mesh3d::point*> points;
00897     for(slice = 0; slice <= slice_count; ++slice)
00898     {
00899         double rel_slice = (double)(slice) / (double)(slice_count);
00900         for(stack = -stack_division; stack <= stack_division; ++stack)
00901         {
00902             double rel_stack = (double)(stack) / (double)(stack_division + 1);
00903 
00904             mesh3d::point *pnt = make_point(rel_slice, rel_stack, radius);
00905 
00906             points.push_back(pnt);
00907         }
00908     }
00909 
00910     /*  Bottom fan  */ 
00911     std::vector<mesh3d::point*> centroids;
00912     {
00913         for(slice = 0; slice < slice_count; ++slice)
00914         {
00915             int next_slice  = (slice + 1);
00916             int stack_base0 = 0;
00917 
00918             double rel_slice = ((double)(slice) + 0.5) / (double)(slice_count);
00919             double rel_stack = 0.5 / (double)(stack_division + 1);
00920 
00921             mesh3d::point *centroid = make_point(rel_slice, rel_stack, radius);
00922 
00923             mesh3d::polygon *pol = g.make_polygon(
00924                 points[(slice      * stack_count) + stack_base0],
00925                 bottom,
00926                 points[(next_slice * stack_count) + stack_base0]
00927             );
00928 
00929             polygon_centroids->set_value(pol, point_locations->value(centroid));
00930             polygon_normals  ->set_value(pol, point_normals  ->value(centroid));
00931         }
00932     }
00933 
00934     /*  Middle quads, bottom up  */ 
00935     for(
00936         stack = -stack_division; 
00937         stack < stack_division; 
00938         ++stack
00939     )
00940     {
00941         int stack_base0      = stack + stack_division;
00942         int next_stack_base0 = stack_base0 + 1;
00943 
00944         double rel_stack = ((double)(stack) + 0.5) / (double)(stack_division + 1);
00945 
00946         for(slice = 0; slice < slice_count; ++slice)
00947         {
00948             int next_slice = (slice + 1);
00949 
00950             double rel_slice = ((double)(slice) + 0.5) / (double)(slice_count);
00951 
00952             mesh3d::point *centroid = make_point(rel_slice, rel_stack, radius);
00953 
00954             mesh3d::polygon *pol = g.make_polygon(
00955                 points[(next_slice * stack_count) + next_stack_base0],
00956                 points[(slice      * stack_count) + next_stack_base0],
00957                 points[(slice      * stack_count) + stack_base0],
00958                 points[(next_slice * stack_count) + stack_base0]
00959             );
00960 
00961             polygon_centroids->set_value(pol, point_locations->value(centroid));
00962             polygon_normals  ->set_value(pol, point_normals  ->value(centroid));
00963         }
00964     }
00965 
00966     /*  Top fan   */ 
00967     for(slice = 0; slice < slice_count; ++slice)
00968     {
00969         int next_slice  = (slice + 1);
00970         int stack       =  stack_division;
00971         int stack_base0 =  stack + stack_division;
00972 
00973         double rel_slice = ((double)(slice) + 0.5) / (double)(slice_count);
00974         double rel_stack = 1.0 - (0.5 / (double)(stack_division + 1));
00975 
00976         mesh3d::point *centroid = make_point(rel_slice, rel_stack, radius);
00977 
00978         mesh3d::polygon *pol = g.make_polygon(
00979             top,
00980             points[(slice      * stack_count) + stack_base0],
00981             points[(next_slice * stack_count) + stack_base0]
00982         );
00983 
00984         polygon_centroids->set_value(pol, point_locations->value(centroid));
00985         polygon_normals  ->set_value(pol, point_normals  ->value(centroid));
00986     }
00987 
00988     build_mesh_from_geometry(false);
00989 }


Member Function Documentation

mesh3d::point * renderstack::shapes::sphere::make_point ( double  rel_slice,
double  rel_stack,
double  radius 
)

Definition at line 819 of file polymesh.cpp.

00824 {
00825     std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::point*, mesh3d::vec3> > point_locations;
00826     std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::point*, mesh3d::vec3> > point_normals;
00827     std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::point*, mesh3d::vec2> > point_texcoords;
00828 
00829     mesh3d::geometry &g = m_geometry;
00830     point_locations = g.point_attributes().find_or_create<mesh3d::vec3>("point_locations");
00831     point_normals   = g.point_attributes().find_or_create<mesh3d::vec3>("point_normals");
00832     point_texcoords = g.point_attributes().find_or_create<mesh3d::vec2>("point_texcoords");
00833 
00834     double  phi             = (RS_TWO_PI * rel_slice);
00835     double  sin_phi         = std::sin(phi);
00836     double  cos_phi         = std::cos(phi);
00837 
00838     double  theta           = (RS_HALF_PI * rel_stack);
00839     double  sin_theta       = std::sin(theta);
00840     double  cos_theta       = std::cos(theta);
00841 
00842     double  stack_radius    = cos_theta;
00843 
00844     float   xVN             = (float)(stack_radius * cos_phi);
00845     float   yVN             = (float)(sin_theta);
00846     float   zVN             = (float)(stack_radius * sin_phi);
00847 
00848     float   xP              = (float)(radius * xVN);
00849     float   yP              = (float)(radius * yVN);
00850     float   zP              = (float)(radius * zVN);
00851 
00852     float   s               = (float)(rel_slice);
00853     float   t               = (float)(0.5f * (1.0f + rel_stack));
00854 
00855     mesh3d::point* pnt = g.make_point();
00856 
00857     point_locations->set_value(pnt, mesh3d::vec3(xP, yP, zP));
00858     point_normals  ->set_value(pnt, mesh3d::vec3(xVN, yVN, zVN));
00859     point_texcoords->set_value(pnt, mesh3d::vec2(s, t));
00860 
00861     return pnt;
00862 }


The documentation for this class was generated from the following files:
Generated on Sun Apr 11 12:23:12 2010 for RenderStack by  doxygen 1.6.3