
#include "stdafx.h"
#include "CMotionBlur.h"
#include "../GLDef.h"

CMotionBlur::CMotionBlur()
{
	_Init();
}

CMotionBlur::CMotionBlur( TVel vel )
{
	_Init();
	_vel = vel;
}

CMotionBlur::CMotionBlur( float width, float height )
{
	_Init();
	_vel.width = width;
	_vel.height = height;
}

void CMotionBlur::_Init()
{
	_vel.width  = 256;
	_vel.height = 256;
	_blend = 0.8f;
	_texture_id = 0;
}

void CMotionBlur::Create()
{
	_texture_id = _CreateEmptyTexture( _vel.width, _vel.height );
}

//---------------------------------------------------------------------
// DRAWIMAGE - vykresli QUAD s texturou -texture_id- na pozice x,y 
// s sirkou -width- a vyskou -height-p
//---------------------------------------------------------------------
void CMotionBlur::_DrawImage(unsigned int texture_id,float width,float height,float x,float y)
{
	glEnable(GL_TEXTURE_2D);
	//glEnable(0x0BC0);						// zapne alphu
	glBindTexture(GL_TEXTURE_2D,texture_id);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);	glVertex3f(x,y,0);
		glTexCoord2f(1.0f, 0.0f);	glVertex3f(x+width,y,0);
		glTexCoord2f(1.0f, 1.0f);	glVertex3f(x+width,y+height,0);
		glTexCoord2f(0.0f, 1.0f);	glVertex3f(x,y+height,0);
	glEnd();
//	glDisable(0x0BC0);						// vypne alphu GL_ALPHA_TEST
	glDisable(GL_TEXTURE_2D);
}

// --------------------------------------------------
// CreateEmptyTexture - alokovani nove textury
// --------------------------------------------------
uint CMotionBlur::_CreateEmptyTexture(int w, int h)
{
	uint ret;
	unsigned char *data;
	data = new unsigned char [w*h*4];			// alokuje novou pamet pro RGBA
	ZeroMemory(data,w*h*4);						// Nulování paměti
	glGenTextures(1, &ret);
	glBindTexture(GL_TEXTURE_2D, ret);
	glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
	
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	delete [] data;								// smaze alokovanou pamet
	return ret;
}

// --------------------------------------------------
// StartMotionBlur - kresli texturu s motion blur
// --------------------------------------------------
void CMotionBlur::StartMotionBlur( float scr_width, float scr_height )
{
	return;
	glPushMatrix();						// ulozi aktalni matici
	glClearColor(0,0,0,1);				
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		
	
	glDisable(GL_LIGHTING);				// vypne svetlo
	glDisable(GL_DEPTH_TEST);			// zakaze testovani hloubky (z-bafr)
	glDepthMask(0);						// vypneme zapis do z-bafru
	glBlendFunc( GL_SRC_ALPHA, GL_ONE );	
	glEnable( GL_BLEND );					

	glMatrixMode( GL_PROJECTION );
	glPushMatrix( );
	glLoadIdentity();
	glOrtho(0, scr_width, 0, scr_height, -1, 1);	
	
	glMatrixMode(GL_MODELVIEW);			
	glLoadIdentity();
	
	glColor4f(1, 1, 1, _blend);			// nastavime alpha
	_DrawImage(_texture_id, scr_width, scr_height, 0, 0);		// vykreslim texturu
	
	glMatrixMode(GL_PROJECTION);		
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);			

	glEnable(GL_DEPTH_TEST);
	glDepthMask(1);
	glDisable(GL_BLEND);
	glEnable(GL_LIGHTING);
	glPopMatrix();
}

void CMotionBlur::EndMotionBlur( float scr_width, float scr_height )
{
	return;
	glEnable(GL_TEXTURE_2D);
	
	// ulozeni aktaulni sceny (= textura[1] s blendingem + vykreslena konev na nove pozici)  do textury
	glBindTexture(GL_TEXTURE_2D, _texture_id);
	glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, _vel.width, _vel.height);

	// Samotne vykresleni
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// smaze baffry	
	glViewport(0, 0, scr_width, scr_height);					// nastavy obrazovku na skutecne hodnoty
	StartMotionBlur(_vel.width, _vel.height );								// vykresli texturu pres celou obrazovku
}

/*
//---------------------------------------------------------------------
// ONDISPLAY - preklesleni okna
//---------------------------------------------------------------------
void onDisplay()
{	
	glLoadIdentity();			
	doPer();					

	rot_z+=5;
	glTranslatef(0,0,-20);
	glRotatef(rot_x,1,0,0);
	glRotatef(rot_y,0,1,0);
	glRotatef(rot_z,0,0,1);
	glViewport(0, 0, ren, ren);								
									
	RenderMotionBlur(ren,ren);					// vykresli texturu 1 na pozadi
	
	(light) ? glEnable(GL_LIGHTING) : glDisable(GL_LIGHTING);

	// Vykresleni konve 
	if(draw_texture) glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	//glColorMask(1,1,1,1);
	glColor3ub(255,255,255);
	glutSolidTeapot(3);
	glEnable(GL_TEXTURE_2D);
	
	// ulozeni aktaulni sceny (= textura[1] s blendingem + vykreslena konev na nove pozici)  do textury
	glBindTexture(GL_TEXTURE_2D, texture[1]);
	glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, ren, ren);

	// Samotne vykresleni
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// smaze baffry	
	glViewport(0, 0, SCRWidth, SCRHeight);					// nastavy obrazovku na skutecne hodnoty
	RenderMotionBlur(ren,ren);								// vykresli texturu pres celou obrazovku
	
	glFlush();												
}*/