//*****************************************************************************************
// Truevision - a 3d modeler for gnome and povray
//
// sphere.cc
//
// 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/sphere.h"
#include "include/viewmanager.h"
#include "include/objectlist.h"
#include "include/tvio.h"
#include <GL/glu.h>

//**************************************
// Constructeur
//**************************************
Sphere::Sphere( app_objs *appref ) : Object3D_with_material( appref )
{
type = TV_OBJ3D_SPHERE;
category = TV_OBJ3D_OBJECTS;
set_name( "Sphere" );

// Base
location = new ObjParam_point( N_("Location"), "LOC", NULL, app_ref, true );
location->set( 0, 0, 0 );
size = new ObjParam_scale( N_("Size"), "SIZE", NULL, app_ref, true );
size->set( 0.2, 0.2, 0.2 );
rotation = new ObjParam_rotation( N_("Rotation"), "ROT", NULL, app_ref, true );
rotation->set( 0, 0, 0 );
}

Sphere::Sphere( Sphere & ref ) : Object3D_with_material( ref )
{
location = new ObjParam_point( *ref.location );
size = new ObjParam_scale( *ref.size );
rotation = new ObjParam_rotation( *ref.rotation );
}

Sphere::~Sphere()
{
delete location;
delete size;
delete rotation;
}

//**************************************
// Display
// Dessin de la sphere
//**************************************
void Sphere::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;
	location->get( x, y, z );
	glTranslatef( x, y, z );
	rotation->gl_rotate();	
	size->get( x, y, z );
	glScalef( x, y, z );

	GLUquadricObj *quadobj = gluNewQuadric();
	if ( !quadobj ) { app_warning( _("Out of memory" ) ); return; }	
	gluQuadricDrawStyle( quadobj, GLU_FILL );
	//gluQuadricNormals( quadobj, GLU_SMOOTH );
	//gluQuadricOrientation( quadobj, GLU_OUTSIDE );
    gluSphere(quadobj, 1, 10, 10);
	//gdk_gl_draw_sphere( TRUE, 1, 10, 10 );
	
	glEnd();
	glPopMatrix();
	list.end();
	}
}


//***********************************************
// Edit
//***********************************************
void Sphere::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 );

texture->get_widget( edit_cont, tt );

gtk_widget_show_all( wid );
}

//***********************************************
// Mouse drag
//***********************************************
void Sphere::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 Sphere::pref_changed()
{
Object3D::pref_changed();
location->pref_changed();
size->pref_changed();
rotation->pref_changed();
}

//***********************************************
// Destroy editor
//***********************************************
void Sphere::destroy_editor()
{
Object3D::destroy_editor();
location->clear_widget();
size->clear_widget();
texture->clear_widget();
rotation->clear_widget();
}

//***********************************************
// Output to povray
//***********************************************
void Sphere::output_to_povray( ofstream & file )
{
if ( !render->value() ) return;
file << "\n\nsphere {\n\t<0,0,0>, 1\n\t";
texture->output_to_povray( file );

float x, y, z;
size->get( x, y, z );
file << "\n\tscale <" << x << ',' << y << ',' << z << ">\n\t";
rotation->output_to_povray( file );
location->get( x, y, z );
file << "\n\ttranslate <" << x << "," << y << "," << -z << ">";
Object3D::output_to_povray( file );
file << "\n}";
}


void Sphere::save( ofstream & file )
{
file << "\nSPHERE{\n";
save_basics( file );
size->save( file );
rotation->save( file );
location->save( file );
texture->save( file );
file << "\n}";
}

bool Sphere::load( ifstream & file, char *ltag )
{
if ( strcmp( ltag, "SPHERE" ) ) return false;
set_load_progress( file );

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;
		
	tvio_skip_section(file );
	} while ( tag != NULL );

return true;
}
