wx_BGI_Graphics
Classic BGI-compatible graphics API with modern OpenGL extension API
Loading...
Searching...
No Matches
bgi_gl_shaders.h
Go to the documentation of this file.
1#pragma once
18// =============================================================================
19// 1. Page-texture program — fullscreen quad with RGBA texture
20// =============================================================================
21
22static const char *kPageTexVertSrc = R"GLSL(
23#version 330 core
24layout(location = 0) in vec2 aPos;
25layout(location = 1) in vec2 aUV;
26out vec2 vUV;
27void main() {
28 vUV = aUV;
29 gl_Position = vec4(aPos, 0.0, 1.0);
30}
31)GLSL";
32
33static const char *kPageTexFragSrc = R"GLSL(
34#version 330 core
35in vec2 vUV;
36out vec4 fragColor;
37uniform sampler2D uTex;
38void main() {
39 fragColor = texture(uTex, vUV);
40}
41)GLSL";
42
43// =============================================================================
44// 2. Flat Phong solid program
45// Vertex shader passes colour and face normal flat to fragment shader.
46// Fragment shader applies full Phong lighting once per fragment (constant
47// per face because the normal is flat-interpolated).
48// =============================================================================
49
50static const char *kFlatVertSrc = R"GLSL(
51#version 330 core
52layout(location = 0) in vec3 aPos;
53layout(location = 1) in vec3 aNormal;
54layout(location = 2) in vec3 aColor;
55
56flat out vec3 vNormal;
57flat out vec3 vColor;
58 out vec3 vFragPos;
59
60uniform mat4 uMVP;
61uniform mat4 uModel; // model = identity (world coords submitted directly)
62uniform mat3 uNormMat; // transpose(inverse(model)) for normal transform
63
64void main() {
65 vFragPos = vec3(uModel * vec4(aPos, 1.0));
66 vNormal = normalize(uNormMat * aNormal);
67 vColor = aColor;
68 gl_Position = uMVP * vec4(aPos, 1.0);
69}
70)GLSL";
71
72static const char *kFlatFragSrc = R"GLSL(
73#version 330 core
74flat in vec3 vNormal;
75flat in vec3 vColor;
76 in vec3 vFragPos;
77out vec4 fragColor;
78
79// Primary light
80uniform vec3 uLightDir; // normalised, in world-space (may be view-rotated outside)
81uniform float uAmbient;
82uniform float uDiffuse;
83uniform float uSpecular;
84uniform float uShininess;
85uniform vec3 uCamPos; // world-space camera position (for specular)
86
87// Fill light
88uniform vec3 uFillLightDir;
89uniform float uFillStrength;
90
91void main() {
92 vec3 N = normalize(vNormal);
93 vec3 L = normalize(-uLightDir);
94 vec3 V = normalize(uCamPos - vFragPos);
95 vec3 R = reflect(uLightDir, N);
96
97 float diff = max(dot(N, L), 0.0);
98 float spec = pow(max(dot(R, V), 0.0), uShininess);
99 float fillDif = max(dot(N, normalize(-uFillLightDir)), 0.0) * uFillStrength;
100
101 vec3 lit = vColor * (uAmbient + uDiffuse * diff + uFillStrength * fillDif)
102 + vec3(1.0) * uSpecular * spec;
103
104 fragColor = vec4(clamp(lit, 0.0, 1.0), 1.0);
105}
106)GLSL";
107
108// =============================================================================
109// 3. Smooth Phong solid program (Gouraud-style: normals interpolated per vertex)
110// =============================================================================
111
112static const char *kSmoothVertSrc = R"GLSL(
113#version 330 core
114layout(location = 0) in vec3 aPos;
115layout(location = 1) in vec3 aNormal;
116layout(location = 2) in vec3 aColor;
117
118out vec3 vNormal;
119out vec3 vColor;
120out vec3 vFragPos;
121
122uniform mat4 uMVP;
123uniform mat4 uModel;
124uniform mat3 uNormMat;
125
126void main() {
127 vFragPos = vec3(uModel * vec4(aPos, 1.0));
128 vNormal = normalize(uNormMat * aNormal);
129 vColor = aColor;
130 gl_Position = uMVP * vec4(aPos, 1.0);
131}
132)GLSL";
133
134static const char *kSmoothFragSrc = R"GLSL(
135#version 330 core
136in vec3 vNormal;
137in vec3 vColor;
138in vec3 vFragPos;
139out vec4 fragColor;
140
141uniform vec3 uLightDir;
142uniform float uAmbient;
143uniform float uDiffuse;
144uniform float uSpecular;
145uniform float uShininess;
146uniform vec3 uCamPos;
147
148uniform vec3 uFillLightDir;
149uniform float uFillStrength;
150
151void main() {
152 vec3 N = normalize(vNormal);
153 vec3 L = normalize(-uLightDir);
154 vec3 V = normalize(uCamPos - vFragPos);
155 vec3 R = reflect(uLightDir, N);
156
157 float diff = max(dot(N, L), 0.0);
158 float spec = pow(max(dot(R, V), 0.0), uShininess);
159 float fillDif = max(dot(N, normalize(-uFillLightDir)), 0.0) * uFillStrength;
160
161 vec3 lit = vColor * (uAmbient + uDiffuse * diff + uFillStrength * fillDif)
162 + vec3(1.0) * uSpecular * spec;
163
164 fragColor = vec4(clamp(lit, 0.0, 1.0), 1.0);
165}
166)GLSL";
167
168// =============================================================================
169// 4. Depth-tested line program — colour passthrough, no lighting
170// =============================================================================
171
172static const char *kLineVertSrc = R"GLSL(
173#version 330 core
174layout(location = 0) in vec3 aPos;
175layout(location = 1) in vec3 aColor;
176out vec3 vColor;
177uniform mat4 uMVP;
178void main() {
179 vColor = aColor;
180 gl_Position = uMVP * vec4(aPos, 1.0);
181}
182)GLSL";
183
184static const char *kLineFragSrc = R"GLSL(
185#version 330 core
186in vec3 vColor;
187out vec4 fragColor;
188void main() {
189 fragColor = vec4(vColor, 1.0);
190}
191)GLSL";