File: background_plane.cpp

package info (click to toggle)
k3d 0.8.0.2-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 40,692 kB
  • ctags: 39,695
  • sloc: cpp: 171,303; ansic: 24,129; xml: 6,995; python: 5,796; makefile: 726; sh: 22
file content (153 lines) | stat: -rw-r--r-- 5,569 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// 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/document_plugin_factory.h>
#include <k3d-i18n-config.h>
#include <k3dsdk/imaterial.h>
#include <k3dsdk/iprojection.h>
#include <k3dsdk/material_sink.h>
#include <k3dsdk/node.h>
#include <k3dsdk/property.h>
#include <k3dsdk/renderable_ri.h>
#include <k3dsdk/vectors.h>

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

/////////////////////////////////////////////////////////////////////////////
// background_plane

class background_plane :
	public k3d::material_sink<k3d::node >,
	public k3d::ri::irenderable
{
	typedef k3d::material_sink<k3d::node > base;

public:
	background_plane(k3d::iplugin_factory& Factory, k3d::idocument& Document) :
		base(Factory, Document),
		m_distance(init_owner(*this) + init_name("distance") + init_label(_("distance")) + init_description(_("Distance")) + init_value(1.0) + init_constraint(k3d::data::constraint::minimum<double>(0.0, k3d::data::constraint::maximum<double>(1.0))))
	{
	}

	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)
		{
			k3d::log() << 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::property::pipeline_value(perspective->near()));
			const double far = boost::any_cast<double>(k3d::property::pipeline_value(perspective->far()));
			plane = k3d::mix(near, far, m_distance.pipeline_value());
			left = plane * boost::any_cast<double>(k3d::property::pipeline_value(perspective->left()));
			right = plane * boost::any_cast<double>(k3d::property::pipeline_value(perspective->right()));
			top = plane * boost::any_cast<double>(k3d::property::pipeline_value(perspective->top()));
			bottom = plane * boost::any_cast<double>(k3d::property::pipeline_value(perspective->bottom()));
		}

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

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

		State.stream.RiAttributeEnd();
	}

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

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::document_plugin_factory<background_plane >factory(
			k3d::uuid(0x80e1c8eb, 0x001d4342, 0xb401551d, 0xd5c45a3d),
			"RenderManBackgroundPlane",
			_("Places a shader on a bilinear patch, aligned with the farplane"),
			"RenderMan",
			k3d::iplugin_factory::STABLE);

		return factory;
	}

private:
	k3d_data(double, immutable_name, change_signal, with_undo, local_storage, with_constraint, writable_property, with_serialization) m_distance;
};

} // namespace

namespace libk3drenderman
{

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

} // namespace libk3drenderman