// K-3D
// Copyright (c) 1995-2004, Timothy M. Shead
//
// Contact: tshead@k-3d.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

/** \file
		\brief Implements the RenderManBackgroundPlane K-3D object, which renders a world-aligned plane, centered at the camera's position, with radius equal to the camera farplane
		\author Tim Shead (tshead@k-3d.com)
*/

#include <k3dsdk/imaterial.h>
#include <k3dsdk/iprojection.h>
#include <k3dsdk/material_collection.h>
#include <k3dsdk/module.h>
#include <k3dsdk/object.h>
#include <k3dsdk/persistence.h>
#include <k3dsdk/plugins.h>
#include <k3dsdk/property.h>
#include <k3dsdk/renderman.h>
#include <k3dsdk/transform.h>
#include <k3dsdk/vectors.h>

#ifdef	WIN32
#ifdef	near
#undef	near
#endif	//near
#ifdef	far
#undef	far
#endif	//far
#endif	//WIN32
namespace
{

/////////////////////////////////////////////////////////////////////////////
// background_plane_implementation

class background_plane_implementation :
	public k3d::material_collection<k3d::persistent<k3d::object> >,
	public k3d::ri::irenderable
{
	typedef k3d::material_collection<k3d::persistent<k3d::object> > base;

public:
	background_plane_implementation(k3d::idocument& Document) :
		base(Document),
		m_distance(k3d::init_name("distance") + k3d::init_description("Distance [scalar]") + k3d::init_value(1.0) + k3d::init_document(Document) + k3d::init_constraint(k3d::constraint::minimum(0.0, k3d::constraint::maximum(1.0))))
	{
		enable_serialization(k3d::persistence::proxy(m_distance));
		register_property(m_distance);
	}

	void renderman_pre_render(const k3d::ri::render_state& State)
	{
	}

	void renderman_render(const k3d::ri::render_state& State)
	{
		// We never generate shadows ...
		if(k3d::ri::SHADOW_MAP == State.render_context)
			return;
		// We only generate output for the last sample ...
		if(!k3d::ri::last_sample(State))
			return;

		k3d::iperspective* const perspective = dynamic_cast<k3d::iperspective*>(&State.projection);
		k3d::iorthographic* const orthographic = dynamic_cast<k3d::iorthographic*>(&State.projection);
		if(!perspective && !orthographic)
			{
				std::cerr << error << k3d_file_reference() << " unknown projection type" << std::endl;
				return;
			}
	
		double left, right, top, bottom, plane;
		if(perspective)
			{
				const double near = boost::any_cast<double>(k3d::get_property_value(document().dag(), perspective->near()));
				const double far = boost::any_cast<double>(k3d::get_property_value(document().dag(), perspective->far()));
				plane = k3d::mix(near, far, m_distance.property_value());
				left = plane * boost::any_cast<double>(k3d::get_property_value(document().dag(), perspective->left()));
				right = plane * boost::any_cast<double>(k3d::get_property_value(document().dag(), perspective->right()));
				top = plane * boost::any_cast<double>(k3d::get_property_value(document().dag(), perspective->top()));
				bottom = plane * boost::any_cast<double>(k3d::get_property_value(document().dag(), perspective->bottom()));
			}

		if(orthographic)
			{
				const double near = boost::any_cast<double>(k3d::get_property_value(document().dag(), orthographic->near()));
				const double far = boost::any_cast<double>(k3d::get_property_value(document().dag(), orthographic->far()));
				plane = k3d::mix(near, far, m_distance.property_value());
				left = boost::any_cast<double>(k3d::get_property_value(document().dag(), orthographic->left()));
				right = boost::any_cast<double>(k3d::get_property_value(document().dag(), orthographic->right()));
				top = boost::any_cast<double>(k3d::get_property_value(document().dag(), orthographic->top()));
				bottom = boost::any_cast<double>(k3d::get_property_value(document().dag(), orthographic->bottom()));
			}
		
		State.engine.RiAttributeBegin();
		State.engine.RiAttributeV("identifier", k3d::ri::parameter_list(1, k3d::ri::parameter("name", k3d::ri::UNIFORM, name())));
		State.engine.RiIdentity();
		State.engine.RiCoordSysTransform(k3d::ri::RI_CAMERA());
		
		k3d::ri::setup_material(m_material.object(), State);

		// Draw a plane ...
		k3d::ri::points points;
		points.push_back(k3d::ri::point(k3d::vector3(left, top, plane)));
		points.push_back(k3d::ri::point(k3d::vector3(right, top, plane)));
		points.push_back(k3d::ri::point(k3d::vector3(left, bottom, plane)));
		points.push_back(k3d::ri::point(k3d::vector3(right, bottom, plane)));
		State.engine.RiPatchV("bilinear", k3d::ri::parameter_list(1, k3d::ri::parameter(k3d::ri::RI_P(), k3d::ri::VERTEX, points)));

		State.engine.RiAttributeEnd();
	}

	k3d::iplugin_factory& factory()
	{
		return get_factory();
	}

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::plugin_factory<k3d::document_plugin<background_plane_implementation> >factory(
			k3d::uuid(0x80e1c8eb, 0x001d4342, 0xb401551d, 0xd5c45a3d),
			"RenderManBackgroundPlane",
			"",
			"Objects",
			k3d::iplugin_factory::STABLE);

		return factory;
	}

private:
	k3d_data_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::with_constraint) m_distance;
};

} // namespace

namespace libk3drenderman
{

k3d::iplugin_factory& background_plane_factory()
{
	return background_plane_implementation::get_factory();
}

} // namespace libk3drenderman



