Added support for shaders written in non-legacy GLSL.

This commit is contained in:
binary1248 2018-05-20 14:06:24 +02:00
parent 1dcad60878
commit db7e683688
No known key found for this signature in database
GPG key ID: E5E52A5D6082224A
18 changed files with 690 additions and 74 deletions

View file

@ -115,6 +115,12 @@ int main()
// Check whether the prerequisites are suppprted
bool prerequisitesSupported = sf::VertexBuffer::isAvailable() && sf::Shader::isAvailable();
// Bind the shader to SFML's drawable interface
terrainShader.setPositionAttribute ("positionAttribute")
.setColorAttribute ("colorAttribute")
.setTextureCoordinateAttribute("texCoordAttribute")
.setModelViewProjectionMatrix ("modelViewProjectionMatrix");
// Set up our graphics resources and set the status text accordingly
if (!prerequisitesSupported)
{

View file

@ -1,3 +1,5 @@
varying vec4 frontColor;
varying vec3 normal;
uniform float lightFactor;
@ -7,5 +9,5 @@ void main()
vec3 eyePosition = vec3(0.0, 0.0, 1.0);
vec3 halfVector = normalize(lightPosition + eyePosition);
float intensity = lightFactor + (1.0 - lightFactor) * dot(normalize(normal), normalize(halfVector));
gl_FragColor = gl_Color * vec4(intensity, intensity, intensity, 1.0);
gl_FragColor = frontColor * vec4(intensity, intensity, intensity, 1.0);
}

View file

@ -1,8 +1,16 @@
attribute vec2 positionAttribute;
attribute vec4 colorAttribute;
attribute vec2 texCoordAttribute;
uniform mat4 modelViewProjectionMatrix;
varying vec4 frontColor;
varying vec3 normal;
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_FrontColor = gl_Color;
normal = vec3(gl_MultiTexCoord0.xy, 1.0);
gl_Position = modelViewProjectionMatrix * vec4(positionAttribute, 0.0, 1.0);
frontColor = colorAttribute;
normal = vec3(texCoordAttribute, 1.0);
}

View file

@ -28,8 +28,15 @@ public:
return false;
m_sprite.setTexture(m_texture);
// Bind the shader to SFML's drawable interface
m_shader.setPositionAttribute ("positionAttribute")
.setColorAttribute ("colorAttribute")
.setTextureCoordinateAttribute("texCoordAttribute")
.setModelViewProjectionMatrix ("modelViewProjectionMatrix")
.setTextureMatrix ("textureMatrix");
// Load the shader
if (!m_shader.loadFromFile("resources/pixelate.frag", sf::Shader::Fragment))
if (!m_shader.loadFromFile("resources/pixelate.vert", "resources/pixelate.frag"))
return false;
m_shader.setUniform("texture", sf::Shader::CurrentTexture);
@ -92,6 +99,13 @@ public:
m_text.setCharacterSize(22);
m_text.setPosition(30, 20);
// Bind the shader to SFML's drawable interface
m_shader.setPositionAttribute ("positionAttribute")
.setColorAttribute ("colorAttribute")
.setTextureCoordinateAttribute("texCoordAttribute")
.setModelViewProjectionMatrix ("modelViewProjectionMatrix")
.setTextureMatrix ("textureMatrix");
// Load the shader
if (!m_shader.loadFromFile("resources/wave.vert", "resources/blur.frag"))
return false;
@ -145,6 +159,12 @@ public:
m_points.append(sf::Vertex(sf::Vector2f(x, y), sf::Color(r, g, b)));
}
// Bind the shader to SFML's drawable interface
m_shader.setPositionAttribute("positionAttribute")
.setColorAttribute ("colorAttribute")
.setModelViewMatrix ("modelViewMatrix")
.setProjectionMatrix ("projectionMatrix");
// Load the shader
if (!m_shader.loadFromFile("resources/storm.vert", "resources/blink.frag"))
return false;
@ -212,8 +232,15 @@ public:
m_entities.push_back(entity);
}
// Bind the shader to SFML's drawable interface
m_shader.setPositionAttribute ("positionAttribute")
.setColorAttribute ("colorAttribute")
.setTextureCoordinateAttribute("texCoordAttribute")
.setModelViewProjectionMatrix ("modelViewProjectionMatrix")
.setTextureMatrix ("textureMatrix");
// Load the shader
if (!m_shader.loadFromFile("resources/edge.frag", sf::Shader::Fragment))
if (!m_shader.loadFromFile("resources/edge.vert", "resources/edge.frag"))
return false;
m_shader.setUniform("texture", sf::Shader::CurrentTexture);
@ -290,6 +317,10 @@ public:
if (!m_logoTexture.loadFromFile("resources/logo.png"))
return false;
// Bind the shader to SFML's drawable interface
m_shader.setPositionAttribute ("positionAttribute")
.setModelViewProjectionMatrix("modelViewProjectionMatrix");
// Load the shader
if (!m_shader.loadFromFile("resources/billboard.vert", "resources/billboard.geom", "resources/billboard.frag"))
return false;

View file

@ -4,8 +4,10 @@ uniform sampler2D texture;
in vec2 tex_coord;
out vec4 fragColor;
void main()
{
// Read and apply a color from the texture
gl_FragColor = texture2D(texture, tex_coord);
fragColor = texture2D(texture, tex_coord);
}

View file

@ -1,5 +1,11 @@
#version 150
in vec2 positionAttribute;
uniform mat4 modelViewProjectionMatrix;
void main()
{
// Transform the vertex position
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_Position = modelViewProjectionMatrix * vec4(positionAttribute, 0.0, 1.0);
}

View file

@ -1,9 +1,10 @@
uniform sampler2D texture;
uniform float blink_alpha;
varying vec4 frontColor;
void main()
{
vec4 pixel = gl_Color;
vec4 pixel = frontColor;
pixel.a = blink_alpha;
gl_FragColor = pixel;
}

View file

@ -1,20 +1,23 @@
uniform sampler2D texture;
uniform float blur_radius;
varying vec4 texCoord;
varying vec4 frontColor;
void main()
{
vec2 offx = vec2(blur_radius, 0.0);
vec2 offy = vec2(0.0, blur_radius);
vec4 pixel = texture2D(texture, gl_TexCoord[0].xy) * 4.0 +
texture2D(texture, gl_TexCoord[0].xy - offx) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy + offx) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy - offy) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy + offy) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy - offx - offy) * 1.0 +
texture2D(texture, gl_TexCoord[0].xy - offx + offy) * 1.0 +
texture2D(texture, gl_TexCoord[0].xy + offx - offy) * 1.0 +
texture2D(texture, gl_TexCoord[0].xy + offx + offy) * 1.0;
vec4 pixel = texture2D(texture, texCoord.xy) * 4.0 +
texture2D(texture, texCoord.xy - offx) * 2.0 +
texture2D(texture, texCoord.xy + offx) * 2.0 +
texture2D(texture, texCoord.xy - offy) * 2.0 +
texture2D(texture, texCoord.xy + offy) * 2.0 +
texture2D(texture, texCoord.xy - offx - offy) * 1.0 +
texture2D(texture, texCoord.xy - offx + offy) * 1.0 +
texture2D(texture, texCoord.xy + offx - offy) * 1.0 +
texture2D(texture, texCoord.xy + offx + offy) * 1.0;
gl_FragColor = gl_Color * (pixel / 16.0);
gl_FragColor = frontColor * (pixel / 16.0);
}

View file

@ -1,29 +1,32 @@
uniform sampler2D texture;
uniform float edge_threshold;
varying vec4 texCoord;
varying vec4 frontColor;
void main()
{
const float offset = 1.0 / 512.0;
vec2 offx = vec2(offset, 0.0);
vec2 offy = vec2(0.0, offset);
vec4 hEdge = texture2D(texture, gl_TexCoord[0].xy - offy) * -2.0 +
texture2D(texture, gl_TexCoord[0].xy + offy) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy - offx - offy) * -1.0 +
texture2D(texture, gl_TexCoord[0].xy - offx + offy) * 1.0 +
texture2D(texture, gl_TexCoord[0].xy + offx - offy) * -1.0 +
texture2D(texture, gl_TexCoord[0].xy + offx + offy) * 1.0;
vec4 hEdge = texture2D(texture, texCoord.xy - offy) * -2.0 +
texture2D(texture, texCoord.xy + offy) * 2.0 +
texture2D(texture, texCoord.xy - offx - offy) * -1.0 +
texture2D(texture, texCoord.xy - offx + offy) * 1.0 +
texture2D(texture, texCoord.xy + offx - offy) * -1.0 +
texture2D(texture, texCoord.xy + offx + offy) * 1.0;
vec4 vEdge = texture2D(texture, gl_TexCoord[0].xy - offx) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy + offx) * -2.0 +
texture2D(texture, gl_TexCoord[0].xy - offx - offy) * 1.0 +
texture2D(texture, gl_TexCoord[0].xy - offx + offy) * -1.0 +
texture2D(texture, gl_TexCoord[0].xy + offx - offy) * 1.0 +
texture2D(texture, gl_TexCoord[0].xy + offx + offy) * -1.0;
vec4 vEdge = texture2D(texture, texCoord.xy - offx) * 2.0 +
texture2D(texture, texCoord.xy + offx) * -2.0 +
texture2D(texture, texCoord.xy - offx - offy) * 1.0 +
texture2D(texture, texCoord.xy - offx + offy) * -1.0 +
texture2D(texture, texCoord.xy + offx - offy) * 1.0 +
texture2D(texture, texCoord.xy + offx + offy) * -1.0;
vec3 result = sqrt(hEdge.rgb * hEdge.rgb + vEdge.rgb * vEdge.rgb);
float edge = length(result);
vec4 pixel = gl_Color * texture2D(texture, gl_TexCoord[0].xy);
vec4 pixel = frontColor * texture2D(texture, texCoord.xy);
if (edge > (edge_threshold * 8.0))
pixel.rgb = vec3(0.0, 0.0, 0.0);
else

View file

@ -0,0 +1,19 @@
attribute vec2 positionAttribute;
attribute vec4 colorAttribute;
attribute vec2 texCoordAttribute;
uniform mat4 modelViewProjectionMatrix;
uniform mat4 textureMatrix;
varying vec4 texCoord;
varying vec4 frontColor;
void main()
{
// Transform the vertex position
gl_Position = modelViewProjectionMatrix * vec4(positionAttribute, 0.0, 1.0);
frontColor = colorAttribute;
texCoord = textureMatrix * vec4(texCoordAttribute, 0.0, 1.0);
}

View file

@ -1,9 +1,12 @@
uniform sampler2D texture;
uniform float pixel_threshold;
varying vec4 texCoord;
varying vec4 frontColor;
void main()
{
float factor = 1.0 / (pixel_threshold + 0.001);
vec2 pos = floor(gl_TexCoord[0].xy * factor + 0.5) / factor;
gl_FragColor = texture2D(texture, pos) * gl_Color;
vec2 pos = floor(texCoord.xy * factor + 0.5) / factor;
gl_FragColor = texture2D(texture, pos) * frontColor;
}

View file

@ -0,0 +1,16 @@
attribute vec2 positionAttribute;
attribute vec4 colorAttribute;
attribute vec2 texCoordAttribute;
uniform mat4 modelViewProjectionMatrix;
uniform mat4 textureMatrix;
varying vec4 texCoord;
varying vec4 frontColor;
void main()
{
gl_Position = modelViewProjectionMatrix * vec4(positionAttribute, 0.0, 1.0);
texCoord = textureMatrix * vec4(texCoordAttribute, 0.0, 1.0);
frontColor = colorAttribute;
}

View file

@ -2,9 +2,17 @@ uniform vec2 storm_position;
uniform float storm_total_radius;
uniform float storm_inner_radius;
attribute vec2 positionAttribute;
attribute vec4 colorAttribute;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
varying vec4 frontColor;
void main()
{
vec4 vertex = gl_ModelViewMatrix * gl_Vertex;
vec4 vertex = modelViewMatrix * vec4(positionAttribute, 0.0, 1.0);
vec2 offset = vertex.xy - storm_position;
float len = length(offset);
if (len < storm_total_radius)
@ -13,7 +21,6 @@ void main()
vertex.xy = storm_position + normalize(offset) * push_distance;
}
gl_Position = gl_ProjectionMatrix * vertex;
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
gl_FrontColor = gl_Color;
gl_Position = projectionMatrix * vertex;
frontColor = colorAttribute;
}

View file

@ -1,15 +1,25 @@
uniform float wave_phase;
uniform vec2 wave_amplitude;
attribute vec2 positionAttribute;
attribute vec4 colorAttribute;
attribute vec2 texCoordAttribute;
uniform mat4 modelViewProjectionMatrix;
uniform mat4 textureMatrix;
varying vec4 texCoord;
varying vec4 frontColor;
void main()
{
vec4 vertex = gl_Vertex;
vertex.x += cos(gl_Vertex.y * 0.02 + wave_phase * 3.8) * wave_amplitude.x
+ sin(gl_Vertex.y * 0.02 + wave_phase * 6.3) * wave_amplitude.x * 0.3;
vertex.y += sin(gl_Vertex.x * 0.02 + wave_phase * 2.4) * wave_amplitude.y
+ cos(gl_Vertex.x * 0.02 + wave_phase * 5.2) * wave_amplitude.y * 0.3;
vec4 vertex = vec4(positionAttribute, 0.0, 1.0);
vertex.x += cos(positionAttribute.y * 0.02 + wave_phase * 3.8) * wave_amplitude.x
+ sin(positionAttribute.y * 0.02 + wave_phase * 6.3) * wave_amplitude.x * 0.3;
vertex.y += sin(positionAttribute.x * 0.02 + wave_phase * 2.4) * wave_amplitude.y
+ cos(positionAttribute.x * 0.02 + wave_phase * 5.2) * wave_amplitude.y * 0.3;
gl_Position = gl_ModelViewProjectionMatrix * vertex;
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
gl_FrontColor = gl_Color;
gl_Position = modelViewProjectionMatrix * vertex;
texCoord = textureMatrix * vec4(texCoordAttribute, 0.0, 1.0);
frontColor = colorAttribute;
}