blender/intern/cycles/render/object.cpp
Ton Roosendaal da376e0237 Cycles render engine, initial commit. This is the engine itself, blender modifications and build instructions will follow later.
Cycles uses code from some great open source projects, many thanks them:

* BVH building and traversal code from NVidia's "Understanding the Efficiency of Ray Traversal on GPUs":
http://code.google.com/p/understanding-the-efficiency-of-ray-traversal-on-gpus/
* Open Shading Language for a large part of the shading system:
http://code.google.com/p/openshadinglanguage/
* Blender for procedural textures and a few other nodes.
* Approximate Catmull Clark subdivision from NVidia Mesh tools:
http://code.google.com/p/nvidia-mesh-tools/
* Sobol direction vectors from:
http://web.maths.unsw.edu.au/~fkuo/sobol/
* Film response functions from:
http://www.cs.columbia.edu/CAVE/software/softlib/dorf.php
2011-04-27 11:58:34 +00:00

214 lines
5.1 KiB
C++

/*
* Copyright 2011, Blender Foundation.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "device.h"
#include "mesh.h"
#include "object.h"
#include "scene.h"
#include "util_foreach.h"
#include "util_progress.h"
CCL_NAMESPACE_BEGIN
/* Object */
Object::Object()
{
name = "";
mesh = NULL;
tfm = transform_identity();
}
Object::~Object()
{
}
void Object::compute_bounds()
{
bounds = mesh->bounds.transformed(&tfm);
}
void Object::apply_transform()
{
if(!mesh || tfm == transform_identity())
return;
for(size_t i = 0; i < mesh->verts.size(); i++)
mesh->verts[i] = transform(&tfm, mesh->verts[i]);
Attribute *attr_fN = mesh->attributes.find(Attribute::STD_FACE_NORMAL);
Attribute *attr_vN = mesh->attributes.find(Attribute::STD_VERTEX_NORMAL);
Transform ntfm = transform_transpose(transform_inverse(tfm));
if(attr_fN) {
float3 *fN = attr_fN->data_float3();
for(size_t i = 0; i < mesh->triangles.size(); i++)
fN[i] = transform_direction(&ntfm, fN[i]);
}
if(attr_vN) {
float3 *vN = attr_vN->data_float3();
for(size_t i = 0; i < mesh->verts.size(); i++)
vN[i] = transform_direction(&ntfm, vN[i]);
}
if(bounds.valid()) {
mesh->compute_bounds();
compute_bounds();
}
tfm = transform_identity();
}
void Object::tag_update(Scene *scene)
{
if(mesh && mesh->transform_applied)
mesh->need_update = true;
scene->mesh_manager->need_update = true;
scene->object_manager->need_update = true;
}
/* Object Manager */
ObjectManager::ObjectManager()
{
need_update = true;
}
ObjectManager::~ObjectManager()
{
}
void ObjectManager::device_update_transforms(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
{
float4 *objects = dscene->objects.resize(OBJECT_SIZE*scene->objects.size());
int i = 0;
foreach(Object *ob, scene->objects) {
Mesh *mesh = ob->mesh;
/* compute transformations */
Transform tfm = ob->tfm;
Transform itfm = transform_inverse(tfm);
Transform ntfm = transform_transpose(itfm);
/* compute surface area */
/* todo: correct for displacement, and move to a better place */
float surfacearea = 0.0f;
foreach(Mesh::Triangle& t, mesh->triangles) {
float3 p1 = transform(&tfm, mesh->verts[t.v[0]]);
float3 p2 = transform(&tfm, mesh->verts[t.v[1]]);
float3 p3 = transform(&tfm, mesh->verts[t.v[2]]);
surfacearea += triangle_area(p1, p2, p3);
}
/* pack in texture */
int offset = i*OBJECT_SIZE;
memcpy(&objects[offset], &tfm, sizeof(float4)*4);
memcpy(&objects[offset+4], &itfm, sizeof(float4)*4);
memcpy(&objects[offset+8], &ntfm, sizeof(float4)*4);
objects[offset+12] = make_float4(surfacearea, 0.0f, 0.0f, 0.0f);
i++;
if(progress.get_cancel()) return;
}
device->tex_alloc("__objects", dscene->objects);
}
void ObjectManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
{
if(!need_update)
return;
device_free(device, dscene);
if(scene->objects.size() == 0)
return;
/* set object transform matrices, before applying static transforms */
progress.set_status("Updating Objects", "Copying Transformations to device");
device_update_transforms(device, dscene, scene, progress);
if(progress.get_cancel()) return;
/* prepare for static BVH building */
/* todo: do before to support getting object level coords? */
if(scene->params.bvh_type == SceneParams::BVH_STATIC) {
progress.set_status("Updating Objects", "Applying Static Transformations");
apply_static_transforms(scene, progress);
}
if(progress.get_cancel()) return;
need_update = false;
}
void ObjectManager::device_free(Device *device, DeviceScene *dscene)
{
device->tex_free(dscene->objects);
dscene->objects.clear();
}
void ObjectManager::apply_static_transforms(Scene *scene, Progress& progress)
{
/* todo: normals and displacement should be done before applying transform! */
/* todo: create objects/meshes in right order! */
/* counter mesh users */
map<Mesh*, int> mesh_users;
foreach(Object *object, scene->objects) {
map<Mesh*, int>::iterator it = mesh_users.find(object->mesh);
if(it == mesh_users.end())
mesh_users[object->mesh] = 1;
else
it->second++;
}
if(progress.get_cancel()) return;
/* apply transforms for objects with single user meshes */
foreach(Object *object, scene->objects) {
if(mesh_users[object->mesh] == 1) {
object->apply_transform();
object->mesh->transform_applied = true;
if(progress.get_cancel()) return;
}
}
}
void ObjectManager::tag_update(Scene *scene)
{
need_update = true;
scene->mesh_manager->need_update = true;
}
CCL_NAMESPACE_END