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
|
// K-3D
// Copyright (c) 1995-2006, 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)
*/
#include <k3d-i18n-config.h>
#include <k3dsdk/document_plugin_factory.h>
#include <k3dsdk/ibounded.h>
#include <k3dsdk/imaterial.h>
#include <k3dsdk/itransform_array_1d.h>
#include <k3dsdk/material_sink.h>
#include <k3dsdk/measurement.h>
#include <k3dsdk/node.h>
#include <k3dsdk/renderable_gl.h>
#include <k3dsdk/renderable_ri.h>
#include <k3dsdk/selection.h>
#include <k3dsdk/transformable.h>
namespace libk3drenderman
{
/////////////////////////////////////////////////////////////////////////////
// array_1d
class array_1d :
public k3d::material_sink<k3d::gl::renderable<k3d::ri::renderable<k3d::transformable<k3d::node > > > >
{
typedef k3d::material_sink<k3d::gl::renderable<k3d::ri::renderable<k3d::transformable<k3d::node > > > > base;
public:
array_1d(k3d::iplugin_factory& Factory, k3d::idocument& Document) :
base(Factory, Document),
m_source(init_owner(*this) + init_name("source") + init_label(_("Source")) + init_description(_("Source")) + init_value<k3d::ri::irenderable*>(0)),
m_layout(init_owner(*this) + init_name("layout") + init_label(_("Layout")) + init_description(_("Layout")) + init_value<k3d::itransform_array_1d*>(0)),
m_count(init_owner(*this) + init_name("count") + init_label(_("count")) + init_description(_("Count")) + init_value(5) + init_constraint(constraint::minimum<k3d::int32_t>(0)) + init_step_increment(1.0) + init_units(typeid(k3d::measurement::scalar)))
{
m_source.changed_signal().connect(make_async_redraw_slot());
m_layout.changed_signal().connect(make_async_redraw_slot());
m_count.changed_signal().connect(make_async_redraw_slot());
}
void draw(const k3d::gl::render_state& State)
{
k3d::ibounded* const bounded = dynamic_cast<k3d::ibounded*>(m_source.pipeline_value());
k3d::itransform_array_1d* const layout = m_layout.pipeline_value();
const unsigned long count = m_count.pipeline_value();
if(!layout)
return;
glDisable(GL_LIGHTING);
glColor3d(0, 1, 1);
glBegin(GL_POINTS);
for(unsigned long i = 0; i != count; ++i)
{
k3d::gl::vertex3d(k3d::point3(0, 0, 0) * layout->get_element(i, count));
}
glEnd();
if(!bounded)
return;
const k3d::bounding_box3 bounds = bounded->extents();
if(bounds.empty())
return;
glMatrixMode(GL_MODELVIEW);
for(unsigned long i = 0; i != count; ++i)
{
glPushMatrix();
k3d::gl::push_matrix(layout->get_element(i, count));
k3d::gl::draw_bounding_box(bounds);
glPopMatrix();
}
}
void on_gl_draw(const k3d::gl::render_state& State)
{
draw(State);
}
void on_gl_select(const k3d::gl::render_state& State, const k3d::gl::selection_state& SelectState)
{
k3d::gl::push_selection_token(this);
draw(State);
k3d::gl::pop_selection_token();
}
void on_renderman_render(const k3d::ri::render_state& State)
{
k3d::ri::irenderable* const renderable = m_source.pipeline_value();
if(!renderable)
return;
k3d::itransform_array_1d* const layout = m_layout.pipeline_value();
if(!layout)
return;
const unsigned long count = m_count.pipeline_value();
// Make sure we don't enter an infinite loop trying to render ourself ...
if(renderable == this)
{
k3d::log() << error << factory().name() << " [" << name() << "] cannot instance itself" << std::endl;
return;
}
const k3d::ri::object_handle handle = State.stream.RiObjectBegin();
k3d::ri::render_state state(State);
state.render_context = k3d::ri::OBJECT_INSTANCE;
renderable->renderman_render(state);
State.stream.RiObjectEnd();
k3d::ri::setup_material(m_material.pipeline_value(), State);
for(unsigned long i = 0; i != count; ++i)
{
State.stream.RiAttributeBegin();
State.stream.RiConcatTransform(k3d::ri::convert(layout->get_element(i, count)));
State.stream.RiObjectInstance(handle);
State.stream.RiAttributeEnd();
}
}
static k3d::iplugin_factory& get_factory()
{
static k3d::document_plugin_factory<array_1d > factory(
k3d::uuid(0x714f0863, 0x6af8447f, 0x9d5a556c, 0x5e2f6940),
"RenderManArray1D",
_("Renders a one-dimensional array of geometric object instances"),
"RenderMan Array",
k3d::iplugin_factory::STABLE);
return factory;
}
private:
k3d_data(k3d::ri::irenderable*, immutable_name, change_signal, with_undo, node_storage, no_constraint, node_property, node_serialization) m_source;
k3d_data(k3d::itransform_array_1d*, immutable_name, change_signal, with_undo, node_storage, no_constraint, node_property, node_serialization) m_layout;
k3d_data(k3d::int32_t, immutable_name, change_signal, with_undo, local_storage, with_constraint, measurement_property, with_serialization) m_count;
};
/////////////////////////////////////////////////////////////////////////////
// array_1d_factory
k3d::iplugin_factory& array_1d_factory()
{
return array_1d::get_factory();
}
} // namespace libk3drenderman
|