#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_shaders.h"
#include "demo_screen.h"

#include "math_vector3D.h"

#include "part_multieffects.h"

bool multieffects_init = false;

GLint me_timer;
GLint me_screen_pos_x;
GLint me_screen_pos_y;
GLint me_screen_zoom;
GLint me_screen_rot;

GLint me_resolution_x;
GLint me_resolution_y;

GLint me_freeze_time;
GLint me_enable_rotate;

int programm55 = PR_EFFEDARES_ZOOM;

float multieffects_position_x = 0.0;
float multieffects_position_y = 0.0;
float multieffects_zoom = 4.0;
float multieffects_rotate = 0.0;

float multieffects_freeze_time = 1.0;
float multieffects_enable_rotate = 1.0;

void multieffects_precalcs()
{
	glClearColor(0.0f, 0.0f, 0.5f, 0.0f);

	me_timer = MyGetUniformLocation(programm55, "ptimer");

	me_screen_pos_x = MyGetUniformLocation(programm55, "screen_pos_x");
	me_screen_pos_y = MyGetUniformLocation(programm55, "screen_pos_y");
	me_screen_zoom = MyGetUniformLocation(programm55, "screen_zoom");
	me_screen_rot = MyGetUniformLocation(programm55, "screen_rot");

	me_resolution_x = MyGetUniformLocation(programm55, "resolution_x");
	me_resolution_y = MyGetUniformLocation(programm55, "resolution_y");

	me_freeze_time = MyGetUniformLocation(programm55, "freeze_time");
	me_enable_rotate = MyGetUniformLocation(programm55, "enable_rotate");

    multieffects_init = true;
}

void DrawQuad55(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.415f

void DrawMultieffects(int ptime)
{
	glDisable(GL_DEPTH_TEST);
	glDisable(GL_CULL_FACE);
	glDisable(GL_BLEND);

	MyUseProgram(programm55);
	MyUniform1f(me_timer, (float)ptime);

	MyUniform1f(me_screen_pos_x, multieffects_position_x);
	MyUniform1f(me_screen_pos_y, multieffects_position_y);
	MyUniform1f(me_screen_zoom, multieffects_zoom);
	MyUniform1f(me_screen_rot, multieffects_rotate);

	MyUniform1f(me_resolution_x, (float)ScreenWidth);
	MyUniform1f(me_resolution_y, (float)ScreenHeight);

	MyUniform1f(me_freeze_time, multieffects_freeze_time);
	MyUniform1f(me_enable_rotate, multieffects_enable_rotate);

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

	DrawQuad55(16.0f);

	NotUseProgram();
}

void multieffects_run(int ptime)
{
	if (!multieffects_init) multieffects_precalcs();

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	DrawMultieffects(ptime);
}
