#!/usr/bin/perl

# modules
use OpenGL ":old",":glutfunctions",":gluconstants",":functions";
use Time::HiRes qw (gettimeofday tv_interval usleep);
use strict; no strict "subs";
use Math::Trig;

# global variables
our ($PI,@tri2d,@tri3d,@options,$cfact,$tsize,$zsize,$fps,$time,$z,$t,$xmin,$xmax,$ymin,$ymax,$zmin,$zmax,$tmin,$tmax,$xres,$zres,$tres,$width,$height);


# window
$xmin = -5;
$xmax = 5;
$ymin = -5;
$ymax = 4;
$zmin = -4;
$zmax = 5;
$tmin = -4;
$tmax = 4;

# points/axis and misc
$xres = 30;
$zres = 50;
$tres = 100;
$fps = 30;
$cfact = 1;

# variables init
$PI = 3.415926535898;
$z = $zmin;
$t = $tmin;
$time = [gettimeofday];
$zsize = 0;
$tsize = 0;
	# 0(0): 2d
	# 0(1): 3d wire
	# 0(2): 3d solid
	# 1(0): stable
	# 1(1): time-function forward
	# 1(2): time-function backward
	# 2(0): orthographic viewing
	# 2(1): perspective viewing
	# 2(2): stereo viewing
	# 2(3): 2nd way of stereo viewing
	# 3(0): no colours
	# 3(1): color-mode 1
@options = (2,0,0,1);


# main program ;-)
init();
glutMainLoop;


sub init{
    # glut init
    glutInit(@ARGV);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(1024, 768);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("graph window");
    glutFullScreen();

    # functions
    glutKeyboardFunc(\&keyboard);
    glutDisplayFunc(\&display);
    glutReshapeFunc(\&resize);
    glutIdleFunc(\&idle);

    glClearColor(0,0,0,0);
    glMatrixMode(GL_PROJECTION);

    glOrtho($xmin,$xmax,$ymin,$ymax,$zmin,$zmax);
    glRotatef(-50, 5, -0.5, 0.0);
    glScalef(0.8,0.8,0.8); 

    reset();
}    


sub idle {
    my (@tripar,$dim,$yreal,$x,$y,$j);
    
    if ($options[0] == 0) {$dim=2;} 
    else {$dim=3;}

    if ($zsize < $zres) {
	$x=$xmin;
	for ($j=0; $j<=($dim*$xres-$dim); $j=$j+$dim) {

	    $yreal = "2*cos(t*sqrt(z*z+x*x)-2*atan(x/z))";
    	    $yreal =~ s/(?<!\w)([xzt])(?!\w)/\$$1$2/g;
    	    $y = eval ($yreal);
#    	    eval { $y=(cos($t*sqrt($z*$z+$x*$x))-sin($t*sqrt($z*$z+$x*$x)))*exp(-sqrt($z*$z+$x*$x))*5; };
#    	    eval { $y=0.8*sin($x*1.5)+0.8*sin($z*1.5+$PI/2)+0.8*sin($t*1.5+$PI); };

    	    $tripar[$j]=$x;
    	    $tripar[$j+1]=$y;
    	    if ($dim == 3) {$tripar[$j+2]=$z;}
    	    $x += ($xmax-$xmin)/($xres-1); 
	}

	$j = 0;
	if ($dim == 3) {
	    $z += ($zmax-$zmin)/($zres-1);
	    $zsize++; }
	elsif ($dim == 2) {
	    $zsize = $zres + 1;}
	
	if ($dim == 3) { 
	    unshift(@tri3d,@tripar);
	    splice(@tri3d,(9*$xres*$zres)); 
	    if ($zsize == $zres) { display();} }
	else {
	    unshift(@tri2d,@tripar);
	    splice(@tri2d,(2*$xres)); 
	    display();}
    }
    elsif (($options[1] == 1) and ($tsize < $tres)) {
	$zsize=0;
	$z=$zmin;
	$t += ($tmax-$tmin)/($tres-1);
	$tsize++;
    }
    elsif (($options[1] == 2) and ($tsize > 0)) {
	$zsize=0;
	$z=$zmin;
	$t -= ($tmax-$tmin)/($tres-1);
	$tsize--;
    }
    elsif ($options[1] == 1) {
	$options[1]=2;
    }
    elsif ($options[1] == 2) {
	$options[1]=1;
    }
    
}
    

sub display {
    my ($h,$i);

    if (tv_interval($time)<(1/$fps)) { 
    	usleep (1/120); 
	return; }
    $time = [gettimeofday];
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    # display2d()
    if ($options[0] == 0) {
	glColor3f(0.8,0.8,0.8);
	glBegin(GL_LINE_STRIP); {
	    if ($options[3] == 1) {scolor($tri2d[1]); }
	    glVertex2f($tri2d[0],$tri2d[1]);
	    for ($i=2; $i<=(2*$xres-2); $i=$i+2) { 
	    	if ($options[3] == 1) {scolor($tri2d[$i+1]); }
	    	glVertex2f($tri2d[$i],$tri2d[$i+1]); }
	} glEnd();
    }
    
    # display3d_wire()
    elsif ($options[0] == 1) {
	glColor3f(0.8,0.8,0.8);
	for $h (0..($zsize-2)) {
	    glBegin(GL_LINE_STRIP); {
	    	# jeweils um einen Punkt springen
	    	for ($i=0; $i<=(3*$xres-4); $i=$i+3) {
		    my $basec = $i+1+3*$h*$xres;
		    # left bottom
		    if ($options[3] == 1) {scolor($basec); }
		    glVertex3f($tri3d[$basec-1],$tri3d[$basec],$tri3d[$basec+1]);
		    # left top
		    if ($options[3] == 1) {scolor($basec+3*$xres); }
		    glVertex3f($tri3d[$basec-1+3*$xres],$tri3d[$basec+3*$xres],$tri3d[$basec+1+3*$xres]);
		    # rigth top
		    if ($options[3] == 1) {scolor($basec+3+3*$xres); }
		    glVertex3f($tri3d[$basec+2+3*$xres],$tri3d[$basec+3+3*$xres],$tri3d[$basec+4+3*$xres]);
		    # left bottom
		    if ($options[3] == 1) {scolor($basec); }
		    glVertex3f($tri3d[$basec-1],$tri3d[$basec],$tri3d[$basec+1]); }
	    } glEnd();
	}
    }
    
    # display3d_solid()
    # FALSCH
    elsif ($options[0] == 2) {
	for $h (0..($zsize-2)) {
	    glBegin(GL_TRIANGLE_STRIP);
	    {
	    	for ($i=0; $i<=(3*$xres-4); $i=$i+3) {
		    my $basec = $i+1+3*$h*$xres;
		    # left bottom
		    scolor($tri3d[$basec]);
		    glVertex3f($tri3d[$basec-1],$tri3d[$basec],$tri3d[$basec+1]);
		    # right top
		    scolor($tri3d[$basec+3*$xres]);
		    glVertex3f($tri3d[$basec+3*$xres-1],$tri3d[$basec+3*$xres],$tri3d[$basec+3*$xres+1]); }
	    } glEnd();
	}
    }

	
    
    glFlush();
    glutSwapBuffers();    
}


sub scolor {
    my $scolor = $_[0];
    glColor3f(0.8*sin($scolor*$cfact),0.8*sin($scolor*$cfact+2*$PI/3),0.8*sin($scolor*$cfact+4*$PI/3));
}

sub keyboard {
    my ($key) = @_;
    $key = chr($key);
    if($key=~/w/i){
    # rotate up
	glRotatef(3, 1, 0, 0); }
    elsif($key=~/s/i){
    # rotate down
        glRotatef(-3, 1, 0, 0); }
    elsif($key=~/a/i){
    # rotate left
        glRotatef(3, 0, 1, 0); }
    elsif($key=~/d/i){
    # rotate right
        glRotatef(-3, 0, 1, 0); }
    if($key=~/\ü/i){
    # translate up
	glTranslatef(0, 0.5, 0); }
    elsif($key=~/\ä/i){
    # translate down
       glTranslatef(0, -0.5, 0); }
    elsif($key=~/\ö/i){
    # translate left
        glTranslatef(-0.5, 0, 0); }
    elsif($key=~/\$/i){
    # translate right
        glTranslatef(0.5, 0, 0); }
    elsif($key=~/q/i){
        exit(); }
    elsif($key=~/m/i){
        screenshot(); }
    elsif($key=~/r/i){
        glScalef(1.1,1.1,1.1); }
    elsif($key=~/e/i){
        glScalef(0.9,0.9,0.9); }
    elsif(($key=~/t/i) and ($tsize < $tres)) {
	$zsize=0;
	$z=$zmin;
	$t += ($tmax-$tmin)/($tres-1);
	$tsize++;} 
    elsif(($key=~/g/i) and ($tsize > 0)) {
	$zsize=0;
	$z=$zmin;
	$t -= ($tmax-$tmin)/($tres-1);
	$tsize--;} 

    # repaint the window
    display(); 
}


sub resize{
    ($width,$height)=@_;
}


sub getheader{
    my ($width,$height) = @_;
    my ($header);

    $header =  pack("s",hex("4d42"));	        #signature
    $header .= pack("i",$width*$height*3+hex(36));#size (inc header)
    $header .= pack("s",0);			#reserved
    $header .= pack("s",0);			#reserved
    $header .= pack("i",hex(36));		#offset
    $header .= pack("i",40);		        #size of BITMAPINFOHEADER structure, must be 40
    $header .= pack("i",$width);		#width
    $header .= pack("i",$height);		#hight
    $header .= pack("s",1);			#number of planes in the image, must be 1
    $header .= pack("s",24);	                #bites per pixel
    $header .= pack("i",0);			#compression type (0=none, 1=RLE-8, 2=RLE-4)
    $header .= pack("i",$width*$height*3);	#size of image data
    $header .= pack("i",0);			#horizontal resolution in pixels per meter (unreliable)
    $header .= pack("i",0);			#vertical resolution in pixels per meter (unreliable)
    $header .= pack("i",0);			#number of colors in image, or zero
    $header .= pack("i",0);			#number of important colors, or zero

    return $header;
}


sub screenshot {
    my $num = 1;
    my $i;
    $num++ while(-f "image$num.bmp");

    if(open(IMAGE,">image$num.bmp")){
        print IMAGE getheader($width,$height);
        my @pixels=glReadPixels_p(0, 0,$width,$height, GL_RGB,GL_UNSIGNED_INT);
        #{print (IMAGE pack("B*", $i )); }for $i (@pixels);
        for $i (0..(($#pixels-1)/3)){
            my ($red, $green, $blue) = ($pixels[$i*3],$pixels[$i*3+1],$pixels[$i*3+2]);
            print IMAGE pack("B B B",$blue,$green,$red); }
        #for $pixel (@pixels){ print IMAGE pack ("B*",$pixel);}
        close(IMAGE);
    }
}


sub reset {
    # gl init
    glLoadIdentity();

           
    # window properties
    glFlush();
    
    # calculation
    idle();
}


#- eval error
#- perldoc -f eval
# color(rot,grün,blau)