| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- // ----------------------------------------------------------------
- // From Game Programming in C++ by Sanjay Madhav
- // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
- //
- // Released under the BSD License
- // See LICENSE in root directory for full details.
- // ----------------------------------------------------------------
- #include "Texture.h"
- #include <SOIL/SOIL.h>
- #include <GL/glew.h>
- #include <SDL/SDL.h>
- Texture::Texture()
- :mTextureID(0)
- ,mWidth(0)
- ,mHeight(0)
- {
-
- }
- Texture::~Texture()
- {
-
- }
- bool Texture::Load(const std::string& fileName)
- {
- int channels = 0;
-
- unsigned char* image = SOIL_load_image(fileName.c_str(),
- &mWidth, &mHeight, &channels, SOIL_LOAD_AUTO);
-
- if (image == nullptr)
- {
- SDL_Log("SOIL failed to load image %s: %s", fileName.c_str(), SOIL_last_result());
- return false;
- }
-
- int format = GL_RGB;
- if (channels == 4)
- {
- format = GL_RGBA;
- }
-
- glGenTextures(1, &mTextureID);
- glBindTexture(GL_TEXTURE_2D, mTextureID);
-
- glTexImage2D(GL_TEXTURE_2D, 0, format, mWidth, mHeight, 0, format,
- GL_UNSIGNED_BYTE, image);
-
- SOIL_free_image_data(image);
-
- // Enable bilinear filtering
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-
- return true;
- }
- void Texture::Unload()
- {
- glDeleteTextures(1, &mTextureID);
- }
- void Texture::SetActive()
- {
- glBindTexture(GL_TEXTURE_2D, mTextureID);
- }
|