opengl_helper.cxx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // helper.cxx
  3. // graphics
  4. //
  5. // Created by Sam Jaffe on 5/18/19.
  6. // Copyright © 2019 Sam Jaffe. All rights reserved.
  7. //
  8. #include "helper.hpp"
  9. #ifdef __APPLE__
  10. #include <OpenGL/gl3.h>
  11. #endif
  12. #include "vector/vector.hpp"
  13. namespace graphics { namespace textures {
  14. static int glfmt(format color_fmt) {
  15. switch (color_fmt) {
  16. case format::RGB:
  17. return GL_RGB;
  18. case format::RGBA:
  19. return GL_RGBA;
  20. }
  21. }
  22. unsigned int init(format color_fmt, math::vec2i dimension,
  23. unsigned char * data) {
  24. unsigned int id;
  25. // Enable texturings
  26. // glEnable( GL_TEXTURE_2D );
  27. // Tell OpenGL that our pixel data is single-byte aligned
  28. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  29. // Ask OpenGL for an unused texName (ID number) to use for this texture
  30. glGenTextures(1, &id);
  31. // Tell OpenGL to bind (set) this as the currently active texture
  32. glBindTexture(GL_TEXTURE_2D, id);
  33. // Set texture clamp vs. wrap (repeat)
  34. // one of: GL_CLAMP_TO_EDGE, GL_REPEAT, GL_MIRRORED_REPEAT,
  35. // GL_MIRROR_CLAMP_TO_EDGE, ...
  36. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  37. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  38. // the format our source pixel data is currently in; any of: GL_RGB,
  39. // GL_RGBA, GL_LUMINANCE, GL_LUMINANCE_ALPHA, ...
  40. int bufferFormat = glfmt(color_fmt);
  41. // the format we want the texture to me on the card; allows us to translate
  42. // into a different texture format as we upload to OpenGL
  43. int internalFormat = bufferFormat;
  44. /* glTexImage2D: Load a 2d texture image
  45. * target: Creating this as a 2d texture.
  46. * level: Which mipmap level to use as the "root" (0 = the highest-quality,
  47. * full-res image), if mipmaps are enabled.
  48. * internalFormat: Type of texel format we want OpenGL to use for this
  49. * texture internally on the video card.
  50. * width: Texel-width of image; for maximum compatibility, use 2^N + 2^B,
  51. * where N is some integer in the range [3,10], and B is the border
  52. * thickness [0,1]
  53. * height: Texel-height of image; for maximum compatibility, use 2^M + 2^B,
  54. * where M is some integer in the range [3,10], and B is the border
  55. * thickness [0,1]
  56. * border: Border size, in texels (must be 0 or 1)
  57. * format: Pixel format describing the composition of the pixel data in
  58. * buffer
  59. * type: Pixel color components are unsigned bytes (one byte per color/alpha
  60. * channel)
  61. * pixels: Location of the actual pixel data bytes/buffer
  62. */
  63. glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, dimension.x(), dimension.y(),
  64. 0, bufferFormat, GL_UNSIGNED_BYTE, data);
  65. glGenerateMipmap(GL_TEXTURE_2D);
  66. // Set magnification (texel > pixel) and minification (texel < pixel)
  67. // filters one of: GL_NEAREST, GL_LINEAR
  68. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  69. // one of: GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST,
  70. // GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST,
  71. // GL_LINEAR_MIPMAP_LINEAR
  72. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  73. GL_NEAREST_MIPMAP_LINEAR);
  74. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 5);
  75. return id;
  76. }
  77. }}
  78. namespace graphics { namespace shaders {
  79. unsigned int init(unsigned int type, std::string const & path) { return 0; }
  80. }}