Commit cf40e924 authored by Geequette's avatar Geequette

Shader example added

parent 4be92526
#include <SFML/Graphics.hpp>
#include <iostream>
// Examples from http://glslsandbox.com/
// Example 1 : mouse on fire
// Example 2 : scrolling of shapes
// Example 3 : cells
// Example 4 : fire
// Example 5 : moving circles
//#define SHADER_FILENAME "../../shaders/example_001.frag"
//#define SHADER_FILENAME "../../shaders/example_002.frag"
#define SHADER_FILENAME "../../shaders/example_003.frag"
//#define SHADER_FILENAME "../../shaders/example_004.frag"
//#define SHADER_FILENAME "../../shaders/example_005.frag"
//#define SHADER_FILENAME "../../shaders/example_006.frag"
int main() {
const float winW = 800;
const float winH = 600;
sf::RenderWindow window(sf::VideoMode(winW, winH), "SFML Shader Example");
window.setMouseCursorVisible(false); // hide the cursor
// Create a texture and a sprite for the shader
sf::Texture tex;
......@@ -14,14 +27,14 @@ int main() {
sf::Sprite spr(tex);
sf::Shader shader;
shader.loadFromFile("../../shaders/example_001.frag", sf::Shader::Fragment); // load the shader
shader.loadFromFile(SHADER_FILENAME, sf::Shader::Fragment); // load the shader
if (!shader.isAvailable()) {
std::cout << "The shader is not available\n";
}
// Set the resolution parameter (the resoltion is divided to make the fire smaller)
shader.setParameter("resolution", sf::Vector2f(winW / 2, winH / 2));
shader.setParameter("resolution", sf::Vector2f(winW , winH));
// Use a timer to obtain the time elapsed
sf::Clock clk;
......@@ -32,13 +45,17 @@ int main() {
sf::Event event;
while (window.pollEvent(event)) {
// Close the window if a key is pressed or if requested
if (event.type == sf::Event::Closed) window.close();
// Exit the app when a key is pressed
if (event.type == sf::Event::KeyPressed)
window.close();
}
// Set the others parameters who need to be updated every frames
shader.setParameter("time", 3*clk.getElapsedTime().asSeconds());
shader.setParameter("time", clk.getElapsedTime().asSeconds());
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
shader.setParameter("mouse", sf::Vector2f(mousePos.x, mousePos.y - winH/2));
......
......@@ -29,8 +29,13 @@ LIBS += -lsfml-system
# Sources files
SOURCES += main.cpp
DISTFILES += ../shaders/example_001.frag
DISTFILES += ../shaders/example_001.vert
DISTFILES += ../shaders/example_001.frag \
../shaders/example_002.frag \
../shaders/example_003.frag \
../shaders/example_004.frag \
../shaders/example_005.frag \
../shaders/example_006.frag
DISTFILES +=
......
......@@ -72,7 +72,9 @@ int main()
// Load shaders
sf::Shader shader;
if (!shader.loadFromFile("../../shaders/example_002.vert", "../../shaders/example_002.frag"))
// if (!shader.loadFromFile("../../shaders/example_011.vert", "../../shaders/example_011.frag"))
if (!shader.loadFromFile("../../shaders/example_011.frag", sf::Shader::Fragment))
{
std::cerr << "Error while shaders" << std::endl;
return -1;
......
......@@ -30,8 +30,9 @@ LIBS += -lsfml-system
SOURCES += main.cpp
DISTFILES += \
../shaders/example_002.frag \
../shaders/example_002.vert
../shaders/example_002.vert \
../shaders/example_012.frag \
../shaders/example_011.frag
......
// Example from http://glslsandbox.com/
#ifdef GL_ES
precision mediump float;
#endif
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
......
// Simple passthrough vertex shader... Nothing fancy here.
void main()
{
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
gl_FrontColor = gl_Color;
}
#version 130
// Example from http://glslsandbox.com/
uniform sampler2D currentTexture; // Our render texture
uniform sampler2D distortionMapTexture; // Our heat distortion map texture
#ifdef GL_ES
precision mediump float;
#endif
uniform float time; // Time used to scroll the distortion map
uniform float distortionFactor; // Factor used to control severity of the effect
uniform float riseFactor; // Factor used to control how fast air rises
#extension GL_OES_standard_derivatives : enable
void main()
{
vec2 distortionMapCoordinate = gl_TexCoord[0].st;
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
// We use the time value to scroll our distortion texture upwards
// Since we enabled texture repeating, OpenGL takes care of
// coordinates that lie outside of [0, 1] by discarding
// the integer part and keeping the fractional part
// Basically performing a "floating point modulo 1"
// 1.1 = 0.1, 2.4 = 0.4, 10.3 = 0.3 etc.
distortionMapCoordinate.t -= time * riseFactor;
float hash(float n) { return fract(sin(n)*43578.5453); }
float hash(vec2 n) { return hash(dot(n, vec2(12.232, 94.454))); }
vec4 distortionMapValue = texture2D(distortionMapTexture, distortionMapCoordinate);
// The values are normalized by OpenGL to lie in the range [0, 1]
// We want negative offsets too, so we subtract 0.5 and multiply by 2
// We end up with values in the range [-1, 1]
vec2 distortionPositionOffset = distortionMapValue.xy;
distortionPositionOffset -= vec2(0.5f, 0.5f);
distortionPositionOffset *= 2.f;
// The factor scales the offset and thus controls the severity
distortionPositionOffset *= distortionFactor;
float de(vec2 p) {
p.x += time*0.2;;
float m = 1.0;
vec2 q = p;
for(int i = 0; i < 3; i++) {
p = q;
vec2 t = floor(p);
p = fract(p) - 0.5;
p.x *= 2.0*floor(1.8*fract(hash(t))) - 1.0;
float d = abs(1.0 - 2.0*abs(p.x + p.y))/(2.0*sqrt(5.0));
m = min(m, smoothstep(0.0, 0.05, d));
q *= 2.0;
}
return m;
}
// The latter 2 channels of the texture are unused... be creative
vec2 distortionUnused = distortionMapValue.zw;
vec3 bump(vec2 p, float e, float z) {
vec2 r = vec2(e, 0.0); vec2 l = r.yx;
vec3 g = vec3(
de(p + r) - de(p - r),
de(p + l) - de(p - l),
z);
return normalize(g);
}
// Since we all know that hot air rises and cools,
// the effect loses its severity the higher up we get
// We use the t (a.k.a. y) texture coordinate of the original texture
// to tell us how "high up" we are and damp accordingly
// Remember, OpenGL 0 is at the bottom
distortionPositionOffset *= (1.f - gl_TexCoord[0].t);
vec3 render(vec2 p) {
vec3 col = vec3(0);
vec3 rd = normalize(vec3(p, 1.97));
vec3 sn = bump(p, 0.01, -0.6);
col += pow(clamp(dot(-rd, sn), 0.3, 1.0), 10.0);
return col;
}
vec2 distortedTextureCoordinate = gl_TexCoord[0].st + distortionPositionOffset;
void main( void ) {
vec2 p = (-resolution + 2.0*gl_FragCoord.xy)/resolution.y;
gl_FragColor = gl_Color * texture2D(currentTexture, distortedTextureCoordinate);
//vec3 col = vec3(de(p));
vec3 col = render(p);
gl_FragColor = vec4(col, 1);
}
// Simple passthrough vertex shader... Nothing fancy here.
void main()
{
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
gl_FrontColor = gl_Color;
}
// Example from http://glslsandbox.com/
#ifdef GL_ES
precision mediump float;
#endif
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
float length2(vec2 p) { return dot(p, p); }
float noise(vec2 p){
return fract(sin(fract(sin(p.x) * (43.13311)) + p.y) * 31.0011);
}
float worley(vec2 p) {
float d = 1e30;
for (int xo = -1; xo <= 1; ++xo) {
for (int yo = -1; yo <= 1; ++yo) {
vec2 tp = floor(p) + vec2(xo, yo);
d = min(d, length2(p - tp - vec2(noise(tp))));
}
}
return 5.0*exp(-4.0*abs(2.0*d - 1.0));
}
float fworley(vec2 p) {
return sqrt(sqrt(sqrt(
1.1 * // light
worley(p*5. + .3 + time*.2525) *
sqrt(worley(p * 50. + 0.3 + time * -1.15)) *
sqrt(sqrt(worley(p * -10. + 9.3))))));
}
void main() {
vec2 uv = gl_FragCoord.xy / resolution.xy;
float t = fworley(uv * resolution.xy / 1500.0);
t *= exp(-length2(abs(0.7*uv - 1.0)));
gl_FragColor = vec4(t * vec3(0.1, 1.5*t, 1.2*t + pow(t, 0.5-t)), 1.0);
}
// Example from http://glslsandbox.com/
#ifdef GL_ES
precision mediump float;
#endif
uniform float time;
uniform vec2 resolution;
#define hash(a) fract(sin(a)*12345.0)
#define noise(p) ((old_noise(p, 883.0, 971.0) + old_noise(p + 0.5, 113.0, 157.0)) * 0.5)
float old_noise(vec3 x, float c1, float c2) {
vec3 p = floor(x);
vec3 f = fract(x);
f = f*f*(3.0-2.0*f);
float n = p.x + p.y*c2+ c1*p.z;
return mix(
mix(
mix(hash(n+0.0),hash(n+1.0),f.x),
mix(hash(n+c2),hash(n+c2+1.0),f.x),
f.y),
mix(
mix(hash(n+c1),hash(n+c1+1.0),f.x),
mix(hash(n+c1+c2),hash(n+c1+c2+1.0),f.x),
f.y),
f.z);
}
float fbm(vec2 n)
{
float total = 0.0, amplitude = 1.0;
for (int i = 0; i < 5; i++)
{
total += noise(vec3(n.x, n.y, 0.0) * 2.0) * amplitude;
n += n;
amplitude *= 0.5;
}
return total;
}
void main( void )
{
vec2 uv = (gl_FragCoord.xy / resolution.xy * 2. - 1.)*2.00;
const vec3 c1 = vec3(0.1, 0.0, 0.0);
const vec3 c2 = vec3(0.7, 0.5, 0.0);
const vec3 c3 = vec3(0.2, 0.0, 0.0);
const vec3 c4 = vec3(1.0, 0.9, 0.0);
const vec3 c5 = vec3(0.1);
const vec3 c6 = vec3(0.9);
vec2 p = gl_FragCoord.xy * 8.0 / resolution.xx;
float q = fbm(p - vec2(0.0, time * 0.4));
vec2 r = vec2(fbm(p + q + time * 0.7 - p.x - p.y), fbm(p + q - vec2(0.0, time * 0.94)));
vec3 c = mix(c1, c2, fbm(p + r * 0.7)) + mix(c3, c4, r.x) - mix(c5, c6, r.y);
c=pow(c * cos(1.57 * gl_FragCoord.y / resolution.y), vec3(3.0));
gl_FragColor = vec4(c, 1);
}
// Example from http://glslsandbox.com/
#ifdef GL_ES
precision mediump float;
#endif
#extension GL_OES_standard_derivatives : enable
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
void main( void )
{
vec2 position = gl_FragCoord.xy;
vec4 finalColor = vec4(0.0, 0.0, 0.0, 1.0);
for (int i = 0; i < 10; i++) {
float a = float(i)*0.25;
vec2 loc = 200.0*vec2(cos(time+a), abs(sin(time+a))) + resolution.xy/2.0;
if(distance(position, loc) < 23.0+sin(time*1.0+a)*22.0){
finalColor += 0.5*vec4(.5, 0.0, .5, 1.0);
}
}
gl_FragColor = finalColor;
}
// Example from http://glslsandbox.com/
#ifdef GL_ES
precision mediump float;
#endif
#extension GL_OES_standard_derivatives : enable
#define NUM_OCTAVES 16
uniform float time;
uniform vec2 resolution;
mat3 rotX(float a) {
float c = cos(a);
float s = sin(a);
return mat3(
1, 0, 0,
0, c, -s,
0, s, c
);
}
mat3 rotY(float a) {
float c = cos(a);
float s = sin(a);
return mat3(
c, 0, -s,
0, 1, 0,
s, 0, c
);
}
float random(vec2 pos) {
return fract(sin(dot(pos.xy, vec2(12.9898, 78.233))) * 43758.5453123);
}
float noise(vec2 pos) {
vec2 i = floor(pos);
vec2 f = fract(pos);
float a = random(i + vec2(0.0, 0.0));
float b = random(i + vec2(1.0, 0.0));
float c = random(i + vec2(0.0, 1.0));
float d = random(i + vec2(1.0, 1.0));
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(a, b, u.x) + (c - a) * u.y * (1.0 - u.x) + (d - b) * u.x * u.y;
}
float fbm(vec2 pos) {
float v = 0.0;
float a = 0.5;
vec2 shift = vec2(100.0);
mat2 rot = mat2(cos(0.5), sin(0.5), -sin(0.5), cos(0.5));
for (int i=0; i<NUM_OCTAVES; i++) {
v += a * noise(pos);
pos = rot * pos * 2.0 + shift;
a *= 0.5;
}
return v;
}
void main(void) {
vec2 p = (gl_FragCoord.xy * 2.0 - resolution.xy) / min(resolution.x, resolution.y);
float t = 0.0, d;
float time2 = time / 2.0;
vec2 q = vec2(0.0);
q.x = fbm(p + 0.00 * time2);
q.y = fbm(p + vec2(1.0));
vec2 r = vec2(0.0);
r.x = fbm(p + 1.0 * q + vec2(1.7, 9.2) + 0.15 * time2);
r.y = fbm(p + 1.0 * q + vec2(8.3, 2.8) + 0.126 * time2);
float f = fbm(p + r);
vec3 color = mix(
vec3(0.101961, 0.619608, 1.666667),
vec3(0.666667, 0.666667, 1.498039),
clamp((f * f) * 4.0, 0.0, 1.0)
);
color = mix(
color,
vec3(0, 0, 0.164706),
clamp(length(q), 0.0, 1.0)
);
color = mix(
color,
vec3(0.666667, 1, 1),
clamp(length(r.x), 0.0, 1.0)
);
color = (f *f * f + 0.6 * f * f + 0.5 * f) * color;
gl_FragColor = vec4(color, 1.0);
}
#version 120
uniform sampler2D currentTexture;
uniform float time;
void main() {
vec2 coord = gl_TexCoord[0].xy;
coord.x += sin(radians(500*time + coord.y * 500)) * 0.02;
coord.y += cos(radians(500*time + coord.x * 250)) * 0.03;
vec4 pixel_color = texture2D(currentTexture, coord);
gl_FragColor = pixel_color;
}
#version 130
uniform sampler2D currentTexture; // Our render texture
uniform sampler2D distortionMapTexture; // Our heat distortion map texture
uniform float time; // Time used to scroll the distortion map
uniform float distortionFactor; // Factor used to control severity of the effect
uniform float riseFactor; // Factor used to control how fast air rises
void main()
{
vec2 distortionMapCoordinate = gl_TexCoord[0].st;
// We use the time value to scroll our distortion texture upwards
// Since we enabled texture repeating, OpenGL takes care of
// coordinates that lie outside of [0, 1] by discarding
// the integer part and keeping the fractional part
// Basically performing a "floating point modulo 1"
// 1.1 = 0.1, 2.4 = 0.4, 10.3 = 0.3 etc.
distortionMapCoordinate.t -= time * riseFactor;
vec4 distortionMapValue = texture2D(distortionMapTexture, distortionMapCoordinate);
// The values are normalized by OpenGL to lie in the range [0, 1]
// We want negative offsets too, so we subtract 0.5 and multiply by 2
// We end up with values in the range [-1, 1]
vec2 distortionPositionOffset = distortionMapValue.xy;
distortionPositionOffset -= vec2(0.5f, 0.5f);
distortionPositionOffset *= 2.f;
// The factor scales the offset and thus controls the severity
distortionPositionOffset *= distortionFactor;
// The latter 2 channels of the texture are unused... be creative
vec2 distortionUnused = distortionMapValue.zw;
// Since we all know that hot air rises and cools,
// the effect loses its severity the higher up we get
// We use the t (a.k.a. y) texture coordinate of the original texture
// to tell us how "high up" we are and damp accordingly
// Remember, OpenGL 0 is at the bottom
distortionPositionOffset *= (1.f - gl_TexCoord[0].t);
vec2 distortedTextureCoordinate = gl_TexCoord[0].st + distortionPositionOffset;
gl_FragColor = gl_Color * texture2D(currentTexture, distortedTextureCoordinate);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment