Ejemplo n.º 1
0
void FeatureDemo::initEnvMap(const std::string& name)
{
    mpEnvMap = createTextureFromFile(name, false, isSrgbFormat(mpDefaultFBO->getColorTexture(0)->getFormat()));
    if (mpEnvMap->getType() != Texture::Type::Texture2D)
    {
        logError("Environment map must be a 2D texture");
        mpEnvMap = nullptr;
    }
}
Ejemplo n.º 2
0
IPXdevLFont XdevLFontSystemRAI::createFromFontFile(IPXdevLFile& file) {
    assert(m_rai && " XdevLFontImpl::createFromFontFile: XdevLFontSystem not initialized.");

    std::vector<std::string> fileNames;
    xdl_int numberOfTextures = 1;
    xdl_uint fontSize = 0;

    XdevLString tmp;
    file->readString(tmp);
    file->readString(tmp);

    // Get font size.
    file->readString(tmp);
    std::stringstream ss(tmp.toString());
    ss >> fontSize;

    // Create the font instance.
    auto font = new XdevLFontRAI();
    font->setFontSize(fontSize);

    // Reset stringstream.
    ss.str("");
    ss.clear();

    file->readString(tmp);
    ss << tmp;
    ss >> numberOfTextures;


    IPXdevLTexture texture = nullptr;

    auto archive = file->getArchive();

    for(auto id = 0; id < numberOfTextures; id++) {
        XdevLFileName filename;
        file->readString(filename);

        // Did the user specify a external functions to create a texture out of an image file?
        if(createTextureFromFile) {
            auto file = archive->open(XdevLOpenForReadOnly(), XdevLFileName(filename));
            // Yes, they use that to create the texture.
            texture = createTextureFromFile(file.get());

        } else {
            // No, let's use the lodepng project import PNG files.
            std::vector<xdl_uint8> imageIn, imageOut;
            auto newFile = archive->open(XdevLOpenForReadOnly(), filename);

            imageIn.reserve(newFile->length());
            imageIn.resize(newFile->length());

            newFile->read(imageIn.data(), newFile->length());
            newFile->close();

            xdl_uint width, height;

            xdl_int error = lodepng::decode(imageOut, width, height, imageIn);
            if(error) {
                std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
                return std::shared_ptr<XdevLFontRAI>();
            }

            // This flipping is neccessary because the library flips the picture up side down.
            // Method is brute force and needs unessarly memory. It creates a vector the same size
            // and starts copying which is not effective at all.
            std::vector<xdl_uint8> vflipedimage(imageOut);
            for(xdl_uint y = 0; y != height; y++) {
                for(xdl_uint x = 0; x != width*4; x++) {
                    vflipedimage[x + y*width*4] = imageOut[x + (height - 1 - y)*width*4];
                }
            }

            texture = m_rai->createTexture();

            texture->init(width, height, XDEVL_RGBA, XDEVL_RGBA, vflipedimage.data());
            imageOut.clear();
            vflipedimage.clear();

        }
        if(texture == nullptr) {
            XDEVL_MODULEX_ERROR(XdevLFontSystemRAI, "XdevLFontImpl::importFromFontFile: Could not create texture: " << filename << std::endl);
            assert(texture && "FontTexture not created.");
        }

        // Setup the texture.
        texture->lock();
        texture->setTextureFilter(XDEVL_TEXTURE_MIN_FILTER, XDEVL_NEAREST);
        texture->setTextureFilter(XDEVL_TEXTURE_MAG_FILTER, XDEVL_NEAREST);
        texture->setTextureWrap(XDEVL_TEXTURE_WRAP_S, XDEVL_CLAMP_TO_EDGE);
        texture->setTextureWrap(XDEVL_TEXTURE_WRAP_T, XDEVL_CLAMP_TO_EDGE);
        texture->unlock();

        font->addFontTexture(texture);

    }

    calculateGlyphInformation(font, file);

    return std::shared_ptr<XdevLFontRAI>(font);
}