Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rayobject_instance.cpp « raytrace « intern « render « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 01e592cba0c9022f9a2d627b160f98d8b073e3b0 (plain)
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
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * 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.
 *
 * The Original Code is Copyright (C) 2009 Blender Foundation.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): André Pinto.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/render/intern/raytrace/rayobject_instance.cpp
 *  \ingroup render
 */


#include <assert.h>

#include "MEM_guardedalloc.h"

#include "BLI_math.h"
#include "BLI_utildefines.h"

#include "rayintersection.h"
#include "rayobject.h"

#define RE_COST_INSTANCE (1.0f)

static int  RE_rayobject_instance_intersect(RayObject *o, Isect *isec);
static void RE_rayobject_instance_free(RayObject *o);
static void RE_rayobject_instance_bb(RayObject *o, float *min, float *max);
static float RE_rayobject_instance_cost(RayObject *o);

static void RE_rayobject_instance_hint_bb(RayObject *UNUSED(o), RayHint *UNUSED(hint),
                                          float *UNUSED(min), float *UNUSED(max))
{}

static RayObjectAPI instance_api =
{
	RE_rayobject_instance_intersect,
	NULL, //static void RE_rayobject_instance_add(RayObject *o, RayObject *ob);
	NULL, //static void RE_rayobject_instance_done(RayObject *o);
	RE_rayobject_instance_free,
	RE_rayobject_instance_bb,
	RE_rayobject_instance_cost,
	RE_rayobject_instance_hint_bb
};

typedef struct InstanceRayObject {
	RayObject rayobj;
	RayObject *target;

	void *ob; //Object represented by this instance
	void *target_ob; //Object represented by the inner RayObject, needed to handle self-intersection
	
	float global2target[4][4];
	float target2global[4][4];
	
} InstanceRayObject;


RayObject *RE_rayobject_instance_create(RayObject *target, float transform[4][4], void *ob, void *target_ob)
{
	InstanceRayObject *obj = (InstanceRayObject *)MEM_callocN(sizeof(InstanceRayObject), "InstanceRayObject");
	assert(RE_rayobject_isAligned(obj) );  /* RayObject API assumes real data to be 4-byte aligned */

	obj->rayobj.api = &instance_api;
	obj->target = target;
	obj->ob = ob;
	obj->target_ob = target_ob;

	copy_m4_m4(obj->target2global, transform);
	invert_m4_m4(obj->global2target, obj->target2global);

	return RE_rayobject_unalignRayAPI((RayObject *) obj);
}

static int  RE_rayobject_instance_intersect(RayObject *o, Isect *isec)
{
	InstanceRayObject *obj = (InstanceRayObject *)o;
	float start[3], dir[3], idot_axis[3], dist;
	int changed = 0, i, res;

	// TODO - this is disabling self intersection on instances
	if (isec->orig.ob == obj->ob && obj->ob) {
		changed = 1;
		isec->orig.ob = obj->target_ob;
	}

	// backup old values
	copy_v3_v3(start, isec->start);
	copy_v3_v3(dir, isec->dir);
	copy_v3_v3(idot_axis, isec->idot_axis);
	dist = isec->dist;

	// transform to target coordinates system
	mul_m4_v3(obj->global2target, isec->start);
	mul_mat3_m4_v3(obj->global2target, isec->dir);
	isec->dist *= normalize_v3(isec->dir);

	// update idot_axis and bv_index
	for (i = 0; i < 3; i++) {
		isec->idot_axis[i]        = 1.0f / isec->dir[i];

		isec->bv_index[2 * i]     = isec->idot_axis[i] < 0.0f ? 1 : 0;
		isec->bv_index[2 * i + 1] = 1 - isec->bv_index[2 * i];

		isec->bv_index[2 * i]     = i + 3 * isec->bv_index[2 * i];
		isec->bv_index[2 * i + 1] = i + 3 * isec->bv_index[2 * i + 1];
	}

	// raycast
	res = RE_rayobject_intersect(obj->target, isec);

	// map dist into original coordinate space
	if (res == 0) {
		isec->dist = dist;
	}
	else {
		// note we don't just multiply dist, because of possible
		// non-uniform scaling in the transform matrix
		float vec[3];

		mul_v3_v3fl(vec, isec->dir, isec->dist);
		mul_mat3_m4_v3(obj->target2global, vec);

		isec->dist = len_v3(vec);
		isec->hit.ob = obj->ob;

#ifdef RT_USE_LAST_HIT
		// TODO support for last hit optimization in instances that can jump
		// directly to the last hit face.
		// For now it jumps directly to the last-hit instance root node.
		isec->last_hit = RE_rayobject_unalignRayAPI((RayObject *) obj);
#endif
	}

	// restore values
	copy_v3_v3(isec->start, start);
	copy_v3_v3(isec->dir, dir);
	copy_v3_v3(isec->idot_axis, idot_axis);

	if (changed)
		isec->orig.ob = obj->ob;

	// restore bv_index
	for (i = 0; i < 3; i++) {
		isec->bv_index[2 * i]     = isec->idot_axis[i] < 0.0f ? 1 : 0;
		isec->bv_index[2 * i + 1] = 1 - isec->bv_index[2 * i];

		isec->bv_index[2 * i]     = i + 3 * isec->bv_index[2 * i];
		isec->bv_index[2 * i + 1] = i + 3 * isec->bv_index[2 * i + 1];
	}

	return res;
}

static void RE_rayobject_instance_free(RayObject *o)
{
	InstanceRayObject *obj = (InstanceRayObject *)o;
	MEM_freeN(obj);
}

static float RE_rayobject_instance_cost(RayObject *o)
{
	InstanceRayObject *obj = (InstanceRayObject *)o;
	return RE_rayobject_cost(obj->target) + RE_COST_INSTANCE;
}

static void RE_rayobject_instance_bb(RayObject *o, float *min, float *max)
{
	//TODO:
	// *better bb.. calculated without rotations of bb
	// *maybe cache that better-fitted-BB at the InstanceRayObject
	InstanceRayObject *obj = (InstanceRayObject *)o;

	float m[3], M[3], t[3];
	int i, j;
	INIT_MINMAX(m, M);
	RE_rayobject_merge_bb(obj->target, m, M);

	//There must be a faster way than rotating all the 8 vertexs of the BB
	for (i = 0; i < 8; i++) {
		for (j = 0; j < 3; j++) t[j] = i & (1 << j) ? M[j] : m[j];
		mul_m4_v3(obj->target2global, t);
		DO_MINMAX(t, min, max);
	}
}