eglutilsgles.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. #include <QDebug>
  5. #include <QString>
  6. #include <QImage>
  7. #include "eglutilsgles.h"
  8. #include <stdio.h>
  9. #include <assert.h>
  10. #ifdef USE_GLES11
  11. #include <GLES/gl.h>
  12. #endif
  13. #ifdef USE_GLES20
  14. #include <GLES2/gl2.h>
  15. #endif
  16. EglUtilsGles::EglUtilsGles() : EglUtils()
  17. {
  18. }
  19. EglUtilsGles::~EglUtilsGles()
  20. {
  21. }
  22. void EglUtilsGles::clear( float r, float g, float b) {
  23. glClearColor( r,g,b, 0.0f );
  24. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  25. }
  26. int EglUtilsGles::loadTexture(const char *fn)
  27. {
  28. // clear previous error
  29. glGetError();
  30. char testr[256];
  31. sprintf(testr, ":/%s", fn );
  32. QString filename = QString( testr );
  33. QImage image = QImage( filename );
  34. GLuint texture;
  35. glGenTextures(1, &texture);
  36. glBindTexture(GL_TEXTURE_2D, texture);
  37. GLuint* pTexData = new GLuint[image.width() * image.height()];
  38. GLuint* sdata = (GLuint*)image.bits();
  39. GLuint* tdata = pTexData;
  40. for (int y=0; y<image.height(); y++)
  41. for (int x=0; x<image.width(); x++) {
  42. //*tdata = *sdata;
  43. *tdata = ((*sdata&255) << 16) | (((*sdata>>8)&255) << 8)
  44. | (((*sdata>>16)&255) << 0) | (((*sdata>>24)&255) << 24);
  45. sdata++;
  46. tdata++;
  47. };
  48. #ifdef USE_GLES20
  49. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
  50. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  51. #else
  52. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  53. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  54. #endif
  55. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 0,
  56. GL_RGBA, GL_UNSIGNED_BYTE, pTexData);
  57. #ifdef USE_GLES20
  58. glGenerateMipmap(GL_TEXTURE_2D);
  59. #endif
  60. #ifndef NDEBUG
  61. GLint error = glGetError();
  62. if(error)
  63. {
  64. qDebug("error: texture upload returned %x",error);
  65. }
  66. #endif
  67. delete [] pTexData;
  68. return texture;
  69. }
  70. void EglUtilsGles::releaseTexture(int textureHandle)
  71. {
  72. if(textureHandle) glDeleteTextures(1, (const GLuint *)&textureHandle);
  73. }