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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "multi_instanced_geometry_device.h"
/*
* Our scene has multiple instantiated trees on the root level,
* which in turn instantiate twigs.
*/
namespace embree {
/*
* Combined quad/triangle meshes.
*/
struct Mesh
{
const unsigned int numVertices;
const float* vertices;
const unsigned int numTriangles;
const unsigned int* triangleIndices;
const unsigned int numQuads;
const unsigned int* quadIndices;
};
/*
* Instances.
*/
struct Instances
{
const unsigned int numInstances;
// These are the column vectors of the 3x4 transformation
// matrix from instance-local space to parent space.
const float* localX;
const float* localY;
const float* localZ;
const float* position;
};
/*
* Create a triangle mesh geometry from the given mesh.
*/
RTCGeometry makeTriangleMesh(RTCDevice device, const Mesh& mesh)
{
RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE);
const unsigned int timeStep = 0;
rtcSetSharedGeometryBuffer(geom,
RTC_BUFFER_TYPE_VERTEX,
timeStep,
RTC_FORMAT_FLOAT3,
mesh.vertices,
0,
3*sizeof(float),
mesh.numVertices);
rtcSetSharedGeometryBuffer(geom,
RTC_BUFFER_TYPE_INDEX,
timeStep,
RTC_FORMAT_UINT3,
mesh.triangleIndices,
0,
3*sizeof(unsigned int),
mesh.numTriangles);
rtcCommitGeometry(geom);
return geom;
}
/*
* Create a quad mesh geometry from the given mesh.
*/
RTCGeometry makeQuadMesh(RTCDevice device, const Mesh& mesh)
{
RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_QUAD);
const unsigned int timeStep = 0;
rtcSetSharedGeometryBuffer(geom,
RTC_BUFFER_TYPE_VERTEX,
timeStep,
RTC_FORMAT_FLOAT3,
mesh.vertices,
0,
3*sizeof(float),
mesh.numVertices);
rtcSetSharedGeometryBuffer(geom,
RTC_BUFFER_TYPE_INDEX,
timeStep,
RTC_FORMAT_UINT4,
mesh.quadIndices,
0,
4*sizeof(unsigned int),
mesh.numQuads);
rtcCommitGeometry(geom);
return geom;
}
/*
* Add a mesh to a scene.
*/
void addMesh(RTCDevice device, RTCScene scene, const Mesh& mesh)
{
if (mesh.numTriangles > 0)
{
RTCGeometry geom = makeTriangleMesh(device, mesh);
rtcAttachGeometry(scene, geom);
rtcReleaseGeometry(geom);
}
if (mesh.numQuads > 0)
{
RTCGeometry geom = makeQuadMesh(device, mesh);
rtcAttachGeometry(scene, geom);
rtcReleaseGeometry(geom);
}
}
/*
* Build a transformation matrix from individual vectors.
*/
__forceinline AffineSpace3fa buildMatrix(const float* localX,
const float* localY,
const float* localZ,
const float* position)
{
return AffineSpace3fa::translate(Vec3fa(position[0], position[1], position[2]))
* AffineSpace3fa(LinearSpace3fa(Vec3fa(localX[0], localX[1], localX[2]),
Vec3fa(localY[0], localY[1], localY[2]),
Vec3fa(localZ[0], localZ[1], localZ[2])));
}
/*
* Add instances to a scene.
* This function also writes normal transforms to the given map.
*/
void addInstances(RTCDevice device,
RTCScene scene,
RTCScene exemplar,
const Instances& instances,
LinearSpace3fa* normalTransforms)
{
for (unsigned int i = 0; i < instances.numInstances; ++i)
{
const unsigned int idx = 3*i;
auto M = buildMatrix(instances.localX + idx,
instances.localY + idx,
instances.localZ + idx,
instances.position + idx);
RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_INSTANCE);
rtcSetGeometryInstancedScene(geom, exemplar);
rtcSetGeometryTimeStepCount(geom, 1);
rtcSetGeometryTransform(geom,
0,
RTC_FORMAT_FLOAT4X4_COLUMN_MAJOR,
reinterpret_cast<float*>(&M));
rtcCommitGeometry(geom);
rtcAttachGeometryByID(scene, geom, i);
rtcReleaseGeometry(geom);
normalTransforms[i] = transposed(rcp(M.l));
}
}
/*
* Initialize the main scene. Note that we store complex geometry in
* separate files purely for readability.
*/
#include "geometry/ground.cpp"
#include "geometry/twig.cpp"
#include "geometry/tree.cpp"
#include "geometry/trees.cpp"
extern "C" void cleanupScene(TutorialData& data)
{
if ( data.g_normalTransforms )
{
#if 0 // workaround
if (data.g_normalTransforms[0]) { alignedUSMFree(data.g_normalTransforms[0]); data.g_normalTransforms[0] = nullptr; }
if (data.g_normalTransforms[1]) { alignedUSMFree(data.g_normalTransforms[1]); data.g_normalTransforms[1] = nullptr; }
if (data.g_normalTransforms) { alignedUSMFree(data.g_normalTransforms); data.g_normalTransforms = nullptr; }
if (data.g_instanceLevels.numInstancesOnLevel) { alignedUSMFree(data.g_instanceLevels.numInstancesOnLevel); data.g_instanceLevels.numInstancesOnLevel = nullptr; }
#endif
data.g_normalTransforms = nullptr;
}
}
extern "C" RTCScene initializeScene(TutorialData& data, RTCDevice device)
{
cleanupScene(data);
data.g_instanceLevels.numLevels = 2;
data.g_instanceLevels.numInstancesOnLevel = (unsigned int*)alignedUSMMalloc(2*sizeof(unsigned int), 16);
data.g_instanceLevels.numInstancesOnLevel[0] = Trees::instances.numInstances;
data.g_instanceLevels.numInstancesOnLevel[1] = Twigs01::instances.numInstances;
data.g_normalTransforms = (LinearSpace3fa**)alignedUSMMalloc(2*sizeof(LinearSpace3fa*), 16);
data.g_normalTransforms[0] = (LinearSpace3fa*)alignedUSMMalloc(data.g_instanceLevels.numInstancesOnLevel[0]*sizeof(LinearSpace3fa), 16);
data.g_normalTransforms[1] = (LinearSpace3fa*)alignedUSMMalloc(data.g_instanceLevels.numInstancesOnLevel[1]*sizeof(LinearSpace3fa), 16);
data.g_instanceLevels.normalTransforms = data.g_normalTransforms;
RandomSampler sampler;
RandomSampler_init(sampler, 98248);
RTCScene twig = rtcNewScene(device);
rtcSetSceneFlags(twig, RTC_SCENE_FLAG_FILTER_FUNCTION_IN_ARGUMENTS);
addMesh(device, twig, Twig::mesh);
rtcCommitScene(twig);
RTCScene tree = rtcNewScene(device);
rtcSetSceneFlags(tree, RTC_SCENE_FLAG_FILTER_FUNCTION_IN_ARGUMENTS);
addInstances(device, tree, twig, Twigs01::instances, data.g_normalTransforms[1]);
addMesh(device, tree, Tree01::mesh);
rtcCommitScene(tree);
rtcReleaseScene(twig);
RTCScene scene = rtcNewScene(device);
rtcSetSceneFlags(scene, RTC_SCENE_FLAG_FILTER_FUNCTION_IN_ARGUMENTS);
addInstances(device, scene, tree, Trees::instances, data.g_normalTransforms[0]);
addMesh(device, scene, Ground::mesh);
rtcReleaseScene(tree);
rtcCommitScene(scene);
return scene;
}
// -----------------------------------------------------------------------------
} // namespace embree
// -----------------------------------------------------------------------------
|