


# 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);               #height
    $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 "$options{image_name}_$num.bmp");

    if(open(IMAGE,">$options{image_name}_$num.bmp")){
        print IMAGE getheader($options{width},$options{height});
        my @pixels=glReadPixels_p(0, 0,$options{width},$options{height},GL_RGB,GL_UNSIGNED_INT);
        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); }
        close(IMAGE);
    }
}
1;