#include "openglstate.h"
#include <GL/glew.h>
#include <iostream>

namespace M3D
{

	OpenGLState::OpenGLState() :
			_depth_test_ ( false ),
			_texture_2d_ ( false ),
			_blend_ ( false ),
			_program_id_ ( -1 ),
			_vao_id_ ( -1 )
	{
	}


	OpenGLState::~OpenGLState()
	{
	}

	void OpenGLState::enable_depth_test ()
	{
		if ( _depth_test_ ) return;
		_depth_test_ = true;
		glEnable ( GL_DEPTH_TEST );
	}
	void OpenGLState::disable_depth_test()
	{
		if ( !_depth_test_ ) return;
		_depth_test_ = false;
		glDisable ( GL_DEPTH_TEST );
	}

	void OpenGLState::enable_texture_2d ( )
	{
		if ( _texture_2d_ ) return;
		_texture_2d_ = true;
		for(GLuint tu = GL_TEXTURE0; tu<=GL_TEXTURE7; ++tu)
		{
			glActiveTexture( tu );
			glEnable ( GL_TEXTURE_2D );	
		}
		glActiveTexture(GL_TEXTURE0);
	}
	void OpenGLState::disable_texture_2d()
	{
		if ( !_texture_2d_ ) return;
		_texture_2d_ = false;
		for(GLuint tu = GL_TEXTURE0; tu<=GL_TEXTURE7; ++tu)
		{
			glActiveTexture( tu );
			glDisable ( GL_TEXTURE_2D );	
		}
		glActiveTexture(GL_TEXTURE0);

	}
	void OpenGLState::enable_blend()
	{
		if ( _blend_ ) return;
		_blend_ = true;
		glEnable ( GL_BLEND );
	}

	void OpenGLState::disable_blend()
	{
		if ( !_blend_ ) return;
		_blend_ = false;
		glDisable ( GL_BLEND );
	}
	void OpenGLState::fill()
	{
		_depth_test_ = glIsEnabled ( GL_DEPTH_TEST );
		_texture_2d_ = glIsEnabled ( GL_TEXTURE_2D );
		_blend_      = glIsEnabled ( GL_BLEND );
		_program_id_ = -1;
		_vao_id_ = -1;
		_2d_texture_id_ = -1;
	}
	void OpenGLState::use_program ( int program_id )
	{
		if ( program_id==_program_id_ ) return;
		_program_id_ = program_id;
		glUseProgram ( program_id );
	}
	void OpenGLState::bind_vertex_array ( int vao_id )
	{
		if ( _vao_id_ == vao_id ) return;
		_vao_id_ = vao_id;
		glBindVertexArray ( vao_id );
	}



}







