#!/usr/bin/perl -w

use OpenGL ":old",":glutfunctions",":gluconstants",":functions";
use strict;
    # because of the OpenGL header functs
#use Time::HighRes;
no strict "subs";
our (@tri3d,@options,$xmin,$xmax,$ymin,$ymax,$zmin,$zmax,$tmin,$tmax,$xres,$yres,$zres,$tres);

$xmin = -10;
$xmax = 10;
$ymin = -10;
$ymax = 10;
$zmin = -10;
$zmax = 10;
$tmin = -10;
$tmax = 10;

$xres = 3;
$yres = 3;
$zres = 3;
$tres = 30;

# (z,t)
@options = (1,1);

init();
    # main loop
glutMainLoop;



#- alle punkte nacheinander
#- 2d array

#- berechnungwen in idle
#- unshift (array) an anfang splice nicht ewig
#- eval error
#- perldoc-eval


sub init{
    glutInit(@ARGV);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

    glutInitWindowSize(600, 600);
    glutInitWindowPosition(50, 50);
    glutCreateWindow("graph window");
    
    # black
    glClearColor(0,0,0,0);
    # reset
    glLoadIdentity();
    # set the window properties
    glOrtho($xmin,$xmax,$ymin,$ymax,$zmin,$zmax);
    # display
#    display3d();
    idle();        
    glFlush();
    glutKeyboardFunc(\&keyboard);
    glutDisplayFunc(\&display3d);
    glutReshapeFunc(\&resize);
    glutIdleFunc(\&idle);
}



sub idle {
    @tri3d = (1,1,1,2,2,2,3,3,3,1,2,3,4,5,6,7,8,9);
    if ($options[1] = 1) { 
	display3d();
    }
}

sub display3d {
    # clear !
    my $i;
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1,1,1);
    glBegin(GL_TRIANGLES);
    {
	for ($i=0; $i<($xres-1); $i++) {
	    glVertex3f($tri3d[$i],$tri3d[$i+1],$tri3d[$i+2]);
	    glVertex3f($tri3d[$i+3],$tri3d[$i+4],$tri3d[$i+5]);
	    glVertex3f($tri3d[$i+$xres],$tri3d[$i+$xres+1],$tri3d[$i+$xres+2]);
	}
    } glEnd();
    glFlush();
    glutSwapBuffers();
}

sub keyboard{
    my ($key) = @_;
    $key = chr($key);
    if($key=~/w/i){
    # rotate up
	glRotatef(6.0, 1, 0, 0.0);
    }elsif($key=~/s/i){
    # rotate down
        glRotatef(-6.0, 1, 0, 0.0);
    }elsif($key=~/a/i){
    # rotate left
        glRotatef(6.0, 0.0, 1, 0);
    }elsif($key=~/d/i){
    # rotate right
        glRotatef(-6.0, 0.0, 1, 0);
    }elsif($key=~/q/i){
        exit();
    }elsif($key=~/m/i){
        screenshot();

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

    }elsif($key=~/f/i){

        glScalef(0.9,0.9,0.9);

    }
    # repaint the window
    display3d(); 
}



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


#sub getheader{
#    my ($width,$hight) = @_;
#    my ($header);
#
#    $header =  pack("s",hex("4d42"));	        #signature
#    $header .= pack("i",$width*$hight*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",$hight);		#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*$hight*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,$hight);
#        my @pixels=glReadPixels_p(0, 0,$width,$hight, 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);
#    }
#}


