#include once "crt/stdio.bi" ' sscanf

#include once "inc/iup.bi"
#include once "inc/iupgl.bi"

dim shared as Ihandle ptr canvas

sub _init constructor
  IupOpen(0,0)
  IupGLCanvasOpen()
end sub

sub _exit destructor
  IupClose()
end sub


function redraw cdecl (self as Ihandle ptr) as integer
  static as single c=0.0
  dim as integer  w, h
  dim as zstring ptr size = IupGetAttribute(self, "RASTERSIZE")
  sscanf(size, "%dx%d", @w, @h)
  if h<1 then h=1
  dim as single ratio = w/h
  IupGLMakeCurrent(self)
  
  
  glFrontFace(GL_CCW)
  glCullFace(GL_BACK)
  glEnable(GL_CULL_FACE)
  
  glShadeModel(GL_SMOOTH)
  glHint(GL_PERSPECTIVE_CORRECTION_HINT ,GL_NICEST)
  
  glEnable(GL_LIGHT0)
  glEnable(GL_LIGHTING)
  
  glMatrixMode  (GL_PROJECTION)
  glLoadIdentity()
  gluPerspective(60, ratio, 0.1, 512.1)
  glMatrixMode  (GL_MODELVIEW)
  glLoadIdentity()

  glClearColor(0, .5, .5, 1.0)
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
  
  glTranslatef(0,0,-5)
  glRotatef(c,1,1,1)
  glcolor3f(0.5,0.5,0.5)
  glBegin(GL_QUADS)
    glNormal3f(0,0,1)
    glVertex3f(-0.5, 0.5, 0.5):glVertex3f(-0.5,-0.5, 0.5)
    glVertex3f( 0.5,-0.5, 0.5):glVertex3f( 0.5, 0.5, 0.5)
    glNormal3f(1,0,0)
    glVertex3f( 0.5, 0.5, 0.5):glVertex3f( 0.5,-0.5, 0.5)
    glVertex3f( 0.5,-0.5,-0.5):glVertex3f( 0.5, 0.5,-0.5)
    glNormal3f  (0,0,-1)
    glVertex3f( 0.5, 0.5,-0.5):glVertex3f( 0.5,-0.5,-0.5)
    glVertex3f(-0.5,-0.5,-0.5):glVertex3f(-0.5, 0.5,-0.5)
    glNormal3f(-1,0,0)
    glVertex3f(-0.5, 0.5,-0.5):glVertex3f(-0.5,-0.5,-0.5)
    glVertex3f(-0.5,-0.5, 0.5):glVertex3f(-0.5, 0.5, 0.5)
    glNormal3f(0,1,0)
    glVertex3f(-0.5, 0.5,-0.5):glVertex3f(-0.5, 0.5, 0.5)
    glVertex3f( 0.5, 0.5, 0.5):glVertex3f( 0.5, 0.5,-0.5)
    glNormal3f(0,-1,0)
    glVertex3f(-0.5,-0.5, 0.5):glVertex3f(-0.5,-0.5,-0.5)
    glVertex3f( 0.5,-0.5,-0.5):glVertex3f( 0.5,-0.5, 0.5)
  glEnd()
   
  IupGLWait(1)
  IupGLSwapBuffers(self) 
  c+=2 : if c>360.0 then c=0.0
  return IUP_DEFAULT
end function

function idle_cb() as integer
  iupRedraw(canvas,0)
  sleep(10)
  return IUP_DEFAULT
end function
' main

dim as Ihandle ptr finale, dg

canvas = IupGLCanvas(NULL)

IupSetAttribute(canvas, IUP_BUFFER, IUP_DOUBLE)
IupSetAttribute(canvas, "RASTERSIZE", "256x256")

dg = IupDialog(IupHbox(IupFill(), _
                 canvas, _
                 IupFill(), _
                 NULL))
IupSetAttribute(dg, "TITLE", "IupGLCanvas")

IupShow(dg)

IupSetCallback(canvas, "ACTION", @redraw)
IupSetFunction ("IDLE_ACTION", cast(Icallback,@idle_cb))
IupMainLoop()

