=begin

/****************************************************************************
** $Id: qt/examples/box/glbox.cpp   2.2.2   edited 2000-08-31 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

/****************************************************************************
**
** This is a simple QGLWidget displaying an openGL wireframe box
**
** The OpenGL code is mostly borrowed from Brian Pauls "spin" example
** in the Mesa distribution
**
****************************************************************************/
=end

#include "glbox.h"

#if defined(_CC_MSVC_)
#pragma warning(disable:4305) #// init: truncation from const double to float
#endif

#/*!
#  Create a GLBox widget
#*/

require "qt2"
include Qt2
require "opengl"
include GL
require "qt2gl"
include Qt2gl

class GLBox < QRGLWidget

def initialize (parent=nil, name = "")
super ( parent,  name )

    @xRot = @yRot = @zRot = 0.0		#// default object rotation
    @scale = 1.25			#// default object scale
    @object = 0
    catchEvent
end


#/*!
#  Release allocated resources
#*/

def finalize(object)

    DeleteLists( object, 1 )
end


#/*!
#  Paint the box. The actual openGL commands for drawing the box are
##  performed here.
#*/

def ruby_paintGL()

    Clear( COLOR_BUFFER_BIT )

    LoadIdentity()
    Translate( 0.0, 0.0, -10.0 )
    Scale( @scale, @scale, @scale )

    Rotate( @xRot, 1.0, 0.0, 0.0 )
    Rotate( @yRot, 0.0, 1.0, 0.0 )
    Rotate( @zRot, 0.0, 0.0, 1.0 )

    CallList( @object )
end


#//  /*!
#//  Set up the OpenGL rendering state, and define display list
#//   *   /

def ruby_initializeGL()

    qglClearColor( Qt::black ) 		#// Let OpenGL clear to black
    @object = makeObject()		#// Generate an OpenGL display list
    ShadeModel( FLAT )
end



#/*!
#  Set up the OpenGL view port, matrix mode, etc.
#*/

def ruby_resizeGL(  w,  h )

    Viewport( 0, 0, w, h )
    MatrixMode( PROJECTION )
    LoadIdentity()
    Frustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 )
    MatrixMode( MODELVIEW )
end


#/*!
#  Generate an OpenGL display list for the object to be shown, i.e. the box
#*/

def makeObject()
	

    list = GenLists( 1 )

    NewList( list, COMPILE )

    qglColor( Qt::white )		      #// Shorthand for glColor3f or glIndex

    LineWidth( 2.0 )

    Begin( LINE_LOOP )
    Vertex(  1.0,  0.5, -0.4 )
    Vertex(  1.0, -0.5, -0.4 )
    Vertex( -1.0, -0.5, -0.4 )
    Vertex( -1.0,  0.5, -0.4 )
    End()

    Begin( LINE_LOOP )
    Vertex(  1.0,  0.5, 0.4 )
    Vertex(  1.0, -0.5, 0.4 )
    Vertex( -1.0, -0.5, 0.4 )
    Vertex( -1.0,  0.5, 0.4 )
    End()

    Begin( LINES )
    Vertex(  1.0,  0.5, -0.4 );   Vertex(  1.0,  0.5, 0.4 )
    Vertex(  1.0, -0.5, -0.4 );   Vertex(  1.0, -0.5, 0.4 )
    Vertex( -1.0, -0.5, -0.4 );   Vertex( -1.0, -0.5, 0.4 )
    Vertex( -1.0,  0.5, -0.4 ) ;  Vertex( -1.0,  0.5, 0.4 )
    End()

    EndList()

     list
end


#/*!
#  Set the rotation angle of the object to \e degrees around the X axis.
#*/

def setXRotation( degrees )

    @xRot = (degrees % 360)
    updateGL()
end


#/*!
#  Set the rotation angle of the object to \e degrees around the Y axis.
#*/

def setYRotation(  degrees )

    @yRot = (degrees % 360)
    updateGL()
end


#/*!
#  Set the rotation angle of the object to \e degrees around the Z axis.
#*/

def setZRotation(  degrees )

    @zRot = (degrees % 360)
    updateGL()
end
end

