-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader_data.cpp
106 lines (77 loc) · 3.05 KB
/
shader_data.cpp
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
#include "shader_data.h"
VA_HELPER_VERTEX_NORMAL_TEXTURE(aPos, 0, aNormal, 1, aTexture, 2);
sMatrixHelper<QMatrix4x4> aMatrixHelper;
QString vertexShaderCode =
"#version 330 core\n"
"layout (location = "+
aPos.location_str()+
") in vec3 " + aPos.name + ";\n"
"layout (location = "+
aNormal.location_str()+
") in vec3 " + aNormal.name + ";\n"
"layout (location = "+
aTexture.location_str()+
") in vec2 " + aTexture.name + ";\n"
"out vec3 Normal;\n"
"out vec3 FragPos;\n"
"out vec3 LightPos;\n"
"out vec2 TexCoords;\n"
"uniform vec3 lightPos;\n"
"uniform mat4 "+aMatrixHelper.model+";\n"
"uniform mat4 "+aMatrixHelper.view+";\n"
"uniform mat4 "+aMatrixHelper.projection+";\n"
"void main(){\n"
"gl_Position = " +
aMatrixHelper.projection + "*" +
aMatrixHelper.view + "*" +
aMatrixHelper.model +
" * vec4(" + aPos.name + ", 1.0);\n"
"FragPos = vec3(" +
aMatrixHelper.view + "*" +
aMatrixHelper.model +
" * vec4(" + aPos.name + ", 1.0));\n"
"LightPos = vec3(" +
aMatrixHelper.view +
" * vec4(lightPos, 1.0));\n"
"TexCoords = aTexture;\n"
// calc with Normal matrix to consider changes in the model matrix for normals
// (not efficient - calc on the CPU side):
"Normal = mat3(transpose(inverse("+aMatrixHelper.view + "*" + aMatrixHelper.model+"))) * aNormal;\n"
// "Normal = aNormal;\n"
"}";
QString fragmentShaderCode =
"#version 330 core\n"
"struct Material{\n"
"sampler2D diffuse;\n"
"sampler2D specular;\n"
"float shininess;\n"
"};\n"
"uniform Material material;\n"
"struct Light{\n"
"vec3 position;\n"
"vec3 ambient;\n"
"vec3 diffuse;\n"
"vec3 specular;\n"
"};\n"
"uniform Light light;\n"
"in vec3 Normal;\n"
"in vec3 FragPos;\n"
"in vec3 LightPos;\n"
"in vec2 TexCoords;\n"
"out vec4 fragColor;\n"
"uniform vec3 cameraPos;\n"
"void main(){\n"
"vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));\n"
"vec3 norm = normalize(Normal);\n"
"vec3 lightDir = normalize(LightPos - FragPos);\n"
"float diff = max(dot(norm, lightDir), 0.0f);\n"
"vec3 diffuse = diff * vec3(texture(material.diffuse, TexCoords)) * light.diffuse;\n"
// both a camera and a fragment position is in the world space:
"vec3 viewDir = normalize(-FragPos);\n"//cameraPos = 0,0,0
"vec3 reflectionDir = reflect(-lightDir, norm);\n"
"float shininess = material.shininess;\n"
"float spec = pow(max(dot(viewDir, reflectionDir), 0.0f), shininess);\n"
"vec3 specular = (spec * vec3(texture(material.specular, TexCoords))) * light.specular;\n"
"vec3 resultColor = diffuse+ambient+specular;\n"
"fragColor = vec4(resultColor, 1.0f);\n"
"}";