-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathradiosity_emptyroom_color.m
182 lines (150 loc) · 5.9 KB
/
radiosity_emptyroom_color.m
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
% Radiosity lighting method for a virtual room, in color.
% The routine "radiosity_emptyroom_Fcomp.m" needs to be computed before
% this one.
%
% Samuli Siltanen January 2021
%% Preliminaries
% Load precomputed stuff
disp('Loading data')
load data/F_emptyroom F n qn d Xmat Ymat Zmat
disp('Data loaded')
% Adjust the dark shades. Colors darker than the threshold will become
% black, so increasing the threshold will darken the image.
threshold = 0.05;
% Sigmoid correction for optimal gray levels. Increasing betapar1 will
% darken the image, especially the shadows. Increasing betapar2 will
% lighten the image, especially highlights.
betapar1 = 1;
betapar2 = 20;
% Camera settings. The camera is located at vector "campos", it is pointed
% towards "camtar", and the view angle is "camang". A larger "camang" value
% will give a more "wide-angle lens", so more of the scene is seen in the
% image.
campos = [.2 -2.3 -.30];
camtar = [0 0 0];
camang = 70;
%% Construct the color vector (B-vector) using the radiosity lighting model.
% Construct the right hand side Evec of the radiosity equation. Evec
% describes the contribution of emitted light in the scene. For example,
% each pixel belonging to a lamp in the virtual space causes a positive
% element in Evec.
Evec = zeros(6*n^2,1);
indvec = repmat(logical(0),size(Evec));
indvec(n^2+[1:n^2]) = sqrt((Xmat(:,2)-.3).^2+Ymat(:,2).^2)<.3; % Ceiling lamp
% indvec(4*n^2+[1:n^2]) = ...
% ((abs(Zmat(:,5)-0)<.3)&(abs(Ymat(:,5)-0)<.1))|...
% ((abs(Zmat(:,5)-0)<.3)&(abs(Ymat(:,5)-1/2)<.1))|...
% ((abs(Zmat(:,5)-0)<.3)&(abs(Ymat(:,5)+1/2)<.1)); % Rectangular lamps in the left wall
% indvec(4*n^2+[1:n^2]) = ...
% sqrt((Zmat(:,5)-0).^2+(Ymat(:,5)-0).^2)<.1 |...
% sqrt((Zmat(:,5)-0).^2+(Ymat(:,5)-1/2).^2)<.1 |...
% sqrt((Zmat(:,5)-0).^2+(Ymat(:,5)+1/2).^2)<.1; % Round lamps in the left wall
% Evec(n^2+round(n^2/2)-2) = 1;
% Evec(3*n^2+round(n^2/2)-2) = 1;
Evec(indvec) = 1;
disp('Right-hand-side constructed')
% The parameter rho adjusts the surface material (how much incoming light
% is reflected away from a patch, 0<rho<=1)
rho = .9*ones(6*n^2,1);
rho(n^2+[1:n^2]) = 1; % Bright ceiling
rho(2*n^2+[1:n^2]) = .7; % Dark floor
% Solve for color vector.
disp('Solving radiosity equation...')
tic
colorvec_orig = gmres(eye(6*n^2)-repmat(rho,1,6*n^2).*F,Evec);
disp(['Radiosity equation solved in ',num2str(toc),' seconds'])
%% Produce a still image of the scene
% Adjust the dark shades and normalize the values of the color vector
% between 0 and 1.
colorvec = colorvec_orig-threshold;
colorvec = max(0,colorvec);
colorvec = colorvec/max(colorvec);
% Sigmoid correction for optimal gray levels.
colorvec = betacdf(colorvec,betapar1,betapar2);
% figure(100)
% clf
% t = linspace(0,1,200);
% plot(t,betacdf(t,betapar1,betapar2));
% title('Grayscale adjustment curve')
% Construct grayscale color matrix by repeating the same color vector for
% red, green and blue channels.
% Construct color matrix
colorvecR = colorvec;
colorvecR(1:n^2) = colorvecR(1:n^2).^.8; % Back wall
colorvecR(3*n^2+[1:n^2]) = colorvecR(3*n^2+[1:n^2]).^1.2; % Right wall
colorvecR(4*n^2+[1:n^2]) = colorvecR(4*n^2+[1:n^2]).^1.2; % Left wall
colorvecG = colorvec;
colorvecG(1:n^2) = colorvecG(1:n^2).^1.4; % Back wall
colorvecB = colorvec;
colorvecB(1:n^2) = colorvecB(1:n^2).^1.4; % Back wall
colorvecB(3*n^2+[1:n^2]) = colorvecB(3*n^2+[1:n^2]).^.7; % Right wall
colorvecB(4*n^2+[1:n^2]) = colorvecB(4*n^2+[1:n^2]).^.7; % Left wall
colormat = [colorvecR(:),colorvecG(:),colorvecB(:)];
% Create plot window
figure(1)
clf
% Draw all the walls consisting of n x n little squares (pixels).
% Pick the gray value of each square from the illumination vector
% calculated by the radiosity method above
% The back wall
colorind = 1;
for iii = 1:(n^2)
p1 = patch(...
[Xmat(iii,1)+d/2,Xmat(iii,1)+d/2,Xmat(iii,1)-d/2,Xmat(iii,1)-d/2],...
[Ymat(iii,1),Ymat(iii,1),Ymat(iii,1),Ymat(iii,1)],...
[Zmat(iii,1)-d/2,Zmat(iii,1)+d/2,Zmat(iii,1)+d/2,Zmat(iii,1)-d/2],...
colormat(colorind,:));
hold on
% If the following line is missing, each patch will have black
% boundaries. Here we set the boundary color to match the patch color,
% so no boundaries are visible.
set(p1,'EdgeColor',colormat(colorind,:))
colorind = colorind+1;
end
% Roof
for iii = 1:(n^2)
p1 = patch(...
[Xmat(iii,2)+d/2,Xmat(iii,2)+d/2,Xmat(iii,2)-d/2,Xmat(iii,2)-d/2],...
[Ymat(iii,2)-d/2,Ymat(iii,2)+d/2,Ymat(iii,2)+d/2,Ymat(iii,2)-d/2],...
[Zmat(iii,2),Zmat(iii,2),Zmat(iii,2),Zmat(iii,2)],...
colormat(colorind,:));
set(p1,'EdgeColor',colormat(colorind,:))
colorind = colorind+1;
end
% Floor
for iii = 1:(n^2)
p1 = patch(...
[Xmat(iii,3)+d/2,Xmat(iii,3)+d/2,Xmat(iii,3)-d/2,Xmat(iii,3)-d/2],...
[Ymat(iii,3)-d/2,Ymat(iii,3)+d/2,Ymat(iii,3)+d/2,Ymat(iii,3)-d/2],...
[Zmat(iii,3),Zmat(iii,3),Zmat(iii,3),Zmat(iii,3)],...
colormat(colorind,:));
set(p1,'EdgeColor',colormat(colorind,:))
colorind = colorind+1;
end
% Right-hand-side wall
for iii = 1:(n^2)
p1 = patch(...
[Xmat(iii,4),Xmat(iii,4),Xmat(iii,4),Xmat(iii,4)],...
[Ymat(iii,4)+d/2,Ymat(iii,4)+d/2,Ymat(iii,4)-d/2,Ymat(iii,4)-d/2],...
[Zmat(iii,4)-d/2,Zmat(iii,4)+d/2,Zmat(iii,4)+d/2,Zmat(iii,4)-d/2],...
colormat(colorind,:));
set(p1,'EdgeColor',colormat(colorind,:))
colorind = colorind+1;
end
% Left-hand-side wall
for iii = 1:(n^2)
p1 = patch(...
[Xmat(iii,5),Xmat(iii,5),Xmat(iii,5),Xmat(iii,5)],...
[Ymat(iii,5)+d/2,Ymat(iii,5)+d/2,Ymat(iii,5)-d/2,Ymat(iii,5)-d/2],...
[Zmat(iii,5)-d/2,Zmat(iii,5)+d/2,Zmat(iii,5)+d/2,Zmat(iii,5)-d/2],...
colormat(colorind,:));
set(p1,'EdgeColor',colormat(colorind,:))
colorind = colorind+1;
end
% Camera settings
camproj('perspective')
set(gca,'CameraPosition',campos,'CameraTarget',camtar,'CameraViewAngle',camang)
% Axis settings
axis equal
axis off
drawnow