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
|
// 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)
*/
#include <k3dsdk/basic_math.h>
#include <k3dsdk/imaterial.h>
#include <k3dsdk/object.h>
#include <k3dsdk/persistence.h>
#include <k3dsdk/material.h>
#include <k3dsdk/material_collection.h>
#include <k3dsdk/measurement.h>
#include <k3dsdk/mesh_source.h>
#include <k3dsdk/module.h>
#include <k3dsdk/plugins.h>
#include <k3dsdk/renderman.h>
#include <k3dsdk/transform.h>
#include <boost/random/lagged_fibonacci.hpp>
#include <iterator>
namespace libk3dmesh
{
/////////////////////////////////////////////////////////////////////////////
// lawn_implementation
class lawn_implementation :
public k3d::material_collection<k3d::mesh_source<k3d::persistent<k3d::object> > >
{
typedef k3d::material_collection<k3d::mesh_source<k3d::persistent<k3d::object> > > base;
public:
lawn_implementation(k3d::idocument& Document) :
base(Document),
m_width(k3d::init_name("width") + k3d::init_description("Lawn width [number]") + k3d::init_document(Document) + k3d::init_value(10) + k3d::init_precision(2) + k3d::init_step_increment(0.1) + k3d::init_units(typeid(k3d::measurement::distance))),
m_depth(k3d::init_name("depth") + k3d::init_description("Lawn depth [number]") + k3d::init_document(Document) + k3d::init_value(10) + k3d::init_precision(2) + k3d::init_step_increment(0.1) + k3d::init_units(typeid(k3d::measurement::distance))),
m_blade_count(k3d::init_name("blade_count") + k3d::init_description("Blade count [unsigned integer]") + k3d::init_value(100) + k3d::init_document(Document) + k3d::init_constraint(k3d::constraint::minimum(0L)) + k3d::init_precision(0) + k3d::init_step_increment(1) + k3d::init_units(typeid(k3d::measurement::scalar))),
m_blade_width(k3d::init_name("blade_width") + k3d::init_description("Blade width [number]") + k3d::init_document(Document) + k3d::init_value(0.2) + k3d::init_precision(2) + k3d::init_step_increment(0.01) + k3d::init_units(typeid(k3d::measurement::distance))),
m_blade_height(k3d::init_name("blade_height") + k3d::init_description("Blade height [number]") + k3d::init_document(Document) + k3d::init_value(2) + k3d::init_precision(2) + k3d::init_step_increment(0.01) + k3d::init_units(typeid(k3d::measurement::distance)))
{
enable_serialization(k3d::persistence::proxy(m_width));
enable_serialization(k3d::persistence::proxy(m_depth));
enable_serialization(k3d::persistence::proxy(m_blade_count));
enable_serialization(k3d::persistence::proxy(m_blade_width));
enable_serialization(k3d::persistence::proxy(m_blade_height));
register_property(m_width);
register_property(m_depth);
register_property(m_blade_count);
register_property(m_blade_width);
register_property(m_blade_height);
m_material.changed_signal().connect(SigC::slot(*this, &lawn_implementation::on_reset_geometry));
m_width.changed_signal().connect(SigC::slot(*this, &lawn_implementation::on_reset_geometry));
m_depth.changed_signal().connect(SigC::slot(*this, &lawn_implementation::on_reset_geometry));
m_blade_count.changed_signal().connect(SigC::slot(*this, &lawn_implementation::on_reset_geometry));
m_blade_width.changed_signal().connect(SigC::slot(*this, &lawn_implementation::on_reset_geometry));
m_blade_height.changed_signal().connect(SigC::slot(*this, &lawn_implementation::on_reset_geometry));
m_output_mesh.need_data_signal().connect(SigC::slot(*this, &lawn_implementation::on_create_geometry));
}
void on_reset_geometry()
{
m_output_mesh.reset();
}
k3d::mesh* on_create_geometry()
{
const double lawn_width = m_width.property_value();
const double lawn_depth = m_depth.property_value();
const unsigned long blade_count = m_blade_count.property_value();
const double blade_width = m_blade_width.property_value();
const double blade_height = m_blade_height.property_value();
boost::lagged_fibonacci607 random;
k3d::mesh* const mesh = new k3d::mesh();
k3d::cubic_curve_group* const group = new k3d::cubic_curve_group();
group->material = m_material.interface();
for(unsigned long i = 0; i != blade_count; ++i)
{
k3d::cubic_curve* const curve = new k3d::cubic_curve();
const k3d::vector3 base = k3d::vector3((random()-0.5) * lawn_width, 0, (random()-0.5) * lawn_depth);
const k3d::vector3 n1 = base + k3d::vector3(0, blade_height * 0.5, 0);
const k3d::vector3 n2 = base + k3d::vector3(0, blade_height, 0);
const k3d::vector3 tip = base + k3d::vector3((random()-0.5) * blade_height, blade_height, (random()-0.5) * blade_height);
k3d::point* const base_p = new k3d::point(base);
k3d::point* const n1_p = new k3d::point(n1);
k3d::point* const n2_p = new k3d::point(n2);
k3d::point* const tip_p = new k3d::point(tip);
mesh->points.push_back(base_p);
mesh->points.push_back(n1_p);
mesh->points.push_back(n2_p);
mesh->points.push_back(tip_p);
curve->control_points.push_back(base_p);
curve->control_points.push_back(n1_p);
curve->control_points.push_back(n2_p);
curve->control_points.push_back(tip_p);
curve->varying_data.resize(2);
curve->varying_data[0]["width"] = blade_width;
curve->varying_data[1]["width"] = 0.0;
curve->varying_data[0]["N"] = k3d::ri::normal(k3d::vector3(random()-0.5, 0, random()-0.5));
curve->varying_data[1]["N"] = k3d::ri::normal(k3d::vector3(random()-0.5, 0, random()-0.5));
group->curves.push_back(curve);
}
mesh->cubic_curve_groups.push_back(group);
return mesh;
}
k3d::iplugin_factory& factory()
{
return get_factory();
}
static k3d::iplugin_factory& get_factory()
{
static k3d::plugin_factory<k3d::document_plugin<lawn_implementation>, k3d::interface_list<k3d::imesh_source > > factory(
k3d::uuid(0xc114dc24, 0xa659409b, 0x8909fa57, 0x1e228231),
"Lawn",
"Creates a simple grass lawn using cubic curves",
"Objects",
k3d::iplugin_factory::EXPERIMENTAL);
return factory;
}
private:
k3d_measurement_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_width;
k3d_measurement_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_depth;
k3d_measurement_property(long, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::with_constraint) m_blade_count;
k3d_measurement_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_blade_width;
k3d_measurement_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_blade_height;
};
/////////////////////////////////////////////////////////////////////////////
// lawn_factory
k3d::iplugin_factory& lawn_factory()
{
return lawn_implementation::get_factory();
}
} // namespace libk3dmesh
|