Beispiel #1
0
int main()
{
  derived *d = new derived;
  downcast(d);

  base1 *b1 = new derived;
  crosscast(b1);
}
Beispiel #2
0
bool binary_primitive<T>::common_subexpressions(const primitive<T>* p) const {

	bool ret_val = false;

	if (const binary_primitive<T>* other = downcast(p)) {

		ret_val = (x==other->x && y==other->y);
	}

	return ret_val;
}
Beispiel #3
0
	/**
	 * @brief Load a single Object from a Result Set
	 * @param[in] Result Set
	 * @return New Domain Object
	 */
	typename D::Ptr load(cursor::row_t r)
	{
		int64_t id = r[0].as<int64_t>();
		if ((m_loaded.find(id) != m_loaded.end()) && ! m_loaded[id].expired())
			return downcast(m_loaded[id].lock());

		typename D::Ptr result = doLoad(id, r);
		m_loaded[id] = Persistent::WeakPtr(upcast(result));
		attachToSession(result);
		afterLoaded(result);
		mark_persistent_clean(result);
		return result;
	}
Beispiel #4
0
 void Box3::setFrom(ptr<Object3D> obj) {
     obj->updateMatrixWorld(true);
     this->makeEmpty();
     
     obj->traverse([&]( ptr<Object3D> node ) {
         if( instance_of(node, Mesh)) {
             ptr<Mesh> mesh = downcast(node, Mesh);
             
             if( mesh->hasGeometry() ) {
                 ptr<Geometry> geom = mesh->getGeometry();
                 
                 for( glm::vec3 vec : geom->getVertices() ) {
                     glm::vec3 trans_vec = glm::vec3(geom->matrixWorld * glm::vec4( vec, 1.0 ) );
                     this->expandByPoint( trans_vec );
                 }
             }
         }
     });
 }
Beispiel #5
0
    virtual void sample (AbstractStorage *stor_, size_t multiplier)
    {
        stor_t *stor = downcast (stor_);

        // initialized late since stor might be empty at ctor time
        rho_ = stor->particle_density ();
        ndata_ += multiplier;
        walltime_ -= gclock ();

        for (size_t n1 = 0; n1 != multiplier; ++n1)
        {
            key_t k1 = stor->random_particle (&random);

            if (method_ == "full")
                do_sample (stor, k1, stor->enumerate_all ());
            else // window
                do_sample (stor, k1, stor->enumerate_box (k1, rmax_));
        }

        walltime_ += gclock ();
    }
Beispiel #6
0
    virtual void init (AbstractStorage *stor_, double bin_width, double rmax)
    {
        stor_t *stor = downcast (stor_);

        binwid_ = bin_width;

        if (rmax == 0.)
        {
            method_ = "full";
            rmax_ = stor->periods ().min (DIM) / 2;
        }
        else
        {
            method_ = "window";
            assert (rmax > 0.);
            rmax_ = rmax;
        }

        hist_.resize (bin (rmax_) - 1., 0);
        ndata_ = 0;
        walltime_ = 0;
    }
 inline void polygon_set_data<coordinate_type>::clean() const {
   if(dirty_) {
     polygon_45_set_data<coordinate_type> tmp;
     if(downcast(tmp) ) {
       tmp.clean();
       data_.clear();
       is_45_ = true;
       polygon_set_data<coordinate_type> tmp2;
       tmp2.insert(tmp);
       data_.swap(tmp2.data_);
       dirty_ = false;
       sort();
     } else {
       sort();
       arbitrary_boolean_op<coordinate_type> abo;
       polygon_set_data<coordinate_type> tmp2;
       abo.execute(tmp2, begin(), end(), end(), end(), 0);
       data_.swap(tmp2.data_);
       is_45_ = tmp2.is_45_;
       dirty_ = false;
     }
   }
 }
Beispiel #8
0
	static GLfloat raycast_stars(const std::vector<star_t>& nebula_stars, volume<glm::vec3, X, Y, Z>& light_volume, const volume<glm::vec4, X, Y, Z>& dust_volume)
	{
		static constexpr GLfloat occlusion = 0.005;
		static constexpr GLfloat stepsize = 0.001;
		static constexpr GLfloat falloff = 1.2;

		GLfloat max_intensity = 0.0;

		for(size_t x = 0; x < X; ++x)
			for(size_t y = 0; y < Y; ++y)
				for(size_t z = 0; z < Z; ++z)
				{
					glm::uvec3 pos(x, y, z);
					glm::vec3 fpos = downcast(pos, fX, fY, fZ);

					glm::vec3 color = glm::vec3(0.0);
					GLfloat total_intensity = 0.0;

					for(const star_t& star : nebula_stars)
					{
						glm::vec3 dir = fpos - star.pos;
						GLfloat len = glm::length(dir);

						if(len == 0.0)
							continue;

						glm::vec3 norm_dir = glm::normalize(dir);
						glm::vec3 delta_dir = norm_dir * stepsize;
						GLfloat delta_dir_len = glm::length(delta_dir);

						glm::vec3 vec = star.pos;

						size_t step_count = len / delta_dir_len - 1;

						GLfloat intensity = 1.0;
						for(size_t i = 0; i < step_count; ++i)
						{
							glm::uvec3 uvec = upcast(vec, fX, fY, fZ);
							intensity -= dust_volume[uvec].a * occlusion * stepsize;

							vec += delta_dir;

							if(intensity <= 0.0)
								break;
						}

						intensity *= 1.0f - std::pow(len*falloff, 2.0f);

						if(intensity > 0.0) // If not completely occluded
						{
							color += star.color * intensity;
							total_intensity += intensity;
						}
					}

					light_volume[pos] = color;
					max_intensity = glm::max(max_intensity, total_intensity);
				}

		return max_intensity;
	}
    void Ex_006_BoundingBoxTest::run() {
        const string path = "/Users/saburookita/Personal Projects/Three.cpp Rev.2/examples/assets/";
        
        ForwardRenderer renderer;
        renderer.init( "Ex 006: Bounding Box Tests", 1600 * 2 / 4, 900 * 2 / 4 );
        renderer.setCameraControl(Arcball::create(2.0f));
        
        /* Create scene */
        auto scene = Scene::create();
        scene->setFog(Fog::create( 0x72645b / 2, 2.0, 15.0 ));
        scene->setViewport( 0.0, 0.0, renderer.getWidth(), renderer.getHeight() );
        scene->setShadowMapType( SHADOW_MAP::PCF );
        
        /* Create camera */
        auto camera = PerspectiveCamera::create( 50.0, renderer.getAspectRatio(), 0.001, 100.0 );
        camera->translate(0.0, 1.5, 5.5);
        camera->lookAt( 0.0, 1.0, 0.0 );
        
        /* Load our ply models */
        vector<string> filenames = {
            "dragon_vrip_res3.ply",
            "happy_vrip_res3.ply",
        };
        
        vector<ptr<Mesh>> statues;
        
        float x_offset = -1.0;
        for( string filename: filenames ) {
            auto statue = Loader::loadPLY(path + "/ply models/", filename,
                                          aiProcess_Triangulate |
                                          aiProcess_OptimizeMeshes |
                                          aiProcess_JoinIdenticalVertices |
                                          aiProcess_GenSmoothNormals | aiProcess_FlipWindingOrder );
            
            statue->setMaterial(PhongMaterial::create(0xcccccc, 0x0, 0x000000, 0x999999, 10, true));
            statue->getGeometry()->setScale(10.0f);
            statue->castShadow      = true;
            statue->receiveShadow   = true;
            
            auto bbox = statue->computeBoundingBox();
            glm::vec3 center = bbox->center();
            glm::vec3 size   = bbox->size();
            statue->translate(x_offset, -(center.y - size.y * 0.5), 0.0);
            
            cout << *bbox << endl;
            
            x_offset += 2.0f;
            scene->add( statue );
            statues.push_back( statue );
        }
        
        auto box = Mesh::create( CubeGeometry::create(1.0f, 3),
                                PhongMaterial::create(0x777777, 0x777777, 0x0, 0x0, 0.0, true) );
        box->setTexture( TextureUtils::loadAsTexture( path, "crate.tga") );
        box->getGeometry()->rotateX(45.0f);
        box->getGeometry()->setScale(1.5f);
        box->translate(3.0f, 0.5, 0.0);
        box->castShadow = true;
        box->receiveShadow = true;
        scene->add( box );
        
        auto cylinder = Mesh::create( CylinderGeometry::create(0.5, 0.5, 1.0, 30, 5, false),
                                      PhongMaterial::create( 0xDDDDDD, 0x0, 0x0, 0x111111, 150.0, true )
                                     );
        cylinder->translate(-3.0f, 0.5f, 0.0f);
        cylinder->castShadow = true;
        cylinder->receiveShadow = true;
        scene->add( cylinder );
        
        
        /* And the ground plane */
        auto plane = Mesh::create( PlaneGeometry::create(50.0f), PhongMaterial::create() );
        plane->rotateX(-90.0f);
        plane->receiveShadow = true;
        scene->add( plane );

        /* Cubemap */
//        auto env = Mesh::create( CubeGeometry::create(50.0f), MeshCubeMapMaterial::create() );
//        env->setTexture( TextureUtils::loadAsEnvMap( path + "cube/pisa",
//                                                    "nx.png", "ny.png", "nz.png",
//                                                    "px.png", "py.png", "pz.png"));
//        
//        cylinder->setEnvMap( env->getTexture() );
//        scene->add( env );
        
        
        /* Create a (rotating) directional light */
        auto dir_light = DirectionalLight::create(0x99CCFF, 1.35, glm::vec3( 3.0, 1.0, 3.0 ) );
        dir_light->castShadow       = true;
        dir_light->shadowBias       = -0.0001;
        dir_light->shadowCameraNear = -10.0;
        dir_light->shadowCameraFar  =  10.0;
        dir_light->shadowMapSize    = glm::vec2(512);
        scene->add( dir_light );
        
        /* Create a spotlight, the shadow should be casted no the left hand side */
        auto spot_light = SpotLight::create(0x99CCFF, 1.0, 20.0, 50.0, 1.0 );
        spot_light->position = glm::vec3(3.0, 2.0, 3.0);
        spot_light->castShadow = true;
        scene->add( spot_light );
        
        
        /* Create an ambient light */
        scene->add( AmbientLight::create(0xCCCCCC));
        
        /* Create a post render callback for objects rotation */
        bool rotate_objects = false;
        float light_rotation_1 = 0.0;
        renderer.setPostRenderCallbackHandler( [&](){
            dir_light->position.x = ( 3.0 * cosf( light_rotation_1 ) );
            dir_light->position.z = ( 3.0 * sinf( light_rotation_1 ) );
            
            light_rotation_1 += 0.01;
            
            if( rotate_objects ) {
                box->rotateY(-1.0f);
                statues[0]->rotateY(-0.5f);
            }
        });
        
        
        renderer.setMouseButtonCallbackHandler([&] (GLFWwindow *, int button, int action, int mod){
            auto descendants = scene->getDescendants();
            /* Reset the color first */
            for( auto obj: descendants ) {
                if( instance_of(obj, Mesh)) {
                    auto mesh = downcast( obj, Mesh );
                    if( instance_of(mesh->getMaterial(), PhongMaterial)){
                        auto phong = downcast(mesh->getMaterial(), PhongMaterial);
                        phong->setDiffuseColor( 0xDDDDDD );
                    }
                }
            }
            
            if( action == GLFW_PRESS ) {
                auto raycaster = Projector::pickingRay(renderer.getCursorPosition(), camera);
                
                /* Upon selected / ray picked, change the diffuse color to red */
                for( auto obj: descendants ) {
                    if( instance_of(obj, Geometry))
                        continue;
                    
                    auto bound = obj->getBoundingBox();
                    
                    if(raycaster->ray->intersects(bound)) {
                        if( instance_of(obj, Mesh)) {
                            auto mesh = downcast( obj, Mesh );
                            
                            if( instance_of(mesh->getMaterial(), PhongMaterial)){
                                auto phong = downcast(mesh->getMaterial(), PhongMaterial);
                                phong->setDiffuseColor( 0xDD0000 );
                            }
                        }
                    }
                }
            }
        });
        
        /* Override key callback handler */
        renderer.setKeyCallbackHandler([&](GLFWwindow *, int key, int scancode, int action, int mod) {
            if( action == GLFW_PRESS ) {
                switch ( key) {
                    case GLFW_KEY_R: /* Toggle rotation */
                        rotate_objects = !rotate_objects;
                        break;
                        
                    default: break;
                }
            }
        });
        
        renderer.setGamma( true, true );
        
        renderer.setClearColor( scene->getFog()->getColor() );
        renderer.render(scene, camera );
    }
 size_t printTo(Print &print) const {
   JsonWriter writer(print);
   downcast().writeTo(writer);
   return writer.bytesWritten();
 }
 size_t printTo(Print &print) const {
   JsonWriter writer(print);
   JsonSerializer::serialize(downcast(), writer);
   return writer.bytesWritten();
 }
 void Ex_004_ShadowMapping::run() {
     const string path = "/Users/saburookita/Personal Projects/Three.cpp Rev.2/examples/assets/";
     
     ForwardRenderer renderer;
     renderer.init( "Ex 004: Shadow Mapping", 1600 * 2 / 4, 900 * 2 / 4 );
     renderer.setCameraControl(Arcball::create(2.0f));
     
     
     /* Create scene */
     auto scene = Scene::create();
     scene->setFog(Fog::create( 0x72645b / 2, 2.0, 15.0 ));
     scene->setViewport( 0.0, 0.0, renderer.getWidth(), renderer.getHeight() );
     scene->setShadowMapType( SHADOW_MAP::PCF_SOFT );
     
     /* Create camera */
     auto camera = PerspectiveCamera::create( 50.0, renderer.getAspectRatio(), 0.001, 100.0 );
     camera->translate(0.0, 1.5, 5.5);
     camera->lookAt( 0.0, 0.0, 0.0 );
     
     /* Create our objects */
     auto sphere = Mesh::create( SphereGeometry::create(30, 20, 0.66f ),
                                PhongMaterial::create(0x777777, 0x0, 0x0, 0x999999, 30, true) );
     
     sphere->setNormalMap( TextureUtils::loadAsNormalMap  ( path, "tutorial_normals07.gif" ) );
     sphere->translate(-2.0, 0.66f, 0.0);
     sphere->castShadow = true;
     sphere->receiveShadow = true;
     
     auto cube = Mesh::create( CubeGeometry::create(1.0, 10),
                              PhongMaterial::create(0x777777, 0x0, 0x0, 0x0, 30, false) );
     cube->setTexture( TextureUtils::loadAsTexture( path, "four_shapes_color.tga" ) );
     cube->translate(0.0, 0.5, 0.0);
     cube->castShadow = true;
     cube->receiveShadow = true;
     
     auto cylinder = Mesh::create( CylinderGeometry::create(0.5, 0.5, 1.0, 30, 5, true),
                                  PhongMaterial::create( 0xCCCCCC, 0x0, 0x0, 0x111111, 150.0, false ) );
     cylinder->getMaterial()->setSide( SIDE::DOUBLE_SIDE );
     cylinder->castShadow = true;
     cylinder->receiveShadow = true;
     cylinder->setTexture  ( TextureUtils::loadAsTexture   ( path, "rock_color.tga" ) );
     cylinder->setNormalMap( TextureUtils::loadAsNormalMap ( path, "rock_normal.tga" ) );
     cylinder->translate(+2.0f, 0.5f, -2.0f);
     
     scene->add( cylinder );
     scene->add( cube );
     scene->add( sphere );
     
     /* And the ground plane */
     auto plane = Mesh::create( PlaneGeometry::create(20.0f),
                               PhongMaterial::create(0x777777, 0x777777, 0x0, 0x999999, 30) );
     plane->name = "plane";
     plane->rotateX(-90.0f);
     plane->translate(0.0, 0.0, 0.0);
     plane->receiveShadow = true;
     scene->add( plane );
     
     /* Cubemap */
     auto env = Mesh::create( CubeGeometry::create(20.0f), MeshCubeMapMaterial::create() );
     env->setTexture( TextureUtils::loadAsEnvMap( path + "cube/pisa",
                                               "nx.png", "ny.png", "nz.png",
                                               "px.png", "py.png", "pz.png"));
     
     sphere->setEnvMap( downcast(env->getTexture(), EnvMap) );
     scene->add( env );
     
     
     /* Create a (rotating) directional light */
     auto dir_light = DirectionalLight::create(0x99CCFF, 1.35, glm::vec3( 3.0, 1.0, 3.0 ) );
     dir_light->castShadow       = true;
     dir_light->shadowBias       = -0.0005;
     dir_light->shadowMapSize    = glm::vec2(512);
     scene->add( dir_light );
     
     /* Create a spotlight, the shadow should be casted no the left hand side */
     auto spot_light = SpotLight::create(0x99CCFF, 1.0, 20.0, 50.0, 1.0 );
     spot_light->position = glm::vec3(3.0, 2.0, 3.0);
     spot_light->castShadow = true;
     scene->add( spot_light );
     
     /* Create an ambient light */
     scene->add( AmbientLight::create(0x777777));
     
     /* Create a post render callback for objects rotation */
     bool rotate_objects = false;
     float light_rotation_1 = 0.0;
     renderer.setPostRenderCallbackHandler( [&](){
         dir_light->position.x = ( 3.0 * cosf( light_rotation_1 ) );
         dir_light->position.z = ( 3.0 * sinf( light_rotation_1 ) );
         
         light_rotation_1 += 0.01;
         
         if( rotate_objects ) {
             cube->rotateX(-1.0f);
             cylinder->rotateX(1.0f);
         }
     });
     
     /* Override key callback handler */
     renderer.setKeyCallbackHandler([&](GLFWwindow *window, int key, int scancode, int action, int mod) {
         if( action == GLFW_PRESS ) {
             switch ( key) {
                 case GLFW_KEY_R: /* Toggle rotation */
                     rotate_objects = !rotate_objects;
                     break;
                     
                 default:
                     break;
             }
         }
     });
     
     renderer.setGamma( true, true );
     
     renderer.setClearColor( scene->getFog()->getColor() );
     renderer.render(scene, camera );
 }