-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathTransparency.cpp
156 lines (136 loc) · 4.65 KB
/
Transparency.cpp
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
// Copyright 2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "Builder.h"
#include "ospray_testing.h"
#include "rkcommon/utility/multidim_index_sequence.h"
using namespace rkcommon::math;
namespace ospray {
namespace testing {
struct Transparency : public detail::Builder
{
Transparency() = default;
~Transparency() override = default;
void commit() override;
cpp::Group buildGroup() const override;
cpp::World buildWorld() const override;
};
// Inlined definitions ////////////////////////////////////////////////////
void Transparency::commit()
{
Builder::commit();
addPlane = true;
}
cpp::Group Transparency::buildGroup() const
{
// Create plane geometry
cpp::Geometry planeGeometry("plane");
{
std::vector<vec4f> coeffs = {{0.f, 0.f, 1.f, 0.f},
{0.f, 0.f, 1.f, 0.f},
{0.f, 0.f, 1.f, 0.f},
{0.f, 0.f, 1.f, 0.f},
{0.f, 0.f, 1.f, -.4f},
{0.f, 0.f, 1.f, -.45f},
{0.f, 0.f, 1.f, -.5f}};
std::vector<box3f> bounds = {{{-2.f, 1.8f, -2.f}, {2.f, 2.f, 2.f}},
{{-2.f, 1.6f, -2.f}, {2.f, 1.8f, 2.f}},
{{-2.f, 1.4f, -2.f}, {2.f, 1.6f, 2.f}},
{{-2.f, 0.2f, -2.f}, {2.f, 1.4f, 2.f}},
{{-1.7f, 1.f, -1.f}, {-.3f, 2.f, 1.f}},
{{-.7f, 1.f, -1.f}, {.7f, 2.f, 1.f}},
{{.3f, 1.f, -1.f}, {1.7f, 2.f, 1.f}}};
planeGeometry.setParam("plane.coefficients", cpp::CopiedData(coeffs));
planeGeometry.setParam("plane.bounds", cpp::CopiedData(bounds));
planeGeometry.commit();
}
// Create geometric model
cpp::GeometricModel gModel(planeGeometry);
{
std::vector<vec3f> colors = {{.8f, 0.f, 0.f},
{0.f, .8f, 0.f},
{0.f, 0.f, .8f},
{.8f, .8f, .8f},
{.5f, 0.f, 0.f},
{0.f, .5f, 0.f},
{0.f, 0.f, .5f}};
std::vector<vec3f> transmissions = {{0.f, 0.f, 0.f},
{0.f, 0.f, 0.f},
{0.f, 0.f, 0.f},
{0.f, 0.f, 0.f},
{.8f, 0.f, 0.f},
{0.f, .8f, 0.f},
{0.f, 0.f, .8f}};
std::vector<cpp::Material> materials;
for (uint32_t i = 0; i < std::min(colors.size(), transmissions.size());
i++) {
materials.emplace_back(cpp::Material("obj"));
materials[i].setParam("kd", colors[i]);
materials[i].setParam("tf", transmissions[i]);
materials[i].commit();
}
gModel.setParam("material", cpp::CopiedData(materials));
gModel.commit();
}
// Create volume object with values being shortest distance to surface for
// gradient shading working correctly
cpp::Volume volume("structuredRegular");
{
volume.setParam("gridOrigin", vec3f(-2.f, -.7f, -.1f));
volume.setParam("gridSpacing", vec3f(.1f));
const vec3i volumeCells{41, 10, 5};
std::vector<float> voxels(volumeCells.long_product());
index_sequence_3D numCell(volumeCells);
for (vec3i i : numCell) {
voxels[numCell.flatten(i)] = (float)std::min(
{i.x < (volumeCells.x / 2) ? i.x : volumeCells.x - i.x - 1,
i.y < (volumeCells.y / 2) ? i.y : volumeCells.y - i.y - 1,
i.z < (volumeCells.z / 2) ? i.z : volumeCells.z - i.z - 1});
}
volume.setParam("data", cpp::CopiedData(voxels.data(), volumeCells));
volume.commit();
}
// Create volume model
cpp::VolumetricModel vModel(volume);
{
// Create transfer function
cpp::TransferFunction transferFunction("piecewiseLinear");
{
std::vector<vec3f> colors = {{1.f, 1.f, 1.f}};
std::vector<float> opacities = {1.f};
transferFunction.setParam("color", cpp::CopiedData(colors));
transferFunction.setParam("opacity", cpp::CopiedData(opacities));
transferFunction.setParam("value", range1f(0.f, 10.f));
transferFunction.commit();
}
vModel.setParam("transferFunction", transferFunction);
vModel.setParam("densityScale", 10.f);
vModel.setParam("gradientShadingScale", 1.f);
vModel.commit();
}
// Create group
cpp::Group group;
group.setParam("geometry", cpp::CopiedData(gModel));
group.setParam("volume", cpp::CopiedData(vModel));
group.commit();
// Done
return group;
}
cpp::World Transparency::buildWorld() const
{
auto world = Builder::buildWorld();
cpp::Light light("distant");
light.setParam("color", vec3f(0.78f, 0.551f, 0.483f));
light.setParam("intensity", 3.14f);
light.setParam("direction", vec3f(0.f, -0.75f, 0.25f));
light.commit();
cpp::Light ambient("ambient");
ambient.setParam("intensity", 0.5f);
ambient.setParam("visible", false);
ambient.commit();
std::vector<cpp::Light> lights{light, ambient};
world.setParam("light", cpp::CopiedData(lights));
return world;
}
OSP_REGISTER_TESTING_BUILDER(Transparency, transparency);
} // namespace testing
} // namespace ospray