[SOLVED] KTX loader - "Could not load texture" error

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
AlfAlien
Posts: 20
Joined: Tue Jan 28, 2014 2:08 pm

[SOLVED] KTX loader - "Could not load texture" error

Post by AlfAlien »

I try to implement KTX loader for Irrlicht. The implementation is limited: only supported format is ECF_ETC1 + loaded KTX must contain mipmaps.
Here is my code (please, note that i'm just beginning c++ programming :wink: ).

//CImageLoaderKTX.h

Code: Select all

 
#ifndef __C_IMAGE_LOADER_KTX_H_INCLUDED__
#define __C_IMAGE_LOADER_KTX_H_INCLUDED__
#include "IrrCompileConfig.h"
#if defined(_IRR_COMPILE_WITH_KTX_LOADER_)
#include "IImageLoader.h"
namespace irr
{
    namespace video
    {
        struct ktxHeader //header specification -> http://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/
        {
            c8 identifier[4];
            u32 endianness;
            u32 glType;
            u32 glTypeSize;
            u32 glFormat;
            u32 glInternalFormat;
            u32 glBaseInternalFormat;
            u32 pixelWidth;
            u32 pixelHeight;
            u32 pixelDepth;
            u32 numberOfArrayElements;
            u32 numberOfFaces;
            u32 numberOfMipmapLevels;
            u32 bytesOfKeyValueData;
        };
        class CImageLoaderKTX: public IImageLoader
        {
        public:
            virtual bool isALoadableFileExtension(const io::path&filename) const _IRR_OVERRIDE_;
            virtual bool isALoadableFileFormat(io::IReadFile*file) const _IRR_OVERRIDE_;
            virtual IImage*loadImage(io::IReadFile*file) const _IRR_OVERRIDE_;
        };
 
    }
}
#endif
#endif
 
 
//CImageLoaderKTX.cpp

Code: Select all

 
#include "CImageLoaderKTX.h"
#if defined(_IRR_COMPILE_WITH_KTX_LOADER_)
#include "IReadFile.h"
#include "os.h"
#include "CColorConverter.h"
#include "CImage.h"
#include "irrString.h"
namespace irr
{
    namespace video
    {
        bool CImageLoaderKTX::isALoadableFileExtension(const io::path&filename) const
        {
            return core::hasFileExtension(filename, "ktx");
        }
        bool CImageLoaderKTX::isALoadableFileFormat(io::IReadFile*file) const
        {
            //TODO -> didn't implement yet
            return true;
        }
 
        //loadImage -> I used CImageLoaderDDS::loadImage(io::IReadFile*file) as reference
        IImage*CImageLoaderKTX::loadImage(io::IReadFile*file) const
        {
            ktxHeader header;
            IImage*image = 0;
            u32 dataSize; //size of loaded image
 
            //read KTX header
            file->seek(0);
            file->read(&header, sizeof(ktxHeader));
 
            /*
             KTX files contain header + optional metadata + actual image data.
             We need to skip header + metadata (size=header.bytesOfKeyValueData) to read image data.
             */
            file->seek(sizeof(ktxHeader) + header.bytesOfKeyValueData);
 
            //the next line was taken from DDS loader - don't completely understand it
            dataSize = header.pixelWidth * header.pixelHeight * header.pixelDepth * 3; //3 bytes for R8G8B8 format
 
            //next -> we allocate data for actual image and read it
            u8*data = new u8[dataSize];
            file->read(data, dataSize);
 
            //we always assume that format is ECF_ETC1 + always have mipmaps
            image = new CImage(ECF_ETC1, core::dimension2d<u32>(header.pixelWidth, header.pixelHeight), data, true, true, true, true);
            return image;
        }
        IImageLoader*createImageLoaderKTX()
        {
            return new CImageLoaderKTX();
        }
    }
}
#endif
 
It compiles w/o errors but when I try to load sample KTX file I get error: "Could not load texture" + KTX texture name
Can anyone say what's wrong?
Last edited by AlfAlien on Tue Apr 15, 2014 7:05 pm, edited 1 time in total.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: KTX loader - "Could not load texture" error

Post by hendu »

Use the debugger luke ;) Your dataSize is 0 unless you tried to load an array.
AlfAlien
Posts: 20
Joined: Tue Jan 28, 2014 2:08 pm

Re: KTX loader - "Could not load texture" error

Post by AlfAlien »

What do you mean? dataSize is local variable - initially it can be any value (not only 0). If you know what is wrong, please, could you point it out.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: KTX loader - "Could not load texture" error

Post by hendu »

The spec says pixelDepth is 0 unless you have an array texture. Multiply by 0 = 0.
AlfAlien
Posts: 20
Joined: Tue Jan 28, 2014 2:08 pm

Re: KTX loader - "Could not load texture" error

Post by AlfAlien »

Thank you, very much! That's exactly what was wrong.
I definitely will learn to use debugger.
)
Post Reply