/// Copyright (c) 2010 Ruben Penalva. All rights reserved.
/// Redistribution and use in source and binary forms, with or without modification, 
/// are permitted provided that the following conditions are met:
///    1. Redistributions of source code must retain the above copyright notice, this 
///    list of conditions and the following disclaimer.
///    2. Redistributions in binary form must reproduce the above copyright notice,
///    this list of conditions and the following disclaimer in the documentation and/or
///    other materials provided with the distribution.
///    3. The name of the author may not be used to endorse or promote products derived
///    from this software without specific prior written permission.
///
/// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
/// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
/// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 
/// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
/// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
/// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
/// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 
/// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef _OPENGL_TEXTURE_H_
#define _OPENGL_TEXTURE_H_

#include <boost\utility.hpp>
#include <GL\glew.h>
#include <boost\shared_array.hpp>
#include <boost\weak_ptr.hpp>

namespace TestFrameWork
{

typedef boost::shared_array<char> TextureData;

enum OpenglTextureFormat
{
	Red,
	RGB,
	RGB32F,
	RGBA,
	RGBA32F
};

enum OpenglTextureFilter
{
	Nearest,
	Linear
};

enum OpenglTextureEdgeValue
{
	Repeat,
	MirroredRepeat,
	ClampToEdge,
	ClampToBorder,
	Clamp
};

class OpenglTexture :
	boost::noncopyable
{
public:
	OpenglTexture(TextureData data, OpenglTextureFormat format, size_t width, 
				  OpenglTextureFilter filter, OpenglTextureEdgeValue edge);

	OpenglTexture(TextureData data, OpenglTextureFormat format, size_t width, size_t height, 
				  OpenglTextureFilter filter, OpenglTextureEdgeValue edge);

	OpenglTexture(TextureData data, OpenglTextureFormat format, size_t width, size_t height, size_t depth, 
				  OpenglTextureFilter filter, OpenglTextureEdgeValue edge);

	~OpenglTexture(void);

	void ModifyData(TextureData data, size_t width);

	void ModifyData(TextureData data, size_t width, size_t height);

	void ModifyData(TextureData data, size_t width, size_t height, size_t depth);

	GLuint GetOpenglId(void) const;

	GLenum GetTarget(void) const;

	static GLenum Format2Gl(OpenglTextureFormat format);

	static GLint Format2InternalFormat(OpenglTextureFormat format);

	static GLfloat Filter2Gl(OpenglTextureFilter filter);

	static GLint EdgeValue2Gl(OpenglTextureEdgeValue edge);

private:
	GLenum m_target;

	GLenum m_format;
	
	GLint m_internalFormat;

	size_t m_width;

	size_t m_height;

	size_t m_depth;

	GLuint m_id;

	void CreateTexture1D(TextureData data, GLfloat filter, GLint edge);

	void CreateTexture2D(TextureData data, GLfloat filter, GLint edge);

	void CreateTexture3D(TextureData data, GLfloat filter, GLint edge);

	void Configure(GLfloat filter, GLint edge) const;
};

typedef boost::shared_ptr<OpenglTexture> OpenglTexturePtr;
typedef boost::weak_ptr<OpenglTexture> OpenglTextureWeakPtr;

}

#endif