Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GLuint MatVGLTexture::loadTextureFromImage(std::string file)
- {
- // The enumeration for GL errors.
- GLenum error = GL_NO_ERROR;
- // Check the very last part of the file name for the extension.
- std::size_t extension = file.find_last_of(".");
- std::string extensionStr = file.substr(extension + 1);
- // Now if the file is a BMP.
- if (extensionStr == "BMP" || extensionStr == "bmp")
- this->m_surface = SDL_LoadBMP(file.c_str()); // Use this function.
- else
- this->m_surface = IMG_Load(file.c_str()); // Else, use SDL_Image.
- this->m_assertion.assert(false, this->m_surface != nullptr, IMG_GetError());
- // Create an OpenGL texture.
- glGenTextures(1, &this->m_textureID);
- error = glGetError();
- this->m_assertion.assert(false, error == GL_NO_ERROR,
- gluErrorString(error));
- // Now bind the texture.
- glBindTexture(GL_TEXTURE_2D, this->m_textureID);
- error = glGetError();
- this->m_assertion.assert(false, error == GL_NO_ERROR,
- gluErrorString(error));
- // Give the image to OpenGL so that it can be used.
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
- this->m_surface->w, this->m_surface->h,
- 0, GL_RGB, GL_UNSIGNED_BYTE,
- this->m_surface->pixels);
- error = glGetError();
- this->m_assertion.assert(false, error == GL_NO_ERROR,
- gluErrorString(error));
- // Now we can free the SDL2 surface.
- SDL_FreeSurface(this->m_surface);
- this->m_surface = nullptr;
- // Good filtering.
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- error = glGetError();
- this->m_assertion.assert(false, error == GL_NO_ERROR,
- gluErrorString(error));
- // Get the texture ID.
- return this->m_textureID;
- }
Advertisement
Add Comment
Please, Sign In to add comment