This repository has been archived by the owner on Mar 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shaders.h
73 lines (71 loc) · 2.54 KB
/
shaders.h
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
#define SHADER_VERT3D \
"#version 330 \n"\
"in vec3 position;\n"\
"in vec2 texcoord;\n"\
"in vec3 normal;\n"\
"out vec3 color;\n"\
"out vec3 Position_world;\n"\
"out vec3 Normal;\n"\
"out vec3 EyeDir;\n"\
"out vec3 LightDir;\n"\
"uniform mat4 model;\n"\
"uniform mat4 view;\n"\
"uniform mat4 proj;\n"\
"uniform vec3 lighting;\n"\
"vec3 hsv2rgb(vec3 c)\n"\
"{\n"\
" vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n"\
" vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n"\
" return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n"\
"}\n"\
"void main(void) { \n"\
" vec3 Position_camera = (view * model * vec4(position, 1.0)).xyz;\n"\
" color = hsv2rgb(vec3(0.5,0.75+(sin(position.x)+1)/4,0.75+(sin(position.y)+1)/4));\n"\
" Position_world = (model * vec4(position, 1.0)).xyz;\n"\
" EyeDir = vec3(0.0, 0.0, 0.0) - Position_camera;\n"\
" vec3 LightPos_camera = (view * vec4(lighting, 1.0)).xyz;\n"\
" LightDir = LightPos_camera + EyeDir;\n"\
" Normal = (view * model * vec4(normal,0)).xyz;\n"\
" gl_Position = proj * view * model * vec4(position, 1.0);\n"\
"}"
//#define SHADER_FRAG_SIMPLE \
//"#version 330 core\n"\
//"in vec2 Texcoord;\n"\
//"in vec3 Position_world;\n"\
//"in vec3 Normal;\n"\
//"in vec3 EyeDir;\n"\
//"in vec3 LightDir;\n"\
//"out vec3 outColor;\n"\
//"uniform sampler2D tex;\n"\
//"void main()"\
//"{"\
//" outColor = texture(tex, Texcoord).xyz;\n"\
//"}"
#define SHADER_FRAG_LIGHTING \
"#version 330 core\n"\
"in vec3 color;\n"\
"in vec3 Position_world;\n"\
"in vec3 Normal;\n"\
"in vec3 EyeDir;\n"\
"in vec3 LightDir;\n"\
"out vec3 outColor;\n"\
"uniform sampler2D tex;\n"\
"uniform vec3 lighting;\n"\
"void main()"\
"{"\
" vec3 LightColor = vec3(1,1,1);\n"\
" float LightPower = 5.0f;\n"\
" vec3 mtlDiffuse = color.xyz;\n"\
" vec3 mtlAmbient = vec3(0.1, 0.1, 0.1) * mtlDiffuse;\n"\
" vec3 mtlSpecular = vec3(0.3, 0.3, 0.3);\n"\
" float distance = length(lighting - Position_world);\n"\
" vec3 n = normalize(Normal);\n"\
" vec3 l = normalize(LightDir);\n"\
" float cosTheta = clamp( dot(n,l), 0,1);\n"\
" vec3 E = normalize(EyeDir);\n"\
" vec3 R = reflect(-l,n);\n"\
" float cosAlpha = clamp( dot(E,R), 0,1);\n"\
" outColor = mtlAmbient +\n"\
" mtlDiffuse * LightColor * LightPower * cosTheta / (distance*distance) +\n"\
" mtlSpecular * LightColor * LightPower * pow(cosAlpha,5) / (distance*distance);\n"\
"}"