#include <sys/time.h>
#include <math.h>
#include "../h/cdefs.h"
#include "grf4.h"

#define PI        3.14159265358979320
#define DTOR	  (PI/180.0)
#define RTOD	  (180.0/PI)
	
#define CROSS	0
#define POLY    1
#define CONE	0
Grf4Object *cone;
Grf4Object *disk;
Grf4Object *sphere;
Grf4Object *p1, *p2, *p3;

float p1_edges[] = { 0.0, -20.0,   0.0, 50.0,   -50.0, -20.0 };

float roll, pitch, yaw;

extern double frandom();

extern void* theFace;
void* spherePoly;
void* conePoly;
void* discPoly;

static double cputime();
	
drawIt()
{
	float a;

	grf4Clear ();
#if CROSS
	printf ("drawing\n");
	grf4Move2 (0.0, 0.0);
	grf4Draw2 (1.0, 1.0);
	grf4Move2 (1.0, 0.0);
	grf4Draw2 (0.0, 1.0);
#endif
#if CONE
	grf4DrawObject (cone);
	conePoly = theFace;

	grf4PushTrans();
	grf4Translate (0.0, 0.0, 95.0);
#if 0
	grf4DrawObject (sphere);
	spherePoly = theFace;
#endif
	grf4Translate (0.0, 25.0, -120.0);
	grf4DrawObject (disk);
	grf4Translate (0.0, 25.0, -120.0);
	grf4DrawObject (disk);
	discPoly = theFace;
	grf4PopTrans();
#endif
#if POLY
	grf4PushTrans();
	grf4Rotate (roll, pitch, yaw);
	grf4Translate (40.0, 0.0, 0.0);
	grf4Rotate (45.0, 0.0, 0.00);
	grf4DrawObject (p2);
	a = 20.0*(sqrt(2.0));
	grf4Translate (-a, a, -40.0);
	grf4Rotate (45.0, -45.0, -90.0);
	grf4DrawObject (p3);
#if 0
	grf4Translate (30.0, 40.0, -20.0);
	grf4DrawObject (p3);
#endif
	grf4PopTrans();
#endif
	grf4Flush ();
}

static double cputime()
{
	struct timeval tv;
	gettimeofday (&tv, (struct timezone*)0);
	return (tv.tv_sec + tv.tv_usec*1e-6);
}

int xButton;

onHideLines(n,code)
int n;
int code;
{
	if (code == BF_INFOCUS)
	 { lightButton(n);
	 }
	else if (code == BF_OUTFOCUS)
	 { clearButton(n);
	 }
	else if (code == BF_RELEASED)
	 { grf4HideLines(1);
	   drawIt();
	   grf4HideLines(0);
	   printf ("Released\n");
	   if (n == xButton)
	    { grf4SetButtonName (n, "x");
	    }
	 }
	else if (code == BF_PRESSED)
	 { printf ("Pressed\n");
	   if (n == xButton)
	    { grf4SetButtonName (n, "foo");
	    }
	 }
}

int winw;
int winh;

main()
{
	float dw, dh;
	void *mat;
	void *light;
	double t0, t1;
	int i;

	grf4AddButton ("hide lines", 0, 1, onHideLines, 0);
	xButton = grf4AddButton ("x", 0, 1, onHideLines, 0);
	grf4AddButton ("y", BF_SAMEROW, 1, onHideLines, 0);
	grf4AddButton ("z", BF_SAMEROW, 1, onHideLines, 0);
	grf4PlaceWindow (300, 300, 400, 400);
	if (grf4Init ("foo", GRF_WHITE, GRF_BLACK, 
			     GRF_VIEW_CONTROL) < 0)
	 { printf ("Can't init graphics\n");
	   exit (-1);
	 }
	grf4SetDrawFxn (drawIt, 0);

	grf4Color (0xffffff);
	grf4GetWindowSize (&winw, &winh);
	dw = 1/(float)winw;
	dh = 1/(float)winh;
#if CROSS
	grf4Ortho (-dw, 1+dw, -dh, 1+dh, -1.0, 1.0, 0.0);
#endif
#if CONE
	grf4Window (-75.0, 75.0, -75.0, 75.0, 300.0, 500.0, 1.0);
	grf4SetView (0.0, 0.0, -400.0, 0.0, 0.0, 0.0);

	mat = grf4DefMaterial(0xffffff, 0, 0x400000, 0, 0.0, 1.0);
	light = grf4DefLight (0xffffff, 0, 1.0, 1.0, 1.0, 0.0);

	grf4SetLight (0, light);

	cone = grf4GenCone (4, 25.0, 25.0, 75.0, mat);
	disk = grf4GenDisc (4, 50.0, 25.0, mat);
	sphere = grf4GenSphere (12, 20.0, mat);
#endif
#if POLY
	grf4Ortho (-75.0, 75.0, -75.0, 75.0, 300.0, 500.0, 1.0);
	grf4SetView (0.0, 0.0, -400.0, 0.0, 0.0, 0.0);

	light = grf4DefLight (0xffffff, 0, 1.0, 1.0, 1.0, 0.0);
	grf4SetLight (0, light);

	p1 = grf4GenPolygon (3, p1_edges, 0);
	p2 = grf4GenRegPolygon (4, 80.0, 0);
	p3 = grf4GenRegPolygon (4, 80.0, 0);
#endif
#if 0
#define NTIMES	5000

	t0 = cputime(User);
	for (i=0; i<NTIMES; i++)
	 { grf4DrawObject (cone);
	 }
	t1 = cputime(User);
	printf ("time = %g msec.\n", (t1-t0)*1000/NTIMES);
#endif

#if 0
	while (1)
	 { roll = frandom (-1.0, 1.0);
	   pitch = frandom (-10.0, 0.0);
	   yaw = frandom (-0.0, 10.0);
	   printf ("%11.8f %11.8f %11.8f\n", roll, pitch, yaw);
	   roll = 0.704318;
	   pitch = -0.790037;
	   yaw = -6.625;
	   drawIt();
	 }
#endif
	drawIt();
	grf4Wait (-1);
}

#if 0
	   roll = -4.49154425;
	   pitch = -0.12384389;
	   yaw = 3.80745387;
#endif
#if 0
	   roll = 1.29885399;
	   pitch =  15.13035870;
	   yaw = 0.81077743;
#endif
