-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransfers.cpp
318 lines (269 loc) · 8.57 KB
/
transfers.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
////////////////////////////////////////////////////////////////////////
//
// transfers.cpp: Calculate the weights used for working out how the
// light is transferred between elements.
//
// For the rendering version, we render the scene from the view of all
// elements to see where the light comes from.
//
// Copyright (c) Simon Frankau 2018
//
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <cmath>
#include <iostream>
#include <vector>
#include "geom.h"
#include "glut_wrap.h"
#include "transfers.h"
#include "weighting.h"
////////////////////////////////////////////////////////////////////////
// Functions to face in cube map directions
//
// For the sides, the forward-facing part is mapped to the bottom of
// the framebuffer, which is the start of the data when read with
// glReadPixels, so that we only need to do the weighted sum on the
// first half of the read data.
static void viewFront(void)
{
// Don't need to do anything to look forward
}
static void viewBack(void)
{
glRotated(180.0, 0.0, 1.0, 0.0);
}
static void viewRight(void)
{
glRotated(+90.0, 0.0, 0.0, 1.0);
glRotated(+90.0, 0.0, 1.0, 0.0);
}
static void viewLeft(void)
{
glRotated(-90.0, 0.0, 0.0, 1.0);
glRotated(-90.0, 0.0, 1.0, 0.0);
}
static void viewUp(void)
{
glRotated(-90.0, 1.0, 0.0, 0.0);
}
static void viewDown(void)
{
glRotated(180.0, 0.0, 0.0, 1.0);
glRotated(+90.0, 1.0, 0.0, 0.0);
}
////////////////////////////////////////////////////////////////////////
// Use scene rendering to calculate the transfer functions.
//
RenderTransferCalculator::RenderTransferCalculator(
std::vector<Vertex> const &vertices,
std::vector<Quad> const &faces,
int resolution)
: m_vertices(vertices),
m_faces(faces),
m_resolution(resolution),
m_win(gwTransferSetup(resolution))
{
}
RenderTransferCalculator::~RenderTransferCalculator()
{
glutDestroyWindow(m_win);
}
// Extremely simple rendering of the scene.
void RenderTransferCalculator::render(void)
{
for (int i = 0, n = m_faces.size(); i < n; ++i) {
m_faces[i].renderIndex(i + 1, m_vertices);
}
}
static const int NUM_CHANS = 4;
// Sum up value of the pixels, with the given weights.
void RenderTransferCalculator::sumWeights(std::vector<double> const &weights)
{
std::vector<GLubyte> pixels(NUM_CHANS * weights.size());
glReadPixels(0, 0, m_resolution, weights.size() / m_resolution,
GL_RGBA, GL_UNSIGNED_BYTE, &pixels[0]);
for (int i = 0, n = pixels.size(); i < n; i += NUM_CHANS) {
// We're not using that many polys, so skip the low bits.
int index = (pixels[i] + (pixels[i+1] << 6) + (pixels[i+2] << 12)) >> 2;
if (index > 0) {
m_sums[index - 1] += weights[i/4];
}
}
}
// Work out contributions from the given face.
void RenderTransferCalculator::calcFace(
Camera const &cam,
viewFn_t view,
std::vector<double> const &weights)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
view();
cam.applyViewTransform();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
render();
sumWeights(weights);
// glutSwapBuffers is unnecessary for offscreen calculation.
}
// Calculate the area subtended by the faces, using a cube map.
std::vector<double> RenderTransferCalculator::calcSubtended(Camera const &cam)
{
m_sums.clear();
m_sums.resize(m_faces.size());
std::vector<double> const &ws = getSubtendWeights();
calcFace(cam, viewFront, ws);
calcFace(cam, viewBack, ws);
calcFace(cam, viewRight, ws);
calcFace(cam, viewLeft, ws);
calcFace(cam, viewUp, ws);
calcFace(cam, viewDown, ws);
return m_sums;
}
// Calculate the light received, using half a cube map.
std::vector<double> RenderTransferCalculator::calcLight(Camera const &cam)
{
m_sums.clear();
m_sums.resize(m_faces.size());
std::vector<double> const &fws = getForwardLightWeights();
std::vector<double> const &sws = getSideLightWeights();
calcFace(cam, viewFront, fws);
// Avoid rendering things we don't need to. Doesn't seem to
// actually make rendering go faster! I also tried calling
// glutReshapeWindow, similarly didn't affect performance. I only
// care about rendering half the scene, but can't make it go much
// faster by trying to convince the renderer of this...
glEnable(GL_SCISSOR_TEST);
glScissor(0, 0, m_resolution, m_resolution / 2);
calcFace(cam, viewRight, sws);
calcFace(cam, viewLeft, sws);
calcFace(cam, viewUp, sws);
calcFace(cam, viewDown, sws);
glDisable(GL_SCISSOR_TEST);
return m_sums;
}
std::vector<double> const &RenderTransferCalculator::getSubtendWeights()
{
if (m_subtendWeights.empty()) {
calcSubtendWeights(m_resolution, m_subtendWeights);
}
return m_subtendWeights;
}
std::vector<double> const &RenderTransferCalculator::getForwardLightWeights()
{
if (m_forwardLightWeights.empty()) {
calcForwardLightWeights(m_resolution, m_forwardLightWeights);
}
return m_forwardLightWeights;
}
std::vector<double> const &RenderTransferCalculator::getSideLightWeights()
{
if (m_sideLightWeights.empty()) {
calcSideLightWeights(m_resolution, m_sideLightWeights);
}
return m_sideLightWeights;
}
void RenderTransferCalculator::calcAllLights(std::vector<double> &weights)
{
int const n = m_faces.size();
weights.clear();
weights.reserve(n * n);
// Iterate over targets
for (int i = 0; i < n; ++i) {
Quad currQuad = m_faces[i];
Vertex eye(paraCentre(currQuad, m_vertices));
Vertex dir(paraCross(currQuad, m_vertices));
Vertex lookAt(eye - dir);
Vertex up(dir.perp());
Camera cam(eye, lookAt, up);
std::vector<double> faceWeights = calcLight(cam);
weights.insert(weights.end(), faceWeights.begin(), faceWeights.end());
// Somewhat slow, so print progress.
std::cerr << ".";
}
std::cerr << std::endl;
}
////////////////////////////////////////////////////////////////////////
// Calculate analytic approximations of the transfer functions.
//
AnalyticTransferCalculator::AnalyticTransferCalculator(
std::vector<Vertex> const &vertices,
std::vector<Quad> const &faces)
: m_vertices(vertices),
m_faces(faces)
{
}
std::vector<double> AnalyticTransferCalculator::calcSubtended(
Camera const &cam)
{
std::vector<double> weights;
for (int i = 0, n = m_faces.size(); i < n; ++i) {
weights.push_back(calcSingleQuadSubtended(cam, m_faces[i]));
}
return weights;
}
double AnalyticTransferCalculator::calcSingleQuadSubtended(
Camera const &cam,
Quad const &quad) const
{
Vertex centre = paraCentre(quad, m_vertices);
Vertex dir = centre - cam.getEyePos();
// Inverse square component.
double l = dir.len();
double r2 = 1.0 / (l * l);
// Area, scaled by angle to camera.
dir = dir.norm();
Vertex norm = paraCross(quad, m_vertices);
double area = fmax(0, dot(norm, dir));
// Normalise to surface area of 6.
return 1.5 * r2 * area / M_PI;
}
void AnalyticTransferCalculator::calcAllLights(std::vector<double> &weights)
{
int const n = m_faces.size();
weights.clear();
weights.reserve(n * n);
Vertex up(0.0, 0.0, 0.0);
// Iterate over targets
for (int i = 0; i < n; ++i) {
Quad currQuad = m_faces[i];
Vertex eye(paraCentre(currQuad, m_vertices));
Vertex lookAt(eye - paraCross(currQuad, m_vertices));
Camera cam(eye, lookAt, up);
// Iterate over sources
for (int j = 0; j < n; ++j) {
weights.push_back(calcSingleQuadLight(cam, m_faces[j]));
}
}
}
std::vector<double> AnalyticTransferCalculator::calcLight(
Camera const &cam)
{
std::vector<double> weights;
for (int i = 0, n = m_faces.size(); i < n; ++i) {
weights.push_back(calcSingleQuadLight(cam, m_faces[i]));
}
return weights;
}
double AnalyticTransferCalculator::calcSingleQuadLight(
Camera const &cam,
Quad const &quad) const
{
Vertex eyePos = cam.getEyePos();
Vertex centre = paraCentre(quad, m_vertices);
Vertex dir = centre - eyePos;
// Inverse square component.
double l = dir.len();
double r2 = 1.0 / (l * l);
// Area, scaled by angle to camera.
dir = dir.norm();
Vertex norm = paraCross(quad, m_vertices);
double area = fmax(0, dot(norm, dir));
// And angle to surface.
Vertex lookVec = (cam.getLookAt() - eyePos).norm();
double cosCamAngle = fmax(0.0, dot(lookVec, dir));
// Normalise to surface area of 6.
return cosCamAngle * r2 * area / M_PI;
}