-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathboid.vert
More file actions
86 lines (67 loc) · 2.19 KB
/
Copy pathboid.vert
File metadata and controls
86 lines (67 loc) · 2.19 KB
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
#version 450 core
struct Boid
{
vec4 position;
vec4 velocity;
vec4 orientation;
};
// UNIFORM
uniform nap
{
mat4 projectionMatrix;
mat4 viewMatrix;
mat4 modelMatrix;
mat4 normalMatrix;
vec3 cameraPosition;
} mvp;
uniform VERTUBO
{
float boidSize;
float fresnelScale;
float fresnelPower;
};
// STORAGE
layout(std430) buffer readonly VERTSSBO
{
Boid boids[10000];
};
// Input Vertex Attributes
in vec3 in_Position;
in vec3 in_Normals;
out vec3 pass_Position;
out vec3 pass_Normal;
out float pass_Fresnel;
flat out uint pass_Id;
// Rotate vector v with quaterion q
vec3 rotate(vec3 v, vec4 q)
{
float l1 = dot(q.xyz, q.xyz);
return v * (q.w * q.w - l1) + q.xyz * (dot(v, q.xyz) * 2.0) + cross(q.xyz, v) * (q.w * 2.0);
}
void main(void)
{
// Use the instance index to read from the boid storage buffer. We make sure the boid count is equal
// to the buffer element count in this shader, the vertex shader, and in the buffer descriptor in JSON.
Boid b = boids[gl_InstanceIndex];
// Fetch position from vertex attribute
vec4 position = vec4(boidSize * in_Position, 1.0);
// Transform the fetched position to world space using the position and orientation of the boid
vec3 flock_position = rotate(position.xyz, b.orientation) + b.position.xyz;
vec4 world_position = mvp.modelMatrix * vec4(flock_position, 1.0);
// Calculate position
gl_Position = mvp.projectionMatrix * mvp.viewMatrix * world_position;
// Calculate vertex world space position and set
pass_Position = world_position.xyz;
// Calculate normal in world coordinates and pass along
vec3 flock_normal = normalize(rotate(in_Normals, b.orientation));
vec3 world_normal = normalize((mvp.modelMatrix * vec4(flock_normal, 0.0)).xyz);
pass_Normal = world_normal;
vec3 eye_to_surface = normalize(world_position.xyz - mvp.cameraPosition);
float fresnel = 0.04 + 0.96 * pow(clamp(1.0 + dot(eye_to_surface, world_normal), 0.0, 1.0), fresnelPower);
pass_Fresnel = fresnelScale * fresnel;
// Pass element id
pass_Id = gl_InstanceIndex;
}