=begin
#/****************************************************************************
** $Id: qt/extensions/opengl/examples/texture/gltexobj.cpp   2.1.0-beta3   edited 2000-02-23 $
**
** Implementation of GLTexobj
** This is a simple QGLWidget demonstrating the use of QImages for textures.
**
** Much of the GL code is inspired by the 'spectex' and 'texcyl'
** public domain demo programs by Brian Paul
**
***************************************************************************#*/
=end
require "qt2"
include Qt2

require "opengl"
include GL
include GLU

RedrawWait = 50

##/*!
#  Create a GLTexobj widget
##*/
class GLTexobj < QRGLWidget

def initialize( parent,name )
    @xRot = @yRot = @zRot = 0.0		#// default object rotation
    @scale = 5.0			#// default object scale
    @object = 0
    @animation = true
    @timer = QTimer.new( self )
    Qt::connect( @timer, QSIGNAL("timeout()"), self,QSLOT("update()") )
    @timer.start( RedrawWait, true )
end


#/*!
#  Release allocated resources
#*/

#GLTexobj::~GLTexobj()

#    glDeleteLists( @object, 1 )
#end


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

def ruby_paintGL()

    if ( @animation )
	@xRot += 1.0
	@yRot += 2.5
	@zRot -= 5.0
    end
    Clear( COLOR_BUFFER_BIT )
    PushMatrix()
    Rotate( @xRot, 1.0, 0.0, 0.0 )
    Rotate( @yRot, 0.0, 1.0, 0.0 )
    Rotate( @zRot, 0.0, 0.0, 1.0 )
    Scale( @scale, @scale, @scale )
    CallList( @object )
    PopMatrix()

    if ( @animation )
	Flush() #// Make sure everything is drawn before restarting timer
	@timer.start( RedrawWait, true ) #// Wait self many msecs before redraw
    end
end


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

def ruby_initializeGL()

    #// Set up the lights

    whiteDir = [2.0, 2.0, 2.0, 1.0]
    whiteAmb = [1.0, 1.0, 1.0, 1.0]
    lightPos = [30.0, 30.0, 30.0, 1.0]

    Enable(LIGHTING)
    Enable(LIGHT0)
    LightModel(LIGHT_MODEL_TWO_SIDE, GL::FALSE)
    LightModel(LIGHT_MODEL_AMBIENT, whiteAmb)

    Material(FRONT, DIFFUSE, whiteDir)
    Material(FRONT, SPECULAR, whiteDir)
    Material(FRONT, SHININESS, 20.0)

    Light(LIGHT0, DIFFUSE, whiteDir)		#// enable diffuse
    Light(LIGHT0, SPECULAR, whiteDir)	#// enable specular
    Light(LIGHT0, POSITION, lightPos)

    #// Set up the textures

    @img1 = QImage.new
    @img1.load( "qtlogo.bmp" )	#// Load first image from file
    if ( @img1.isNull() ) 		#// File not found handling
	#warning( "Could not read image file, using single-color instead." )
	dummy = QImage.new
             dummy.create( 128, 128, 32 )
	dummy.fill( Qt::red.rgbQRgb() )
	@img1 = dummy
    else
	#@img1 = @img1.convertDepth( 32 )	#// Make sure it has the right depth
	#//@img1 = @img1.mirror()		#// OpenGL wants it upside down
	#//@img1 = @img1.swapRGB()		#// OpenGL wants BGR
             @img1 = QGLWidget::convertToGLFormat(@img1)
    end

    @img2 = QImage.new()
    @img2.load( "gllogo.bmp" )	#// Load second image from file
    if ( @img2.isNull() ) 		#// File not found handling
	#warning( "Could not read image file, using single-color instead." )
	dummy = QImage.new
        dummy.create( 128, 128, 32 )
	dummy.fill( Qt::green.rgbQRgb() )
	@img2 = dummy
    else
        #//@img2 = @img2.convertDepth( 32 ).mirror().swapRGB()	#// as above
        #@img2 = @img2.convertDepth(32)
        @img2 = QGLWidget::convertToGLFormat(@img2)
    end

    TexParameter( TEXTURE_2D, TEXTURE_MAG_FILTER, LINEAR )
    TexParameter( TEXTURE_2D, TEXTURE_MIN_FILTER, LINEAR )
    Enable( TEXTURE_2D )

    #// Set up various other stuff

    ClearColor( 0.0, 0.0, 0.0, 0.0 ) #// Let OpenGL clear to black
    Enable( CULL_FACE )  	#// don't need Z testing for convex objects
    Hint( PERSPECTIVE_CORRECTION_HINT, NICEST )

    #// Make the object display list

    @object = makeObject( @img2, @img1 )	#// Generate an OpenGL display list
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, 10.0, 100.0 )
    MatrixMode( MODELVIEW )
    LoadIdentity()
    Translate( 0.0, 0.0, -70.0 )
end


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

def makeObject ( tex1, tex2 )
    #@tex1 = tex1
    #@tex2 = tex2
    q = NewQuadric()
    cylinderObj = GenLists(1)
    NewList( cylinderObj, COMPILE )

    Translate( 0.0, 0.0, -1.0 )

    #// cylinder
    TexImage2D( TEXTURE_2D, 0, 3, tex1.width(), tex1.height(), 0,
		  RGBA, UNSIGNED_BYTE, tex1.bits() )
    QuadricNormals( q, SMOOTH )
    QuadricTexture( q, GL::TRUE )
    Cylinder(q, 0.6, 0.6, 2.0, 24, 1)

    #// end cap
    TexImage2D( TEXTURE_2D, 0, 3, tex2.width(), tex2.height(), 0,
		  RGBA, UNSIGNED_BYTE, tex2.bits() )
    Translate( 0.0, 0.0, 2.0 )
    Disk( q, 0.0, 0.6, 24, 1 )

    #// other end cap
    Translate( 0.0, 0.0, -2.0 )
    QuadricOrientation( q, INSIDE )
    Disk( q, 0.0, 0.6, 24, 1 )

    EndList()
    DeleteQuadric( q )

    return cylinderObj
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


#/*!
#  Turns animation on or off
#*/

def toggleAnimation()

    @animation = !@animation
    if ( @animation )
	updateGL()
    else
	@timer.stop()
    end
end
end
