45 lines · 1.5 KB · C++
RawDownload
////  TextureCache.cpp//  interface////  Created by Andrzej Kapolka on 8/6/13.//  Copyright (c) 2013 High Fidelity, Inc. All rights reserved. #include <glm/gtc/random.hpp> #include "TextureCache.h" TextureCache::TextureCache() : _permutationNormalTextureID(0) {} TextureCache::~TextureCache() {    if (_permutationNormalTextureID != 0) {        glDeleteTextures(1, &_permutationNormalTextureID);    }} GLuint TextureCache::getPermutationNormalTextureID() {    if (_permutationNormalTextureID == 0) {        glGenTextures(1, &_permutationNormalTextureID);        glBindTexture(GL_TEXTURE_2D, _permutationNormalTextureID);                // the first line consists of random permutation offsets        unsigned char data[256 * 2 * 3];        for (int i = 0; i < 256 * 3; i++) {            data[i] = rand() % 256;        }        // the next, random unit normals        for (int i = 256 * 3; i < 256 * 3 * 2; i += 3) {            glm::vec3 randvec = glm::sphericalRand(1.0f);            data[i] = ((randvec.x + 1.0f) / 2.0f) * 255.0f;            data[i + 1] = ((randvec.y + 1.0f) / 2.0f) * 255.0f;            data[i + 2] = ((randvec.z + 1.0f) / 2.0f) * 255.0f;        }        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, data);        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);                glBindTexture(GL_TEXTURE_2D, 0);    }    return _permutationNormalTextureID;}