Exemplo n.º 1
0
void shade_floor(int radius) {
  glBegin(GL_POINTS);
  Vector3d normal_hat = Vector3d(0.0,cos(0.25),-sin(0.25));
  for (int x = 0; x < viewport.w; x++) {
    for (int y = 0; y < viewport.h; y++) {  
      Vector3d pixel_color = Vector3d::Zero();
    
      Vector3d pos = Vector3d(x, 0, -y);

      // Calculate intensity
      Vector3d intensity = Vector3d::Zero();

      // Loop over point lights
      for( int i = 0; i < pl_color.size(); i++ ) {
        Vector3d i_pl = ((pl_pos[i] * radius) + Vector3d(trans_x, trans_y, 0) - pos);
        // Diffuse light
        Vector3d i_hat_pl = i_pl.normalized();
        double i_pl_dot_n = (i_hat_pl.dot( normal_hat ));
        Vector3d diff_pl = kd.cwise() * pl_color[i] * max(0.0, i_pl_dot_n);
        // Specular light 
        Vector3d r_pl = -i_hat_pl + 2 * i_pl_dot_n * normal_hat;
        Vector3d spec_pl = ks.cwise() * pl_color[i] * pow(max(0.0, r_pl.normalized().dot( Vector3d(0.0,0.0,1.0) )), sp);

        pixel_color += diff_pl + spec_pl;
        intensity += pl_color[i];
      }
      // Loop over directional lights
      for( int i = 0; i < dl_color.size(); i++ ) {
        Vector3d i_dl = dl_dir[i];
        // Diffuse light
        Vector3d i_hat_dl = -dl_dir[i].normalized();
        double i_dl_dot_n = (i_hat_dl.dot( normal_hat ));
        Vector3d diff_dl = kd.cwise() * dl_color[i] * max(0.0, i_dl_dot_n);
        // Specular light 
        Vector3d r_dl = -i_hat_dl + 2 * i_dl_dot_n * normal_hat;
        Vector3d spec_dl = ks.cwise() * dl_color[i] * pow(max(0.0, r_dl.normalized().dot( Vector3d(0.0,0.0,1.0) )), sp);

        pixel_color += diff_dl + spec_dl;
        intensity += dl_color[i];
      }

      // Ambient light
      Vector3d amb = ka.cwise() * intensity;

      pixel_color += amb;
//      cout << pixel_color(0) << ", " << pixel_color(1) << ", " << pixel_color(2) << endl;
      // Set the pixel color
      setPixel(x, y/2, pixel_color(0)*(viewport.h-y)/viewport.h, pixel_color(1)*(viewport.h-y)/viewport.h, pixel_color(2)*(viewport.h-y)/viewport.h);

    }
  }
  glEnd();
}
bool
MeshGeometry::handleRayPick(const Eigen::Vector3d& pickOrigin,
                            const Eigen::Vector3d& pickDirection,
                            double /* clock */,
                            double* distance) const
{
    Vector3d meshScale = m_meshScale.cast<double>();
    Matrix3d invScale = meshScale.cwise().inverse().asDiagonal();
    Vector3d origin = invScale * pickOrigin;
    Vector3d direction = (invScale * pickDirection).normalized();

    double closestHit = numeric_limits<double>::infinity();

    for (vector<counted_ptr<Submesh> >::const_iterator iter = m_submeshes.begin();
         iter != m_submeshes.end(); ++iter)
    {
        double submeshDistance = 0.0;

        // TODO: Check the bounding volume of the submesh before doing the actual mesh
        // intersection test.
        if ((*iter)->rayPick(origin, direction, &submeshDistance))
        {
            if (submeshDistance < closestHit)
            {
                closestHit = submeshDistance;
            }
        }
    }

    if (closestHit < numeric_limits<double>::infinity())
    {
        *distance = (meshScale.cwise() * direction).norm() * closestHit;
        return true;
    }
    else
    {
        return false;
    }
}
Exemplo n.º 3
0
void shaded_sphere(int radius, int x_offset, int y_offset) {
  // Draw inner circle
  glBegin(GL_POINTS);

  for (int y = -radius; y <= radius; y++) {
    int width = (int)(sqrt((double)(radius*radius-y*y)) + 0.5f);
    for (int x = -width; x <= width; x++) {

      Vector3d pixel_color = Vector3d::Zero();

      // Calculate normal
      double z = sqrt(radius*radius - x*x - y*y); 
      Vector3d normal = Vector3d(x,y,z);
      Vector3d normal_hat = normal.normalized();

      // Calculate intensity
      Vector3d intensity = Vector3d::Zero();

      assert( pl_color.size() == pl_pos.size());
      assert( dl_color.size() == dl_dir.size());


      // Loop over point lights
      for( int i = 0; i < pl_color.size(); i++ ) {
        bool intersection = false;

        double their_t;
        Vector3d i_pl = -((pl_pos[i] * radius) + Vector3d(trans_x, trans_y, 0) - Vector3d(x_offset, y_offset,0)- normal);
#if 0
        for( int j = 0; j < spheres.size(); j++ ) {
          Vector3d pl_trans = ((pl_pos[i] * radius)) - Vector3d(spheres[j].x_offset, spheres[j].y_offset,0);
          if( spheres[j].radius == radius && spheres[j].x_offset == x_offset && spheres[j].y_offset == y_offset ) {
            continue;
          }
          else {
            if (intersect( pl_trans, i_pl, their_t, (double) radius) && their_t < 1.0) {
              //cout << "INTERSECT" << endl; 
              intersection =  true;
              break;
            }
          }

        }
#endif
        if( !intersection ) {
          // Diffuse light
          Vector3d i_hat_pl = -i_pl.normalized();
          double i_pl_dot_n = (i_hat_pl.dot( normal_hat ));
          Vector3d diff_pl = kd.cwise() * pl_color[i] * max(0.0, i_pl_dot_n);
          // Specular light 
          Vector3d r_pl = -i_hat_pl + 2 * i_pl_dot_n * normal_hat;
          Vector3d spec_pl = ks.cwise() * pl_color[i] * pow(max(0.0, r_pl.normalized().dot( Vector3d(0.0,0.0,1.0) )), sp);

          pixel_color += diff_pl + spec_pl;
          intensity += pl_color[i];
        }
      }
      // Loop over directional lights
      for( int i = 0; i < dl_color.size(); i++ ) {

        bool intersection = false;
        double their_t;
        Vector3d i_dl = dl_dir[i];
#if 0
        for( int j = 0; j < spheres.size(); j++ ) {
          Vector3d dl_pos = 2*(Vector3d(spheres[j].x_offset, spheres[j].y_offset, 0) - Vector3d(x_offset, y_offset, 0)) + normal;

          if( spheres[j].radius == radius && spheres[j].x_offset == x_offset && spheres[j].y_offset == y_offset ) {
            continue;
          }
          else {
            if (intersect(dl_pos, i_dl, their_t, (double) radius)) {
              //cout << "INTERSECT" << endl; 
              intersection =  true;
              break;
            }
          }

        }
#endif
        if( !intersection ) {
          // Diffuse light
          Vector3d i_hat_dl = -dl_dir[i].normalized();
          double i_dl_dot_n = (i_hat_dl.dot( normal_hat ));
          Vector3d diff_dl = kd.cwise() * dl_color[i] * max(0.0, i_dl_dot_n);
          // Specular light 
          Vector3d r_dl = -i_hat_dl + 2 * i_dl_dot_n * normal_hat;
          Vector3d spec_dl = ks.cwise() * dl_color[i] * pow(max(0.0, r_dl.normalized().dot( Vector3d(0.0,0.0,1.0) )), sp);

          pixel_color += diff_dl + spec_dl;
          intensity += dl_color[i];
        }
      }

      // Ambient light
      Vector3d amb = ka.cwise() * intensity;

      pixel_color += amb;
      // Set the pixel color
      setPixel(x + x_offset, y + y_offset, pixel_color(0), pixel_color(1), pixel_color(2));
    }
  }
  /*
  for (int i = 0 ; i < pl_pos.size(); i++) {
    Vector3d lightPos = pl_pos[i]*radius;
    for (int x = 0; x < 5; x++) {
      for (int y = 0; y < 5; y++) { 
        setPixel(lightPos(0)+trans_x+x, lightPos(1)+trans_y+y, pl_color[i](0), pl_color[i](1), pl_color[i](2));
      }
    }
  }
  */
  glEnd();
}