File: texture_map.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 (209 lines) | stat: -rw-r--r-- 8,290 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// 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
		\author Timothy M. Shead (tshead@k-3d.com)
		\author Romain Behar (romainbehar@yahoo.com)
*/

#include <k3dsdk/axis.h>
#include <k3dsdk/bitmap_filter.h>
#include <k3dsdk/file_filter.h>
#include <k3dsdk/ibitmap_write_format.h>
#include <k3dsdk/irenderman.h>
#include <k3dsdk/irender_farm.h>
#include <k3dsdk/object.h>
#include <k3dsdk/persistence.h>
#include <k3dsdk/measurement.h>
#include <k3dsdk/module.h>
#include <k3dsdk/plugins.h>

#include <iterator>

namespace libk3drenderman
{

/////////////////////////////////////////////////////////////////////////////
// texture_map_implementation

class texture_map_implementation :
	public k3d::bitmap_filter<k3d::persistent<k3d::object> >,
	public k3d::ri::itexture
{
	typedef k3d::bitmap_filter<k3d::persistent<k3d::object> > base;

public:
	texture_map_implementation(k3d::idocument& Document) :
		base(Document),
		m_swrap(k3d::init_name("swrap") + k3d::init_description("S Wrap [string]") + k3d::init_value(k3d::ri::RI_CLAMP()) + k3d::init_document(document()) + k3d::init_values(wrap_values())),
		m_twrap(k3d::init_name("twrap") + k3d::init_description("T Wrap [string]") + k3d::init_value(k3d::ri::RI_CLAMP()) + k3d::init_document(document()) + k3d::init_values(wrap_values())),
		m_filter(k3d::init_name("filter") + k3d::init_description("Filter [string]") + k3d::init_value(k3d::ri::RI_GAUSSIAN()) + k3d::init_document(document()) + k3d::init_values(filter_values())),
		m_swidth(k3d::init_name("swidth") + k3d::init_description("Filter S Width [positive real]") + k3d::init_value(2.0) + k3d::init_document(document()) + k3d::init_constraint(k3d::constraint::minimum(std::numeric_limits<double>::epsilon())) + k3d::init_precision(2) + k3d::init_step_increment(1.0) + k3d::init_units(typeid(k3d::measurement::scalar))),
		m_twidth(k3d::init_name("twidth") + k3d::init_description("Filter T Width [positive real]") + k3d::init_value(2.0) + k3d::init_document(document()) + k3d::init_constraint(k3d::constraint::minimum(std::numeric_limits<double>::epsilon())) + k3d::init_precision(2) + k3d::init_step_increment(1.0) + k3d::init_units(typeid(k3d::measurement::scalar)))
	{
		m_input.changed_signal().connect(SigC::slot(*this, &texture_map_implementation::on_reset_bitmap));
		m_output.need_data_signal().connect(SigC::slot(*this, &texture_map_implementation::on_create_bitmap));
		
		enable_serialization(k3d::persistence::proxy(m_swrap));
		enable_serialization(k3d::persistence::proxy(m_twrap));
		enable_serialization(k3d::persistence::proxy(m_filter));
		enable_serialization(k3d::persistence::proxy(m_swidth));
		enable_serialization(k3d::persistence::proxy(m_twidth));
		
		register_property(m_swrap);
		register_property(m_twrap);
		register_property(m_filter);
		register_property(m_swidth);
		register_property(m_twidth);
	}
	
	void setup_renderman_texture(k3d::irender_frame& Frame, k3d::ri::irender_engine& Engine)
	{
		m_ri_image_path = boost::filesystem::path();
		m_ri_texture_path = boost::filesystem::path();

		// If we don't have an input bitmap, we're done ...
		const k3d::bitmap* const input = m_input.property_value();
		if(!input)
			return;

		m_ri_image_path = Frame.add_input_file("texture");
		return_if_fail(!m_ri_image_path.empty());
		
		m_ri_texture_path = Frame.add_input_file("texture");
		return_if_fail(!m_ri_texture_path.empty());

/*
		// If "render from source" is enabled, just copy the source file to the frame directory ...
		if(m_render_from_source.property_value())
			{
				// Copy the file ...
#ifndef K3D_PLATFORM_WIN32_NATIVE
				const std::string copycommand = "cp " + m_image_path.value().native_file_string() + " " + m_ri_image_path.native_file_string();
#else // !K3D_PLATFORM_WIN32_NATIVE
				const std::string copycommand = "copy " + m_image_path.value().native_file_string() + " " + m_ri_image_path.native_file_string();
#endif // K3D_PLATFORM_WIN32_NATIVE
				system(copycommand.c_str());

			}
		// Otherwise, save the in-memory image data as a TIFF file ...
		else
*/
			{
				k3d::ibitmap_write_format* const filter = k3d::file_filter<k3d::ibitmap_write_format>("TIFFWriter");
				return_if_fail(filter);
				return_if_fail(filter->write_file(m_ri_image_path, *input));
			}
			
		Engine.RiMakeTextureV(m_ri_image_path.native_file_string(), m_ri_texture_path.native_file_string(), m_swrap.property_value(), m_twrap.property_value(), m_filter.property_value(), m_swidth.property_value(), m_twidth.property_value());
	}

	const boost::filesystem::path renderman_texture_path(const k3d::ri::render_state& State)
	{
		return m_ri_texture_path;
	}

	void on_reset_bitmap()
	{
		m_output.reset();
	}
	
	k3d::bitmap* on_create_bitmap()
	{
		// If we don't have any input bitmap, we're done ...
		const k3d::bitmap* const input = m_input.property_value();
		if(!input)
			return 0;

		// Otherwise, we make a copy of the input bitmap and modify the copy ...
		k3d::bitmap* const output = new k3d::bitmap(*input);

		return output;
	}

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

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::plugin_factory<
			k3d::document_plugin<texture_map_implementation>,
			k3d::interface_list<k3d::ibitmap_source,
			k3d::interface_list<k3d::ibitmap_sink,
			k3d::interface_list<k3d::ri::itexture> > > > factory(
				k3d::uuid(0x956071b5, 0xb1d0418b, 0x89c5037d, 0xf7618d54),
				"RenderManTextureMap",
				"Converts a bitmap into a RenderMan Texture Map",
				"Objects",
				k3d::iplugin_factory::STABLE);

		return factory;
	}

private:
	/// Stores the absolute path to the saved TIFF version of this texture_map (for use during RenderMan rendering)
	boost::filesystem::path m_ri_image_path;
	/// Stores the absolute path to the version of this texture_map converted by the renderer (via RiMakeTexture())
	boost::filesystem::path m_ri_texture_path;
	
	k3d_list_property(std::string, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_swrap;
	k3d_list_property(std::string, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_twrap;
	k3d_list_property(std::string, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_filter;
	k3d_measurement_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::with_constraint) m_swidth;
	k3d_measurement_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::with_constraint) m_twidth;

	const k3d::ilist_property<std::string>::values_t& wrap_values()
	{
		static k3d::ilist_property<std::string>::values_t values;
		if(values.empty())
			{
				values.push_back(k3d::ri::RI_BLACK());
				values.push_back(k3d::ri::RI_CLAMP());
				values.push_back(k3d::ri::RI_PERIODIC());
			}
		return values;
	}
	
	const k3d::ilist_property<std::string>::values_t& filter_values()
	{
		static k3d::ilist_property<std::string>::values_t values;
		if(values.empty())
			{
				values.push_back(k3d::ri::RI_GAUSSIAN());
				values.push_back(k3d::ri::RI_BOX());
				values.push_back(k3d::ri::RI_TRIANGLE());
				values.push_back(k3d::ri::RI_CATMULL_ROM());
				values.push_back(k3d::ri::RI_SINC());
			}
		return values;
	}
};

/////////////////////////////////////////////////////////////////////////////
// texture_map_factory

k3d::iplugin_factory& texture_map_factory()
{
	return texture_map_implementation::get_factory();
}

} // namespace libk3drenderman