/// 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.

/// @file Render.h
/// This source file is part of CUDA Ray Casting test.
/// The file holds the Render class interface.
/// @author Ruben Penalva (ruben.penalva@rpenalva.com)
#ifndef _Render_H_
#define _Render_H_

#include <vector>
#include "boost/smart_ptr/shared_array.hpp"
#include "Vector3.h"
#include "Matrix4x4.h"

#include "PixelBuffer.h"
#include "driver_types.h"

namespace CUDARayCasting
{

/// @brief  Opengl Render tailored specifically for 
///	CUDARayCasting test.
/// It can render these 2d geometry:
///		- Points
///		- Lines
///		- Curves
/// It only supports a material being a configurable 
///	emissive color.
class Render
{
public:
	/// Constructor
	Render(int width, int height);

	/// Destructor
	~Render(void);

	/// @brief Loads a default opengl configuration
	/// @param width render window width
	/// @param height render window height
	/// @note It's very ugly to have a separete method
	/// for initialization and don't use the constructor.
	void Initialize(boost::shared_array<unsigned char> dataPtr, 
					int texWidth, int texHeight, int texDepth,
					boost::shared_array<float> transferFunctionPtr,
					int transferFunctionWidth, float step, int stepNumber,
					const Matrix4x4& viewMatrix, const Matrix4x4& volumeMatrix, 
					const Vector3& bboxMin, const Vector3& bboxMax);

	/// @brief Reshapes opengl viewport and initialize states
	/// @param width render window width
	/// @param height render window height
	void Reshape(int width, int height);

	/// @brief Begins render frame
	void BeginFrame(void);

	/// @brief Ends render frame
	void EndFrame(void);

	void DrawVolume(void);

	void UpdateTransferFunction(void);

	void SetStep(float step);

	void SetStepNumber(int stepNumber);

	void DrawString(const std::string& s, int x, int y, const Vector3& color);

	void SetCamera(const Matrix4x4& viewMatrix);

	void SetVolumeMatrix(const Matrix4x4& volumeMatrix);

private:
	int m_width;
	int m_height;

	Matrix4x4 m_viewMatrix;
	Matrix4x4 m_volumeMatrix;

	boost::shared_array<float> m_transferFunctionPtr;

	PixelBufferPtr m_pixelBuffer;

	Vector3 m_bboxMin;
	Vector3 m_bboxMax;

	int m_steps;
	float m_step;

	int m_transferFunctionWidth;

	/// @note hide copy constructor from interface user
	Render(const Render&);

	/// @note hide assignment operator from interface user
	Render& operator=(const Render&);
};

}

#endif
