コード例 #1
0
ファイル: triangle.cpp プロジェクト: dtbinh/raytracer
/*recursively computes the specular component of this triangle
 *identically implement as that of sphere. After ray hits the surface position 
 *it will spawn a reflected ray of the surface, and potentailly a refracted ray
 *we then recursively call the specular function on those new rays*/
Color3 Triangle::compute_specular(const Scene* scene, const Vector3 &normal, 
const Vector3 &incoming_ray, const Vector3 &surface_pos, int depth) const{

	Geometry* const* sceneObjects = scene->get_geometries();
        Vector3 refl_ray = normalize(incoming_ray - 2*dot(incoming_ray,normal)*normal);
        Vector3 slop_pos = surface_pos + EPSILON*refl_ray;
        real_t minTime = UNINITIALIZED;
        Geometry* geo = sceneObjects[0];
        Color3 tex_color = compute_texture();
        for(unsigned int i=0;i<scene->num_geometries(); i++){
        Geometry* temp_geo = sceneObjects[i];
                 if(temp_geo->is_intersecting(refl_ray,slop_pos,&minTime) != NOINTERSECTION){
                         geo = temp_geo;
                 }
 
        }
        if(minTime != UNINITIALIZED){
                Vector3 new_pos = surface_pos + refl_ray*minTime;
                Color3 returnColor = geo->color_at_pixel(scene,new_pos);
                if(depth == 1)  
                        return returnColor;
                else{
                        int new_depth = depth-1;
                        Color3 specular = geo->get_specular();
                        Vector3 new_norm = geo->normal_of(new_pos);
                        // RECURSIVE CALL IS HERE!!!!
                	return tex_color*(returnColor + specular*geo->compute_specular(scene,new_norm,refl_ray,new_pos,new_depth));
         	     }    
        }    
	return tex_color*scene->background_color;
}
コード例 #2
0
ファイル: triangle.cpp プロジェクト: dtbinh/raytracer
/* based on the ambient, and diffuse components of color 
 * and the barycentric coordinates of the point on the triangle
 * we compute the color at the given pixel coordinates
 * and then interpolate with alpha beta gamma
 * */
Color3 Triangle::color_at_pixel(const Scene* scene, const Vector3 &surface_pos) const {

	Color3 ambA = vertices[0].material->ambient;
	Color3 ambB = vertices[1].material->ambient;
	Color3 ambC = vertices[2].material->ambient;

	Color3 diffA = vertices[0].material->diffuse;
	Color3 diffB = vertices[1].material->diffuse;
	Color3 diffC = vertices[2].material->diffuse;

	Color3 bary_amb  = ALPHA*ambA + BETA*ambB + GAMMA*ambC;
	Color3 bary_diff = ALPHA*diffA + BETA*diffB + GAMMA*diffC;
	Vector3 bary_normal = normal_of(surface_pos);
	Color3 tex_color = compute_texture();
	return tex_color*(scene->ambient_light*bary_amb + bary_diff*compute_diffuse(scene,bary_normal, surface_pos));
}
コード例 #3
0
ファイル: Scene.cpp プロジェクト: weaselp/cgal
void Scene::compute_elements(int mode)
{
    pos_points.resize(0);
    pos_lines.resize(0);
    pos_poly.resize(0);
    pos_cut_segments.resize(0);
    tex_map.resize(0);
    pos_grid.resize(66);
    pos_plane.resize(18);
    float diag = .6f * float(bbox_diag());
    //The Points
    {
        std::list<Point>::iterator pit;
        for(pit = m_points.begin(); pit != m_points.end(); pit++)
        {
            const Point& p = *pit;
            pos_points.push_back(p.x());
            pos_points.push_back(p.y());
            pos_points.push_back(p.z());
        }
    }
    //The Segements
    {
        std::list<Segment>::iterator sit;
        for(sit = m_segments.begin(); sit != m_segments.end(); sit++)
        {
            const Segment& s = *sit;
            const Point& p = s.source();
            const Point& q = s.target();

            pos_lines.push_back(p.x());
            pos_lines.push_back(p.y());
            pos_lines.push_back(p.z());

            pos_lines.push_back(q.x());
            pos_lines.push_back(q.y());
            pos_lines.push_back(q.z());
        }
    }
    //The Polygon's edges
    {
        Polyhedron::Edge_iterator he;
        for(he = m_pPolyhedron->edges_begin();
            he != m_pPolyhedron->edges_end();
            he++)
        {
            const Point& a = he->vertex()->point();
            const Point& b = he->opposite()->vertex()->point();
            pos_poly.push_back(a.x());
            pos_poly.push_back(a.y());
            pos_poly.push_back(a.z());

            pos_poly.push_back(b.x());
            pos_poly.push_back(b.y());
            pos_poly.push_back(b.z());
        }
    }
    //The cutting segments
    {
        for ( std::vector<Segment>::const_iterator csit = m_cut_segments.begin(),
              end = m_cut_segments.end() ; csit != end ; ++csit )
        {
            const Point& a = csit->source();
            const Point& b = csit->target();

            pos_cut_segments.push_back(a.x());
            pos_cut_segments.push_back(a.y());
            pos_cut_segments.push_back(a.z());

            pos_cut_segments.push_back(b.x());
            pos_cut_segments.push_back(b.y());
            pos_cut_segments.push_back(b.z());
        }
    }
    //The cutting plane
    {

        pos_plane[0]= -diag; pos_plane[1]=-diag; pos_plane[2]=0.0;
        pos_plane[3]= -diag; pos_plane[4]= diag; pos_plane[5]=0.;
        pos_plane[6]=  diag; pos_plane[7]= diag; pos_plane[8]=0.;
        pos_plane[9]= -diag; pos_plane[10]= -diag; pos_plane[11]=0.;
        pos_plane[12]= diag;    pos_plane[13]= diag; pos_plane[14]= 0.;
        pos_plane[15]= diag;    pos_plane[16]= -diag; pos_plane[17]= 0.;

        //UV Mapping
        tex_map.push_back(-0.11f);
        tex_map.push_back(-0.11f);

        tex_map.push_back(-0.11f);
        tex_map.push_back(1.11f);

        tex_map.push_back(1.11f);
        tex_map.push_back(1.11f);

        tex_map.push_back(-0.11f);
        tex_map.push_back(-0.11f);

        tex_map.push_back(1.11f);
        tex_map.push_back(1.11f);

        tex_map.push_back(1.11f);
        tex_map.push_back(-0.11f);





    }
    //The grid
    {
        float z = 0;
        float x = (2 * diag)/10.0;
        float y = (2 * diag)/10.0;
        for(int u = 0; u < 11; u++)
        {

            pos_grid.push_back(-diag + x* u);
            pos_grid.push_back(-diag);
            pos_grid.push_back(z);

            pos_grid.push_back(-diag + x* u);
            pos_grid.push_back(diag);
            pos_grid.push_back(z);
        }
        for(int v=0; v<11; v++)
        {

            pos_grid.push_back(-diag);
            pos_grid.push_back(-diag + v * y);
            pos_grid.push_back(z);

            pos_grid.push_back(diag);
            pos_grid.push_back(-diag + v * y);
            pos_grid.push_back(z);

        }

    }
    //The texture
    switch(mode)
    {
    case _SIGNED:
        for( int i=0 ; i < texture->getWidth(); i++ )
        {
            for( int j=0 ; j < texture->getHeight() ; j++)
            {
                compute_texture(i,j,m_red_ramp,m_blue_ramp);
            }
        }
        break;
    case _UNSIGNED:
        for( int i=0 ; i < texture->getWidth(); i++ )
        {
            for( int j=0 ; j < texture->getHeight() ; j++)
            {
                compute_texture(i,j,m_thermal_ramp,m_thermal_ramp);
            }
        }
        break;}
    sampler_location = tex_rendering_program.attributeLocation("texture");
}
コード例 #4
0
void Scene_implicit_function_item::compute_elements()
{
    v_cube.resize(0);
    v_plan.resize(0);
    texture_map.resize(0);
    // The Quad
       {


           //A
         v_plan.push_back(bbox().xmin);
         v_plan.push_back(bbox().ymin);
         v_plan.push_back(0);


           //B
         v_plan.push_back(bbox().xmin);
         v_plan.push_back(bbox().ymax);
         v_plan.push_back(0);


           //C
         v_plan.push_back(bbox().xmax);
         v_plan.push_back(bbox().ymin);
         v_plan.push_back(0);



           //C
         v_plan.push_back(bbox().xmax);
         v_plan.push_back(bbox().ymin);
         v_plan.push_back(0);


           //B
         v_plan.push_back(bbox().xmin);
         v_plan.push_back(bbox().ymax);
         v_plan.push_back(0);


           //D
         v_plan.push_back(bbox().xmax);
         v_plan.push_back(bbox().ymax);
         v_plan.push_back(0);


           //UV Mapping
           texture_map.push_back(0.0);
           texture_map.push_back(0.0);

           texture_map.push_back(0.0);
           texture_map.push_back(1.0);

           texture_map.push_back(1.0);
           texture_map.push_back(0.0);

           texture_map.push_back(1.0);
           texture_map.push_back(0.0);

           texture_map.push_back(0.0);
           texture_map.push_back(1.0);

           texture_map.push_back(1.0);
           texture_map.push_back(1.0);
       }
    //The box
    {

        v_cube.push_back(bbox().xmin);
        v_cube.push_back(bbox().ymin);
        v_cube.push_back(bbox().zmin);

        v_cube.push_back(bbox().xmin);
        v_cube.push_back(bbox().ymin);
        v_cube.push_back(bbox().zmax);

        v_cube.push_back(bbox().xmin);
        v_cube.push_back(bbox().ymin);
        v_cube.push_back(bbox().zmin);

        v_cube.push_back(bbox().xmin);
        v_cube.push_back(bbox().ymax);
        v_cube.push_back(bbox().zmin);

        v_cube.push_back(bbox().xmin);
        v_cube.push_back(bbox().ymin);
        v_cube.push_back(bbox().zmin);

        v_cube.push_back(bbox().xmax);
        v_cube.push_back(bbox().ymin);
        v_cube.push_back(bbox().zmin);

        v_cube.push_back(bbox().xmax);
        v_cube.push_back(bbox().ymin);
        v_cube.push_back(bbox().zmin);

        v_cube.push_back(bbox().xmax);
        v_cube.push_back(bbox().ymax);
        v_cube.push_back(bbox().zmin);

        v_cube.push_back(bbox().xmax);
        v_cube.push_back(bbox().ymin);
        v_cube.push_back(bbox().zmin);

        v_cube.push_back(bbox().xmax);
        v_cube.push_back(bbox().ymin);
        v_cube.push_back(bbox().zmax);

        v_cube.push_back(bbox().xmin);
        v_cube.push_back(bbox().ymax);
        v_cube.push_back(bbox().zmin);

        v_cube.push_back(bbox().xmin);
        v_cube.push_back(bbox().ymax);
        v_cube.push_back(bbox().zmax);

        v_cube.push_back(bbox().xmin);
        v_cube.push_back(bbox().ymax);
        v_cube.push_back(bbox().zmin);

        v_cube.push_back(bbox().xmax);
        v_cube.push_back(bbox().ymax);
        v_cube.push_back(bbox().zmin);

        v_cube.push_back(bbox().xmax);
        v_cube.push_back(bbox().ymax);
        v_cube.push_back(bbox().zmin);

        v_cube.push_back(bbox().xmax);
        v_cube.push_back(bbox().ymax);
        v_cube.push_back(bbox().zmax);

        v_cube.push_back(bbox().xmin);
        v_cube.push_back(bbox().ymin);
        v_cube.push_back(bbox().zmax);

        v_cube.push_back(bbox().xmin);
        v_cube.push_back(bbox().ymax);
        v_cube.push_back(bbox().zmax);

        v_cube.push_back(bbox().xmin);
        v_cube.push_back(bbox().ymin);
        v_cube.push_back(bbox().zmax);

        v_cube.push_back(bbox().xmax);
        v_cube.push_back(bbox().ymin);
        v_cube.push_back(bbox().zmax);

        v_cube.push_back(bbox().xmax);
        v_cube.push_back(bbox().ymax);
        v_cube.push_back(bbox().zmax);

        v_cube.push_back(bbox().xmin);
        v_cube.push_back(bbox().ymax);
        v_cube.push_back(bbox().zmax);

        v_cube.push_back(bbox().xmax);
        v_cube.push_back(bbox().ymax);
        v_cube.push_back(bbox().zmax);

        v_cube.push_back(bbox().xmax);
        v_cube.push_back(bbox().ymin);
        v_cube.push_back(bbox().zmax);

    }
    //The texture
   for(int i=0; i < texture->getWidth(); i++)
       for( int j=0 ; j < texture->getHeight() ; j++)
       {
          compute_texture(i,j);
       }
}