#ifndef SCENEPICKER_H
#define SCENEPICKER_H

#include "scene/scenenode.h"

#include <bullet/BulletCollision/CollisionDispatch/btCollisionWorld.h>

class ScenePicker : public SceneNode
{
    static Mesh* generateMesh();

    glm::vec3 m_pickedPos;
    SceneNode* m_pickedNode;
    bool m_pickSucceeded;

public:
    ScenePicker();

    virtual ~ScenePicker();

    virtual void update();

    void pick();

    glm::vec3 getIntersection() { return m_pickedPos; }
    bool getTraceSucceeded() { m_pickSucceeded; }

    /**
     * @brief pickFromCamera performs picking in the physic world
     * @param pickPos is the coordinates to pick on the screen (in pixels)
     */
    btCollisionWorld::ClosestRayResultCallback pickFromCamera(glm::ivec2 pickPos);

    /**
     * @brief pickFromCamera performs picking in the physic world
     * this will trace a ray from the center of the screen in the direction of the view
     */
    btCollisionWorld::ClosestRayResultCallback pickFromCamera();

    /**
     * @brief pickPhysicsFromCamera performs picking in the physic world
     * this will trace a ray from the center of the screen in the direction of the view
     */
    btCollisionWorld::ClosestRayResultCallback pick(glm::vec3 start, glm::vec3 end);

    /**
     * @brief pickPixel picks a pixel directly in the last frame's positions texture
     * @warning accessing the GPU's buffers is a heavy operation, do not overuse it
     *
     * @param pixelPos the position of the picked pixel
     * @return the coordinates of the pixel
     */
    glm::vec3 pickPixel(glm::ivec2 pixelPos);
};

#endif // SCENEPICKER_H
