Commit d57ff44f authored by Geequette's avatar Geequette

add water effect

parent 22b7e6c2
#version 120
uniform sampler2D currentTexture;
//uniform float[9] conMatrix;
//uniform float conWeight;
//uniform vec2 conPixel;
vec2 conPixel =vec2(1./256.,1./256.);
float conMatrix[9] = float[](1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0);
float conWeight=1./9.;
void main(void)
{
vec4 color = vec4(0.0);
vec2 texCoord = gl_TexCoord[0].st;
vec2 offset = conPixel * 1.5;
vec2 start = texCoord - offset;
vec2 current = start;
for (int i = 0; i < 9; i++)
{
color += texture2D( currentTexture, current ) * conMatrix[i];
current.x += conPixel.x;
if (i == 2 || i == 5) {
current.x = start.x;
current.y += conPixel.y;
}
}
gl_FragColor = color * conWeight;
}
......@@ -16,11 +16,11 @@
// Example 2 : breath
// Example 3 : shaked
// Example 4 : fade
#define SHADER_FILENAME "../example_001.frag"
//#define SHADER_FILENAME "../example_001.frag"
//#define SHADER_FILENAME "../example_002.frag"
//#define SHADER_FILENAME "../example_003.frag"
//#define SHADER_FILENAME "../example_004.frag"
#define SHADER_FILENAME "../example_005.frag"
#define WINDOW_WIDTH 256
#define WINDOW_HEIGHT 256
......
......@@ -29,7 +29,8 @@ LIBS += -lsfml-system
# Sources files
SOURCES += main.cpp
DISTFILES += example_001.frag
DISTFILES += example_001.frag \
example_005.frag
DISTFILES += example_002.frag
DISTFILES += example_003.frag
DISTFILES += example_004.frag
......
......@@ -70,8 +70,6 @@ int main()
// Load shaders
sf::Shader shader;
// if (!shader.loadFromFile("../../shaders/example_011.vert", "../../shaders/example_011.frag"))
if (!shader.loadFromFile("../example_001.frag", sf::Shader::Fragment))
{
std::cerr << "Error while shaders" << std::endl;
......
......@@ -31,9 +31,6 @@ SOURCES += main.cpp
# Fragment files
DISTFILES += example_001.frag
DISTFILES += example_002.frag
DISTFILES += example_003.frag
DISTFILES += example_004.frag
......
/*!
* \file main.cpp
* \brief Music and sound examples (http://www.lucidarme.me/?p=6127)
* \author Philippe Lucidarme
* \brief apply a heat shader to a sprite (http://www.lucidarme.me/?p=6127)
* \author Philippe Lucidarme (from https://github.com/SFML/SFML/wiki/Source:-HeatHazeShader)
* \version 1.0
* \date 12/20/2016
* \date 12/19/2016
*/
#include <SFML/Audio.hpp>
// SFML libraries
#include <SFML/Graphics.hpp>
#include <iostream>
#include <math.h>
// Wait for ms milliseconds
void delay_ms(int ms)
int main()
{
sf::Clock Timer;
while (Timer.getElapsedTime().asMilliseconds()<ms);
}
// _____________________
// ::: Create window :::
// Create a window with the same pixel depth as the desktop
sf::VideoMode desktopMode = sf::VideoMode::getDesktopMode();
sf::RenderWindow window(sf::VideoMode( desktopMode.width,
desktopMode.height,
desktopMode.bitsPerPixel),
"SFML part 5",
sf::Style::Fullscreen);
int main()
{
// _____________________________
// ::: load background music :::
// Enable vertical sync. (vsync)
window.setVerticalSyncEnabled (true);
// ____________________
// ::: Load texture :::
sf::Music music;
if (!music.openFromFile("../music.ogg"))
// Create texture from PNG file
sf::Texture texture;
if (!texture.loadFromFile("../sfml-icon-small.png"))
{
std::cerr << "Error while loading background music file" << std::endl;
std::cerr << "Error while loading texture" << std::endl;
return -1;
}
// Enable the smooth filter. The texture appears smoother so that pixels are less noticeable.
texture.setSmooth(true);
// __________________
// ::: Load sound :::
// _______________________________________
// ::: Create sprite and apply texture :::
sf::SoundBuffer buffer;
if (!buffer.loadFromFile("../birds.wav"))
// Create the sprite and apply the texture
sf::Sprite sprite;
sprite.setTexture(texture);
sf::FloatRect spriteSize=sprite.getGlobalBounds();
// Set origin in the middle of the sprite
sprite.setOrigin(spriteSize.width/2.,spriteSize.height/2.);
// _______________
// ::: Shaders :::
// Check if shaders are available
if (!sf::Shader::isAvailable())
{
std::cerr << "Error while loading sound file" << std::endl;
std::cerr << "Shader are not available" << std::endl;
return -1;
}
// ____________________
// ::: Play sounds :::
// Play music
std::cout << "Start background music" << std::endl;
music.play();
delay_ms(1500);
// Play wav sound over the music
std::cout << "Play wav sound" << std::endl;
sf::Sound sound;
sound.setBuffer(buffer);
sound.play();
delay_ms(1500);
// Increase pitch
std::cout << "Increase pitch" << std::endl;
music.setPitch(1.4);
delay_ms(1500);
// Reset pitch
std::cout << "Reset pitch" << std::endl;
music.setPitch(1.);
delay_ms(1500);
// Modify volume
std::cout << "Fade out" << std::endl;
for (int vol=100;vol>5;vol--)
// Load shaders
sf::Shader shader;
if (!shader.loadFromFile("../water.frag", sf::Shader::Fragment))
{
music.setVolume(vol);
delay_ms(50);
std::cerr << "Error while shaders" << std::endl;
return -1;
}
// Stop music
std::cout << "Stop music" << std::endl;
music.stop();
// ___________________________
// ::: Load distortion map :::
sf::Texture distortionMap;
if (!distortionMap.loadFromFile("../noiseWater.png"))
{
sf::err() << "Error while loading distortion map" << std::endl;
return -1;
}
// It is important to set repeated to true to enable scrolling upwards
distortionMap.setRepeated(true);
// Setting smooth to true lets us use small maps even on larger images
distortionMap.setSmooth(true);
// Set texture to the shader
shader.setParameter("currentTexture", sf::Shader::CurrentTexture);
shader.setParameter("distortionMapTexture", distortionMap);
// Shader parameters
float level = .5f;
// _________________
// ::: Main loop :::
sf::Clock timer;
while (window.isOpen())
{
// Process events
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();
// If a key is pressed
if (event.type == sf::Event::KeyPressed)
{
switch (event.key.code)
{
// If escape is pressed, close the application
case sf::Keyboard::Escape : window.close(); break;
// Process the up, down, left and right keys to modify parameters
case sf::Keyboard::Up : level -= .005f; break;
case sf::Keyboard::Down: level += .005f; break;
default : break;
}
}
}
if (level>0.8) level=0.8;
if (level<0.2) level=0.2;
// Set shader parameters
shader.setParameter("time", timer.getElapsedTime().asSeconds());
shader.setParameter("level",level);
// Clear the window and apply grey background
window.clear( sf::Color(127,127,127));
// Draw the sprite and apply shader
sprite.setPosition(window.getSize().x/2.,window.getSize().y/2.);
sprite.setScale(2,2);
window.draw(sprite,&shader);
// Update display and wait for vsync
window.display();
}
return 0;
}
......@@ -29,10 +29,12 @@ LIBS += -lsfml-system
# Sources files
SOURCES += main.cpp
# Audio files
DISTFILES += birds.wav
DISTFILES += music.ogg
# Fragment files
DISTFILES += \
noiseWater.png \
sfml-icon-small.png \
water.frag
#version 130
uniform sampler2D currentTexture;
uniform sampler2D distortionMapTexture;
uniform float time;
uniform float level;
void main()
{
vec2 coord = gl_TexCoord[0].xy;
// Get the color of the noise texture at a position the current fragment position offset by the time
vec4 noiseTexCol = texture2D(distortionMapTexture, vec2(gl_TexCoord[0].x + 0.5*time, gl_TexCoord[0].y + 0.5*time));
// Reduce the offset
float reducedOffset = noiseTexCol.x / 80;
// Upper part is normal
if (coord.y+reducedOffset<level)
{
// lookup the pixel in the texture
vec4 pixel = texture2D(currentTexture, gl_TexCoord[0].xy);
// multiply it by the color
gl_FragColor = gl_Color * pixel;
}
else
{
// Get the color of the screen at the offset location
vec4 col = texture2D(currentTexture, gl_TexCoord[0].xy + vec2(reducedOffset, reducedOffset));
// Set the fragment color
gl_FragColor = vec4 (col.r/2,col.g/2,col.b/1,col.a);
}
}
/*!
* \file main.cpp
* \brief start SFML in full screen and display a sprite (http://www.lucidarme.me/?p=6127)
* \brief Music and sound examples (http://www.lucidarme.me/?p=6127)
* \author Philippe Lucidarme
* \version 1.0
* \date 12/18/2016
* \date 12/20/2016
*/
// SFML libraries
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
int main()
// Wait for ms milliseconds
void delay_ms(int ms)
{
// _____________________
// ::: Create window :::
sf::Clock Timer;
while (Timer.getElapsedTime().asMilliseconds()<ms);
}
// Create a window with the same pixel depth as the desktop
sf::VideoMode desktopMode = sf::VideoMode::getDesktopMode();
sf::RenderWindow window(sf::VideoMode( desktopMode.width,
desktopMode.height,
desktopMode.bitsPerPixel),
"SFML part 3",
sf::Style::Fullscreen);
int main()
{
// _____________________________
// ::: load background music :::
sf::Music music;
if (!music.openFromFile("../music.ogg"))
{
std::cerr << "Error while loading background music file" << std::endl;
return -1;
}
// Enable vertical sync. (vsync)
window.setVerticalSyncEnabled (true);
// _________________
// ::: Load font :::
// __________________
// ::: Load sound :::
sf::Font font;
if (!font.loadFromFile("../example_font.ttf"))
sf::SoundBuffer buffer;
if (!buffer.loadFromFile("../birds.wav"))
{
std::cerr << ".Error while loading font" << std::endl;
std::cerr << "Error while loading sound file" << std::endl;
return -1;
}
sf::Text text;
text.setFont(font); // font est un sf::Font
text.setString(L"www.lucidarme.me");
text.setCharacterSize(60); // exprimée en pixels, pas en points !
text.setColor(sf::Color::Black);
text.setOrigin(text.getLocalBounds().width/2. , text.getLocalBounds().height/2.);
// ____________________
// ::: Play sounds :::
// Play music
std::cout << "Start background music" << std::endl;
music.play();
delay_ms(1500);
// Play wav sound over the music
std::cout << "Play wav sound" << std::endl;
sf::Sound sound;
sound.setBuffer(buffer);
sound.play();
delay_ms(1500);
std::cout << text.getLocalBounds().width << "\t" << text.getLocalBounds().height;
// _________________
// ::: Main loop :::
// Increase pitch
std::cout << "Increase pitch" << std::endl;
music.setPitch(1.4);
delay_ms(1500);
sf::Clock timer;
while (window.isOpen())
// Reset pitch
std::cout << "Reset pitch" << std::endl;
music.setPitch(1.);
delay_ms(1500);
// Modify volume
std::cout << "Fade out" << std::endl;
for (int vol=100;vol>5;vol--)
{
// Process events
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();
if (event.type == sf::Event::KeyPressed) window.close();
}
// Clear the window and apply grey background
window.clear( sf::Color(127,127,127));
text.setPosition(window.getSize().x/2. , window.getSize().y/2.);
window.draw(text);
// Update display and wait for vsync
window.display();
music.setVolume(vol);
delay_ms(50);
}
// Stop music
std::cout << "Stop music" << std::endl;
music.stop();
return 0;
}
......@@ -4,7 +4,7 @@ QT -= gui
# Binary name
TARGET = part_00*
TARGET = part_009
# Console app
CONFIG += console
# Puts the executable into a bundle
......@@ -29,8 +29,9 @@ LIBS += -lsfml-system
# Sources files
SOURCES += main.cpp
DISTFILES += \
example_font.ttf
# Audio files
DISTFILES += birds.wav
DISTFILES += music.ogg
......
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe
/*!
* \file main.cpp
* \brief start SFML in full screen and display a sprite (http://www.lucidarme.me/?p=6127)
* \author Philippe Lucidarme
* \version 1.0
* \date 12/18/2016
*/
// SFML libraries
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
// _____________________
// ::: Create window :::
// Create a window with the same pixel depth as the desktop
sf::VideoMode desktopMode = sf::VideoMode::getDesktopMode();
sf::RenderWindow window(sf::VideoMode( desktopMode.width,
desktopMode.height,
desktopMode.bitsPerPixel),
"SFML part 3",
sf::Style::Fullscreen);
// Enable vertical sync. (vsync)
window.setVerticalSyncEnabled (true);
// _________________
// ::: Load font :::
sf::Font font;
if (!font.loadFromFile("../example_font.ttf"))
{
std::cerr << ".Error while loading font" << std::endl;
return -1;
}