summaryrefslogtreecommitdiffstats
path: root/examples/qt3d/bigscene-instanced-qml/instanced.vert
blob: f713957e347793f19d78c6e7f5172eba4417fef6 (plain)
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
#version 150 core

#define M_PI 3.14159

in vec3 vertexPosition;
in vec3 vertexNormal;

out vec3 position;
out vec3 normal;
out vec3 kd;

uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform mat3 modelViewNormal;
uniform int instanceCount;

uniform float time;

const vec3 ZAXIS = vec3(0.0, 0.0, 1.0);
const vec3 XAXIS = vec3(1.0, 0.0, 0.0);
const float radius = 30.0;
float det = 1.0 / float(instanceCount);

mat4 rotationMatrix(vec3 axis, float angle)
{
    axis = normalize(axis);
    float s = sin(angle);
    float c = cos(angle);
    float oc = 1.0 - c;

    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,
                oc * axis.x * axis.y + axis.z * s,  oc * axis.y * axis.y + c,           oc * axis.y * axis.z - axis.x * s,  0.0,
                oc * axis.z * axis.x - axis.y * s,  oc * axis.y * axis.z + axis.x * s,  oc * axis.z * axis.z + c,           0.0,
                0.0,                                0.0,                                0.0,                                1.0);
}


mat4 translationMatrix(vec3 translation)
{
    return mat4(1.0, 0.0, 0.0, 0.0,
                0.0, 1.0, 0.0, 0.0,
                0.0, 0.0, 1.0, 0.0,
                translation.x, translation.y, translation.z, 0);

}

void main()
{
    float angle = M_PI * 2.0f * gl_InstanceID * det * 10.0 * time * 0.05;
    vec3 translation = vec3(radius * cos(angle), 200.0 * gl_InstanceID * det, radius * sin(angle));

    mat4 modelMatrix = translationMatrix(translation) *
                       rotationMatrix(ZAXIS, radians(angle * 45.0)) *
                       rotationMatrix(XAXIS, radians(angle * 30.0));

    vec4 eyePosition = viewMatrix *
                       modelMatrix *
                       vec4(vertexPosition, 1.0);

    position = eyePosition.xyz;
    kd = vec3(abs(cos(angle)), 0.75, 25);
    normal = normalize(mat3(modelMatrix) * modelViewNormal *
                       vertexNormal);

    gl_Position = projectionMatrix * vec4(position, 1.0);
}