# SPDX-Licence-Identifier: GPL-3.0-or-later

from OpenGL.GL import *
from PIL import Image

def loadTexture(path):
   image = Image.open(path).convert("RGB")

   texid = glGenTextures(1)

   glBindTexture(GL_TEXTURE_2D, texid)
   glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB, image.width, image.height,
      0, GL_RGB, GL_UNSIGNED_BYTE, image.tobytes())

   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)

   return texid


def solidTexture(r, g, b):
   texid = glGenTextures(1)

   glBindTexture(GL_TEXTURE_2D, texid)
   glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB, 1, 1, 0, GL_RGB, GL_FLOAT, [r, g, b])

   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)

   return texid
