English 中文(简体)
C++ OpenGL TGA Failing
原标题:C++ OpenGL TGA Loading Failing

我正在通过装上TGA文档的开放式理论基础,作为3个物体的文本。 我早就能够把数据从TGA的头盔上载,但在我试图装上实际图像数据时,数据却失败。 我不敢肯定会发生错。 这里是我装货舱:

Header file:

    struct TGA_Header 
{
    GLbyte  ID_Length;
    GLbyte  ColorMapType;
    GLbyte  ImageType;
    // Color map specifications
    GLbyte firstEntryIndex[2];      
    GLbyte colorMapLength[2];
    GLbyte colorMapEntrySize;

    //image specification
    GLshort xOrigin;
    GLshort yOrigin;
    GLshort ImageWidth;
    GLshort ImageHeight;
    GLbyte  PixelDepth;
    GLbyte ImageDescriptor;
};

class Texture
{
public:
    Texture(string in_filename, string in_name = "");
    ~Texture();

public:
    unsigned short  width;
    unsigned short  height;
    unsigned int    length;
    unsigned char   type;   
    unsigned char   *imageData;
    unsigned int    bpp;
    unsigned int    texID;

    string          name;

    static vector<Texture *> textures;

private:
    bool loadTGA(string filename);
    bool createTexture(unsigned char *imageData, int width, int height, int type);

    void swap(unsigned char * ori, unsigned char * dest, GLint size);
    void flipImage(unsigned char * image, bool flipHorizontal, bool flipVertical, GLushort width, GLushort height, GLbyte bpp);
};

此处是装货单在装货单上的功能:

bool Texture::loadTGA(string filename)
{
    TGA_Header TGAheader;

    ifstream file( filename.data(), std::ios::in, std::ios::binary );

    //make sure the file was opened properly
    if (!file.is_open() )
        return false;

    if( !file.read( (char *)&TGAheader, sizeof(TGAheader) ) )
        return false;


    //make sure the image is of a type we can handle
    if( TGAheader.ImageType != 2 )
        return false;

    width = TGAheader.ImageWidth;
    height = TGAheader.ImageHeight;
    bpp = TGAheader.PixelDepth;

    if( width < 0   ||              // if the width or height is less than 0, than
        height <= 0 ||              // the image is corrupt
        (bpp != 24 && bpp != 32) )  // make sure we are of the correct bit depth
    {
        return false;
    }

    //check for an alpha channel
    GLuint type = GL_RGBA;
    if ( bpp == 24 )
        type = GL_RGB;

    GLuint bytesPerPixel = bpp / 8;

    //allocate memory for the TGA so we can read it
    GLuint imageSize = width * height * bytesPerPixel;
    imageData = new GLubyte[imageSize];

    if ( imageData == NULL )
        return false;

    //make sure we are in the correct position to load the image data
    file.seekg(-imageSize, std::ios::end);

    // if something when wrong, make sure we free up the memory
    //NOTE: It never gets past this point. The conditional always fails.
    if ( !file.read( (char *)imageData, imageSize ) )
    {
        delete imageData;

        return false;
    }

    //more code is down here, but it doesnt matter because it does not pass the above function
}

似乎装着一些数据,但数据却不断回落,无法成功。 非常感谢对原因的任何帮助。 如果它有点字眼,但我不敢肯定什么是或不是重要的。

UPDATE: So, I just rewrote the function. The ifsteam I was using, seemed to be the cause of the problem. Specifically, it would try and load far more bytes of data than I had entered. I don t know the cause of the behavior, but I ve listed my functioning code below. Thank you every one for your help.

最佳回答

因此,我从使用一种源头改为国际能源年。 如果是流,则试图装上比我在论点中列举的更远的 by。 这里是新的法典。 (注:它仍然需要选择。) 我认为,有一些未使用的变量环绕着,但完全可行。 再次感谢大家的帮助。

负责人档案:

    //struct to hold tga data
struct TGA_Header
{
    GLbyte  ID_Length;
    GLbyte  ColorMapType;
    GLbyte  ImageType;
    // Color map specifications
    GLbyte firstEntryIndex[2];      
    GLbyte colorMapLength[2];
    GLbyte colorMapEntrySize;

    //image specification
    GLshort xOrigin;
    GLshort yOrigin;
    GLshort ImageWidth;
    GLshort ImageHeight;
    GLbyte  PixelDepth;
    GLbyte ImageDescriptor;
};

class Texture
{
public:
    //functions
    Texture(string in_filename, string in_name = "");
    ~Texture();

public:
    //vars
    unsigned char   *imageData;
    unsigned int    texID;

    string          name;

    //temp global access point for accessing all loaded textures
    static vector<Texture *> textures;

private:
    //can add additional load functions for other image types
    bool loadTGA(string filename);
    bool createTexture(unsigned char *imageData, int width, int height, int type);

    void swap(unsigned char * ori, unsigned char * dest, GLint size);
    void flipImage(unsigned char * image, bool flipHorizontal, bool flipVertical, GLushort width, GLushort height, GLbyte bpp);
};

#endif

此处是装货单功能:

    bool Texture::loadTGA(string filename)
{
    //var for swapping colors
    unsigned char colorSwap = 0;

    GLuint type;
    TGA_Header TGAheader;

    FILE* file = fopen(filename.c_str(), "rb");

    unsigned char Temp_TGAheader[18];

    //check to make sure the file loaded
    if( file == NULL )
        return false;   

    fread(Temp_TGAheader, 1, sizeof(Temp_TGAheader), file);

    //pull out the relavent data. 2 byte data (short) must be converted
    TGAheader.ID_Length = Temp_TGAheader[0];
    TGAheader.ImageType = Temp_TGAheader[2];
    TGAheader.ImageWidth = *static_cast<unsigned short*>(static_cast<void*>(&Temp_TGAheader[12]));
    TGAheader.ImageHeight = *static_cast<unsigned short*>(static_cast<void*>(&Temp_TGAheader[14]));
    TGAheader.PixelDepth = Temp_TGAheader[16];


    //make sure the image is of a type we can handle
    if( TGAheader.ImageType != 2 || TGAheader.ImageWidth <= 0 || TGAheader.ImageHeight <= 0 )
    {
        fclose(file);
        return false;
    }

    //set the type
    if ( TGAheader.PixelDepth == 32 )
    {
        type = GL_RGBA;
    } 
    else if ( TGAheader.PixelDepth == 24 ) 
    {
        type = GL_RGB;
    }
    else 
    {
        //incompatable image type
        return false;
    }


    //remember bits != bytes. To convert we need to divide by 8
    GLuint bytesPerPixel = TGAheader.PixelDepth / 8;

    //The Memory Required For The TGA Data  
    unsigned int imageSize = TGAheader.ImageWidth * TGAheader.ImageHeight * bytesPerPixel;// Calculate 

    //request the needed memory
    imageData = new GLubyte[imageSize];

    if ( imageData == NULL ) // just in case
        return false;

    if( fread(imageData, 1, imageSize, file) != imageSize )
    {       
        //Kill it
        delete [] imageData;
        fclose(file);                                           
        return false;   
    }       

    fclose(file);

    for (unsigned int x = 0; x < imageSize; x +=bytesPerPixel)  
    {
        colorSwap = imageData[x];       
        imageData[x] = imageData[x + 2];        
        imageData[x + 2] = colorSwap;   
    }   

    createTexture( imageData, TGAheader.ImageWidth, TGAheader.ImageHeight, type );

    return true;
}
问题回答

问题可能取决于不支持压缩TGA的TGA算法。

确保你不会压制宗教大会,保证宗教大会的命令(无重要意义)源自于底层。

我通常与全球化学品统一分类标签制度合作,此时此刻,控制低温压缩并使下游分离。

I m不熟悉C++, s。

您是否确定这一行程file.seekg(-imageSize, 编:ios:end); 不应成为file.seekg(headerSize, 编:ios:start);?

从一开始就更有意义。

You should also check for ColorMapType != 0.

P.S. 这里的if(wid < 0 ́th hexachloroturt<=0 widtheck应改为<=





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签