// glview.h
//

#ifndef GLVIEW_3D_IMAGE_INCLUDE
#define GLVIEW_3D_IMAGE_INCLUDE

#include <dibsect.h>
#include "matrix3d.h"

void ComputeNormalVector(double p1[3], double p2[3],
				  double p3[3], double n[3]);
void NormalVector(GLdouble p1[3], GLdouble p2[3],
				  GLdouble p3[3], GLdouble n[3]);

class GLView3D {
	void InitImage(void)
	{
		m_hGLContext = 0;
		m_IsInitialized = 0;
		m_red_bg = m_green_bg = m_blue_bg = 0.0;
	}
	void InitGeometry(void);
protected:
		// OpenGL specific
	BOOL SetWindowPixelFormat(HDC hDC);
	BOOL SetImagePixelFormat(HDC hDC);
	void MakeImageCurrent(HDC hDC)
	{
		wglMakeCurrent(hDC,m_hGLContext);
	}
	BOOL CreateImageGLContext(HDC hDC);
	int  CreateView(void);
	void InitializeOpenGL(void);

	void PushMatrixValues(void);
	void PopMatrixValues(void);

protected:
	  // OpenGL attributes
	HGLRC m_hGLContext;
	int m_IsInitialized;
	int m_GLPixelIndex;

	DIBSection m_dib;
	Matrix3D m_view_matrix;
	Matrix3D m_model_matrix;
	Matrix3D m_saved_matrix;

	double m_red_bg; // colors between 0 and 1
	double m_green_bg;
	double m_blue_bg;
public:
	GLView3D(void);
	virtual ~GLView3D(void);

	DIBSection * GetDIB(void){ return &m_dib;}

	void CreateImage(UINT32 width, UINT32 height);
	void SizeImage(UINT32 width, UINT32 height);

	virtual void RenderImage(void);
	virtual void RenderPrimaryImage(void);

	virtual void DrawImage(CWnd * wnd);

	int IsInitialized(void)const{ return m_IsInitialized;}

	void SetViewState(const Matrix3D& matrix);
	Matrix3D& GetViewState(void){ return m_view_matrix;}

	void SetModelState(const Matrix3D& matrix){ m_model_matrix = matrix;}
	Matrix3D& GetModelState(void){ return m_model_matrix;}

	void SetSavedState(const Matrix3D& matrix);
	Matrix3D& GetSavedState(void){ return m_saved_matrix;}

	void SetBackgroundColor(double r, double g, double b);
};

#endif