
#ifndef __WFX_SCENE_OBJLOADER_H__
#define __WFX_SCENE_OBJLOADER_H__

#include <WFX/WFXIncludes.h>
#include <WFX/io/File.h>
#include <WFX/scene/Node.h>

namespace WFX {

    namespace scene {

        /**
         * internal class representing Face info
         */
        class OBJFace {
            public:
                /** VertexCount */
                UInt VertexCount;
                /** v */
                std::vector<UInt>   Verts;
                /** vt */
                std::vector<UInt>   TexCoords;
                /** vn */
                std::vector<UInt>   Normals;
                
                /** Ctor */
                OBJFace(const std::string& l);

                /** print some debug info */
                void printDebug();
            protected:
            private:
        };
        
        /**
         * Simple Loader for the Wavefront .obj format
         * see http://www.dcs.ed.ac.uk/home/mxr/gfx/3d/OBJ.spec
         */
        class OBJLoader {
            public:
                /** Ctor */
                OBJLoader();
                ~OBJLoader();
                
                /**
                 * static method that wraps loadFromFile
                 */
                static Node* load(const WFX::io::File& f);


                /**
                 * tries to parse the given X3D File and returns a
                 * Node*
                 */
                Node* loadFromFile(const WFX::io::File& f);

            protected:
            
            private:
                /** input stream */
                std::istream* Input;
                /** output node */
                Node* Output;
                
                /** Current Object Index */
                UInt CurrentObject;

                /** timekeeping */
                ULong StartTicks;
                /** current Line */
                UInt CurrentLine;
                
               
                /** Objects */
                std::vector<std::string> Objects;
                /** Vertices */
                std::vector<WFX::math::Vector3> Vertices;
                /** Normals */
                std::vector<WFX::math::Vector3> Normals;
                /** TexCoords */
                std::vector<WFX::math::Vector2> TexCoords;
                /** faces */
                std::map<UInt, std::vector<OBJFace> > Faces;
                

                /** starts parsing */
                void parse();
                /** parses an object Line */
                void parseObject(const std::string& l);
                /** parses a face Line */
                void parseFace(const std::string& l);
                /** finalizes and does some cleaning up */
                void finalize();
                /** parses a vector3 line */
                WFX::math::Vector3 parseVector3(const std::string& l);
                /** parses a vector2 line */
                WFX::math::Vector2 parseVector2(const std::string& l);

                /** get next line from Input */
                std::string getLine();
                /** strips the type from the line */
                std::string stripType(const std::string& l);

        };

    }

}


#endif //include guard


