Jump to content

theDaftDev

Member Since 21 Mar 2014
Offline Last Active May 18 2017 04:34 AM
-----

#97158 The member photo album.

Posted by theDaftDev on 17 November 2015 - 05:10 PM

I'm in the Matrix for a living.

 

BSg4Dqs.jpg

 

IlTXMKy.jpg

 

Perception neuron mocap suit + htc vive




#84555 Nous Sommes Tous Charlie

Posted by theDaftDev on 09 January 2015 - 12:13 PM

It happend roughly ten minutes from the place I study at.

I barely even feel concerned about France most of the time, but these events were dramatic.

There has been this heavy and surreal atmosphere all the week long. 

 

Three terrorists who were involved in the killing got neutralized and killed a hour ago.

One of them is still on the run, a woman, according to the police. 

One of my friends was stuck in his house all the afternoon, since he lives where shootings took place, followed by hostages being held in a store. 

He already had a hard time handling all this shit, so that kinda sucks. It's a relief that all this shit is now over. It's been a stressing week. 

shit shit shit. 




#78896 New to computers

Posted by theDaftDev on 27 August 2014 - 10:37 PM

I'm not sure if this is the place it should go but I need some help. I'm 17 and I really want to learn about computers and programming. Where do I start? Everything is so overwhelming. Can any of you give me some tips? It would be greatly appreciated.

 

It relies on what you would like to do. 

Do you want to make websites, webapps, games, reverse engineering.. etc?

As an introduction, I'd suggest you to get started with web languages, such as html, css and eventually js or php, which mean that you would start by building websites.




#75119 The member photo album.

Posted by theDaftDev on 21 July 2014 - 01:12 AM

Ehh, I usually don't like myself enough to post pics of my bitch face around the internet, but why not to give it a try, I guess.

 

EW.png

 

Ew. The hell am I doing with my mouth.




#74666 Hey look it's Sethre's Birthday

Posted by theDaftDev on 13 July 2014 - 07:12 AM

HB late :c ...


#72334 The Daft's Draft

Posted by theDaftDev on 11 June 2014 - 07:52 PM

Well, here is the first stuff I thought someone could find interesting. 
 
The goal of this post is to create a sample game featuring a quick and easy shaders implementation for SFML. 
 
Shaders are quick and simple programs, written in GLSL language, running on your graphic card, that give you a high flexibility in order to manipulate your renders. Countless games are using them today,
while graphic cards and API tend to be based on shaders, leaving behind the old 'fixed pipelines'. There exists two kind of shaders, the fragment and the vertex shaders.
To make it simple, the first ones are used to manipulate the geometry, while fragment shaders work on pixels. 
 
As the point of the post is not to explain in details how a shader works, I won't go deeper into that. 
 
Here is a video of one of my projects, showing shaders at work, in a SFML based game:
 

 
Well, as said before, vertex shaders are being used to transform the geometry, so they are actually very useful and widespread in 3D programming. However, using SFML for a 2D game probably won't get you to use a lot of them.
That's why this implementation of the shaders is only featuring fragments shaders. 
You could easily make it work with vertex shaders though. 
 
Since its 2.0 release, SFML supports a new class, the RenderTexture, which is actually a RenderTarget allowing you to 'stack' your renders on a single texture that you can 'extract' when you're done drawing.
This class is the perfect way to apply fragment shaders on your whole image.
 
The first thing we want to do is to take a deep breathe (c'mon do it), and see what 'ingredients' we are going to need.
Most of the time, when they're not hard-coded, shaders are being loaded from your disk, so the best would be to make sure we won't be loading the same shader twice. 
We could store them into a manager, from the one we would get the shader through its path to the disk, and that would check if the shader has been loaded when it's requested, and load
it if needed, before returning it. Ideally, our manager would be globally accessible, which means we need it to be static.
 
sf::Shader inherits from sf::NonCopyable, and is passed to sf::RenderStates through a pointer, so we will save time dynamically creating it and storing it through a pointer in our manager . Remember our manager is static though,
we must not forget to delete our shader when the program is ending. Either we can add a "clear()" function to our manager, which will delete all the shaders, or we could just create a ShaderHolder class, that would
allocate the shader when we ask it to load one, and that would delete it when the destructor is called. We will add a basic game Entity, which will hold a sprite, and a World class, which will manager the entities, render it, and apply the shaders. 
The world class needs to be accessible from each entity, and needs to be unique, so we will make it a singleton. 
 
We now have to decide what sample shader we will be using in our program. 
Well, for this program, we will be using a small shader that will add a light behind our sprite, with a colour changing after a defined amount of time. 
 
 
uniform vec2 frag_LightOrigin;	// The light's position
uniform vec3 frag_LightColor; 	// Its colour
uniform float frag_LightAttenuation;	// We could call this its luminosity
uniform vec2 frag_ScreenResolution; // We need this because SFML reverses the Y axis. Note: we could just be using a single float for the Y resolution. 

void main()

{		

	vec2 baseDistance =  gl_FragCoord.xy; // Storing our pixel's position
	baseDistance.y = frag_ScreenResolution.y-baseDistance.y; // Fixing the reversed Y axis
	vec2 distance=frag_LightOrigin - baseDistance; // Getting the distance between our light and the pixel, as a 2D vector
	float linear_distance = length(distance); // Getting the linear distance through a normalization 
	float attenuation=1.0/( frag_LightAttenuation*linear_distance + frag_LightAttenuation*linear_distance);	// The closer our pixel is to the light source, the smaller the light attenuation will be. 
	vec4 lightColor = vec4(frag_LightColor, 1.0);
	vec4 color = vec4(attenuation, attenuation, attenuation, 1.0) * lightColor; // We multiply our light's colour with the attenuation vector, which will give us the final 'luminosity' of our pixel. 
	gl_FragColor=color; // We give our pixel its final colour. 


}
 
 
 
Also, here is the sample texture we're going to use here:
 
shaders_sample_texture.png
 

I think we are done thinking it, so let's code this now! 
 
------ World.hpp ------
#include <SFML/Graphics.hpp>


#define DEFAULT_WIDTH 800
#define DEFAULT_HEIGHT 600


class Entity;


class World
{


public:


static World& getInstance(); // We get the unique instance


void run(); // We run the game loop
void applyShader(sf::RenderStates); // We apply a shader passed through render states


private:


World();
~World();


void draw(); // The draw loop
void update(float frametime); // We update our entities


sf::RenderWindow myWindow;
sf::RenderTexture myRenderTexture;
sf::Sprite mySprite;


std::vector<Entity> myEntities; // Note: for a real game, a std::list is more relevant


};
 
 
------ World.cpp ------
 
#include "World.hpp"
#include "Entity.hpp"


World::World(){}


World::~World(){}


World& World::getInstance()
{


static World myself;
return myself;


}


void World::draw()
{


myWindow.clear(); // We clear the window
myRenderTexture.clear(); // We clear the render texture


for(int i(0); i<myEntities.size(); i++)
{
myEntities[i].draw(myRenderTexture); // We 'stack' our sprites on the texture, eventually shaders will be applied.
}


myRenderTexture.display(); // We're done drawing on the RenderTexture
myWindow.draw(mySprite); // We draw it on the RenderWindow
myWindow.display(); // We display the window


}


void World::update(float frametime)
{


for(int i(0); i<myEntities.size(); i++)
{
myEntities[i].update(frametime); // We update our entities, based on the frametime, to avoid different behaviours because of the framerate.
}


}


void World::run()
{


myWindow.create(sf::VideoMode(DEFAULT_WIDTH, DEFAULT_HEIGHT, 32), "SFML - Shaders", sf::Style::Close);
myRenderTexture.create(DEFAULT_WIDTH, DEFAULT_HEIGHT); // Don't forget to create the RenderTexture, like you would do for the window.
mySprite.setTexture(myRenderTexture.getTexture()); // We get the texture and link it to our sprite.


sf::Event event;
    sf::Clock clock;
    bool isRunning = true;


sf::Texture texture; // We make sure to load the same texture only once, so we don't set it as an Entity class attribute.
texture.loadFromFile("texture.png"); // And we load it here. 


    // We add entities, and place it some where on the screen.
    for(int i(0); i<3; i++)
    {
        myEntities.push_back(Entity());
myEntities.back().setTexture(texture);
        myEntities.back().setPosition( (i+1)*200,300 );
myEntities.back().setCurrentColor(i);
    }


    while(isRunning)
    {


while(myWindow.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
isRunning=false;
break;
}
}


        update(clock.restart().asSeconds());
draw();


}


}


void World::applyShader(sf::RenderStates states)
{
myRenderTexture.draw(mySprite, states); // We apply our shader on the current render
}
 
 
------ Entity.hpp ------
 
#include <SFML/Graphics.hpp>


class Entity
{


public:


Entity();
~Entity();


void draw(sf::RenderTarget& target) const;
void update(float frametime);


void setTexture( const sf::Texture& texture );
void setPosition(float x, float y);
void setCurrentColor( int color );


private:


sf::Sprite mySprite;


std::vector<sf::Vector3f> colors; // Array of 3 colors we will be using


int currentColor; // This stores the current id of our color
float colorTimeUpdate; // This variable is used to 'stack' the time, and see when we can change our color


};
 
 
------ Entity.cpp ------
 
#include "Entity.hpp"
#include "World.hpp"
#include "ShadersManager.hpp"


Entity::Entity()
{


// We store the colors we will be displaying


colors.push_back(sf::Vector3f(255,0,0)); // Red
colors.push_back(sf::Vector3f(0,255,0)); // Green
colors.push_back(sf::Vector3f(0,0,255)); // Blue


currentColor = 0;
colorTimeUpdate = 0;


/* 	Here we both "pre-load" our shader, so it doesn't need to slow our game when loading while the world is running,

		and set the resolution variable so we don't have to bother with it later */


	ShadersManager::getShader("shader.frag")->setParameter("frag_ScreenResolution",sf::Vector2f(DEFAULT_WIDTH, DEFAULT_HEIGHT));


}


Entity::~Entity(){}


void Entity::draw(sf::RenderTarget& target) const
{


target.draw(mySprite); // First, we draw our sprite


/*


So here are the parameters we need to set:


uniform vec2 frag_LightOrigin; // The light's position
uniform vec3 frag_LightColor;  // Its colour
uniform float frag_LightAttenuation; // We could call this its luminosity

Note: We already set the resolution.
*/


sf::Shader* shader = ShadersManager::getShader("shader.frag");
shader->setParameter("frag_LightOrigin", mySprite.getPosition());
shader->setParameter("frag_LightColor", colors[currentColor]);
shader->setParameter("frag_LightAttenuation", 10);


sf::RenderStates states; // We prepare our render states
states.shader = shader; // We give the render states our shader
states.blendMode = sf::BlendAdd; // We set the blendMode to Add so that our changes are overlaying the other shader, not erasing them.
World::getInstance().applyShader(states); // We apply the shader on the whole texture
}


void Entity::update(float frametime)
{


colorTimeUpdate+=frametime; // We update the time


if(colorTimeUpdate>=2) // We check if it is higher than 2 seconds
{
currentColor++; // If so, we swap for the next color


if(currentColor>colors.size()-1) // We check that currentColor doesn't reach out the last color
currentColor=0; // If it happens we restart the cycle


colorTimeUpdate=0; // and we reset the update variable for another 2 seconds cycle
}


mySprite.rotate(10*frametime); // We rotate our sprite cuz yolo.


}


void Entity::setTexture( const sf::Texture& texture)
{
mySprite.setTexture(texture);
mySprite.setOrigin(mySprite.getTextureRect().width/2, mySprite.getTextureRect().height/2); // We set the origin to the middle of the sprite
}


void Entity::setPosition(float x, float y)
{
mySprite.setPosition(x,y);
}


void Entity::setCurrentColor(int color)
{


if(color<colors.size() && color>0)
currentColor = color;


}

 
 
------ ShadersManager.hpp ------
 
#include <SFML/Graphics.hpp>
#include <map>


class ShadersManager
{


public:


static sf::Shader* getShader(const std::string& path);


private:


class ShaderHolder
{


public:


ShaderHolder();
~ShaderHolder();


sf::Shader* getShader() const; // We get a pointer on the shader
bool loadShaderFromFile(const std::string& path); // Loads the shader from the path, and returns true if succeed.


// Note: we could also add other functions to load shaders from different streams.


private:


sf::Shader* myShader;


};


typedef std::map<std::string, ShaderHolder> ShadersMap;
static ShadersMap myShaders;


};
 
 
------ ShadersManager.cpp ------
 
#include "ShadersManager.hpp"
#include <iostream>


ShadersManager::ShadersMap ShadersManager::myShaders; // We don't forget to init the static shaders map


sf::Shader* ShadersManager::getShader(const std::string& path)
{


ShadersMap::iterator it = myShaders.find(path);
    if(it != myShaders.end())
return it->second.getShader(); // We search for the shader, and if it exists, we return it


// The shader has not been loaded yet, we load & return it.
if(!myShaders[path].loadShaderFromFile(path))
std::cout << "An error occured while loading the following shader:" << path << std::endl; // Note: In a real video game, you want to handle this error.


return myShaders[path].getShader();


}


ShadersManager::ShaderHolder::ShaderHolder()
{ myShader = NULL; }


ShadersManager::ShaderHolder::~ShaderHolder()
{
if(myShader)
delete myShader; 
}


sf::Shader* ShadersManager::ShaderHolder::getShader() const
{ return myShader; };


bool ShadersManager::ShaderHolder::loadShaderFromFile(const std::string& path)
{ 
if(!myShader)
myShader=new sf::Shader;


return myShader->loadFromFile(path, sf::Shader::Fragment);
}

 
 
------ main.cpp ------
 
#include "World.hpp"


int main()
{


World::getInstance().run();
return 0;


}
 
 
Okey, we now have everything, let's compile this! 
 

 
There, our program works as expected :)
 
You now have all the keys in your hand to make your own implementation of the shaders!
I hope this was not too long and too boring to read. Have fun filling your game with plenty of cool effects. 
 
Download the sample program with its sources here !

Next time, we will see how to generate a realistic looking stars cluster, and I also thought about setting up a small tutorial
dealing with a basic game architecture. But I have still to work on my own project, so I can't tell you when this will be done. 
 
Thanks for reading!


#72218 Why So Serious

Posted by theDaftDev on 10 June 2014 - 05:01 AM

gUgkpTx.jpg




#72007 So this is a thing I now own...

Posted by theDaftDev on 07 June 2014 - 04:00 AM

At least I now won't have to get you one for your birthday.

Everytime I see your avatar when I'm scrolling down, I know I have to take a deep breathe. Thank you. 




#72006 Luigi Meme Makes News

Posted by theDaftDev on 07 June 2014 - 03:57 AM

This totally deserves the news. 




#71949 Redemption has come, my friends.

Posted by theDaftDev on 06 June 2014 - 01:15 PM

I don't even have a reply for that

 

that's just cool

 

Much serenity.




#71945 Redemption has come, my friends.

Posted by theDaftDev on 06 June 2014 - 01:06 PM

http://rustledjimmies.com/




#71768 Why So Serious

Posted by theDaftDev on 04 June 2014 - 12:21 PM

dma-172.jpg

gifs_02.gif

dma-272.jpg

the-dar-13.jpg

the-dar-24.jpg

^ I almost died on this one ^

the-dar-31.jpg

funny-animals-17.jpg

funny-animals-1.jpg




#71625 Why So Serious

Posted by theDaftDev on 03 June 2014 - 01:18 AM

Also, I'm not actually mad; just yell-y.  Blame JonTron.

We can still have a high five, you :wub:  . 

 

Internet+High+Five.+Join+the+high-fivers




#71580 Why So Serious

Posted by theDaftDev on 02 June 2014 - 05:20 PM

 

How am I still alive.. I CAN'T BREATHE.




#71464 Why So Serious

Posted by theDaftDev on 01 June 2014 - 02:51 PM

 

This got me laughing so hard that I seriously thought about making a whole thread out of it. 

EXCELLENT.