File: background_plane.cpp

package info (click to toggle)
k3d 0.4.3.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 51,716 kB
  • ctags: 81,610
  • sloc: cpp: 283,698; ansic: 64,095; xml: 61,533; sh: 9,026; makefile: 5,282; python: 431; perl: 308; awk: 130
file content (162 lines) | stat: -rw-r--r-- 5,916 bytes parent folder | download
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
154
155
156
157
158
159
160
161
162
// 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