//*****************************************************************************************
// Truevision - a 3d modeler for gnome and povray
//
// heightfield.cc
//
// Kent Gustavsson  <oden@gmx.net>
// Vincent LE PRINCE <vincentleprince@users.sourceforge.net>
// Copyright (C) 2000-2001 Vincent LE PRINCE
// This file is part of the TRUEVISION Package

//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 of the License, or
//   (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
//*******************************************************************************************
#include "include/heightfield.h"
#include "include/viewmanager.h"
#include "include/objectlist.h"
#include "include/tvio.h"

//**************************************
// Constructeur
//**************************************

const int hfiletype_num = 6;
const char *hfiletype_ext[hfiletype_num] = { "gif","tga","pot","png","pgm","ppm" };
const char *hfiletype_def[hfiletype_num] = { "gif ","tga ","pot ","png ","pgm ","ppm " };

Heightfield::Heightfield( app_objs *appref ) : Object3D_with_material( appref )
{
type = TV_OBJ3D_HEIGHTFIELD;
category = TV_OBJ3D_OBJECTS;
set_name( "HeightField" );

// Base
location = new ObjParam_point( _("Location"), "LOC", NULL, app_ref, true );
location->set( 0, 0, 0 );
size = new ObjParam_scale( _("Size"), "SIZE", NULL, app_ref, true );
size->set( 0.2, 0.2, 0.2 );
rotation = new ObjParam_rotation( _("Rotation"), "ROT", NULL, app_ref, true );
rotation->set( 0, 0, 0 );
water_level = new ObjParam_float (_("Water level"), "W_LEVEL", NULL, app_ref, true, 0 );
water_level->set_range( 1, 0, 0.01, 5 );
smooth = new ObjParam_bool (_("Smooth"), "SMOOTH", NULL, app_ref, true, false );
hierarchy = new ObjParam_bool (_("Hierarchy"), "HIERARCHY", NULL, app_ref, true, true);
bitmap = new TvWidget_file( _("Image"), "BITMAP", NULL, app_ref );
bitmap->set_list( hfiletype_ext, hfiletype_def, hfiletype_num );
}

Heightfield::Heightfield( Heightfield & ref ) : Object3D_with_material( ref )
{
location = new ObjParam_point( *ref.location );
size = new ObjParam_scale( *ref.size );
rotation = new ObjParam_rotation( *ref.rotation );
smooth = new ObjParam_bool( *ref.smooth );
water_level = new ObjParam_float( *ref.water_level );
hierarchy = new ObjParam_bool( *ref.hierarchy );
bitmap = new TvWidget_file( *ref.bitmap );
}

Heightfield::~Heightfield()
{
delete location;
delete size;
delete rotation;
delete smooth;
delete water_level;
delete hierarchy;
delete bitmap;
}

//**************************************
// Display
// Dessin de la boite
//**************************************
void Heightfield::display( glview *view )
{
if ( hidden->value() ) return;
if ( location->changed() || size->changed() || rotation->changed() )
	list.invalidate();

Object3D::display( view );	
if ( ! list.exec() )
	{
	// mise à zero des parametres
	location->unchange();
	size->unchange();
	rotation->unchange();

	// creation de la liste si necessaire
	list.begin();
	glPushMatrix();

	set_color();
	
	// Position et direction
	gfloat x, y, z;
	gfloat a, b, c;
	location->get( x, y, z );
	glTranslatef( x, y, z );
	rotation->gl_rotate();

	size->get( a, b, c );
	x = -a/2; y = -b/2; z = -c/2;
	a = -x; b = -y; c = -z;

	gfloat vertex[24][3] = { {x,y,z}, {x,y,c}, {x,b,c}, {x,b,z},
							 {x,b,z}, {x,b,c}, {a,b,c}, {a,b,z},
							 {a,b,z}, {a,b,c}, {a,y,c}, {a,y,z},
							 {a,y,z}, {a,y,c}, {x,y,c}, {x,y,z},
							 {x,y,z}, {x,b,z}, {a,b,z}, {a,y,z},
							 {a,b,c}, {x,b,c}, {x,y,c}, {a,y,c}	};
	gfloat normal[6][3] = { {-1,0,0}, {0,1,0}, {1,0,0}, {0,-1,0}, {0,0,-1}, {0,0,1} };

	register int i,j;
	glBegin( GL_QUADS );
	for ( i = 0; i < 24 ; i++ )
		{
		j = i/4;
		glNormal3f( normal[j][0], normal[j][1], normal[j][2] );
		glVertex3f( vertex[i][0], vertex[i][1], vertex[i][2] );			
		}		
	glEnd();
	glPopMatrix();
	list.end();
	}
}


//***********************************************
// Edit
//***********************************************
void Heightfield::edit_widget( GtkWidget *wid )
{
bool tt = true;
// Options communes
Object3D::edit_widget( wid );

// Options de geometrie
new_frame( edit_cont, _("General settings") );
location->get_widget( frame, tt );
size->get_widget( frame, tt );
rotation->get_widget( frame, tt );
smooth->get_widget( frame, tt );
water_level->get_widget( frame, tt);
hierarchy->get_widget( frame, tt);
texture->get_widget( edit_cont, tt );
bitmap->get_widget( frame, tt);
gtk_widget_show_all( wid );
}

//***********************************************
// Mouse drag
//***********************************************
void Heightfield::mouse_drag( struct drag_info *drag )
{
VMAN_DEF
OBJLIST_DEF

switch( vmanager->get_pointer_mode() )
	{
	case TV_PMODE_TRANSLATE:
		location->mouse_drag( drag );
		break;

	case TV_PMODE_SCALE:
		{ size->mouse_drag( drag ); }
		break;

	case TV_PMODE_ROTATE:
		{ rotation->mouse_drag( drag ); break; }

	case TV_PMODE_CUSTOM:
		((ObjParam_point*)(objlist->get_current_param()))->mouse_drag( drag );
		break;

	default:
		break;
	}
}

//***********************************************
// Pref_changed
//***********************************************
void Heightfield::pref_changed()
{
Object3D::pref_changed();
location->pref_changed();
size->pref_changed();
rotation->pref_changed();
smooth->pref_changed();
water_level->pref_changed();
hierarchy->pref_changed();
 bitmap->pref_changed();
}

//***********************************************
// Destroy editor
//***********************************************
void Heightfield::destroy_editor()
{
Object3D::destroy_editor();
location->clear_widget();
size->clear_widget();
texture->clear_widget();
rotation->clear_widget();
smooth->clear_widget();
water_level->clear_widget();
hierarchy->clear_widget();
bitmap->clear_widget();
}

//***********************************************
// Output to povray
//***********************************************
void Heightfield::output_to_povray( ofstream & file )
{
if ( !render->value() ) return;
file << "\n\n//Height Field " << name->value();
file << "\nheight_field {\n\t";
bitmap->output_to_povray( file );
 file << "\n\t";
if ( hierarchy->value() == 0 ) file << "hierarchy off\n\t"; // Doesn't work
if ( smooth->value() ) file << "smooth\n\t";
if ( water_level->value() != 0 ) file << "water_level " << water_level->value() << "\n\t";
float a, b, c, x, y, z;
texture->output_to_povray( file );
rotation->output_to_povray( file );
location->get( x, y, z );
size->get( a, b, c );
a = a/2; b = b/2; c = c/2;
file << "scale <" << a*2 << ',' << b*2 << ',' << c*2 << ">\n\t";
file << "\n\ttranslate <" << x-a << "," << y-b << "," << -z-c << "> \n\t";
Object3D::output_to_povray( file );
file << "\n}";
}


void Heightfield::save( ofstream & file )
{
file << "\nHEIGHTFIELD{\n";
save_basics( file );
location->save( file );
size->save( file );
rotation->save( file );
texture->save( file );
smooth->save( file );
water_level->save( file );
hierarchy->save( file );
bitmap->save( file );
file << "\n}";
}

bool Heightfield::load( ifstream & file, char *ltag )
{
if ( strcmp( ltag, "HEIGHTFIELD" ) ) return false;

char * tag = NULL;
do {
	tag = tvio_get_next_tag( file );
	if ( tag == NULL ) break;
	
	if ( load_basics( file, tag ) ) continue;
 	if ( location->load( file, tag ) ) continue;
 	if ( size->load( file, tag ) ) continue;
 	if ( rotation->load( file, tag ) ) continue;
	if ( smooth->load( file, tag ) ) continue;
	if ( water_level->load( file, tag ) ) continue;
	if ( hierarchy->load( file, tag ) ) continue;
	if ( bitmap->load( file, tag ) ) continue;

	tvio_skip_section(file );
	} while ( tag != NULL );

return true;
}
