PolyGLTexture.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyGLHeaders.h"
  20. #include "PolyGLTexture.h"
  21. #include "PolyCoreServices.h"
  22. #include "PolyRenderer.h"
  23. using namespace Polycode;
  24. #ifdef _WINDOWS
  25. extern PFNGLDELETEFRAMEBUFFERSEXTPROC glDeleteFramebuffersEXT;
  26. #endif
  27. OpenGLTexture::OpenGLTexture(unsigned int width, unsigned int height, char *textureData, bool clamp, bool createMipmaps, int filteringMode, int type) : Texture(width, height, textureData,clamp, createMipmaps, type) {
  28. this->filteringMode = filteringMode;
  29. glTextureLoaded = false;
  30. frameBufferID = FRAMEBUFFER_NULL;
  31. switch(type) {
  32. case Image::IMAGE_RGB:
  33. glTextureType = GL_RGB;
  34. glTextureFormat = GL_RGB;
  35. pixelType = GL_UNSIGNED_BYTE;
  36. break;
  37. case Image::IMAGE_FP16:
  38. glTextureType = GL_RGBA;
  39. glTextureFormat = GL_RGBA16F_ARB;
  40. pixelType = GL_FLOAT;
  41. break;
  42. default:
  43. glTextureType = GL_RGBA;
  44. glTextureFormat = GL_RGBA;
  45. pixelType = GL_UNSIGNED_BYTE;
  46. break;
  47. }
  48. recreateFromImageData();
  49. }
  50. void OpenGLTexture::recreateFromImageData() {
  51. Number anisotropy = CoreServices::getInstance()->getRenderer()->getAnisotropyAmount();
  52. if(glTextureLoaded) {
  53. glDeleteTextures(1, &textureID);
  54. }
  55. glGenTextures(1, &textureID);
  56. glBindTexture(GL_TEXTURE_2D, textureID);
  57. if(clamp) {
  58. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  59. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  60. } else {
  61. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  62. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  63. }
  64. switch(filteringMode) {
  65. case Renderer::TEX_FILTERING_LINEAR:
  66. if(anisotropy > 0) {
  67. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy);
  68. }
  69. if(createMipmaps) {
  70. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  71. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  72. if(textureData) {
  73. gluBuild2DMipmaps(GL_TEXTURE_2D, glTextureFormat, width, height, glTextureType, pixelType, textureData );
  74. }
  75. } else {
  76. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  77. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  78. if(textureData) {
  79. glTexImage2D(GL_TEXTURE_2D, 0, glTextureFormat, width, height, 0, glTextureType, pixelType, textureData);
  80. }
  81. }
  82. break;
  83. case Renderer::TEX_FILTERING_NEAREST:
  84. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  85. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  86. if(textureData) {
  87. glTexImage2D(GL_TEXTURE_2D, 0, glTextureFormat, width, height, 0, glTextureType, pixelType, textureData);
  88. }
  89. break;
  90. }
  91. glTextureLoaded = true;
  92. }
  93. OpenGLTexture::OpenGLTexture(unsigned int width, unsigned int height) : Texture(width, height, NULL ,true, true) {
  94. }
  95. void OpenGLTexture::setGLInfo(GLuint textureID, GLuint frameBufferID) {
  96. this->textureID = textureID;
  97. this->frameBufferID = frameBufferID;
  98. }
  99. void OpenGLTexture::setTextureData(char *data) {
  100. /*
  101. glBindTexture(GL_TEXTURE_2D, textureID);
  102. glDrawBuffer(GL_AUX0);
  103. glDrawPixels(width, height, glTextureType, pixelType, data);
  104. glReadBuffer(GL_AUX0);
  105. // glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 128, 128, 0);
  106. */
  107. }
  108. OpenGLTexture::~OpenGLTexture() {
  109. glDeleteTextures(1, &textureID);
  110. if(frameBufferID != FRAMEBUFFER_NULL) {
  111. glDeleteFramebuffersEXT(1, &frameBufferID);
  112. }
  113. }
  114. GLuint OpenGLTexture::getFrameBufferID() {
  115. return frameBufferID;
  116. }
  117. GLuint OpenGLTexture::getTextureID() {
  118. return textureID;
  119. }