-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshader.go
More file actions
197 lines (151 loc) · 4.84 KB
/
shader.go
File metadata and controls
197 lines (151 loc) · 4.84 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"fmt"
"strings"
"github.com/go-gl/gl/v3.3-core/gl"
)
func newProgram(vertexShaderSource, fragmentShaderSource, geometryShaderSource string) (uint32, error) {
vertexShader, err := compileShader(vertexShaderSource, gl.VERTEX_SHADER)
if err != nil {
return 0, err
}
fragmentShader, err := compileShader(fragmentShaderSource, gl.FRAGMENT_SHADER)
if err != nil {
return 0, err
}
program := gl.CreateProgram()
gl.AttachShader(program, vertexShader)
defer gl.DeleteShader(vertexShader)
gl.AttachShader(program, fragmentShader)
defer gl.DeleteShader(fragmentShader)
if geometryShaderSource != "" {
geometryShader, err := compileShader(geometryShaderSource, gl.GEOMETRY_SHADER)
if err != nil {
return 0, err
}
gl.AttachShader(program, geometryShader)
defer gl.DeleteShader(geometryShader)
}
gl.LinkProgram(program)
var status int32
gl.GetProgramiv(program, gl.LINK_STATUS, &status)
if status == gl.FALSE {
var logLength int32
gl.GetProgramiv(program, gl.INFO_LOG_LENGTH, &logLength)
log := strings.Repeat("\x00", int(logLength+1))
gl.GetProgramInfoLog(program, logLength, nil, gl.Str(log))
return 0, fmt.Errorf("failed to link program: %v", log)
}
return program, nil
}
func compileShader(source string, shaderType uint32) (uint32, error) {
shader := gl.CreateShader(shaderType)
csources, free := gl.Strs(source)
gl.ShaderSource(shader, 1, csources, nil)
free()
gl.CompileShader(shader)
var status int32
gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status)
if status == gl.FALSE {
var logLength int32
gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength)
log := strings.Repeat("\x00", int(logLength+1))
gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log))
return 0, fmt.Errorf("failed to compile %v: %v", source, log)
}
return shader, nil
}
var vertexShader = `
#version 330
uniform float Time;
uniform mat4 ProjectionMatrix;
uniform mat4 ViewMatrix;
uniform mat4 ProjectionViewMatrix;
uniform vec3 DiffuseLightPosition;
in vec3 VertexPosition;
in vec3 VertexNormal;
in vec2 VertexUV;
in vec3 InstancePosition;
in vec3 InstanceHeading;
out vec3 FragmentColor;
const float SWIM_SPEED = 4;
const float SWIM_ROLL_OFFSET = 0.7;
const float SIZE = 0.5;
mat4 LookAt(float size, vec3 pos, vec3 direction) {
vec3 up = vec3(0, 1, 0);
vec3 ww = normalize(-direction);
vec3 uu = normalize(cross(up, ww));
vec3 vv = normalize(cross(ww, uu));
// not sure whether correct
return mat4(
uu * size, 0,
vv * size, 0,
ww * size, 0,
pos, 1
);
}
mat4 LookAtOptimized(float size, vec3 pos, vec3 direction) {
vec3 ww = -direction;
vec3 uu = normalize(vec3(ww.z, 0, -ww.x));
vec3 vv = normalize(vec3(ww.y*uu.z, ww.z*uu.x - ww.x*uu.z, -ww.y*uu.x));
// not sure whether correct
return mat4(
uu * size, 0,
vv * size, 0,
ww * size, 0,
pos, 1
);
}
vec3 RotateZ(vec3 original, vec2 twistRotation) {
float sn, cs;
sn = twistRotation.x;
cs = twistRotation.y;
vec3 r = original;
r.x = original.x * cs - original.y * sn;
r.y = original.x * sn + original.y * cs;
return r;
}
vec3 Swim(vec3 original, vec2 twistRotation, float wiggleAmount) {
original = RotateZ(original, twistRotation);
vec3 result = original;
result.x += wiggleAmount;
return result;
}
vec3 hsv2rgb(vec3 c)
{
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
void main() {
float phase = mod(gl_InstanceID, 3.14);
mat4 modelMatrix = LookAtOptimized(SIZE, InstancePosition, InstanceHeading);
mat4 normalMatrix = transpose(inverse(ViewMatrix * modelMatrix));
float twistAmount = sin(-VertexPosition.z + phase + Time * SWIM_SPEED - SWIM_ROLL_OFFSET)*0.3;
float wiggleAmount = sin(Time * SWIM_SPEED - VertexPosition.z + phase) * 0.2;
vec2 twistRotation = vec2(sin(twistAmount), cos(twistAmount));
vec3 position = Swim(VertexPosition, twistRotation, wiggleAmount);
vec3 normal = normalize(Swim(VertexPosition + VertexNormal, twistRotation, wiggleAmount) - position);
vec4 fragmentPosition = modelMatrix * vec4(position, 1);
gl_Position = ProjectionViewMatrix * fragmentPosition;
// lighting
float hue = mod(gl_InstanceID * 0.011111 + Time * 2, 1);
//float hue = mod(gl_InstanceID * 0.001 * sin(Time), 1);
if(hue < 0) hue = -hue;
float light = mod(gl_InstanceID * 0.035124 + Time * 0.5, 0.75) + 0.25;
vec3 albedo = hsv2rgb(vec3(hue, 0.4, 0.7));
float ambientLight = 0.3;
vec3 screenNormal = normalize(mat3(normalMatrix) * normal);
vec3 diffuseLightDirection = normalize(DiffuseLightPosition - fragmentPosition.xyz);
float diffuseShade = clamp(dot(screenNormal, diffuseLightDirection), 0.0, 1.0);
FragmentColor = albedo * (ambientLight + diffuseShade);
}
` + "\x00"
var fragmentShader = `
#version 330
in vec3 FragmentColor;
out vec4 OutputColor;
void main() {
OutputColor = vec4(FragmentColor, 1);
}
` + "\x00"