1 : |
jhr |
2061 |
/*! \file vr.diderot
|
2 : |
|
|
*
|
3 : |
|
|
* \author John Reppy
|
4 : |
|
|
*
|
5 : |
|
|
* simple volume renderer, with a single directional light,
|
6 : |
|
|
* Phong shading, and the new camera.
|
7 : |
|
|
*/
|
8 : |
jhr |
2058 |
|
9 : |
jhr |
2061 |
/*
|
10 : |
jhr |
3349 |
* This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
|
11 : |
|
|
*
|
12 : |
|
|
* COPYRIGHT (c) 2015 The University of Chicago
|
13 : |
jhr |
2061 |
* All rights reserved.
|
14 : |
|
|
*/
|
15 : |
|
|
|
16 : |
jhr |
2067 |
// input image
|
17 : |
|
|
input image(3)[] hand = image("data/vfrhand-nohip.nhdr");
|
18 : |
|
|
|
19 : |
jhr |
2058 |
// image parameters
|
20 : |
|
|
input int imgResU = 480;
|
21 : |
|
|
input int imgResV = 345;
|
22 : |
jhr |
2067 |
input real rayStep = 0.5;
|
23 : |
jhr |
2058 |
|
24 : |
|
|
// camera controls
|
25 : |
|
|
input vec3 camEye = [127.331, -1322.05, 272.53];
|
26 : |
|
|
input vec3 camAt = [63.0, 82.6536, 98.0];
|
27 : |
|
|
input vec3 camUp = [0.9987, 0.0459166, -0.0221267];
|
28 : |
|
|
input real camNear = -78.0;
|
29 : |
|
|
input real camFar = 78.0;
|
30 : |
|
|
input real camFOV = 5.0;
|
31 : |
|
|
|
32 : |
|
|
/* we control the opacity by its center value and the +/- keys */
|
33 : |
|
|
input real valOpacMid = 550.0; // 550 for skin, 1300.0 for bone
|
34 : |
|
|
real valOpacMin = valOpacMid - 150.0;
|
35 : |
|
|
real valOpacMax = valOpacMid + 150.0;
|
36 : |
|
|
|
37 : |
|
|
real camDist = |camAt - camEye|;
|
38 : |
|
|
real camVspNear = camDist + camNear;
|
39 : |
|
|
real camVspFar = camDist + camFar;
|
40 : |
|
|
vec3 camN = normalize(camAt - camEye);
|
41 : |
|
|
vec3 camU = normalize(camN × camUp);
|
42 : |
|
|
vec3 camV = camN × camU;
|
43 : |
|
|
real camVmax = tan(camFOV*π/360.0)*camDist;
|
44 : |
|
|
real camUmax = camVmax*real(imgResU)/real(imgResV);
|
45 : |
|
|
|
46 : |
|
|
vec3 lightVspDir = [0.9, -1.0, -2.5];
|
47 : |
|
|
vec3 lightDir = normalize(lightVspDir[0]*camU + lightVspDir[1]*camV + lightVspDir[2]*camN);
|
48 : |
|
|
//vec3 lightRGB = [1.0, 1.0, 1.0];
|
49 : |
|
|
|
50 : |
|
|
real phongKa = 0.05;
|
51 : |
|
|
real phongKd = 0.65;
|
52 : |
|
|
real phongKs = 0.45;
|
53 : |
|
|
real phongSp = 50.0;
|
54 : |
|
|
|
55 : |
jhr |
2067 |
field#2(3)[] F = bspln3 ⊛ hand;
|
56 : |
jhr |
2058 |
|
57 : |
|
|
strand RayCast (int ui, int vi)
|
58 : |
|
|
{
|
59 : |
|
|
real rayU = lerp(-camUmax, camUmax, -0.5, real(ui), real(imgResU)-0.5);
|
60 : |
jhr |
2070 |
// we flip the v coordinate, since we are using DrawPixels to render the image
|
61 : |
|
|
real rayV = lerp(-camVmax, camVmax, -0.5, real(imgResV - vi - 1), real(imgResV)-0.5);
|
62 : |
jhr |
2058 |
vec3 rayVec = (camDist*camN + rayU*camU + rayV*camV)/camDist;
|
63 : |
|
|
vec3 toEye = normalize(-rayVec);
|
64 : |
|
|
|
65 : |
|
|
real rayN = camVspNear;
|
66 : |
|
|
// ########## BEGIN per-ray initialization
|
67 : |
|
|
real rayTransp = 1.0;
|
68 : |
|
|
// vec3 rayRGB = [0.0, 0.0, 0.0];
|
69 : |
|
|
real rayGrey = 0.0;
|
70 : |
|
|
output vec4 outRGBA = [0.0, 0.0, 0.0, 0.0];
|
71 : |
|
|
// ########## END per-ray initialization
|
72 : |
|
|
|
73 : |
|
|
update
|
74 : |
|
|
{
|
75 : |
|
|
vec3 rayPos = camEye + rayN*rayVec;
|
76 : |
|
|
if (inside (rayPos,F)) {
|
77 : |
|
|
// ########## BEGIN per-sample code
|
78 : |
|
|
real val = F(rayPos);
|
79 : |
|
|
if (val > valOpacMin) { // we have some opacity
|
80 : |
|
|
vec3 grad = ∇F(rayPos);
|
81 : |
|
|
vec3 norm = normalize(-∇F(rayPos));
|
82 : |
|
|
real alpha = min(1.0, lerp(0.0, 1.0, valOpacMin, val, valOpacMax));
|
83 : |
|
|
real ld = max(0.0, norm • lightDir);
|
84 : |
|
|
real hd = max(0.0, norm • normalize(lightDir + toEye));
|
85 : |
|
|
real pnt = phongKa
|
86 : |
|
|
+ phongKd * ld
|
87 : |
|
|
+ phongKs * hd^phongSp;
|
88 : |
|
|
rayGrey += rayTransp*alpha*pnt;
|
89 : |
|
|
rayTransp = rayTransp*(1.0 - alpha);
|
90 : |
|
|
}
|
91 : |
|
|
// ########## END per-sample code
|
92 : |
|
|
}
|
93 : |
|
|
if (rayTransp < 0.01) { // early ray termination
|
94 : |
|
|
rayTransp = 0.0;
|
95 : |
|
|
stabilize;
|
96 : |
|
|
}
|
97 : |
|
|
if (rayN > camVspFar) {
|
98 : |
|
|
stabilize;
|
99 : |
|
|
}
|
100 : |
|
|
rayN += rayStep;
|
101 : |
|
|
}
|
102 : |
|
|
|
103 : |
|
|
stabilize {
|
104 : |
|
|
outRGBA = [rayGrey, rayGrey, rayGrey, 1.0-rayTransp];
|
105 : |
|
|
}
|
106 : |
|
|
|
107 : |
|
|
}
|
108 : |
|
|
|
109 : |
|
|
initially [ RayCast(ui, vi) | vi in 0..(imgResV-1), ui in 0..(imgResU-1) ];
|