-
Notifications
You must be signed in to change notification settings - Fork 5
/
1matmul.txt
127 lines (94 loc) · 4.22 KB
/
1matmul.txt
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
vec3 randomDirectionInSphere(float seed) {
float u = getrandom(vec3(12.9898, 78.233, 151.7182), seed);
float v = getrandom(vec3(63.7264, 10.873, 623.6736), seed);
float up = u * 2.0 - 1.0; // cos(theta)
float over = sqrt(1.0 - up * up); // sin(theta)
float around = v * 3.14;
return vec3( up, cos(around) * over, sin(around) * over );
}
mat4 multiply(in mat4 mat, in mat4 mato) {
mat4 dest;
// Cache the matrix values (makes for huge speed increases!)
float a00 = mat[0][0], a01 = mat[0][1], a02 = mat[0][2], a03 = mat[0][3];
float a10 = mat[1][0], a11 = mat[1][1], a12 = mat[1][2], a13 = mat[1][3];
float a20 = mat[2][0], a21 = mat[2][1], a22 = mat[2][2], a23 = mat[2][3];
float a30 = mat[3][0], a31 = mat[3][1], a32 = mat[3][2], a33 = mat[3][3];
// Cache only the current line of the second matrix
float b0 = mato[0][0], b1 = mato[0][1], b2 = mato[0][2], b3 = mato[0][3];
dest[0][0] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
dest[0][1] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
dest[0][2] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
dest[0][3] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
b0 = mato[1][0];
b1 = mato[1][1];
b2 = mato[1][2];
b3 = mato[1][3];
dest[1][0] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
dest[1][1] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
dest[1][2] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
dest[1][3] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
b0 = mato[2][0];
b1 = mato[2][1];
b2 = mato[2][2];
b3 = mato[2][3];
dest[2][0] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
dest[2][1] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
dest[2][2] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
dest[2][3] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
b0 = mato[3][0];
b1 = mato[3][1];
b2 = mato[3][2];
b3 = mato[3][3];
dest[3][0] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
dest[3][1] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
dest[3][2] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
dest[3][3] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
return dest;
}
vec3 getRandomPointOnSphere(in GEOM sphere, in float seed){
float radius = 0.5;
vec3 point;
float theta, phi;
theta = 180.0 * getrandom(vec3(12.9898, 78.233, 151.7182), seed); //0-180
phi = 360.0 * getrandom(vec3(78.233, 12.9898, 151.7182), seed+1.0); //0-360
float degToRad = PI / 180.0;
point.x = radius * sin(theta * degToRad) * cos(phi * degToRad);
point.y = radius * sin(theta * degToRad) * sin(phi * degToRad);
point.z = radius * cos(theta * degToRad);
vec3 randPoint = (sphere.model * vec4(point, 1.0)).xyz;
return randPoint;
}
vec3 getRandomPointOnCube(in GEOM cube, in float seed){
vec3 origin = (cube.model * vec4(0.0,0.0,0.0,1.0)).xyz;
vec3 xmax = (cube.model * vec4(0.5,0.0,0.0,1.0)).xyz;
vec3 ymax = (cube.model * vec4(0.0,0.5,0.0,1.0)).xyz;
vec3 zmax = (cube.model * vec4(0.0,0.0,0.5,1.0)).xyz;
float xradius = distance(origin, xmax);
float yradius = distance(origin, ymax);
float zradius = distance(origin, zmax);
//get surface areas of sides
vec3 radii = vec3(xradius,yradius,zradius);
float side1 = radii.x * radii.y * 4.0; //x-y face
float side2 = radii.z * radii.y * 4.0; //y-z face
float side3 = radii.x * radii.z * 4.0; //x-z face
float totalarea = 2.0 * (side1+side2+side3);
//pick random face, weighted by surface area
float russianRoulette = getrandom(vec3(12.9898, 78.233, 151.7182), seed); //0-1
float rand1 = getrandom(vec3(78.233, 12.9898, 151.7182), seed + 1.0) - 0.5; //-0.5-0.5
float rand2 = getrandom(vec3(151.7182, 12.9898, 78.233), seed + 2.0) - 0.5; //-0.5-0.5
vec3 point = vec3(0.5,0.5,0.5);
if(russianRoulette<(side1/totalarea))
point = vec3(rand1, rand2, 0.5);
else if(russianRoulette<((side1*2.0)/totalarea))
point = vec3(rand1, rand2, -0.5);
else if(russianRoulette<(((side1*2.0)+(side2))/totalarea))
point = vec3(0.5, rand1, rand2);
else if(russianRoulette<(((side1*2.0)+(side2*2.0))/totalarea))
point = vec3(-0.5, rand1, rand2);
else if(russianRoulette<(((side1*2.0)+(side2*2.0)+(side3))/totalarea))
point = vec3( rand1, 0.5, rand2);
else
point = vec3( rand1, -0.5, rand2);
vec3 randPoint = (cube.model * vec4(point,1.0)).xyz;
return randPoint;
}