Example #1
0
double RayTracer::Shade(const Shape* pShapeToShade, const Point& rPointOnShapeToShade)
{
	double intensity = mAmbientIntensity * pShapeToShade->AmbientReflectivity(); // the ambient intensity of the scene
	Ray light_ray; // the ray that goes from the intersection point to the light sources
	double dot_product;
	Shape* closest_shape; // the shape closest from the intersection point to the light source
	Point light_intersect; // the intersection point of the ray that goes from the intersection point to the light source 
	light_ray.Origin(rPointOnShapeToShade); // lightRay. org= object. intersect;
	Ray light_ray_from_light;

	LightListIterator iter = mpLights->begin();
	mSpecularColor.Red(0);
	mSpecularColor.Green(0);
	mSpecularColor.Blue(0);

	while ( iter != mpLights->end() ) // foreach light in LightList do
	{

		light_ray.Direction(Vector(rPointOnShapeToShade,(*iter)->LightPoint())); // lightRay. dir= light. dir
		light_ray_from_light.Origin((*iter)->LightPoint());
		light_ray_from_light.Direction(Vector((*iter)->LightPoint(),rPointOnShapeToShade));

		closest_shape = QueryScene(light_ray_from_light, light_intersect);
		if ( closest_shape == NULL || (closest_shape == pShapeToShade && light_ray.Direction().Dot(pShapeToShade->Normal(rPointOnShapeToShade, light_ray_from_light.Origin() )) >= 0.0 )  ) //if (QueryScene( lightRay)= NIL)
		{
			Vector normal_vector = pShapeToShade->Normal(rPointOnShapeToShade, Point() );
			dot_product = normal_vector.Dot(light_ray.Direction().Normalize());
			dot_product *= (*iter)->Intensity();

			if (dot_product < 0.0)
			{
				if (pShapeToShade->Type() == "Triangle")
					dot_product = dot_product*-1.0;
				else
					dot_product = 0.0;
			}
			intensity = unit_limiter( intensity + dot_product );

			if ( light_ray.Direction().Dot(pShapeToShade->Normal(rPointOnShapeToShade, light_ray_from_light.Origin() )) >= 0.0 )
			{
				double specular = Specular(pShapeToShade, rPointOnShapeToShade, *iter);
				mSpecularColor = mSpecularColor + Color(specular,specular,specular);
			}
		}

		iter++;
	}

	return intensity;
}
Example #2
0
void Terrain::Draw(int type, const Camera& camera, const Light& light){


	//Get new position of the cube and update the model view matrix
    Eigen::Affine3f wMo;//object to world matrix
    Eigen::Affine3f cMw;
    Eigen::Affine3f proj;

    glUseProgram(m_shader);
#ifdef __APPLE__
    glBindVertexArrayAPPLE(m_vertexArrayObject); 
#else
	glBindVertexArray(m_vertexArrayObject);
#endif
    
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferObject);//use as current buffer
    
    GLint world2camera = glGetUniformLocation(m_shader, "cMw"); 
	GLint projection = glGetUniformLocation(m_shader, "proj");
    GLint kAmbient = glGetUniformLocation(m_shader,"kAmbient");
    GLint kDiffuse = glGetUniformLocation(m_shader,"kDiffuse");
    GLint kSpecular = glGetUniformLocation(m_shader,"kSpecular");
    GLint shininess = glGetUniformLocation(m_shader,"shininess");
    GLint camera_position = glGetUniformLocation(m_shader, "cameraPosition");
    GLint light_position = glGetUniformLocation(m_shader, "lightPosition");
    
    //generate the Angel::Angel::Angel::matrixes
    proj = Util::Perspective( camera.m_fovy, camera.m_aspect, camera.m_znear, camera.m_zfar );
	cMw = camera.m_cMw;//LookAt(camera.position,camera.lookat, camera.up );
    
    Eigen::Vector4f v4color(0.55,0.25,0.08,1.0);
    Eigen::Vector4f Ambient;
    Ambient = 0.3*v4color;
    Eigen::Vector4f Diffuse;
    Diffuse = 0.5*v4color;
    Eigen::Vector4f Specular(0.3,0.3,0.3,1.0);
    
    glUniformMatrix4fv( world2camera, 1, GL_FALSE, cMw.data() );
    glUniformMatrix4fv( projection, 1, GL_FALSE, proj.data() );
    
    glUniform4fv(kAmbient, 1, Ambient.data());
    glUniform4fv(kDiffuse, 1, Diffuse.data()); 
    glUniform4fv(kSpecular, 1, Specular.data());
    glUniform4fv(camera_position, 1, camera.m_position.data());
    glUniform4fv(light_position, 1, light.m_position.data());
    glUniform1f(shininess, 10);

    switch (type) {
        case DRAW_MESH:
            glUniform1i(glGetUniformLocation(m_shader, "renderType"), 1);
            glDrawArrays(GL_LINES, 0, m_NTrianglePoints);
            break;
        case DRAW_PHONG:
            glUniform1i(glGetUniformLocation(m_shader, "renderType"), 2);
            glDrawArrays(GL_TRIANGLES, 0, m_NTrianglePoints);
            break;
    }

	//draw the obstacles
	for(int i = 0; i < m_obstacles.size(); i++)
	{
		m_obstacles[i]->Draw(type,camera, light);
	}

	for(int i = 0; i < m_foods.size(); i++)
	{
		m_foods[i]->Draw(type,camera, light);
	}

	//draw the obstacles
	for(int i = 0; i < m_surface_objects.size(); i++)
	{
		m_surface_objects[i]->Draw(type,camera, light);
	}

}
Example #3
0
void ON_Light::Dump( ON_TextLog& dump ) const
{
  ON_BOOL32 bDumpDir = false;
  ON_BOOL32 bDumpLength = false;
  ON_BOOL32 bDumpWidth = false;

  const char* sStyle = "unknown";
  switch(Style())
  {
  //case ON::view_directional_light:
  //  sStyle = "view_directional_light";
  //  bDumpDir = true;
  //  break;
  //case ON::view_point_light:
  //  sStyle = "view_point_light";
  //  break;
  //case ON::view_spot_light:
  //  sStyle = "view_spot_light";
  //  bDumpDir = true;
  //  break;
  case ON::camera_directional_light:
    sStyle = "camera_directional_light";
    bDumpDir = true;
    break;
  case ON::camera_point_light:
    sStyle = "camera_point_light";
    break;
  case ON::camera_spot_light:
    sStyle = "camera_spot_light";
    bDumpDir = true;
    break;
  case ON::world_directional_light:
    sStyle = "world_directional_light";
    bDumpDir = true;
    break;
  case ON::world_point_light:
    sStyle = "world_point_light";
    break;
  case ON::world_spot_light:
    sStyle = "world_spot_light";
    bDumpDir = true;
    break;
  case ON::world_linear_light:
    sStyle = "linear_light";
    bDumpDir = true;
    bDumpLength = true;
    break;
  case ON::world_rectangular_light:
    sStyle = "rectangular_light";
    bDumpDir = true;
    bDumpLength = true;
    bDumpWidth = true;
    break;
  case ON::ambient_light:
    sStyle = "ambient_light";
    break;
  case ON::unknown_light_style:
    sStyle = "unknown";
    break;
  default:
    sStyle = "unknown";
    break;
  }
  dump.Print("index = %d  style = %s\n",LightIndex(),sStyle);

  dump.Print("location = "); dump.Print(Location()); dump.Print("\n");
  if ( bDumpDir )
    dump.Print("direction = "); dump.Print(Direction()); dump.Print("\n");
  if ( bDumpLength )
    dump.Print("length = "); dump.Print(Length()); dump.Print("\n");
  if ( bDumpWidth )
    dump.Print("width = "); dump.Print(Width()); dump.Print("\n");

  dump.Print("intensity = %g%%\n",Intensity()*100.0);

  dump.Print("ambient rgb = "); dump.PrintRGB(Ambient()); dump.Print("\n");
  dump.Print("diffuse rgb = "); dump.PrintRGB(Diffuse()); dump.Print("\n");
  dump.Print("specular rgb = "); dump.PrintRGB(Specular()); dump.Print("\n");

  dump.Print("spot angle = %g degrees\n",SpotAngleDegrees());
}
void Scene::BuildCornellBoxScene(std::vector<Shape*> &objects)
{
	camera = new Camera(
		Point(-260, 0, 90),
		Point(-199, 0, 90)
		);

	float lightEnergy = 35e8;

	lights.push_back(new SphereLight(Point(0, 0, 160), 3, Color(lightEnergy,lightEnergy,lightEnergy),1));

	Color walls(0.1f,0.1f,0.1f);
	Material* white = new Material(Ambient(0.3,0.3,0.3),Diffuse(0.9, 0.9, 0.9),walls);

	AddObject("files\\cornell_box\\cornell_box_floor.3ds", new Material(Color(0.3,0.3,0.3),Color(0.9,0.9,0.9),walls, Reflect(0.2, 0.2, 0.2)), objects);
	AddObject("files\\cornell_box\\cornell_box_ceil.3ds", white, objects);
	AddObject("files\\cornell_box\\cornell_box_right_wall.3ds", new Material(Color(0.,0.1,0.),Color(0.4,1,0.4),walls), objects);
	AddObject("files\\cornell_box\\cornell_box_back.3ds", white, objects);
	AddObject("files\\cornell_box\\cornell_box_left_wall.3ds", new Material(Color(0.1,0.,0.),Color(1,0.4,0.4),walls), objects);

	AddObject("files\\cornell_box\\cornell_box_box1.3ds", white, objects);
	AddObject("files\\cornell_box\\cornell_box_box2.3ds", white, objects);

	objects.push_back(new Sphere(Point(-35,-45,20),15,new Material(Ambient(0,0,0),Diffuse(0,0,0),Specular(0,0,0), Reflect(0,0,0), Refract(1,1,1), 1.51, 1), Shape::GetUniqueID()));
	objects.push_back(new Sphere(Point(-50,50,15),15,new Material(Ambient(0,0,0),Diffuse(0,0,0),Specular(0,0,0), Reflect(1,1,1)), Shape::GetUniqueID() ));
}