#!/usr/bin/perl
#
# Name:		GLgraph
#
# Version:	0.1.6
#
# License:	GPL
#
# 2002/04/12	Jonas Jermann <jjermann@gmx.ch>
#
# pfy rulez!	(He told me to write it down...)
#
################################################################################
#
# M O D U L E S

use OpenGL ":old",":glutfunctions",":gluconstants",":functions";
use strict; no strict "subs";
use Math::Trig;
use Getopt::Long;


################################################################################
#
# M A I N

# Variables
our (@geometry,$fullscreen,$PI,$tsize,$zsize,$n,$z,$t,@tri2d,@tri3d,@yreal,@options,$cfact,$fps,$xmin,$xmax,$ymin,$ymax,$zmin,$zmax,$tmin,$tmax,$xres,$zres,$tres,$width,$height);

options();
commandline();
init();
reset_all();
idle();
glutMainLoop;


################################################################################
#
# S U B

# VERSION
sub version {
    my $version_string="0.1.6";
    print "GLgraph version $version_string\n";
    exit;
}

# USAGE
sub usage {
print STDERR <<EOF;

Usage: glgraph [options] 'function1' 'function2' ...

options:
  function         Function: f(x,z,t)=function
  -(no)fs          Fullscreen                                (DEFAULT = ON)
  -geometry        Window Geometry                           (DEFAULT = 1024x768+0+0)
  -2d              2D Graph
  -wire            3D Graph: Wireframe
  -3d              3D Graph: Solid                           (DEFAULT)
  -(no)time        Time Graph                                (DEFAULT = ON)
  -(no)colors      Color Mode (0=nocolors, 1:mode1, 2:mode2) (DEFAULT =   1)
  -xmin            Beginning of x interval (Window)          (DEFAULT =  -5)
  -xmax            Ending of x interval (Window)             (DEFAULT =   5)
  -ymin            Beginning of y interval (Window)          (DEFAULT =  -5)
  -ymax            Ending of y interval (Window)             (DEFAULT =   5)
  -zmin            Beginning of z interval (Window)          (DEFAULT =  -5)
  -zmax            Ending of z interval (Window)             (DEFAULT =   5)
  -tmin            Beginning of t (time) interval (Window)   (DEFAULT =  -4)
  -tmax            Ending of t (time) interval (Window)      (DEFAULT =   4)
  -xres            Resolution of x axis (relevant dots)      (DEFAULT =  40)
  -zres            Resolution of z axis (relevant dots)      (DEFAULT =  50)
  -xzres           Space Resolution (xres and zres)
  -tres            Resolution of time "axis" (frames)        (DEFAULT =  50)
  -cfact           Interval of color function                (DEFAULT = 1.5)
  -h, --help       Display this help message
  -v, --version    Display GLgraph's version number

keys:
  q                Quit
  m                Screenshot (doesn't work properly)
  w/a/s/d          Rotation
  ü/ö/ä/\$          Translation
  e and r          Scaling
  b and n          Change functions
  1 and 2          Change graphic mode
  3 and 4          Change color-range
  c                Toggle color-mode
  t                Toggle time-mode (notime is BUGGY)
  y and x          Seeking
  z and u          Change space resolution (BUGGY)
  h and j          Change time resolution (BUGGY)

EOF
    exit 1;
}

# COMMAND LINE
sub commandline {
    GetOptions(
        "fullscreen|fs"      => \$fullscreen,
        "nofullscreen|nofs"  => sub { $fullscreen=0; },
        "geometry=s"         => sub { @geometry=split(/[x+]/,"$_[1]") },
       	"2d"                 => sub { $options[0]=0 },
	"wire|3dwire"        => sub { $options[0]=1 },
	"3d|3dsolid"         => sub { $options[0]=2 },
	"time"               => sub { $options[1]=1 },
	"notime"             => sub { $options[1]=0 },
	"colors=i"           => \$options[3],
	"nocolors"           => sub { $options[3]=0 },
	"xmin=f"             => \$xmin,
        "xmax=f"             => \$xmax,
        "ymin=f"             => \$ymin,
        "ymax=f"             => \$ymax,
        "zmin=f"             => \$zmin,
        "zmax=f"             => \$zmax,
        "tmin=f"             => \$tmin,
        "xres=f"             => \$xres,
        "zres=f"             => \$zres,
        "xzres=f"            => sub { $xres=$_[1]; $zres=$_[1]; },
	"tres=f"             => \$tres,
        "fps=f"              => \$fps,
        "cfact=f"            => \$cfact,
        "help|h"             => \&usage,
        "version|v"          => \&version,
    ) || usage();

    # Add command line functions
    @yreal = (@ARGV, @yreal);
    for (@yreal) { $_ =~ s/(?<!\w)([xzt])(?!\w)/\$$1$2/g; }
    for (@yreal) { $_ =~ s/PI/\$PI/g; }
}

# DEFAULT OPTIONS
sub options {
    # Init variables
    $fullscreen = 1;
    @geometry = (1024,768,0,0);
    $n = 0;
    $PI = 3.415926535898;

    # OPTIONS
    # 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
    # 3(2): color-mode 2 (pfy)
    @options = (2,1,0,1);

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

    # Axis and misc
    $xres = 40;
    $zres = 50;
    $tres = 50;
    $fps = 30; #TODO
    $cfact = 1.5;

    # Functions
    $yreal[0] = '2*cos(t*sqrt(z*z+x*x)-2*atan(x/z))';
    $yreal[1] = '(cos(t*sqrt(z*z+x*x))-sin(t*sqrt(z*z+x*x)))*exp(-sqrt(z*z+x*x))*5';
    $yreal[2] = '0.1*z*sin(t*(z-x))-x*cos(z*x-t)';
    $yreal[3] = 'x*x*x';
    $yreal[4] = 'sin(x*x+z*z+t*t)';
}



# INIT
sub init {
    # Glut init
    glutInit();
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize($geometry[0],$geometry[1]);
    glutInitWindowPosition($geometry[2],$geometry[3]);
    glutCreateWindow("graph window");
    if ($fullscreen) { glutFullScreen(); }

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

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



# COLOR
sub scolor { 
    if ($options[3] == 1) { glColor3f(0.8*sin($_[0]*$cfact),0.8*sin($_[0]*$cfact+2*$PI/3),0.8*sin($_[0]*$cfact+4*$PI/3)); }
    elsif ($options[3] == 2) { 
	my ($r,$b,$g,$a);
	$r=$b=$g=0; 
	$a=(($_[0]-$ymin)/($ymax-$ymin)*$cfact);

	$r=$a*3;
	$r=1 if ($r > 1);

	$g=($a*3-1) if ((($a*3-1) > 0));
	$g=1 if ($g > 1);

	$b=($a*3-2) if ((($a*3-2) > 0));
	$b=1 if ($b > 1);

	glColor3f($r,$g,$b);
    }
}



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

sub reset_all {
    # Variables
    $z = $zmin;
    $t = $tmin;
    $zsize = 0;
    $tsize = 0;

    # GL stuff
    glLoadIdentity();
    glFlush();

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



# IDLE
sub idle {
    if ($zsize < $zres) {
        if ($options[0] == 0) {
            idle_2d();
            display();
        }
        if ($options[0] >= 1) {
    	    idle_3d();
    	    if ($zsize == $zres) {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; }
}
    


# MAIN DISPLAY
sub display {
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(0.8,0.8,0.8);
    
    if    ($options[0] == 0) { display_2d(); }
    elsif ($options[0] == 1) { display_3dwire(); }
    elsif ($options[0] == 2) { display_3d(); }

    glFlush();
    glutSwapBuffers();    
}



# KEYBOARD
sub keyboard {
    my ($key) = @_; 
    $key = chr($key);
    
    # Misc
    if   ($key=~/q/i){ exit(); }
    elsif($key=~/m/i){ screenshot(); }
    elsif($key=~/3/i){ $cfact=$cfact*0.98; }
    elsif($key=~/4/i){ $cfact=$cfact*1.02; }

    # Function change
    elsif($key=~/n/i){ $n++; reset_all(); }
    elsif(($key=~/b/i) and ($n > 0)){ $n--; reset_all; }

    # Mode change
    elsif(($key=~/1/i) and ($options[0]>0)){ $options[0]--; $zsize=0; $z=$zmin; }
    elsif(($key=~/2/i) and ($options[0]<2)){ $options[0]++; $zsize=0; $z=$zmin; }
    elsif($key=~/c/i) { 
	$options[3]++;
	if ($options[3] >=3) { $options[3]=0; } }
    elsif($key=~/t/i) { 
	if ($options[1] == 0) { $options[1]=1; }
	else { $options[1]=0; } }

    # Seeking
    elsif($key=~/x/i) { $zsize=0; $z=$zmin;
    	if (($options[1] > 0) and ($tsize <= ($tres-5))) { $t += 5*($tmax-$tmin)/($tres-1); $tsize=$tsize+5; }
	elsif ($tsize < $tres) { $t += ($tmax-$tmin)/($tres-1); $tsize++; } } 
    elsif($key=~/y/i) { $zsize=0; $z=$zmin; 
    	if (($options[1] > 0) and ($tsize >= 5)) { $t -= 5*($tmax-$tmin)/($tres-1); $tsize=$tsize-5; }
	elsif ($tsize > 0) { $t -= ($tmax-$tmin)/($tres-1); $tsize--; } } 

    # Resolution
    elsif($key=~/u/i){ $xres=int($xres*1.1+0.9); $zres=int($zres*1.1+0.9); reset_all(); }
    elsif($key=~/z/i){ $xres=int($xres*0.91+0.2); $zres=int($zres*0.91+0.2); reset_all(); }
    elsif($key=~/j/i){ $tres=int($tres*1.1+0.9); reset_all(); }
    elsif($key=~/h/i){ $tres=int($tres*0.91+0.2); reset_all(); }

    # Rotation
    elsif($key=~/w/i){ glRotatef(3, 1, 0, 0); }
    elsif($key=~/s/i){ glRotatef(-3, 1, 0, 0); }
    elsif($key=~/a/i){ glRotatef(3, 0, 1, 0); }
    elsif($key=~/d/i){ glRotatef(-3, 0, 1, 0); }

    # Translation
    elsif($key=~/\ü/i){ glTranslatef(0, 0.5, 0); }
    elsif($key=~/\ä/i){ glTranslatef(0, -0.5, 0); }
    elsif($key=~/\ö/i){ glTranslatef(-0.5, 0, 0); }
    elsif($key=~/\$/i){ glTranslatef(0.5, 0, 0); }

    # Scaling
    elsif($key=~/r/i){ glScalef(1.1,1.1,1.1); }
    elsif($key=~/e/i){ glScalef(0.91,0.91,0.91); }

#    # Repaint the window
#    display();

}



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

    $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;
}


# SCREENSHOT
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 IDLE (2D)
sub idle_2d {
    my (@tripar,$x,$y,$j,$eval_block);
    $x=$xmin;
    $j=0;
    
    $eval_block= '
    while ($j<=(2*$xres-2)) {
    ';
    $eval_block.= "\$y = $yreal[$n];";
    $eval_block.= '
        $tripar[$j]=$x;
        $tripar[$j+1]=$y;
        $x += ($xmax-$xmin)/($xres-1); 
	$j=$j+2;
    }
    return 1;
    ';
    
    while ($j<=(2*$xres-2)){
        last if eval($eval_block);
        $tripar[$j]=$x;
        $tripar[$j+1]=$y;
        $x += ($xmax-$xmin)/($xres-1);
        $j=$j+2;
    }
    
    $j = 0;
    $zsize = $zres + 1;
	
    unshift(@tri2d,@tripar); 
    splice(@tri2d,(2*$xres)); 
}    


# SUB IDLE (3D)
sub idle_3d {
    my (@tripar,$x,$y,$j,$eval_block);
    $x=$xmin;
    $j=0;
    
    $eval_block='
    while ($j<=(3*$xres-3)) {
    ';
    $eval_block.= "\$y = $yreal[$n];";
    $eval_block.= '	
        $tripar[$j]=$x;
        $tripar[$j+1]=$y;
        $tripar[$j+2]=$z;
        $x += ($xmax-$xmin)/($xres-1); 
        $j=$j+3;
    }
    return 1;
    ';
    
    while ($j<=(3*$xres-3)){
        last if eval($eval_block);
        $tripar[$j]=$x;
        $tripar[$j+1]=$y;
        $tripar[$j+2]=$z;
        $x += ($xmax-$xmin)/($xres-1);
        $j=$j+3;
    }

    $j = 0;
    $z += ($zmax-$zmin)/($zres-1); $zsize++; 

    unshift(@tri3d,@tripar); 
    splice(@tri3d,(9*$xres*$zres)); 
}    



# SUB DISPLAY (2D)
sub display_2d {
    my $i;

    glBegin(GL_LINE_STRIP); {
        if ($options[3] > 0) {scolor($tri2d[1]);}
        glVertex2f($tri2d[0],$tri2d[1]);
        for ($i=2; $i<=(2*$xres-2); $i=$i+2) { 
    	    if ($options[3] > 0) {scolor($tri2d[$i+1]); }
    	    glVertex2f($tri2d[$i],$tri2d[$i+1]);}
    } glEnd();
}


# SUB DISPLAY (3D WIRE)
sub display_3dwire {
    my ($h,$i);
    
    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] > 0) {scolor($tri3d[$basec]); }
		glVertex3f($tri3d[$basec-1],$tri3d[$basec],$tri3d[$basec+1]);
		# left top
		if ($options[3] > 0) {scolor($tri3d[$basec+3*$xres]); }
		glVertex3f($tri3d[$basec-1+3*$xres],$tri3d[$basec+3*$xres],$tri3d[$basec+1+3*$xres]);
		# rigth top
		if ($options[3] > 0) {scolor($tri3d[$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] > 0) {scolor($tri3d[$basec]); }
		glVertex3f($tri3d[$basec-1],$tri3d[$basec],$tri3d[$basec+1]); }
		if ($options[3] > 0) {scolor($tri3d[3*$xres-2+3*$h*$xres]); }
		glVertex3f($tri3d[3*$xres-3+3*$h*$xres],$tri3d[3*$xres-2+3*$h*$xres],$tri3d[3*$xres-1+3*$h*$xres]); 
		if ($options[3] > 0) {scolor($tri3d[3*$xres-2+3*$h*$xres+3*$xres]); }
		glVertex3f($tri3d[3*$xres-3+3*$h*$xres+3*$xres],$tri3d[3*$xres-2+3*$h*$xres+3*$xres],$tri3d[3*$xres-1+3*$h*$xres+3*$xres]); 
	} glEnd();
    }
}


# SUB DISPLAY (3D SOLID)
sub display_3d {
    my ($h,$i);

    glBegin(GL_TRIANGLES);
    {
	for $h (0..($zsize-2)) {
	    for ($i=0; $i<=(3*$xres-4); $i=$i+3) {
		my $basec = $i+1+3*$h*$xres;
		# left bottom
		if ($options[3] > 0) {scolor($tri3d[$basec]); }
		glVertex3f($tri3d[$basec-1],$tri3d[$basec],$tri3d[$basec+1]);
		# left top
		if ($options[3] > 0) {scolor($tri3d[$basec+3*$xres]); }
		glVertex3f($tri3d[$basec+3*$xres-1],$tri3d[$basec+3*$xres],$tri3d[$basec+3*$xres+1]); 
		# right bottom
		if ($options[3] > 0) {scolor($tri3d[$basec+3]); }
		glVertex3f($tri3d[$basec+2],$tri3d[$basec+3],$tri3d[$basec+4]); 
		# left top
		if ($options[3] > 0) {scolor($tri3d[$basec+3*$xres]); }
		glVertex3f($tri3d[$basec+3*$xres-1],$tri3d[$basec+3*$xres],$tri3d[$basec+3*$xres+1]); 
		# right top
		if ($options[3] > 0) {scolor($tri3d[$basec+3*$xres+3]); }
		glVertex3f($tri3d[$basec+3*$xres+2],$tri3d[$basec+3*$xres+3],$tri3d[$basec+3*$xres+4]); 
		# right bottom
		if ($options[3] > 0) {scolor($tri3d[$basec+3]); }
		glVertex3f($tri3d[$basec+2],$tri3d[$basec+3],$tri3d[$basec+4]); 
	    }
	}
    } glEnd();
}
