#define NO_SDL_GLEXT

#include <GL/glew.h>
#include <SDL/SDL_OpenGL.h>
#include <GL/gl.h>
#include <GL/glu.h>

#include <math.h>

#include "demo_script.h"
#include "demo_textures.h"
#include "demo_meshes.h"
#include "demo_shaders.h"
#include "demo_screen.h"
#include "demo_timer.h"

#include "math_vector3D.h"

#include "part_blur.h"

GLuint bufferTextureBlur;

bool blur_init = false;

int programm_blur = PR_BLUR;

void blur_precalcs()
{
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glDisable(GL_DEPTH_TEST);
	glDisable(GL_CULL_FACE);

	glActiveTextureARB(GL_TEXTURE0);
	glEnable(GL_TEXTURE_2D);
	bufferTextureBlur = GenTexture(ScreenWidth, ScreenHeight, GEN_VORONOI, 2, FILTER_BLURSUM);
	glBindTexture(GL_TEXTURE_2D, bufferTextureBlur);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

    blur_init = true;
}

void DrawQuadBlur(float l)
{
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex2f(-l, -l);
		glTexCoord2f(1.0f, 0.0f); glVertex2f( l, -l);
		glTexCoord2f(1.0f, 1.0f); glVertex2f( l, l);
		glTexCoord2f(0.0f, 1.0f); glVertex2f(-l, l);
	glEnd();
}

#define shite -2.41425f

void DrawBlur(int ptime)
{
	MyUseProgram(programm_blur);

	glLoadIdentity();
	glTranslatef(0.0f, 0.0f, shite * 16.0f);
	DrawQuadBlur(16.0f);
}

void blur_run(int ptime)
{
	if (!blur_init) blur_precalcs();

	DrawBlur(ptime);
}
