ICT290 / src / engine / textureManager.cpp
textureManager.cpp
Raw
#include "textureManager.h"

std::vector<Texture> TextureManager::textureList{};
Texture TextureManager::defaultTexture{};

bool TextureManager::init(const std::string& fileName) {
    bool success = false;
    std::ifstream fileIn;
    std::string textureName;

    fileIn.open(fileName);

    if (fileIn.is_open()) {
        // file has been opened, set return to true
        success = true;

        // while another line exists, read it in as a filename
        while (getline(fileIn, textureName)) {
            // load in a texture of the read in filename
            loadBMPTexture(textureName);
        }

        fileIn.close();
    }
    return success;
}

// code referenced from https://cplusplus.com/articles/GwvU7k9E/
unsigned int TextureManager::loadBMPTexture(const std::string& fileName,
                                            double angle) {
    Texture newTexture;
    BMP image;

    if (!image.readInBMP(TEXTURE_FILE_PATH + fileName)) {
        return 0;  // BMP class has failed to read in image at this filename
    }

    newTexture.fileName = fileName;
    newTexture.textureSize = image.getInfoHeader()->biSizeImage;
    newTexture.width = image.getInfoHeader()->biWidth;
    newTexture.height = image.getInfoHeader()->biHeight;

    // rotate the picture if required as BMP's are actually upside down
    if (angle != 0) {
        image.rotateBMP(angle);
    }

    /*******************GENERATING TEXTURES*******************/

    // get texture ID from openGL
    glGenTextures(1, &newTexture.ID);

    // get texture ID from openGL
    glBindTexture(GL_TEXTURE_2D, newTexture.ID);

    // Texture Clamping for skybox's
    if (newTexture.fileName.find("skybox") != std::string::npos) {
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    } else {
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    }

    glTexParameterf(GL_TEXTURE_2D,
                    GL_TEXTURE_MIN_FILTER,
                    GL_NEAREST_MIPMAP_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 3);
    glTexParameterf(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    float anisotropicFiltering{1.0f};
    glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &anisotropicFiltering);
    glTexParameterf(GL_TEXTURE_2D,
                    GL_TEXTURE_MAX_ANISOTROPY_EXT,
                    anisotropicFiltering);

    // Load the texture onto openGL
    glTexImage2D(GL_TEXTURE_2D,
                 0,
                 GL_RGB,
                 (GLsizei)newTexture.width,
                 (GLsizei)newTexture.height,
                 0,
                 GL_BGR,
                 GL_UNSIGNED_BYTE,
                 image.getPixels());

    // this requires an extension, not recognised
    // glGenerateMipmap(GL_TEXTURE_2D);

    textureList.push_back(newTexture);

    // Output a successful message
    std::cout << "Texture \"" << fileName << "\" successfully loaded.\n";

    return newTexture.ID;  // Return and ID > 0 on success
}

const Texture& TextureManager::getTexture(const unsigned int ID) {
    for (unsigned int i = 0; i < textureList.size(); i++) {
        if (textureList[i].ID == ID)
            return textureList[i];
    }
    return defaultTexture;
}

const Texture& TextureManager::getTexture(const std::string& textureName) {
    for (unsigned int i = 0; i < textureList.size(); i++) {
        if (textureList[i].fileName == textureName)
            return textureList[i];
    }
    return defaultTexture;
}

unsigned int TextureManager::getTextureID(const std::string& textureName) {
    for (unsigned int i = 0; i < textureList.size(); i++) {
        if (textureList[i].fileName == textureName)
            return textureList[i].ID;
    }
    return DEFAULTID;
}

// end textureManager.cpp