Expose object node in XML API #8

Merged
Brecht Van Lommel merged 1 commits from howetuft/cycles:caustics into main 2024-01-15 20:04:23 +01:00
2 changed files with 132 additions and 13 deletions

View File

@ -0,0 +1,65 @@
<cycles>
<integrator
caustics_reflective="true"
caustics_refractive="true"
/>
<!--Camera-->
<transform
rotate="65.75944715 0.93999645 0.10960667 0.32309913"
translate="3 -12 5"
scale="1 1 -1" >
<camera
type="perspective"
fov="0.78539819"
full_width="800"
full_height="600"
/>
</transform>
<!--PointLight-->
<shader name="PointLight_shader">
<emission
name="PointLight_emit"
color="1.0 1.0 1.0"
strength="1000"
/>
<connect from="PointLight_emit emission" to="output surface"/>
</shader>
<state shader="PointLight_shader">
<light
name="PointLight"
light_type="point"
strength="1 1 1"
tfm="1 0 0 4 0 1 0 0 0 0 1 4"
use_caustics="true"
/>
</state>
<!--Suzanne-->
<shader name="Suzanne">
<glass_bsdf name="Suzanne_bsdf" IOR="1.5" color="1.0 1.0 1.0" />
<connect from="Suzanne_bsdf bsdf" to="output surface"/>
</shader>
<transform
rotate="90.0 0.0 0.0 1.0"
translate="0.0 0.0 1.0"
>
<object name="Suzanne" is_caustics_caster="true"/>
<state interpolation="smooth" shader="Suzanne" object="Suzanne">
<include src="objects/suzanne.xml"/>
</state>
</transform>
<!--Ground-->
<shader name="Ground">
<diffuse_bsdf name="Ground_bsdf" color="0.60 0.60 0.60"/>
<connect from="Ground_bsdf bsdf" to="output surface"/>
</shader>
<object name="Ground" is_caustics_receiver="true" />
<state interpolation="smooth" shader="Ground" object="Ground">
<mesh P="-6 6 0 6 6 0 6 -6 0 -6 -6 0" nverts="4" verts="0 1 2 3" />
</state>
</cycles>

View File

@ -46,8 +46,9 @@ struct XMLReadState : public XMLReader {
Shader *shader; /* Current shader. */
string base; /* Base path to current file. */
float dicing_rate; /* Current dicing rate. */
Object *object; /* Current object. */
XMLReadState() : scene(NULL), smooth(false), shader(NULL), dicing_rate(1.0f)
XMLReadState() : scene(NULL), smooth(false), shader(NULL), dicing_rate(1.0f), object(NULL)
{
tfm = transform_identity();
}
@ -405,9 +406,16 @@ static void xml_read_background(XMLReadState &state, xml_node node)
/* Mesh */
static Mesh *xml_add_mesh(Scene *scene, const Transform &tfm)
static Mesh *xml_add_mesh(Scene *scene, const Transform &tfm, Object *object)
{
/* create mesh */
if (object && object->get_geometry()->is_mesh()) {
/* Use existing object and mesh */
object->set_tfm(tfm);
Geometry *geometry = object->get_geometry();
return static_cast<Mesh *>(geometry);
}
else {
/* Create mesh */
Mesh *mesh = new Mesh();
scene->geometry.push_back(mesh);
@ -418,12 +426,13 @@ static Mesh *xml_add_mesh(Scene *scene, const Transform &tfm)
scene->objects.push_back(object);
return mesh;
}
}
static void xml_read_mesh(const XMLReadState &state, xml_node node)
{
/* add mesh */
Mesh *mesh = xml_add_mesh(state.scene, state.tfm);
Mesh *mesh = xml_add_mesh(state.scene, state.tfm, state.object);
array<Node *> used_shaders = mesh->get_used_shaders();
used_shaders.push_back_slow(state.shader);
mesh->set_used_shaders(used_shaders);
@ -613,7 +622,7 @@ static void xml_read_transform(xml_node node, Transform &tfm)
static void xml_read_state(XMLReadState &state, xml_node node)
{
/* read shader */
/* Read shader */
string shadername;
if (xml_read_string(&shadername, node, "shader")) {
@ -632,6 +641,25 @@ static void xml_read_state(XMLReadState &state, xml_node node)
}
}
/* Read object */
string objectname;
if (xml_read_string(&objectname, node, "object")) {
bool found = false;
foreach (Object *object, state.scene->objects) {
if (object->name == objectname) {
state.object = object;
found = true;
break;
}
}
if (!found) {
fprintf(stderr, "Unknown object \"%s\".\n", objectname.c_str());
}
}
xml_read_float(&state.dicing_rate, node, "dicing_rate");
/* read smooth/flat */
@ -643,6 +671,26 @@ static void xml_read_state(XMLReadState &state, xml_node node)
}
}
/* Object */
static void xml_read_object(XMLReadState &state, xml_node node)
{
Scene *scene = state.scene;
/* create mesh */
Mesh *mesh = new Mesh();
scene->geometry.push_back(mesh);
/* create object */
Object *object = new Object();
object->set_geometry(mesh);
object->set_tfm(state.tfm);
xml_read_node(state, object, node);
scene->objects.push_back(object);
}
/* Scene */
static void xml_read_include(XMLReadState &state, const string &src);
@ -690,6 +738,12 @@ static void xml_read_scene(XMLReadState &state, xml_node scene_node)
xml_read_include(state, src);
}
}
else if (string_iequals(node.name(), "object")) {
XMLReadState substate = state;
xml_read_object(substate, node);
xml_read_scene(substate, node);
}
#ifdef WITH_ALEMBIC
else if (string_iequals(node.name(), "alembic")) {
xml_read_alembic(state, node);