#include "Player.h"
#include "FramePipeline.h"

#include <gl/GL.h>
#include <gl/GLU.h>

#include "OpenCVGLCanvas.h"

BEGIN_EVENT_TABLE(OpenCVGLCanvas,wxGLCanvas)
	EVT_PAINT(OpenCVGLCanvas::OnPaint)
	EVT_CHAR(OpenCVGLCanvas::OnKeyPress)
END_EVENT_TABLE()

OpenCVGLCanvas::OpenCVGLCanvas(wxWindow * parent, wxWindowID winid, const wxPoint& pos)
: wxGLCanvas(parent,winid,pos)
{
	pipeline.reset(new SimpleFramePipeline());
	init = true;
}
OpenCVGLCanvas::~OpenCVGLCanvas(void)
{
}

void OpenCVGLCanvas::OnPaint( wxPaintEvent& event )
{
	wxPaintDC dc(this);
	SetCurrent();

	if(init) {
		init = false;
		pipeline->init();
	}

	if(capture->isValid() && capture->getCurrentFrame())
		pipeline->execute(capture);
	

	SwapBuffers();
}

void OpenCVGLCanvas::OnKeyPress( wxKeyEvent & evt )
{
	Model & m = ((SimpleFramePipeline*)getPipeLine())->getModel();
	static float motionBlend[3] = {1.0f, 0.0f, 0.0f};

	if(evt.GetKeyCode() == 'a')
		m.rotateLeft();
	else if(evt.GetKeyCode() == 'd')
		m.rotateRight();
	else if(evt.GetKeyCode() == 'w') 
		m.translateForward();
	else if(evt.GetKeyCode() == 's')
		m.translateBackward();
	
	m.setState(m.STATE_MOTION,0.3f);
	m.setMotionBlend(motionBlend,0.1f);
}