# To change this template, choose Tools | Templates
# and open the template in the editor.

require 'rubygems'
require 'gl'
require 'glu'
require 'glut'

include Gl, Glu, Glut

class Perspective

    def initialize
        @x0 = 100.0; @y0 = 50.0; @z0 = 50.0
        @xref = 50.0; @yref = 50.0; @zref = 0.0
        @vx = 0.0; @vy = 1.0; @vz = 0.0
        @xmin = -40.0; @ymin = -60.0; @xmax = 40.0; @ymax = 60.0
        @dnear = 25.0; @dfar = 125.0

        @winwidth = 600; @winheight = 600

        glutInit
        glutInitDisplayMode GLUT_SINGLE | GLUT_RGB
        glutInitWindowPosition 50, 50
        glutInitWindowSize @winwidth, @winheight
        @window = glutCreateWindow "Perspective View of a Square"

        init
        glutDisplayFunc method(:display).to_proc
        glutReshapeFunc methos(:reshape).to_proc
        glutKeyboardFunc method(:keyboard).to_proc

        glutMainLoop
    end

    def reshape(new_width, new_height)
        glViewport 0, 0, new_width, new_height

        @winwidth = new_width
        @winheight = new_height
    end

    def keyboard(key, x, y)
        case key
        when ?\e
            glutDestroyWindow @window
            exit 0
        end
    end

    def init
        glClearColor 1, 1, 1, 0
        glMatrixMode GL_MODELVIEW
        gluLookAt @x0, @y0, @z0, @xref, @yref, @zref, @vx, @vy, @vz
        glMatrixMode GL_PROJECTION
        glFrustum @xwmin, @xwmax, @ywmin, @ywmax, @dnear, @dfar
    end

    def display
        glClear GL_COLOR_BUFFER_BIT

        glColor3f 0, 1, 0
        glPolygonMode GL_FRONT, GL_FILL
        glPolygonMode GL_BACK, GL_LINE

        glBegin GL_QUADS
            glVertex3f 0, 0, 0
            glVertex3f 100, 0, 0
            glVertex3f 100, 100, 0
            glVertex3f 0, 100, 0
        glEnd
        
        glFlush
    end
end

Perspective.new