vitimiti

Function

Jul 24th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. GLuint MatVGLTexture::loadTextureFromImage(std::string file)
  2. {
  3.     // The enumeration for GL errors.
  4.     GLenum error = GL_NO_ERROR;
  5.  
  6.     // Check the very last part of the file name for the extension.
  7.     std::size_t extension = file.find_last_of(".");
  8.     std::string extensionStr = file.substr(extension + 1);
  9.  
  10.     // Now if the file is a BMP.
  11.     if (extensionStr == "BMP" || extensionStr == "bmp")
  12.         this->m_surface = SDL_LoadBMP(file.c_str()); // Use this function.
  13.     else
  14.         this->m_surface = IMG_Load(file.c_str()); // Else, use SDL_Image.
  15.  
  16.     this->m_assertion.assert(false, this->m_surface != nullptr, IMG_GetError());
  17.  
  18.     // Create an OpenGL texture.
  19.     glGenTextures(1, &this->m_textureID);
  20.     error = glGetError();
  21.     this->m_assertion.assert(false, error == GL_NO_ERROR,
  22.                              gluErrorString(error));
  23.  
  24.     // Now bind the texture.
  25.     glBindTexture(GL_TEXTURE_2D, this->m_textureID);
  26.     error = glGetError();
  27.     this->m_assertion.assert(false, error == GL_NO_ERROR,
  28.                              gluErrorString(error));
  29.  
  30.     // Give the image to OpenGL so that it can be used.
  31.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
  32.                  this->m_surface->w, this->m_surface->h,
  33.                  0, GL_RGB, GL_UNSIGNED_BYTE,
  34.                  this->m_surface->pixels);
  35.     error = glGetError();
  36.     this->m_assertion.assert(false, error == GL_NO_ERROR,
  37.                              gluErrorString(error));
  38.  
  39.     // Now we can free the SDL2 surface.
  40.     SDL_FreeSurface(this->m_surface);
  41.     this->m_surface = nullptr;
  42.  
  43.     // Good filtering.
  44.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  45.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  46.     error = glGetError();
  47.     this->m_assertion.assert(false, error == GL_NO_ERROR,
  48.                              gluErrorString(error));
  49.  
  50.     // Get the texture ID.
  51.     return this->m_textureID;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment