Debian Codegraph @

tests/manual/bigscene-instanced-qml/instanced.vert

raw
1 #version 150 core
2
3 #define M_PI 3.14159265358979323846
4
5 in vec3 vertexPosition;
6 in vec3 vertexNormal;
7
8 out vec3 position;
9 out vec3 normal;
10 out vec3 kd;
11
12 uniform mat4 viewMatrix;
13 uniform mat4 projectionMatrix;
14 uniform mat3 modelViewNormal;
15 uniform int instanceCount;
16
17 uniform float time;
18
19 const vec3 ZAXIS = vec3(0.0, 0.0, 1.0);
20 const vec3 XAXIS = vec3(1.0, 0.0, 0.0);
21 const float radius = 30.0;
22 float det = 1.0 / float(instanceCount);
23
24 mat4 rotationMatrix(vec3 axis, float angle)
25 {
26 axis = normalize(axis);
27 float s = sin(angle);
28 float c = cos(angle);
29 float oc = 1.0 - c;
30
31 return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
32 oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
33 oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
34 0.0, 0.0, 0.0, 1.0);
35 }
36
37
38 mat4 translationMatrix(vec3 translation)
39 {
40 return mat4(1.0, 0.0, 0.0, 0.0,
41 0.0, 1.0, 0.0, 0.0,
42 0.0, 0.0, 1.0, 0.0,
43 translation.x, translation.y, translation.z, 0);
44
45 }
46
47 void main()
48 {
49 float angle = M_PI * 2.0f * gl_InstanceID * det * 10.0 * time * 0.05;
50 vec3 translation = vec3(radius * cos(angle), 200.0 * gl_InstanceID * det, radius * sin(angle));
51
52 mat4 modelMatrix = translationMatrix(translation) *
53 rotationMatrix(ZAXIS, radians(angle * 45.0)) *
54 rotationMatrix(XAXIS, radians(angle * 30.0));
55
56 vec4 eyePosition = viewMatrix *
57 modelMatrix *
58 vec4(vertexPosition, 1.0);
59
60 position = eyePosition.xyz;
61 kd = vec3(abs(cos(angle)), 0.75, 25);
62 normal = normalize(mat3(modelMatrix) * modelViewNormal *
63 vertexNormal);
64
65 gl_Position = projectionMatrix * vec4(position, 1.0);
66 }