VISH  0.2
DemoSphere.cpp

An example of a bunch of spheres.

#include <stdio.h>

#include <ocean/GLvish/VRenderObject.hpp>
#include <ocean/plankton/VCreator.hpp>
#include <ocean/plankton/VTime.hpp>
#include <ocean/GLvish/BoundingBox.hpp>

#include <stdio.h>

using namespace Wizt;


/* Draw a torus */
static void redbook_torus(int numc, int numt)
{
   int i, j, k;
   double s, t, x, y, z, twopi;

   twopi = 2 * 3.141592;
   for (i = 0; i < numc; i++) {
      glBegin(GL_QUAD_STRIP);
      for (j = 0; j <= numt; j++) {
         for (k = 1; k >= 0; k--) {
            s = (i + k) % numc + 0.5;
            t = j % numt;

            x = (1+.1*cos(s*twopi/numc))*cos(t*twopi/numt);
            y = (1+.1*cos(s*twopi/numc))*sin(t*twopi/numt);
            z = .1 * sin(s * twopi / numc); 
glNormal3f(x,y,z);
            glVertex3f(x, y, z);
         }
      }
      glEnd();
   }
}


static void torus(int numc, int numt)
{
int i, j, k; 
double s, t, x, y, z, twopi; 

using Eagle::PhysicalSpace::point; 
using Eagle::PhysicalSpace::vector; 

        twopi = 2 * 3.141592; 
        for (i = 0; i < numc; i++) 
        {
                glBegin(GL_QUAD_STRIP); 
                for (j = 0; j <= numt; j++) 
                {
                        for (k = 1; k >= 0; k--) 
                        {
                                s = (i + k) % numc + 0.5; 
                                t = j % numt; 

                        point Center( cos(t*twopi/numt),
                                      sin(t*twopi/numt),
                                      0); 
                        vector N(.1*cos(s*twopi/numc)*cos(t*twopi/numt),
                                 .1*cos(s*twopi/numc)*sin(t*twopi/numt),
                                 .1 * sin(s * twopi / numc) ); 

                        glNormal( *N ); 
                        glVertex( Center + N);
                        }
                } 
                glEnd();
        }
}



/*
 Render object.
 */
class   Spheres : public Wizt::VRenderObject
{
        struct  Quadric : ReferenceBase<Quadric>
        {
                ::GLUquadric*quad;

                Quadric()
                : ReferenceBase<Quadric>(this)
                {
                        quad = gluNewQuadric();
                }

                ~Quadric()
                {
                        gluDeleteQuadric( quad ); 
                }


                void Sphere(GLdouble radius,
                            GLint slices,
                            GLint stacks )
                {
                        gluSphere( quad,
                                   radius,
                                   slices,
                                   stacks );
                }
        };

        RefPtr<State> newState() const
        {
                return new AccelState(); 
        }

        struct  Sphere : Quadric
        {
                point   origin;
                double  radius;

                Sphere(const point&center = point(0,0,0), double R=1.0 )
                : origin( center )
                , radius( R )
                {}

                void mkSphere(GLdouble radius,
                              GLint slices,
                              GLint stacks )
                {
                        glPushMatrix(); 
                        glTranslate( origin );
                        Quadric::Sphere(radius, slices, stacks ); 
                        glPopMatrix();
                }
        };

        typedef std::vector<RefPtr<Sphere> > Quadrics_t;

        struct  AccelState : State
        {
                double  last_t;
                float omega,
                      target_omega;

                Quadrics_t Quadrics;

                AccelState()
                {
                        Quadrics.push_back( new Sphere() ); 
//                      Quadrics.push_back( new Sphere( point(1,0,0), 0.5 ) );
                }

                ~AccelState()
                {}


                void update(double t)
                {
//                      Quadrics[ 1 ]->origin = point( sin(omega*t), cos(omega*t), 0);
                }
        };

        override void render(VRenderContext&Context) const
        {
                glEnable(GL_DEPTH_TEST); 
                glEnable(GL_LIGHTING); 
                glEnable(GL_NORMALIZE); 

                glEnable( GL_CULL_FACE ); 
                glCullFace( GL_BACK ); 

                glColor3f (1.0, 0.0, 1.0); 
                glShadeModel(GL_SMOOTH); 

        int     objects = 100; 
                getParameterValue(objects, "objects", Context); 

                if (objects & 4)
                {
                tvector dir = Context.CameraSettings.Observer - point(0,0,0);
                double distance = norm(dir);
                int     n = 4; 
                        if (distance>0)
                                n = int(300.0/distance); 

                        if (n<7) n=7; 
                        if (n>19) n=19; 

                        torus(n, 3*n); 
                }

                /* draw six faces of a cube */ 
                if (objects & 2)
                {
                        glBegin(GL_QUADS); 
                        glNormal3f( 0.0F, 0.0F, 1.0F); 
                        glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f(-0.5F, 0.5F, 0.5F); 
                        glVertex3f(-0.5F,-0.5F, 0.5F); glVertex3f( 0.5F,-0.5F, 0.5F); 

                        glNormal3f( 0.0F, 0.0F,-1.0F); 
                        glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f(-0.5F, 0.5F,-0.5F); 
                        glVertex3f( 0.5F, 0.5F,-0.5F); glVertex3f( 0.5F,-0.5F,-0.5F); 

                        glNormal3f( 0.0F, 1.0F, 0.0F); 
                        glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f( 0.5F, 0.5F,-0.5F); 
                        glVertex3f(-0.5F, 0.5F,-0.5F); glVertex3f(-0.5F, 0.5F, 0.5F); 

                        glNormal3f( 0.0F,-1.0F, 0.0F); 
                        glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f( 0.5F,-0.5F,-0.5F); 
                        glVertex3f( 0.5F,-0.5F, 0.5F); glVertex3f(-0.5F,-0.5F, 0.5F); 

                        glNormal3f( 1.0F, 0.0F, 0.0F); 
                        glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f( 0.5F,-0.5F, 0.5F); 
                        glVertex3f( 0.5F,-0.5F,-0.5F); glVertex3f( 0.5F, 0.5F,-0.5F); 

                        glNormal3f(-1.0F, 0.0F, 0.0F); 
                        glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f(-0.5F,-0.5F, 0.5F); 
                        glVertex3f(-0.5F, 0.5F, 0.5F); glVertex3f(-0.5F, 0.5F,-0.5F); 
                        glEnd();
                }


        RefPtr<AccelState> myState = getState(Context); 
                if (!myState)
                        return; 

//              myState->update(Context.time.seconds);

                if (objects&1)
                for(Quadrics_t::const_iterator q = myState->Quadrics.begin(); 
                    q != myState->Quadrics.end(); q++)
                {
                Sphere&S = **q; 
                tvector dir = S.origin - Context.CameraSettings.Observer; 
                double distance = norm(dir); 
//                      printf("Distance: %lg inv %lg\n", distance, 1/distance);
                int     n = 4; 
                        if (distance>0)
                                n = int(700.0/distance); 

//                      printf("nDistance: %d\n", n);

                        if (n<7) n=7; 
                        if (n>19) n=19; 

                        (*q)->mkSphere( 0.52, n, n);
                }
        }


public:

        Spheres(const string&name, int p, const RefPtr<VCreationPreferences>&VP)
        : VRenderObject(name, p, VP)
        {
                addParam("objects", 7 ); 

//              addParam("speed", 10 ); 
//              addParam("number", 10, new VCreationPreferences("local") ); 
//              addInterface( new BoundingBox(point(-1,-1,-1), point(1,1,1) ) );
        }

        ~Spheres()
        {}

        override bool update(VRequest&Req, double precision)
        {
                setBoundingBall( Req, new BoundingBox(point(-1,-1,-1), point(1,1,1) ) );
                return true;
        }

        override bool request(VRequest&R, double precision)
        {
//              touch();
                return VRenderObject::request(R,precision);
        }

};



static VCreator<Spheres> sphereCreator("SphereCrowd", 0);


VISH_DEFAULT_INIT