/*****************************************************************************

File: GeometryReader.h
Desc: A class for reading geometry
		
Date: 2005/01/01

Author: Wim Lemkens

*****************************************************************************/

#ifndef GEOMETRYREADER_H
#define GEOMETRYREADER_H

#include <OgreStringConverter.h>
#include <OgreVector3.h>
#include <OgreConfigFile.h>
#include "VertexBuffer.h"

namespace Ogre {
	
class GeometryReader{
public:

	GeometryReader();
	virtual ~GeometryReader();

	/**
	 * Sets the file the geometry is to be read of
	 */	
	virtual void setFile(String file);

	/**
	 * Return how many lods of geometry there are in the file
	 */
	virtual int getGeometryDepth() const;
	
	/**
	 * Returns the scale of the geometry
	 */
	virtual Vector3 getScale() const{
		return mScale;
	}
	/**
	 * Returns the offset the geometry in the world
	 */
	virtual Vector3 getOffset() const{
		return mOffset;
	}

	/**
	 * Return the size in vertices of the given level
	 * @param level	The level to return the size of
	 */
	inline long getLevelSize(int level) const
	{
		return (1<<level)+1;
	}

	/**
	 * Returns the clipRegion size
	 */
	virtual Real getClipRegion(){
		return clipRegion;
	}
	
	/**
	 * Returns the unscaled space between 2 vertices of the given level
	 */
	virtual Real getLevelQuadSize(int level) const;

	/**
	 * Return the name of the material
	 */
	virtual String getMaterial() const {
		return material;
	}
	
	
private:
protected:

	/**
	 * Return the position in the file where the height of the given vertex
	 * is stored
	 * @param x		The x-coordinate of the vertex to return
	 * @param y		The y-coordinate of the vertex to return
	 * @param level	The level of detail of the vertex to be returned
	 */
	virtual int getVertexPosition(int x,int y,int level) const;

	/**
	 * Return the position in the file where the given levels starts
	 * @param level	The level of detail from which the position is to be returned
	 */
	virtual int getLevelPosition(int level) const;

	/**
	 * Return the position in the file where the height of the given vertex
	 * is stored
	 * @param x		The x-coordinate of the vertex to return
	 * @param y		The y-coordinate of the vertex to return
	 * @param level	The level of detail of the vertex to be returned
	 */
	virtual int getVertexSize() const
	{
		return sizeof(Real);
	}
	/**
	 * How many lods of geometry there are in the file
	 */
	int mGeometryDepth;
	
	/**
	 * The file to be read from
	 */
	FILE* mapfile;
	
	/**
	 * The amount the geometry is to be scaled
	 */
	Vector3 mScale;
	 
	/**
	 * The offset in world the geometry is to be set
	 */
	Vector3 mOffset;
	
	VertexBuffer *vertexBuffer;

	/**
	 * Opens a file for reading an gives it the given handle
	 */
	virtual void openFile(String filename, FILE* &file);
	
	/**
	 * Creates a file for reading and writing an gives it the given handle
	 */
	virtual void openFileForWriting(String filename, FILE* &file);
	
	/**
	 * Returns the size of the header of the preprocessed file
	 */
	virtual int getHeaderSize() const
	{
		return sizeof(int);
	}
	/**
	 * The size of the clipregion
	 */
	Real clipRegion;
	
	/// The name of the material for the geometry
	String material;
	
	

};

} // namespace Ogre

#endif // GEOMETRYREADER_H
