Skip to content

Instantly share code, notes, and snippets.

@drako0812
Last active August 27, 2016 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drako0812/0f47ad46f6d4b4c5c15b234cbc15e280 to your computer and use it in GitHub Desktop.
Save drako0812/0f47ad46f6d4b4c5c15b234cbc15e280 to your computer and use it in GitHub Desktop.
Retro-ification Fragment Shader
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <string>
#include <memory>
#include <cmath>
constexpr sf::Uint8 ColorFloatToU8(float val) {
return static_cast<sf::Uint8>(255.0f * val);
}
sf::Color ColorFromHSV(float hue, float sat, float val, float alpha=1.0f) {
int i;
float f, p, q, t;
sf::Uint8 a = ColorFloatToU8(alpha);
sf::Uint8 r,g,b;
if(sat == 0) {
r = g = b = ColorFloatToU8(val);
return sf::Color(r, g, b, a);
}
hue *= 6.0;
hue = std::fmod(hue, 6.0f);
i = std::floor(hue);
f = hue - i;
p = val * (1 - sat);
q = val * (1 - sat * f);
t = val * (1 - sat * (1 - f));
switch(i) {
case 0:
r = ColorFloatToU8(val);
g = ColorFloatToU8(t);
b = ColorFloatToU8(p);
break;
case 1:
r = ColorFloatToU8(q);
g = ColorFloatToU8(val);
b = ColorFloatToU8(p);
break;
case 2:
r = ColorFloatToU8(p);
g = ColorFloatToU8(val);
b = ColorFloatToU8(t);
break;
case 3:
r = ColorFloatToU8(p);
g = ColorFloatToU8(q);
b = ColorFloatToU8(val);
break;
case 4:
r = ColorFloatToU8(t);
g = ColorFloatToU8(p);
b = ColorFloatToU8(val);
break;
default:
r = ColorFloatToU8(val);
g = ColorFloatToU8(p);
b = ColorFloatToU8(q);
}
return sf::Color(r, g, b, a);
}
int main() {
std::cout << "Creating RenderWindow\n";
sf::RenderWindow window(
sf::VideoMode(800, 600),
"SFML window",
sf::Style::Close,
sf::ContextSettings(0, 0, 0, 4, 4, sf::ContextSettings::Default)
);
std::cout << "Loading Shader\n";
sf::Shader shader;
// vvv ATTENTION: This is where the glsl shader is failing to compile! vvv
if(!shader.loadFromFile("retro-frag.glsl", sf::Shader::Fragment)) {
std::cerr << "ERROR: Unable to load shader!\n";
return 1;
}
std::cout << "Creating Image : ";
sf::Image img;
img.create(256, 256, sf::Color(255, 255, 255));
for(unsigned int j = 0; j < 256; ++j) {
for(unsigned int i = 0; i < 256; ++i) {
img.setPixel(i, j,
ColorFromHSV(
i / 255.0f,
j / 255.0f,
1.0f));
}
}
std::cout << "Creating Texture\n"; std::cout.flush();
sf::Texture tex;
tex.loadFromImage(img);
sf::Texture tex2;
tex2.loadFromImage(img);
std::cout << "Creating Sprites\n"; std::cout.flush();
sf::Sprite sprite(tex);
sf::Sprite sprite2(tex2);
sprite2.setPosition(400, 0);
shader.setUniform("texture", sf::Shader::CurrentTexture);
shader.setUniform("bit_precision", sf::Glsl::Vec3(3, 3, 2));
std::cout << "Starting Game Loop\n"; std::cout.flush();
while(window.isOpen()) {
sf::Event event;
while(window.pollEvent(event)) {
if(event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::Green);
window.draw(sprite);
window.draw(sprite2, &shader);
window.display();
}
return 0;
}
#version 440 compatibility
uniform sampler2D texture;
uniform ivec3 bit_precision; // A syntax error happens on this line for some reason
void main() {
vec3 prec_fac = vec3(float(1 << bit_precision.x), float(1 << bit_precision.y), float(1 << bit_precision.z));
vec3 factor_adjust = 1.0f / (prec_fac - 1.0f);
vec4 color = texture2D(texture, gl_TexCoord[0].xy);
vec4 low_color = vec4(
floor(color.x * (prec_fac.x + 0.01f)) * factor_adjust.x,
floor(color.y * (prec_fac.y + 0.01f)) * factor_adjust.y,
floor(color.z * (prec_fac.z + 0.01f)) * factor_adjust.z,
1.0f);
gl_FragColor = low_color;
}
@drako0812
Copy link
Author

drako0812 commented Aug 27, 2016

For some reason I'm getting a syntax error when trying to compile this shader.

Exact error:

Failed to compile fragment shader:
Fragment shader failed to compile with the following errors:
ERROR: 0:4: error(#132) Syntax error: ERROR: error(#273) 1 compilation errors.  No code generated

Edit:

Associated stackoverflow question: http://stackoverflow.com/q/39176620/2186002

Edit 2:

FIXED!!!

@drako0812
Copy link
Author

Here's the output of the provided main.cpp:

screenshot1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment