=begin
/****************************************************************************
** $Id: qt/examples/sharedbox/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 a box
**
** The OpenGL code is mostly borrowed from Brian Pauls "spin" example
** in the Mesa distribution
**
****************************************************************************/
=end

#include "glbox.h"

#// Initialize static class variables:

#// Shared display list id:
#GLuint GLBox::sharedDisplayList = 0

#// Counter keeping track of number of GLBox instances sharing 
#// the display list, so that the last instance can delete it:
#int GLBox::sharedListUsers = 0
require "qt2"
include Qt2
require "opengl"
include GL
require "qt2gl"
include Qt2gl


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

class GLBox < QRGLWidget
@@sharedDisplayList = 0
@@sharedListUsers = 0


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

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


#/*!
#  Set up the OpenGL rendering state. Robustly access shared display list.
#*/

def ruby_initializeGL()


    #// Let OpenGL clear to black
    qglClearColor( Qt::black )

    Enable(DEPTH_TEST)

    if ( @@sharedListUsers == 0 ) 	#// No shared list has been made yet
	@@sharedDisplayList = makeObject()	#// Make one
	@object = @@sharedDisplayList		#// Use it
	@@sharedListUsers += 1				#// Keep reference count
	print ( "GLBox #{name()} created shared display list.\n" )
    else 				#// There is a shared diplay list
	if ( isSharing() ) 		#// Can we access it?
	    @object = @@sharedDisplayList		#// Yes, use it
	    @@sharedListUsers += 1		#// Keep reference count
	    print( "GLBox #{name()} uses shared display list.\n" )
	else 				
	    @localDisplayList = makeObject()	#// No, roll our own
	    @object = @localDisplayList		#// and use that
	    print( "GLBox #{name()} uses private display list.\n" )
	end
    end
end



#/*!
#  Release allocated resources
#*/

def finalize

    makeCurrent()				#// We're going to do gl calls
    if ( @localDisplayList != 0 ) 		#// Did we make our own?
	DeleteLists( @localDisplayList, 1 )	#// Yes, delete it
	print( "GLBox #{name()} deleted private display list.\n" )
    else
	@@sharedListUsers -= 1	#// No, we used the shared one keep refcount
	if ( @@sharedListUsers == 0 )  			#// Any sharers left?
	    DeleteLists( @@sharedDisplayList, 1 )	#// No, delete it
	    @@sharedDisplayList = 0
	    print( "GLBox #{name()} deleted shared display list.\n" )
	end
    end
end


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

def ruby_paintGL()

    Clear( COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT )

    MatrixMode( MODELVIEW )
    LoadIdentity()
    Translate( 0.0, 0.0, -3.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 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, 1.0, 10.0)
end

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

def makeObject()

    list = GenLists( 1 )

    NewList( list, COMPILE )

    Begin(QUADS)
    #/* Front face */
    qglColor( Qt::green )
    Vertex(-1.0, 1.0, 1.0)
    Vertex(1.0, 1.0, 1.0)
    Vertex(1.0, -1.0, 1.0)
    Vertex(-1.0, -1.0, 1.0)
    #/* Back face */
    qglColor( Qt::yellow )
    Vertex(-1.0, 1.0, -1.0)
    Vertex(1.0, 1.0, -1.0)
    Vertex(1.0, -1.0, -1.0)
    Vertex(-1.0, -1.0, -1.0)
    #/* Top side face */
    qglColor( Qt::blue )
    Vertex(-1.0, 1.0, 1.0)
    Vertex(1.0, 1.0, 1.0)
    Vertex(1.0, 1.0, -1.0)
    Vertex(-1.0, 1.0, -1.0)
    #/* Bottom side face */
    qglColor( Qt::red )
    Vertex(-1.0, -1.0, 1.0)
    Vertex(1.0, -1.0, 1.0)
    Vertex(1.0, -1.0, -1.0)
    Vertex(-1.0, -1.0, -1.0)
    End()
    EndList()

    return 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

