No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

texture.cpp 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* The MIT License
  2. *
  3. * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "texture.h"
  24. #include <GL/gl.h>
  25. #include <GL/glu.h>
  26. Texture::Texture(int width, int height, int components, unsigned char *pixels)
  27. {
  28. m_width = width;
  29. m_height = height;
  30. glGenTextures(1, &m_textureId);
  31. glBindTexture(GL_TEXTURE_2D, m_textureId);
  32. GLenum format = 0;
  33. switch(components) {
  34. case 4:
  35. format = GL_RGBA;
  36. break;
  37. case 3:
  38. format = GL_RGB;
  39. break;
  40. case 2:
  41. format = GL_LUMINANCE_ALPHA;
  42. break;
  43. case 1:
  44. format = GL_LUMINANCE;
  45. break;
  46. }
  47. glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, format, GL_UNSIGNED_BYTE, pixels);
  48. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  49. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  50. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  51. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  52. }
  53. Texture::~Texture()
  54. {
  55. if(m_textureId > 0)
  56. glDeleteTextures(1, &m_textureId);
  57. }
  58. void Texture::bind()
  59. {
  60. glBindTexture(GL_TEXTURE_2D, m_textureId);
  61. }
  62. void Texture::enableBilinearFilter()
  63. {
  64. bind();
  65. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  66. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  67. }
  68. void Texture::copyFromScreen(int xoffset, int yoffset, int x, int y, int width, int height)
  69. {
  70. bind();
  71. glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, width, height);
  72. }