;; where type is GL_UNSIGNED_BYTE GL_FLOAT GL_SHORT etc..
;; if texid is zero then assign a new tex id otherwise use the texid provided
(bind-func gl_load_tex_ext_bgr
  (lambda (width height channels type data textype texid)
    (let* ((tex:i32* (salloc)))
      (pset! tex 0 texid)
      ;(printf "load image: size: w:%d h:%d: channels:%d\n" width height channels)
      (if (< texid 1) (glGenTextures 1 tex))
      ;(printf "tex: %d\n" (pref tex 0))
      (glBindTexture textype (pref tex 0))
      ;;(glPixelStorei GL_UNPACK_ALIGNMENT 1) ;; this causes problems!!!
      (glTexParameteri textype GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE)
      (glTexParameteri textype GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE)
      (glTexParameteri textype GL_TEXTURE_MAG_FILTER GL_NEAREST)
      (glTexParameteri textype GL_TEXTURE_MIN_FILTER GL_NEAREST)
      (glTexImage2D textype 0 channels ;GL_RGBA
        width height
        0
        (cond ((= channels 1) GL_RED)
        ((= channels 3) GL_BGR)
        ((= channels 4) GL_BGRA) ;GL_RGBA)
        (else GL_BGRA))
        type
        ; GL_FLOAT; GL_UNSIGNED_BYTE
        data)
      (pref tex 0))))