#!/usr/bin/perl -w
use strict;
use OpenGL qw/ :all /;
use Math::Trig;

use constant N_SHAPES => 7;

#表示ウィンドウの位置、サイズ
glutInitWindowPosition(100, 100);
glutInitWindowSize(320, 240);
#glut初期化
#glutInit(&argc, &argv); # mainの引数をそのまま渡すと、引数の処理をしてくれる
glutInit();
glutInitDisplayMode(GLUT_RGBA);
glutCreateWindow('test');
glutDisplayFunc(\&display);
glutReshapeFunc(\&resize);
glutMouseFunc(\&mouse);
init();
#glut実行
glutMainLoop();

sub mouse {
  my ($button, $state, $x, $y) = @_;
#x,y: 左上を(0,0)とした画面のピクセル座標
#print "mouse ($x, $y)\n";

  our ($x0, $y0);
  if ($button == GLUT_LEFT_BUTTON) {
    if($state == GLUT_UP) {
      glColor3d(0.0, 0.0, 0.0);
        glBegin(GL_LINES);
        glVertex2i($x0, $y0);
        glVertex2i($x,  $y);
      glEnd();
      glFlush();
    }
    elsif($state == GLUT_DOWN) {
      $x0 = $x;
      $y0 = $y;
    }
  }
  elsif ($button == GLUT_RIGHT_BUTTON) {
  }
  else {
  	print "  button is ";
  }
  
}

sub resize
{
	my ($w, $h) = @_;
# $w, $h: 表示ウインドウのサイズが入ってくる

#表示するサイズ
	glViewport(0, 0, $w, $h);
	glLoadIdentity();
#マウスの座標と描画座標を合わせて手抜きをする
	glOrtho(-0.5, $w - 0.5, $h - 0.5, -0.5, -1.0, 1.0);
}

sub display
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glFlush();
}

sub init
{
	glClearColor(1.0, 1.0, 1.0, 1.0);
}
