#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_volume1.h"
#include "data_volumes.h"
#include "fx_colors.h"

bool volume1_init = false;

const int vol1dat_num = VOL_PLASMA1;

Cmatrix rot_matrix;
Cvector rot_vecs;
float cam_zoom;
float iso_surf = 0.8f;

unsigned int v1_argb[256];

bool alpha_on = true;

void volume1_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 volume1_init_colours()
{

//	SetPalette(v1_argb, GetColor(0,0,0,0), GetColor(255,255,255,63), 0, 127);
//	SetPalette(v1_argb, GetColor(255,255,255,63), GetColor(0,0,0,0), 128, 255);

//    SetPalette(v1_argb, GetColor(0,0,0,0), GetColor(0,0,0,0), 0, 191);
//    SetPalette(v1_argb, GetColor(0,0,0,0), GetColor(255,255,255,31), 192, 255);

//    SetPalette(v1_argb, GetColor(0,0,0,0), GetColor(0,0,0,0), 0, 191);
//    SetPalette(v1_argb, GetColor(0,0,0,0), GetColor(255,255,255,31), 192, 255);

//    SetPalette(v1_argb, GetColor(0,0,0,0), GetColor(0,0,0,0), 0, 191);
//    SetPalette(v1_argb, GetColor(0,0,0,0), GetColor(255,255,255,31), 192, 255);


    SetPalette(v1_argb, GetColor(0,0,0,0), GetColor(255,127,0,191), 0, 95);
    SetPalette(v1_argb, GetColor(255,127,0,192), GetColor(127,255,255,255), 96, 127);
	SetPalette(v1_argb, GetColor(127,255,255,255), GetColor(0,127,255,192), 128, 159);
    SetPalette(v1_argb, GetColor(0,127,255,191), GetColor(0,0,0,0), 160, 255);

	GenPalletteTexture(256, v1_argb, 0);
}

void volume1_init_frame()
{
	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);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	glActiveTextureARB(GL_TEXTURE0);
	glDisable(GL_TEXTURE_2D);
	glEnable(GL_TEXTURE_3D);
	glBindTexture(GL_TEXTURE_3D, volumes[vol1dat_num]->GetVolumeID());
}

void volume1_precalcs()
{
	volume1_init_colours();

	volumes[vol1dat_num]->InitVolumeTexture();

	iso_surf = volumes[vol1dat_num]->GetIsoDepth();

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

    volume1_init = true;
}


void DrawVolume1(int ptime)
{
	volume1_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[vol1dat_num]->SetIsoDepth(iso_surf);
	volumes[vol1dat_num]->RenderVolume(VRND_SHADERS, alpha_on, pos, dir, up, right);
}

void volume1_run(int ptime)
{
	if (!volume1_init) volume1_precalcs();

	volume1_init_frame();
	DrawVolume1(ptime);
}
