Commit e480de3c authored by Geequette's avatar Geequette

Improve shader and put ressources in folders

parent cd9fc04d
#version 120
uniform sampler2D currentTexture;
uniform float time;
void main()
{
vec2 coord = gl_TexCoord[0].xy;
float factorx=0.02*( 1 + cos(5.*time));
float factory=0.02*( 1 - cos(5.*time));
coord.x = (coord.x+factorx)/(1.+2.*factorx);
coord.y = (coord.y+factory)/(1.+2.*factory);
vec4 pixel_color = texture2D(currentTexture, coord);
gl_FragColor = pixel_color;
}
......@@ -2,11 +2,10 @@
uniform sampler2D currentTexture;
uniform float time;
void main() {
void main()
{
vec2 coord = gl_TexCoord[0].xy;
coord.x = coord.x/(0.5+0.2*cos(5*time));
coord.y += cos(radians(1500*time + coord.y * 0)) * 0.05*sin(time);
coord.y += cos(radians(3000*time + coord.y * 0)) * 0.05*sin(time);
vec4 pixel_color = texture2D(currentTexture, coord);
gl_FragColor = pixel_color;
}
#version 120
uniform sampler2D currentTexture;
uniform float time;
void main() {
vec2 coord = gl_TexCoord[0].xy;
vec4 pixel_color = texture2D(currentTexture, coord);
float alpha=0.5+0.5*cos(2*time);
gl_FragColor = vec4 (pixel_color.xyz,min(alpha,pixel_color.w));
}
/*!
* \file main.cpp
* \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)
* \brief apply a shaders to a sprite (http://www.lucidarme.me/?p=6127)
* \author Philippe Lucidarme
* \version 1.0
* \date 12/19/2016
* \date 12/20/2016
*/
// SFML libraries
......@@ -11,23 +11,43 @@
#include <iostream>
#include <math.h>
// Sprite speed (high values = high speed)
#define SPRITE_SPEED 5
// Example 1 : distorsion
// Example 2 : breath
// Example 3 : shaked
#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 WINDOW_WIDTH 256
#define WINDOW_HEIGHT 256
int main()
{
// Create a non resizable window
sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "SFML Shader Example", sf::Style::Titlebar | sf::Style::Close);
// Create a window size texture and a sprite for the shader
sf::Texture tex;
tex.create(WINDOW_WIDTH, WINDOW_HEIGHT);
sf::Sprite spr(tex);
// _____________________
// ::: 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",
"SFML part 6",
sf::Style::Fullscreen);
*/
// Enable vertical sync. (vsync)
window.setVerticalSyncEnabled (true);
......@@ -38,7 +58,7 @@ int main()
// Create texture from PNG file
sf::Texture texture;
if (!texture.loadFromFile("../../img/sfml-icon-small.png"))
if (!texture.loadFromFile("../sfml-icon-small.png"))
{
std::cerr << "Error while loading texture" << std::endl;
return -1;
......@@ -72,9 +92,7 @@ int main()
// Load shaders
sf::Shader shader;
// if (!shader.loadFromFile("../../shaders/example_011.vert", "../../shaders/example_011.frag"))
if (!shader.loadFromFile("../../shaders/example_010.frag", sf::Shader::Fragment))
if (!shader.loadFromFile(SHADER_FILENAME, sf::Shader::Fragment))
{
std::cerr << "Error while shaders" << std::endl;
return -1;
......@@ -82,35 +100,11 @@ int main()
// ___________________________
// ::: Load distortion map :::
sf::Texture distortionMap;
if (!distortionMap.loadFromFile("../../img/distortion_map.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 distortionFactor = .05f;
float riseFactor = .2f;
// _________________
// ::: Main loop :::
sf::Clock timer;
sf::Clock time;
while (window.isOpen())
{
// Process events
......@@ -120,29 +114,12 @@ int main()
// 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 : distortionFactor *= 2.f; break;
case sf::Keyboard::Down: distortionFactor /= 2.f; break;
case sf::Keyboard::Left: riseFactor *= 2.f; break;
case sf::Keyboard::Right: riseFactor /= 2.f; break;
default : break;
}
}
// If escape is pressed, close the application
if (event.type == sf::Event::KeyPressed && event.key.code==sf::Keyboard::Escape) window.close();
}
// Set shader parameters
shader.setParameter("time", timer.getElapsedTime().asSeconds());
shader.setParameter("distortionFactor", distortionFactor);
shader.setParameter("riseFactor", riseFactor);
shader.setParameter("time", time.getElapsedTime().asSeconds());
// Clear the window and apply grey background
window.clear( sf::Color(127,127,127));
......
......@@ -33,7 +33,11 @@ DISTFILES += \
../shaders/example_002.vert \
../shaders/example_012.frag \
../shaders/example_011.frag \
../shaders/example_010.frag
../shaders/example_010.frag \
example_001.frag \
example_002.frag \
example_003.frag \
example_004.frag
......
# 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 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/19/2016
*/
// SFML libraries
#include <SFML/Graphics.hpp>
#include <iostream>
#include <math.h>
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 5",
sf::Style::Fullscreen);
// Enable vertical sync. (vsync)
window.setVerticalSyncEnabled (true);
// ____________________
// ::: Load texture :::
// Create texture from PNG file
sf::Texture texture;
if (!texture.loadFromFile("../sfml-icon-small.png"))
{
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);
// _______________________________________
// ::: Create sprite and apply texture :::
// 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 << "Shader are not available" << std::endl;
return -1;
}
// 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;
return -1;
}
// ___________________________
// ::: Load distortion map :::
sf::Texture distortionMap;
if (!distortionMap.loadFromFile("../distortion_map.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 distortionFactor = .05f;
float riseFactor = .2f;
// _________________
// ::: 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 : distortionFactor *= 2.f; break;
case sf::Keyboard::Down: distortionFactor /= 2.f; break;
case sf::Keyboard::Left: riseFactor *= 2.f; break;
case sf::Keyboard::Right: riseFactor /= 2.f; break;
default : break;
}
}
}
// Set shader parameters
shader.setParameter("time", timer.getElapsedTime().asSeconds());
shader.setParameter("distortionFactor", distortionFactor);
shader.setParameter("riseFactor", riseFactor);
// 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;
}
# Dont' need Qt libs
QT -= core
QT -= gui
# Binary name
TARGET = part_007
# Console app
CONFIG += console
# Puts the executable into a bundle
CONFIG -= app_bundle
# This is an applications
TEMPLATE = app
# Create directories for temporrary files
OBJECTS_DIR = tmp
MOC_DIR = tmp
# Binaries will be placed in the bin directory
DESTDIR = bin
LIBS += -lsfml-audio
LIBS += -lsfml-graphics
LIBS += -lsfml-network
LIBS += -lsfml-window
LIBS += -lsfml-system
# Sources files
SOURCES += main.cpp
DISTFILES += \
../shaders/example_002.vert \
../shaders/example_012.frag \
../shaders/example_011.frag \
../shaders/example_010.frag \
example_001.frag \
example_002.frag \
example_003.frag \
example_004.frag
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