image.nim 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # author: Ethosa
  2. import
  3. ../thirdparty/opengl,
  4. ../thirdparty/sdl2,
  5. ../thirdparty/sdl2/image,
  6. ../core/vector2
  7. discard image.init()
  8. type
  9. GlTexture* = object
  10. texture*: Gluint
  11. size*: Vector2Ref
  12. proc load*(file: cstring, size: var Vector2Ref, mode: Glenum = GL_RGB): Gluint =
  13. ## Loads image from file and returns texture ID.
  14. ##
  15. ## Arguments:
  16. ## - `file` - image path.
  17. var
  18. surface = image.load(file) # load image from file
  19. textureid: Gluint
  20. size.x = surface.w.float
  21. size.y = surface.h.float
  22. # OpenGL:
  23. glGenTextures(1, textureid.addr)
  24. glBindTexture(GL_TEXTURE_2D, textureid)
  25. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
  26. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
  27. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
  28. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
  29. glTexImage2D(GL_TEXTURE_2D, 0, mode.GLint, surface.w, surface.h, 0, mode, GL_UNSIGNED_BYTE, surface.pixels)
  30. glBindTexture(GL_TEXTURE_2D, 0)
  31. # free memory
  32. surface.freeSurface()
  33. surface = nil
  34. textureid
  35. proc load*(file: cstring, mode: Glenum = GL_RGB): GlTexture =
  36. ## Loads GL texture.
  37. ##
  38. ## Arguments:
  39. ## - `file` - image path.
  40. var
  41. size: Vector2Ref = Vector2Ref()
  42. textureid: Gluint
  43. textureid = load(file, size, mode)
  44. GlTexture(texture: textureid, size: size)