#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 <stdio.h>

#include "math_matrix.h"
#include "math_vector3D.h"
#include "project_shaders.h"
#include "project_screen.h"
#include "project_textures.h"
#include "part_volume3.h"
#include "data_volumes.h"

#include "fx_plasma.h"
#include "fx_blobs.h"
#include "fx_fire.h"
#include "fx_water.h"

bool volume3_init = false;

bool fx_on = true;
int fx_num = 4;
int curr_fx = 0;

const int vol3dat_num = VOL_PLASMA1;

void volume3_init_rot_matrix(float rot_x, float rot_y, float rot_z)
{
	float c_rx = cos(rot_x);
	float c_ry = cos(rot_y);
	float c_rz = cos(rot_z);
	float s_rx = sin(rot_x);
	float s_ry = sin(rot_y);
	float s_rz = sin(rot_z);

	Cmatrix rot_matrix_X(	1,	0,		0, 
							0,	c_rx,	-s_rx, 
							0,	s_rx,	c_rx);

	Cmatrix rot_matrix_Y(	c_ry,	0,		s_ry, 
							0,		1,		0, 
							-s_ry,	0,		c_ry);

	Cmatrix rot_matrix_Z(	c_rz,	-s_rz,	0, 
							s_rz,	c_rz,	0, 
							0,		0,		1);

	rot_matrix = rot_matrix_X * rot_matrix_Y * rot_matrix_Z;
}

void volume3_init_frame(int ptime)
{
	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);

	glDisable(GL_BLEND);
	//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	InitFxBlobsColors();

	if (fx_on)
	{
		switch(curr_fx)
		{
			case 0:
				InitFxPlasmaColors();
				FxRunPlasma(volumes[vol3dat_num], ptime);
			break;

			case 1:
				//InitFxBlobsColors();
				//FxRunBlobs(volumes[vol3dat_num], ptime);
			break;

			case 2:
				//InitFxFireColors();
				//FxRunFire(volumes[vol3dat_num], ptime);
			break;

			case 3:
				//InitFxWaterColors();
				//FxRunWater(volumes[vol3dat_num], ptime);
			break;
		}

//		ClearBorders(volumes[vol3dat_num]->GetWidth(), volumes[vol3dat_num]->GetHeight(), volumes[vol3dat_num]->GetDepth(), volumes[vol3dat_num]->GetDataPointer());
		volumes[vol3dat_num]->UploadVolumeTexture();
	}
}

void volume3_precalcs()
{
	volumes[vol3dat_num]->InitVolumeTexture();
//	volumes[vol3dat_num]->UploadVolumeTexture();

	cam_zoom = 2.0f;
	rot_vecs.x = rot_vecs.y = rot_vecs.z = 0;

    volume3_init = true;
}


void DrawVolume3(int ptime)
{
	volume3_init_rot_matrix(rot_vecs.x, rot_vecs.y, rot_vecs.z);

	Cvector pos = Cvector(0,0,cam_zoom);
	Cvector dir = Cvector(0,0,-1);
	Cvector up = Cvector(0,1,0);
	Cvector right = Cvector(1,0,0);

	pos = rot_matrix.mul_vec(pos);
	dir = rot_matrix.mul_vec(dir);
	up = rot_matrix.mul_vec(up);
	right = rot_matrix.mul_vec(right);

	volumes[vol3dat_num]->SetIsoDepth(iso_surf);
	volumes[vol3dat_num]->RenderVolume(VRND_SHADERS, alpha_on, pos, dir, up, right);
}

void volume3_run(int ptime)
{
	if (!volume3_init) volume3_precalcs();

	volume3_init_frame(ptime);
	DrawVolume3(ptime);
}
