Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [ODFAEG] (Open Source Development Framework Adapted for Every Game)  (Read 155775 times)

0 Members and 2 Guests are viewing this topic.

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: [ODFAEG] (Open Source Development Framework Adapted for Every Game)
« Reply #90 on: February 26, 2014, 11:34:31 am »
Also, don't forget, if you fail to make it work for _every_ type of game, your acronym becomes ODFAG and no one wants to make games with an odd fag ^^ (no offense intended)

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: [ODFAEG] (Open Source Development Framework Adapted for Every Game)
« Reply #91 on: February 26, 2014, 12:06:15 pm »
Ok if you're not convinced that we can create a common framework for every kind of games and if you thing it'll fail, it's your problem, but it's sure it'll take a long time and I don't force another ones to use another framework for 3D games or 2D games if they are not convinced but the'll have to re-encode everything if they need to create a new game.

The actual gaming industry is so varied now, that we cannot survive which only one kind of game.
Moreover it becomes very difficult to find a king of game which is unique.
Each studio now have at least 2 different game to offer to they clients.
If I remember the discussion about the Sorrok project it takes severals mounths to find out an unique gameplay.
So I think that it's no more the use of the library and the kind of game which can make the difference in the market actually.

What is different in each kind of game results on only three things : (If I resume everything)

-The gameplay.
-The graphisms.
-The sounds.

So I just want to have to change only this think if I want to create a new game, and not to have to reimplement all the networking and physic's if I pass from a 2D to a 3D game by example. (And to readapt the code because the libraries are not the same, it's really a wast of time to do that)

So I need more flexibility as you said just above. :)





« Last Edit: February 26, 2014, 12:11:28 pm by Lolilolight »

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: [ODFAEG] (Open Source Development Framework Adapted for Every Game)
« Reply #92 on: February 26, 2014, 03:53:44 pm »
Re, in the next version (I'll include the 3D, and I've already begin to do that and modifying SFML to do that)

I'll try to rewrite the depth with a fragment shader to also do a test with the alpha component of each  pixels, and try to do only one draw for all entities using the same texture without having artefact effects with textures which are semi-transparent.
Because having so much draws to draw the ground I don't thing it's really good for the performances.

So I'll use the odfaeg::RenderTarget and odfaeg::RenderWindow in the 3D module classes to optimize the render time I think rather than using the sfml classes of the sfml graphic module.

I've found in interessant technique at this link :
http://www.gamedev.net/topic/546977-how-to-perform-depth-test-in-pixel-shader/

But I don't know if it's possible, I've never tested.








Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: [ODFAEG] (Open Source Development Framework Adapted for Every Game)
« Reply #93 on: February 26, 2014, 04:04:53 pm »
Wait... I thought your framework was... a framework, meaning that it would work with our default SFML installation ...? If you do need to alter SFML, does it mean we have to have this altered version ? If yes, sorry but nobody will do that.

MadMartin

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: [ODFAEG] (Open Source Development Framework Adapted for Every Game)
« Reply #94 on: February 26, 2014, 04:33:09 pm »
For someone's own learning purposes (learning by doing) it could be a good idea to write such a framework. But from a possible user's point of view I only can say that this project grows more and more into YAGNI.
I'm sorry, Lolilolight, but with your current development philosophy I guess there won't be many persons interested in your framework.

Asides, I wish you good luck and motivation to continue  ;)

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [ODFAEG] (Open Source Development Framework Adapted for Every Game)
« Reply #95 on: February 26, 2014, 04:56:09 pm »
I'm sceptical.
I don't think it's possible to create a framework that will work equally well to implement tic-tac-toe, pacman, a MUD, World of Warcraft, Kings Quest, Myst, Quake and more. But please, do surprise us.
At the very least - if you want to gain users - you should write example games of extremely different genres using your framework, to show that it is possible.

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: [ODFAEG] (Open Source Development Framework Adapted for Every Game)
« Reply #96 on: February 26, 2014, 07:38:34 pm »
Quote
I'm sceptical.
I don't think it's possible to create a framework that will work equally well to implement tic-tac-toe, pacman, a MUD, World of Warcraft, Kings Quest, Myst, Quake and more. But please, do surprise us.
At the very least - if you want to gain users - you should write example games of extremely different genres using your framework, to show that it is possible.

Ok, I'll do that I've just to finish the 3D part for the games like quake for example. ^^

And for those who thinks that this framework'll not interess everyone, I'll just answer this : I don't care if this framework allow me to create games of different extremely different genres.




Deathbeam

  • Jr. Member
  • **
  • Posts: 82
  • VB6, VB.NET, C#, HTML, PHP, CSS, JavaScript nerd.
    • View Profile
    • My portfolio
    • Email
Re: [ODFAEG] (Open Source Development Framework Adapted for Every Game)
« Reply #97 on: March 01, 2014, 01:26:14 pm »
Don't say that I'm doing another framework like my competitors again, please, when you don't know what I'm doing, thor don't use a 2D isometric perspective!!! (And the SFM-game book too)
So It's not that I don't want to listen to the advices of the other which have made a good framework. (I don't want to blame them)
But one library use a particule system, a resource holding system and a good action system (the thor library) but not 2D isometric perspective and an entity manager, another library use only GUIs (SfGui), another only the network (sfNul), others one only entity managers and other only the 3D without the SFML phylosophy and code design. (Irrlicht, Ogre, etc...)
I want to regroup all this libraries into a framework, that's why I've created ODFAEG, to simplify the things and to make it all faster.

And I don't thing there is actually a framework which do all of that with SFML.
For the moment you are just able to create small 2D or 2D isometric games, that's right.
But as I say, 3D games and multiplayers games'll come in the next versions, so, plz be patient.

I want to make a framework adapted for every game as you said, it's something unique because I don't know a framework which allow us to do that. (2D, 2D isometric and 3D game with the same framework)

You are always forced to use a different framework for each kind of game and sometimes several frameworks, and their simplicity and code design doesn't always follow the phylosophy of SFML)

So don't worry about that, later, you'll be able to create 3D FPS and big 3D mmorpgs like in this example :

http://linor.fr/tutoriaux/sommaire-3-creation-un-jeu-video.php

I've just to implement the code of all my projects into the framework but before to do that I want to have a stable version with only one kind of game. :)

But my priority for the moment is my Sorrok project, and even without an example for another kind of game you should be able to do another kind of 2D game (and  3D game in the next framework version) with the framework. :)

The use of the framework isn't so complex, I'll post a pdf on the git-hub. :)


(y) If I will know atleast a bit of C++, I can safely say that I would choose this your framework for my game. I like what you are doing, and who cares if it is a bit more complex. When anyone wanna make game, they should not be lazy ****** to avoid some complexity when it will work.
Spooker Framework - Open source gaming library
My portfolio
Indie Armory - Small community of a game developers. Everyone is welcome. Bring your friends, family, pets...

therocode

  • Full Member
  • ***
  • Posts: 125
    • View Profile
    • Development blog
Re: [ODFAEG] (Open Source Development Framework Adapted for Every Game)
« Reply #98 on: March 01, 2014, 02:29:36 pm »
(y) If I will know atleast a bit of C++, I can safely say that I would choose this your framework for my game. I like what you are doing, and who cares if it is a bit more complex. When anyone wanna make game, they should not be lazy ****** to avoid some complexity when it will work.

There is a difference between good complexity and bad complexity. Also, what the complexity in ODFAEG makes to "work" would work as well with much less complexity with no drawback in practice. When using external libraries and even working with your own code, avoiding complexity saves so much time and headaches.

Whenever I look for a library to use, the complexity of the API is a key thing that I look at to pick the one that is easiest to work with (while still working etc of course!)

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: [ODFAEG] (Open Source Development Framework Adapted for Every Game)
« Reply #99 on: March 01, 2014, 02:41:33 pm »
The framework is not so complicated to use, I've put a tutorial in the tutorial folder. (on the git-hub)

(Only the last chapter is remaining but I'll write a little 2D isometic game and then Write a 3D fps for the next version.)


Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: [ODFAEG] (Open Source Development Framework Adapted for Every Game)
« Reply #100 on: March 02, 2014, 01:00:33 pm »
Hi!

New functionnality : an alpha test and a depthtest with shaders!

I was trying hard to find a solution to get rid of the depth sorting.

And I thing that I've found one, but, it needs the support of shaders and fbo.

So I've kept the two possibilities into the framework :

The first possibility is simply by doing a z-sorting like I do yet in the map class. (It's less optimized because you have to do more calls to the draw methods but compatible with the old PCs which does'nt support shaders)

The first possibility is to choise if your PC does'nt support shaders and fbo so.

The second possibility is to use a shader to store the depth value and the colors of the pixels of the screen into rendertextures like this : (The class with do that is called TileMap and we just have to pass to the tilemaps objects, a pointer to the current view, and optionnally a zorder (tilemaps can also be drawn with a special order))
A tilemap is flat so there is no depth value. (It's just represent the current framebuffer and depthbuffer values)


#ifndef TILEMAP
#define TILEMAP
#include "renderTexture.h"
#include "tile.h"
namespace odfaeg {
    namespace g3d {
class TileMap : public Drawable, public Transformable
{
public:
    TileMap (View* view, int zOrder = 0) :
        Transformable (Vec3f (view->getPosition().x, view->getPosition().y, zOrder), Vec3f(view->getSize().x, view->getSize().y, 0), Vec3f(view->getSize().x * 0.5f, view->getSize().y * 0.5f, 0)) {
        this->zOrder = zOrder;
        depthBuffer.create(view->getSize().x, view->getSize().y, true);
        frameBuffer.create(view->getSize().x, view->getSize().y, true);
        depthBufferGenerator->loadFromFile("Shaders/vertex_shader.vert", "Shaders/depthTextGen.frag");
        frameBufferGenerator->loadFromFile("Shaders/vertex_shader.vert", "Shaders/fragment_shader.frag");
        depthBuffer.setView(view);
        frameBuffer.setView(view);
    }
   
    static void clearBufferBits(sf::Color color) {
        frameBuffer.clear(color);
    }
    static void clearDepthBits() {
        depthBuffer.clear(sf::Color(0, 0, 0, 0));
    }

    bool load(const std::string& tileset, sf::Vector2u tileSize, std::vector<Tile*> tiles, unsigned int width, unsigned int height)
    {
        // load the tileset texture
        if (!m_tileset.loadFromFile(tileset))
            return false;

        // resize the vertex array to fit the level size
        m_vertices.setPrimitiveType(sf::Quads);
        m_vertices.resize(width * height * 4);

        // populate the vertex array, with one quad per tile
        for (unsigned int i = 0; i < width; ++i)
            for (unsigned int j = 0; j < height; ++j)
            {
                // get the current tile number
                int tileNumber = i + j * width;
                // get a pointer to the current tile's quad
                Vertex* quad = &m_vertices[(i + j * width) * 4];
                Vec2f position (tiles[i]->getPosition().x, tiles[i]->getPosition().y);
                float zOrder = tiles[i]->getPosition().z;
                Vec2f size = Vec2f(tiles[i]->getSize().x, tiles[i]->getSize().y);
                sf::IntRect subRect = tiles[i]->getTextureRect();
                // define its 4 corners
                quad[0].position = sf::Vector3f(position.x, position.y, zOrder);
                quad[1].position = sf::Vector3f(position.x + size.x, position.y, zOrder);
                quad[2].position = sf::Vector3f(position.x + size.x, position.y + size.y, zOrder);
                quad[3].position = sf::Vector3f(position.x, position.x + size.y, zOrder);

                // define its 4 texture coordinates
                quad[0].texCoords = sf::Vector2f(subRect.left, subRect.top);
                quad[1].texCoords = sf::Vector2f(subRect.left + subRect.width, subRect.top);
                quad[2].texCoords = sf::Vector2f(subRect.left + subRect.width, subRect.top + subRect.height);
                quad[3].texCoords = sf::Vector2f(subRect.left, subRect.top + subRect.height);
            }

        return true;
    }
private:
    virtual void draw(RenderTarget& target, RenderStates states) const
    {
        // apply the transform
        states.transform = getTransform();

        // apply the tileset texture
        states.texture = &m_tileset;
        states.shader = frameBufferGenerator;
        //Update the depth buffer and the frame buffer of the current frame.
        frameBufferGenerator->setParameter("depthBuffer", depthBuffer.getTexture());
        frameBufferGenerator->setParameter("frameBuffer", frameBuffer.getTexture());
        frameBufferGenerator->setParameter("texture", sf::Shader::CurrentTexture);
        frameBuffer.draw(m_vertices, states);
        sf::IntRect subRect(frameBuffer.getView()->getPosition().x, frameBuffer.getView()->getPosition().y, frameBuffer.getView()->getSize().x, frameBuffer.getView()->getSize().y);
        const_cast<TileMap*>(this)->tile = new Tile (&frameBuffer.getTexture(), frameBuffer.getView()->getPosition(), frameBuffer.getView()->getSize(), subRect, zOrder);
        states.shader = nullptr;
        target.draw(*tile, states);
        //Update the depth buffer.
        depthBufferGenerator->setParameter("depthTexture", depthBuffer.getTexture());
        states.shader = depthBufferGenerator;
        depthBuffer.draw(m_vertices, states);
    }
    VertexArray m_vertices;
    sf::Texture m_tileset;
    Tile *tile;
    static RenderTexture depthBuffer;
    static RenderTexture frameBuffer;
    static sf::Shader *depthBufferGenerator;
    static sf::Shader *frameBufferGenerator;
    int zOrder;
};
}
}
#endif // TILEMAP
 

The alpha tests and depthtests are performed into the shaders and you can re-program them, very usefull because the depth test of the default opengl pipeline is not programmable.

The vertex shader is unchanged :

/*Vertex shader utilisé pour généré la depth texture.*/
void main () {
    //Transforme la position.
        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
        // transforme les coordonnées de texture
    gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
        //Passage de la couleur au fragment shader.
        gl_FrontColor = gl_color;
}
 

The interressant part is in the fragments shaders : the first one update the depthbuffer, the second one do the depth and the alpha test and affect the final color of the pixel if the pixel isn't hidden by another object.

//The depth texture.
uniform sampler2D depthTexture;
/*Génère une texture de profondeur, plus le pixel est noir, plus il se situe en fond d'écran.*/
void main () { 
    // récupère le pixel dans la depth texture       
        vec4 pixel = texture2D(depthTexture, gl_TexCoord[0].xy);
        /* Si la profondeur du pixel de ce qu'on veut dessiner est plus grande que celle de la depth texture,
           alors on remet à jour notre depth texture, sinon, on laisse comme ça.*/

        float depth = 1 - gl_FragCoord.z;
        if (depth >= pixel.r)
                gl_FragColor = vec4(depth, depth, depth, depth);
        /*Voilà nous avons maintenant une rendertexture contenant la profondeur de tout les
        pixels qui ont été dessinés (dans les composantes rgba de la rendertexture)
        nous avons donc créer un depthbuffer dans une texture, c'est note depthTexture!*/

}
 
//The current depth buffer texture.
uniform sampler2D depthBuffer;
//The current frame buffer texture.
uniform sampler2D frameBuffer;
//The current texture.
uniform sampler2D texture;

void main () { 
    // récupère le pixel dans la depth buffer texture.        
        vec4 depth = texture2D(depthBuffer, gl_TexCoord[0].xy);
        // récupère le pixel dans la frame buffer texture.
        vec4 color = texture2D(frameBuffer, gl_TexCoord[0].xy);
        vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);
        vec4 finalColor = gl_color * pixel;
        if (color.a < finalColor.a) {
                gl_FragColor = finalColor;
        } else if (gl_FragCoord.z >= depth.z) {
                gl_FragColor = finalColor;
        }                      
}
 

So you'll just have to test if your pc support shader and fbo, if it's the case you can put all your tiles in the tilemap and draw it, otherwise you should draw all the tiles one by one.

PS : I haven't tested this technique yet but it should works, I simply hope that the renderTexture of SFML'll support it.
« Last Edit: March 02, 2014, 01:02:24 pm by Lolilolight »

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Re: [ODFAEG] (Open Source Development Framework Adapted for Every Game)
« Reply #101 on: March 03, 2014, 07:33:19 pm »
Be careful about the include guards. I would prepend ODFAEG in all the include guards (ODFAEG_TILEMAP for example).

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
[ODFAEG] (Open Source Development Framework Adapted for Every Game)
« Reply #102 on: March 04, 2014, 01:46:36 pm »
Why are you still using raw pointers rather than utilizing RAII? There's no argument really against using it, and it will make the library a whole lot more attractive to users.
Current Projects:
Technoport

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: [ODFAEG] (Open Source Development Framework Adapted for Every Game)
« Reply #103 on: March 04, 2014, 01:48:56 pm »
Why are you still using raw pointers rather than utilizing RAII? There's no argument really against using it, and it will make the library a whole lot more attractive to users.
Ahh don't start it again! :D
Instead go back a few pages, he somewhere mentioned his "reasons".
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: [ODFAEG] (Open Source Development Framework Adapted for Every Game)
« Reply #104 on: March 05, 2014, 11:18:20 am »
Hi, I've finished to implement my own depthtest, alpha test and blending  in a fragment shader and the result is very much better.

I thing I'll add the possibility to use the 3D even to draw 2D stuff. (It'll avoid me to do a z sorting)

It draws each semi-transparent textures without any problem.

I had just to correct the source code in my shader :

#ifndef TILEMAP
#define TILEMAP
#include "renderTexture.h"
#include "tile.h"
#include "shader.h"
namespace odfaeg {
    namespace g3d {
class TileMap : public Drawable, public Transformable
{
public:
    TileMap (View* view, int zOrder = 0) :
        Transformable (Vec3f (view->getPosition().x, view->getPosition().y, zOrder), Vec3f(view->getSize().x, view->getSize().y, 0), Vec3f(view->getSize().x * 0.5f, view->getSize().y * 0.5f, 0)) {
        this->zOrder = zOrder;
        this->view = view;
    }
    static void genBuffers(int screenWidth, int screenHeight) {
        resolution = Vec2f(screenWidth, screenHeight);
        depthBuffer.create(resolution.x, resolution.y);
        frameBuffer.create(resolution.x, resolution.y);
        const std::string vertexShader = \
        "void main () {" \
            "gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;" \
            "gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;" \
            "gl_FrontColor = gl_Color;" \
        "}";
        const std::string  depthGenFragShader = \
        "uniform sampler2D depthBuffer;" \
        "uniform vec2 resolution;" \
        "void main () {" \
              "vec2 position = ( gl_FragCoord.xy / resolution.xy );" \
              "vec4 pixel = texture2D(depthBuffer, position);" \
              "if (gl_FragCoord.z > pixel.z) {" \
                "gl_FragColor = vec4(0, 0,  gl_FragCoord.z, 1);" \
              "} else {" \
                 "gl_FragColor = gl_Color * pixel;" \
              "}" \
        "}";
        const std::string frameBufferGenFragShader = \
        "uniform sampler2D depthBuffer;" \
        "uniform sampler2D frameBuffer;" \
        "uniform sampler2D texture;" \
        "uniform vec2 resolution;" \
        "void main () { " \
            "vec2 position = ( gl_FragCoord.xy / resolution.xy );" \
            "vec4 depth = texture2D(depthBuffer, position);" \
            "vec4 color = texture2D(frameBuffer, position);" \
            "vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);" \
            "vec4 finalColor = pixel;" \
            "if (gl_FragCoord.z >= depth.z) {" \
                "float delta = color.a - finalColor.a;" \
                "gl_FragColor = color + vec4(finalColor.r * delta, finalColor.g * delta, finalColor.b * delta, delta);" \
                "gl_FragColor = finalColor;" \
            "} else if (color.a < finalColor.a) {" \
                "float delta = finalColor.a - color.a;" \
                "gl_FragColor = color + vec4(finalColor.r * delta, finalColor.g * delta, finalColor.b * delta, delta);" \
            "}" \
            "}";
        if (!depthBufferGenerator->loadFromMemory(vertexShader, depthGenFragShader))
            throw Erreur(50, "Failed to load depth buffer generator shader", 0);
        if (!frameBufferGenerator->loadFromMemory(vertexShader, frameBufferGenFragShader))
            throw Erreur(51, "Failed to load frame buffer generator shader", 0);
         depthBufferGenerator->setParameter("resolution",resolution.x, resolution.y);
         frameBufferGenerator->setParameter("resolution",resolution.x, resolution.y);
    }
    static void clearBufferBits(sf::Color color) {
        frameBuffer.clear(sf::Color(color.r, color.g, color.b, 0));
    }
    static void clearDepthBits() {
        depthBuffer.clear(sf::Color(0, 0, 0, 255));
    }

    bool load(const Texture& tileset, std::vector<Tile*> tiles)
    {
        // load the tileset texture
        m_tileset = tileset;

        // resize the vertex array to fit the level size
        m_vertices.setPrimitiveType(sf::Quads);
        m_vertices.resize(tiles.size() * 4);

        // populate the vertex array, with one quad per tile
        for (unsigned int i = 0; i < tiles.size(); i++) {
                // get a pointer to the current tile's quad
                Vertex* quad = &m_vertices[i * 4];
                Vec2f position (tiles[i]->getPosition().x, tiles[i]->getPosition().y);
                float zOrder = tiles[i]->getPosition().z;
                Vec2f size = Vec2f(tiles[i]->getSize().x, tiles[i]->getSize().y);
                sf::IntRect subRect = tiles[i]->getTextureRect();
                // define its 4 corners
                quad[i].position = sf::Vector3f(position.x, position.y, zOrder);
                quad[i+1].position = sf::Vector3f(position.x + size.x, position.y, zOrder);
                quad[i+2].position = sf::Vector3f(position.x + size.x, position.y + size.y, zOrder);
                quad[i+3].position = sf::Vector3f(position.x, position.y + size.y, zOrder);

                // define its 4 texture coordinates
                quad[i].texCoords = sf::Vector2f(subRect.left, subRect.top);
                quad[i+1].texCoords = sf::Vector2f(subRect.left + subRect.width, subRect.top);
                quad[i+2].texCoords = sf::Vector2f(subRect.left + subRect.width, subRect.top + subRect.height);
                quad[i+3].texCoords = sf::Vector2f(subRect.left, subRect.top + subRect.height);
            }

        return true;
    }
    static Tile& getFrameBufferTile () {
        sf::IntRect subRect(0, 0, frameBuffer.getView()->getSize().x, frameBuffer.getView()->getSize().y);
        Tile* tile = new Tile (&frameBuffer.getTexture(), frameBuffer.getView()->getPosition(), frameBuffer.getView()->getSize(), subRect, 0);
        //tile->setCenter(Vec3f(frameBuffer.getView()->getPosition().x, frameBuffer.getView()->getPosition().y, 0));
        return *tile;
    }
    static void update () {
        sf::IntRect subRect(0, 0, depthBuffer.getView()->getSize().x, depthBuffer.getView()->getSize().y);
        Tile tile (&depthBuffer.getTexture(), depthBuffer.getView()->getPosition(), depthBuffer.getView()->getSize(), subRect, 0);
    }
    static Tile& getDepthBufferTile() {
        sf::IntRect subRect(0, 0, depthBuffer.getView()->getSize().x, depthBuffer.getView()->getSize().y);
        Tile *tile = new Tile (&depthBuffer.getTexture(), depthBuffer.getView()->getPosition(), depthBuffer.getView()->getSize(), subRect, 0);
        return *tile;
    }
private:
    virtual void draw(RenderTarget& target, RenderStates states) const
    {
        // apply the transform
        states.transform = getTransform();
        states.texture = &m_tileset;
        states.shader = frameBufferGenerator;
        //Update the depth buffer and the frame buffer of the current frame.
        frameBufferGenerator->setParameter("depthBuffer", depthBuffer.getTexture());
        frameBufferGenerator->setParameter("frameBuffer", frameBuffer.getTexture());
        frameBufferGenerator->setParameter("texture", Shader::CurrentTexture);
        frameBuffer.setView(view);
        frameBuffer.draw(m_vertices, states);
        frameBuffer.display();
        sf::IntRect subRect(0, 0, view->getSize().x, view->getSize().y);


        //Update the depth buffer.
        depthBuffer.setView(view);
        depthBufferGenerator->setParameter("depthBuffer", depthBuffer.getTexture());
        states.shader = depthBufferGenerator;
        depthBuffer.draw(m_vertices, states);
        depthBuffer.display();
        states.shader = nullptr;
        Tile& tile = getFrameBufferTile();
        //tile.setCenter(Vec3f(view->getPosition().x, view->getPosition().y, 0));
        target.draw(tile, states);

    }
    VertexArray m_vertices;
    Texture m_tileset;
    static RenderTexture depthBuffer;
    static RenderTexture frameBuffer;
    static Shader *depthBufferGenerator;
    static Shader *frameBufferGenerator;
    int zOrder;
    static Vec2f resolution;
    View* view;
};
}
}
#endif // TILEMAP
 

So I've a depthTexture (the blue component is the z value of my pixels) and a frameBufferTexture which contains all pixels which are previously drawn on the screen.

So, when I want to draw the textures I just have to do that :

#include "ODFAEG/Graphics/3D/renderWindow.h"
#include "ODFAEG/Graphics/3D/tile.h"
#include "ODFAEG/Graphics/3D/tileMap.h"
#include "ODFAEG/Graphics/3D/cube.h"
#include "ODFAEG/Math/projMatrix.h"
using namespace odfaeg;
using namespace odfaeg::g3d;

int main() {

    g3d::RenderWindow window(sf::VideoMode(800, 600), "TestODFAEG", sf::Style::Default, sf::ContextSettings(32));
    View view (800, 600, 10, Vec3f(0, -1, 0));
    window.setView(&view);
    view.scale(-1, 1, 1);
    TileMap::genBuffers(view.getSize().x, view.getSize().y);
    Texture text;

    std::vector<Tile*> tiles1, tiles2;

    text.loadFromFile("tilesets/herbe.png");
    TileMap tm1(&view), tm2(&view);



    Tile tile1 (&text, Vec2f(0, 0), Vec2f(120, 60), sf::IntRect(0, 0, 100, 50),0);
    tiles1.push_back(&tile1);
    tm1.load(text, tiles1);
    Texture text2;
    text2.loadFromFile("tilesets/dalles.png");



     //std::cout<<text2.m_cacheId<<std::endl;
     //std::cout<<text2->m_cacheId<<std::endl;
    Tile tile2 (&text2, Vec2f(50, 25), Vec2f(120, 60), sf::IntRect(0, 0, 100, 50),-1);
    tiles2.push_back(&tile2);
    tm2.load(text2, tiles2);
    BoundingRectangle rect (0, 0, 1000, 1000);
    //tiles = generate_floor(tiles,rect);
    Cube cube(Vec3f(-1, 1, 1), 2, 2, 2, sf::Color(255, 0, 0));

    while(window.isOpen()) {
        //}

        sf::Event event;
        while(window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear(sf::Color(0, 0, 0));
        TileMap::clearBufferBits(sf::Color(0, 0, 0));
        TileMap::clearDepthBits();
        window.draw(tm1);
        window.draw(tm2);
        TileMap::update();
        window.display();

    }

    return 0;
}
 

The view : the two first parameters are the view's width and height, the thirst is the max Z value, and the last the direction of the head of the view.

So -1 as y gives me a view which point at the bottom.

I do a scale(-1, 1, 1) on the view them because otherwise the x coordinates are inverted.

Then I gen the buffers of the TileMap. (And I pass it the resolution)

After I create the tileMaps and I pass them the current view and the Tiles.
Finally I clear the depth and framebuffer textures before drawing the tilemaps at render Time.
I've to call the TileMap::Update() function otherwise the first tilemap doesn't display. (I don't know why)

PS : this is -1 and not 1 for the z value of the second tile because the zAxis point to us with opengl.






« Last Edit: March 05, 2014, 11:24:11 am by Lolilolight »