#include once "FBTinyGL.bi"
#include once "FBImage.bi"

' test of texture quality glHint()
chdir exepath()
' textures must be in RGB or RGBA
var img = LoadRGBAFile("fragile.bmp")



dim as ulong Tex
dim as ubyte ptr pPixels
dim as integer   iWidth,iHeight,iBytesPerPixel
dim as single rtri,rquad
dim as integer frames,fps=60
dim as double tLast = timer()
dim as ulong  mode
dim as integer scr_w = 640
dim as integer scr_h = 480
dim as integer scr_b =  16
screenres scr_w,scr_h,scr_b

var ctx = CreateRenderContext()

glViewport 0, 0, scr_w, scr_h
glMatrixMode(GL_PROJECTION)
glLoadIdentity
gluPerspective(45.0, scr_w/scr_h, 0.3, 1000)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity
glClearColor(0,.5,.5)

glGenTextures(1,@Tex)
glBindTexture(GL_TEXTURE_2D, Tex)
ImageInfo img,iWidth,iHeight,iBytesPerPixel,,pPixels
glTexImage2D(GL_TEXTURE_2D, 0, iBytesPerPixel, iWidth,iHeight, 0,  GL_RGB, GL_UNSIGNED_BYTE, pPixels)
glEnable(GL_TEXTURE_2D)

glTranslatef(0,0,-3)
while inkey=""
  if rquad>=360 then
    glGetIntegerv(GL_PERSPECTIVE_CORRECTION_HINT,@mode)
    select case as const mode
    case GL_DONT_CARE : mode=GL_NICEST
    case GL_NICEST    : mode=GL_DONT_CARE
    end select
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,mode)
    rquad-=360
  end if

  glClear(GL_COLOR_BUFFER_BIT)

  glPushMatrix()
  glRotatef(-70, 1,0,0)
  glRotatef(rquad, 0,0,1)

  glBegin(GL_QUADS)
     glTexCoord2f(0,0) :  glVertex2f(-1,-1)
     glTexCoord2f(1,0) :  glVertex2f( 1,-1)
     glTexCoord2f(1,1) :  glVertex2f( 1, 1)
     glTexCoord2f(0,1) :  glVertex2f(-1, 1)
  glEnd()
  glPopMatrix()

  rquad += 0.01
  frames+=1
  'screensync ' <- ~60 Hz
  screenlock
    SwapBuffers(ctx)
    draw string (0,0), "frames: " & frames & " fps: " & fps
    glGetIntegerv(GL_PERSPECTIVE_CORRECTION_HINT,@mode)
    select case as const mode
    case GL_DONT_CARE : draw string (0,16),"mode = GL_DONT_CARE"
    case GL_NICEST    : draw string (0,16),"mode = GL_NICEST"
    end select
  screenunlock
  
  if frames mod 10=0 then
    dim as double tNow=timer()
    fps = 10/(tNow-tLast)
    tLast=tNow
  end if
  'sleep 10
wend

