PolyGLTexture.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * PolyGLTexture.cpp
  3. * Poly
  4. *
  5. * Created by Ivan Safrin on 3/16/08.
  6. * Copyright 2008 __MyCompanyName__. All rights reserved.
  7. *
  8. */
  9. #include "PolyGLTexture.h"
  10. using namespace Polycode;
  11. OpenGLTexture::OpenGLTexture(unsigned int width, unsigned int height, char *textureData, bool clamp, int filteringMode, int type) : Texture(width, height, textureData,clamp, type) {
  12. this->filteringMode = filteringMode;
  13. glTextureLoaded = false;
  14. glTextureType = GL_RGBA;
  15. if(type == Image::IMAGE_RGB) {
  16. glTextureType = GL_RGB;
  17. }
  18. recreateFromImageData();
  19. }
  20. void OpenGLTexture::recreateFromImageData() {
  21. if(glTextureLoaded)
  22. glDeleteTextures(1, &textureID);
  23. glGenTextures(1, &textureID);
  24. glBindTexture(GL_TEXTURE_2D, textureID);
  25. if(clamp) {
  26. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  27. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  28. } else {
  29. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  30. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  31. }
  32. switch(filteringMode) {
  33. case Renderer::TEX_FILTERING_LINEAR:
  34. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  35. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  36. break;
  37. case Renderer::TEX_FILTERING_NEAREST:
  38. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  39. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  40. break;
  41. }
  42. if(textureData) {
  43. glTexImage2D(GL_TEXTURE_2D, 0, glTextureType, width, height, 0, glTextureType, GL_UNSIGNED_BYTE, textureData);
  44. }
  45. glTextureLoaded = true;
  46. }
  47. OpenGLTexture::OpenGLTexture(unsigned int width, unsigned int height) : Texture(width, height, NULL ,true) {
  48. }
  49. void OpenGLTexture::setGLInfo(GLuint textureID, GLuint frameBufferID) {
  50. this->textureID = textureID;
  51. this->frameBufferID = frameBufferID;
  52. }
  53. void OpenGLTexture::setTextureData(char *data) {
  54. glBindTexture(GL_TEXTURE_2D, textureID);
  55. glDrawBuffer(GL_AUX0);
  56. glDrawPixels(width, height, glTextureType, GL_UNSIGNED_BYTE, data);
  57. glReadBuffer(GL_AUX0);
  58. // glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 128, 128, 0);
  59. }
  60. OpenGLTexture::~OpenGLTexture() {
  61. glDeleteTextures(1, &textureID);
  62. }
  63. GLuint OpenGLTexture::getFrameBufferID() {
  64. return frameBufferID;
  65. }
  66. GLuint OpenGLTexture::getTextureID() {
  67. return textureID;
  68. }