예제 #1
0
void LoadTextureAMT::processCPU_Texture2D(const RJNode& header)
{
    if(ji::hasChild(header, "images"))
    {
        const RJNode& images = ji::getChild(header, "images");
        if(images.is_array() && images.size() > 0)
        {
            m_target = GL_TEXTURE_2D;

            const RJNode& img = images.at(0);
            std::string imageFilePath = ji::read(img, "filePath", DV_STRING);

            // Fail loading if given extension is not supported or file is invalid.
            ImageReader* reader = getImageFileByExtension(imageFilePath);
            if(!reader || !reader->isValid())
            {
                utils::deleteAndNull(reader);
                return;
            }

            m_imageReaders[0] = reader;
            m_imageGLFormat = reader->getOpenGLInternalFormat();
        }
    }
}
예제 #2
0
void LoadTextureAMT::processCPU_CubeMap(const RJNode& header)
{
    if(ji::hasChild(header, "images"))
    {
        const RJNode& images = ji::getChild(header, "images");
        if(images.is_array() && images.size() > 0)
        {
            m_target = GL_TEXTURE_CUBE_MAP;

            int index = 0;
            for(const RJNode& img : images)
            {
                std::string targetStr = ji::read(img, "target", DV_STRING);
                ETextureTarget target = textureTarget::fromString(targetStr);
                std::string filePath = ji::read(img, "filePath", DV_STRING);

                ImageReader* reader = getImageFileByExtension(filePath);
                if(!reader || !reader->isValid())
                {
                    utils::deleteAndNull(reader);
                }

                switch(target)
                {
                case ETextureTarget::CubeMapNegativeX:
                    m_imageReaders[0] = reader;
                    break;
                case ETextureTarget::CubeMapPositiveX:
                    m_imageReaders[1] = reader;
                    break;
                case ETextureTarget::CubeMapNegativeY:
                    m_imageReaders[2] = reader;
                    break;
                case ETextureTarget::CubeMapPositiveY:
                    m_imageReaders[3] = reader;
                    break;
                case ETextureTarget::CubeMapNegativeZ:
                    m_imageReaders[4] = reader;
                    break;
                case ETextureTarget::CubeMapPositiveZ:
                    m_imageReaders[5] = reader;
                    break;
                default:
                    assert(0);
                    break;
                }
            }
        }
    }
}
예제 #3
0
C2DTextureResource::C2DTextureResource(const std::string &filePath, const int flags) : CTextureResource(filePath)
{
    GLenum iFormat;
    int levels;
	//uint32_t size;
    ImageReader *img;

    m_textureId = 0;
    m_target = GL_TEXTURE_2D;
    m_type = ETT_TEX2D;

    // Fail loading if given extension is not supported.
    if((img = getImageFileByExtension(filePath)) == 0) { return; }

    // Fail loading if loaded file was invalid.
    if(!img->isValid()) { return; }

    iFormat = img->getOpenGLInternalFormat(flags);

    glActiveTexture(GL_TEXTURE0);
    glGenTextures(1, &m_textureId);
    glBindTexture(m_target, m_textureId);

    if(flags & ETF_MIPMAPS)
    {
        levels = img->getLevelAmount();

		if(levels == 1)
		{
            // If need mipmaps and image has only base level, set number of mipmap levels to generate
            levels = gmath::imageLevelCount(img->getWidth(0), img->getHeight(0), 1);
		}
    }
    else
    {
        levels = 1;
    }

	glTexStorage2D(m_target, levels, iFormat, img->getWidth(0), img->getHeight(0));

    for(int i = 0; i < img->getLevelAmount(); i++)
    {
        if(img->isCompressedFormat())
        {
            glCompressedTexSubImage2D(m_target, i, 0, 0, img->getWidth(i), img->getHeight(i), img->getFormat(), img->getLevelSize(i), img->getData(i));
        }
        else
        {
            glTexSubImage2D(m_target, i, 0, 0, img->getWidth(i), img->getHeight(i), img->getFormat(), GL_UNSIGNED_BYTE, img->getData(i));
        }
    }

	glTexParameteri(m_target, GL_TEXTURE_BASE_LEVEL, 0);

	if((img->getLevelAmount() == 1) && (flags & ETF_MIPMAPS))
	{
        glGenerateMipmap(m_target);
	}

	if(flags & ETF_MIPMAPS)
	{
		glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexParameteri(m_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	}
    else
    {
        glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(m_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(m_target, GL_TEXTURE_MAX_LEVEL, 0);
	}

    // Anisotropy filtering
	float maximumAnistropy;
	glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maximumAnistropy);
	glTexParameterf(m_target, GL_TEXTURE_MAX_ANISOTROPY_EXT, maximumAnistropy);

	//size = approxSize(iFormat, img->getXBlocks(0), img->getYBlocks(0), 1, flags & ETF_MIPMAPS);

    glBindTexture(m_target, 0);

    delete img;

    m_loaded = true;
}