-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcube.cpp
More file actions
158 lines (140 loc) · 5.02 KB
/
cube.cpp
File metadata and controls
158 lines (140 loc) · 5.02 KB
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
////////////////////////////////////////////////////////////////////////
//
// cube.cpp: Calculate radiosity inside a cube.
//
// Copyright (c) Simon Frankau 2018
//
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <iostream>
#include <cmath>
#include <vector>
#include "geom.h"
#include "glut_wrap.h"
#include "rendering.h"
#include "transfers.h"
// Relative change in total light in the scene by the point we stop
// iterating.
const double CONVERGENCE_TARGET = 0.001;
// Break up each base quad into subdivision^2 subquads for radiosity
// calculations.
int const SUBDIVISION = 32;
////////////////////////////////////////////////////////////////////////
// Radiosity calculations
void initLighting(std::vector<Quad> &qs, std::vector<Vertex> const &vs)
{
for (std::vector<Quad>::iterator iter = qs.begin(), end = qs.end();
iter != end; ++iter) {
Vertex c = paraCentre(*iter, vs);
// Put a big light in the top centre of the box.
if (fabs(c.x()) < 0.5 && fabs(c.z()) < 0.5 & c.y() > 0.9) {
iter->materialColour = iter->screenColour = Colour(2.0, 2.0, 2.0);
iter->isEmitter = true;
}
// Make the left wall red, the right wall blue.
if (c.x() < -0.999) {
iter->materialColour = iter->materialColour * Colour(1.0, 0.5, 0.5);
} else if (c.x() > 0.999) {
iter->materialColour = iter->materialColour * Colour(0.5, 0.5, 1.0);
}
}
}
void iterateLighting(std::vector<Quad> &qs, std::vector<double> const &transfers)
{
int const n = qs.size();
std::vector<Colour> updatedColours(n);
// Iterate over targets
for (int i = 0; i < n; ++i) {
Colour incoming;
if (qs[i].isEmitter) {
// Emission is just like having 1.0 light arrive.
incoming = Colour(1.0, 1.0, 1.0);
} else {
// Iterate over sources
for (int j = 0; j < n; ++j) {
if (i == j) {
continue;
}
incoming += qs[j].screenColour * transfers[i * n + j];
}
}
updatedColours[i] = incoming * qs[i].materialColour;
}
for (int i = 0; i < n; ++i) {
qs[i].screenColour = updatedColours[i];
}
}
// Calculate the total light in the scene, as area-weight sum of
// screenColour.
double calcLight(std::vector<Quad> &qs, std::vector<Vertex> const &vs)
{
double totalLight = 0.0;
for (std::vector<Quad>::iterator iter = qs.begin(), end = qs.end();
iter != end; ++iter) {
totalLight += iter->screenColour.asGrey() * paraArea(*iter, vs);
}
return totalLight;
}
////////////////////////////////////////////////////////////////////////
// And the main rendering bit...
// Geometry.
static std::vector<Quad> faces;
static std::vector<Vertex> vertices;
// Array of quad-to-quad light transfers.
static std::vector<double> transfers;
// And data for generating Gouraud shading.
static std::vector<SubdivInfo> subdivs;
// Subdivide the faces
void initGeometry(void)
{
vertices = cubeVertices;
// Draw the outer 'scene' cube, by subdividing the prototype.
for (std::vector<Quad>::const_iterator iter = cubeFaces.begin(),
end = cubeFaces.end(); iter != end; ++iter) {
subdivs.push_back(subdivide(*iter, vertices, faces,
SUBDIVISION, SUBDIVISION));
}
// Then draw the inner cube: Take the basic scene cube, scale it
// down, rotate and move it...
std::vector<Quad> sceneFaces(cubeFaces); // Enclosed cube
scale(0.4, sceneFaces, vertices);
flip(sceneFaces, vertices);
rotate(Vertex(1.0, 0.0, 0.0), M_PI / 3.0, sceneFaces, vertices);
rotate(Vertex(0.0, 0.0, 1.0), M_PI / 6.0, sceneFaces, vertices);
translate(Vertex(0.0, -0.25, 0.0), sceneFaces, vertices);
// Copy the subdivided version into 'faces' (lower subdivisions,
// as smaller).
for (std::vector<Quad>::const_iterator iter = sceneFaces.begin(),
end = sceneFaces.end(); iter != end; ++iter) {
subdivs.push_back(subdivide(*iter, vertices, faces,
SUBDIVISION / 2, SUBDIVISION / 2));
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
initGeometry();
initLighting(faces, vertices);
RenderTransferCalculator(vertices, faces, 256).calcAllLights(transfers);
double light = 0.0;
double relChange;
do {
iterateLighting(faces, transfers);
double newLight = calcLight(faces, vertices);
relChange = fabs(light / newLight - 1.0);
light = newLight;
std::cout << "Total light: " << light << std::endl;
} while (relChange > CONVERGENCE_TARGET);
normaliseBrightness(faces, vertices);
std::vector<Vertex> gVertices;
std::vector<GouraudQuad> gourauds;
for (std::vector<SubdivInfo>::iterator iter = subdivs.begin(),
end = subdivs.end(); iter != end; ++iter) {
iter->generateGouraudQuads(gourauds, gVertices);
}
renderGouraud(gourauds, gVertices);
return 0;
}