Was sind Vertices? (Vertex)
✅
Was sind Shader-Programme?
✅
* fest bezieht sich hier auf die sog. Fixed Function Pipeline
Bild: Gemeinfrei
Vertex
Shader
Fragment
Shader
Rasterizer
Tests &
Blending
🖥️
Eingabe: Vertices
Ausgabe: Bild
programmierbare Funktion
feste Funktion
{...}
{...}
Vertices
Transformierte Vertices
Fragmente
Finalisierte
Fragmente
Pixel
Hinweis: Wir werden diese Pipeline später erweitern.
gl_Position)Läuft für jeden Pixel (sog. Fragment), der ausgegeben wird
Bestimmt die endgültige Pixel-Farbe
gl_FragColor
Nutzt interpolierte Daten vom Vertex-Shader (Eingabe)
Berechnet Effekte pro Pixel, u.a.:
Licht, Texturen, Schatten, Nebel
Ist meist der rechenintensivste Teil der Grafik-Pipeline
🎯 Unser Fokus im Praktikum!
< / >
if, if/else, switch for
while ist häufig deaktiviertreturn, break, continue void main() {
float x = 0.3;
if (x > 0.5) {
gl_FragColor = vec4(1.0);
} else {
gl_FragColor = vec4(0.0);
}
}void main() {
float sum = 0.0;
for (int i = 0; i < 5; i++) {
sum += 0.2;
}
gl_FragColor = vec4(sum);
}Schleifen werden in der Regel vor Kompilierung "ausgerollt", d.h. Anzahl Durchläufe sollte vorher bekannt sein
| Datentyp | Beschreibung |
|---|---|
vec2, vec3, vec4 |
float-Vektoren |
ivec2, ivec3, ivec4 |
integer-Vektoren |
bvec2, bvec3, bvec4 |
boolean-Vektoren |
mat2 |
2x2 Matrix |
mat3 |
3x3 Matrix |
mat4 |
4x4 Matrix |
Standard-Datentypen: float, boolean, int, uint
🚀
< / >
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}Testen!
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
gl_FragColor = vec4(uv.x, 0.0, 0.0, 1.0);
}Testen!
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
gl_FragColor = vec4(uv.x, uv.y, 0.5, 1.0);
}Testen!
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
void main() {
float t = sin(u_time) * 0.5 + 0.5;
gl_FragColor = vec4(t, 0.0, 1.0 - t, 1.0);
}Testen!
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution - 0.5;
float d = length(uv);
float c = step(d, 0.2);
gl_FragColor = vec4(vec3(c), 0.800);
}Testen!
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution - 0.5;
float d = length(uv);
float c = smoothstep(0.368, 0.19, d);
gl_FragColor = vec4(vec3(c), 1.0);
}Testen!
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
float c = step(0.3, uv.x) * step(uv.x, 0.7) *
step(0.3, uv.y) * step(uv.y, 0.7);
gl_FragColor = vec4(vec3(c), 1.0);
}Testen!
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
void main() {
vec2 uv = floor(gl_FragCoord.xy / 20.0);
float c = mod(uv.x + uv.y, 2.0);
gl_FragColor = vec4(vec3(c), 1.0);
}Testen!
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
float y = sin(uv.x * 10.0 + u_time);
float c = step(uv.y, 0.5 + y * 0.1);
gl_FragColor = vec4(vec3(c), 1.0);
}Testen!