Commit 1ebf6c2f authored by Geequette's avatar Geequette

part 005 added

parent bb087772
# 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>
// Sprite speed (high values = high speed)
#define SPRITE_SPEED 5
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("../../img/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_001.vert", "../../shaders/example_001.frag"))
{
std::cerr << "Error while shaders" << std::endl;
return -1;
}
// _________________
// ::: 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;
default : break;
}
}
}
shader.setParameter("time", timer.getElapsedTime().asSeconds());
// 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.);
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_005
# 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_001.frag
DISTFILES += ../shaders/example_001.vert
......@@ -72,7 +72,7 @@ int main()
// Load shaders
sf::Shader shader;
if (!shader.loadFromFile("../../shaders/example_001.vert", "../../shaders/example_001.frag"))
if (!shader.loadFromFile("../../shaders/example_002.vert", "../../shaders/example_002.frag"))
{
std::cerr << "Error while shaders" << std::endl;
return -1;
......
......@@ -4,7 +4,7 @@ QT -= gui
# Binary name
TARGET = part_005
TARGET = part_006
# Console app
CONFIG += console
# Puts the executable into a bundle
......@@ -30,8 +30,8 @@ LIBS += -lsfml-system
SOURCES += main.cpp
DISTFILES += \
../shaders/example_001.frag \
../shaders/example_001.vert
../shaders/example_002.frag \
../shaders/example_002.vert
......
#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);
}
// 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;
}
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