Debian Codegraph @

Shaders/object_instancing.glsl

raw
1 // Yaw
2 void rotation_matrix_H(in float sinRz, in float cosRz, out mat3 rotmat)
3 {
4 rotmat = mat3( cosRz, -sinRz, 0.0,
5 sinRz, cosRz, 0.0,
6 0.0 , 0.0 , 1.0);
7 }
8
9 // Pitch
10 void rotation_matrix_P(in float sinRy, in float cosRy, out mat3 rotmat)
11 {
12 rotmat = mat3( cosRy, 0.0 , sinRy,
13 0.0 , 1.0 , 0.0,
14 -sinRy, 0.0 , cosRy);
15 }
16
17 // Roll
18 void rotation_matrix_R(in float sinRx, in float cosRx, out mat3 rotmat)
19 {
20 rotmat = mat3( 1.0 , 0.0 , 0.0,
21 0.0 , cosRx , -sinRx,
22 0.0 , sinRx , cosRx);
23 }
24
25 void scale_matrix(in float scale, out mat3 scalemat)
26 {
27 scalemat = mat3(scale, 0.0 , 0.0 ,
28 0.0 , scale, 0.0 ,
29 0.0 , 0.0 , scale);
30 }
31
32 /* apply_instance_transforms
33 * Takes the position, normal of an vertex, and position/rotation/scale of
34 * one instance, and transforms the original position and normal of the
35 * vertex to match it's instanced version.
36 */
37 void apply_instance_transforms(inout vec3 position, inout vec3 normal, in vec3 instance_position, in vec4 instance_rotation_and_scale)
38 {
39 // Handle rotation and scaling
40 mat3 ScaleMat;
41 scale_matrix(instance_rotation_and_scale.w, ScaleMat);
42
43 mat3 RotMatH;
44 mat3 RotMatP;
45 mat3 RotMatR;
46
47 float hdg = instance_rotation_and_scale.x;
48 float pitch = instance_rotation_and_scale.y;
49 float roll = instance_rotation_and_scale.z;
50 // if (roll > 90.0 || roll < -90.0) {roll = -roll;}
51 float cosRx = cos(radians(-roll));
52 float sinRx = sin(radians(-roll));
53 float cosRy = cos(radians(-pitch));
54 float sinRy = sin(radians(-pitch));
55 float cosRz = cos(radians(-hdg));
56 float sinRz = sin(radians(-hdg));
57
58 rotation_matrix_R(sinRx, cosRx, RotMatR);
59 rotation_matrix_P(sinRy, cosRy, RotMatP);
60 rotation_matrix_H(sinRz, cosRz, RotMatH);
61
62 // Transform position using scaling and rotation matrices
63 position = RotMatH * RotMatP * RotMatR * ScaleMat * position;
64
65 // Offset model to correct location w.r.t instancing center
66 position = position + instance_position.xyz;
67
68 // Transform normal using rotation matrices
69 normal = gl_NormalMatrix * (RotMatH * RotMatP * RotMatR * gl_Normal);
70 }