C++三维模型io、二进制文件读取(badbit)和#DEN问题



我目前正在为我的路径跟踪器开发基础框架。我试图创建一个简单的二进制模型加载程序(读写),但在读回文件时遇到了一个问题。提供更多上下文:

写入我的数据似乎进展顺利,即没有出现异常,文件大小似乎也不错。当我开始读取数据时,一切似乎都很顺利,直到某个点(在读取所有顶点位置和一些法线之后)我的数据被破坏。当我调试时,所有的数字都是正确的,但很明显,在某个时刻,我得到了#DEN,而不是正确的浮点值。在某个点上,我的所有网格都会发生同样的情况,所以在读取UV或三角形等时可能会发生这种情况。

(图像#DEN处于第54个正常(第3个浮动)。。。声誉不足)

我一口气阅读了所有的法线(对于1个子网格)。当我查看ifstream时,它会重置回文件的开头。当我开始读取三角形索引(一个新的stream.read())时,坏比特就设置好了。

我使用Visual studio 2010 Ultimate,运行的是Windows 7 Professional x64。我正在使用C和C++。

我将尝试只显示关键代码:

if(meshHeader.numberOfNormals > 0)
{           
    Normal* _normals = Read<Normal>(meshHeader.numberOfNormals);
    if(_normals)
    {
        // Copy from array to vector
        _normalsVec = new vector<Normal>(_normals, _normals + meshHeader.numberOfNormals);
        // Cleanup              
        delete[] _normals;
    }
    else
    {
        // Cleanup
        delete[] _normals;
        throw runtime_error("Failed reading vertex normals");
    }
}

读取方法:

template <typename T> 
T* BinaryModelIO::Read(unsigned int _size)
{
    T* _data = new T[_size];
    unsigned int _numberOfBytes = Read(sizeof(T) * _size, static_cast<void*>(_data));
    if(_numberOfBytes != (sizeof(T) * _size))
    {
        delete[] _data;
        _data = 0;
    }
    return _data;
}

(static_cast(_data)不是原因)更多阅读:

unsigned int BinaryModelIO::Read(unsigned int _numberOfBytes, void *_buffer)
{
    if(isWriter)
        return 0;
    // no fail bit set?
    if(!readStream->fail())
    {
        try
        {
            readStream->read((char*)_buffer, _numberOfBytes);
        }
        catch(exception &e)
        {
            cerr << e.what() << endl;
            //return 0;
        }
        return _numberOfBytes;
    }
    else
    {
        if(readStream->badbit)
            cerr << "badbit" << endl;
        else if(readStream->eofbit)
            cerr << "eofbit" << endl;
        return 0;
    }
}

读取法线会按预期进行,但经过一定量的浮动(法线)后会出错。我试着用for循环读取整个Normals数组,然后将所有元素复制到向量中,但这不是问题所在。读取三角形数据时(以类似方式完成),会设置坏比特(或读取三角形之前的某个位置)。在下面的图片中,我在阅读三角形时进行了调试,坏消息被打印出来。

(图像显示打印…没有足够的声誉)

我正在尝试读/写的数据结构不包含虚拟函数,全部由浮点组成(三角形除外,它有3个无符号int)。点(3个浮点)、UV(2个浮点),法线(3个浮动),三角形(3个无符号整数)。

编辑:以下是普通的定义

class Normal 
{
public:
    // Vector Public Methods
    Normal(float _x = 0.0f, float _y = 0.0f, float _z = 0.0f) : x(_x), y(_y), z(_z) {   }
        explicit Normal(const Vector3& _vector);
        explicit Normal(const Point& _point);
    // Overloaded Operators
    Normal operator+(const Normal& _norm) const 
    {
        return Normal(x + _norm.x, y + _norm.y, z + _norm.z);
    }   
    Normal& operator+=(const Normal& _norm) 
    {
        x += _norm.x; 
        y += _norm.y; 
        z += _norm.z;
        return *this;
    }
    Normal operator-(const Normal& _norm) const 
    {
        return Normal(x - _norm.x, y - _norm.y, z - _norm.z);
    }   
    Normal& operator-=(const Normal& _norm) 
    {
        x -= _norm.x;
        y -= _norm.y; 
        z -= _norm.z;
        return *this;
    }
    bool operator==(const Normal& _norm) const 
    {
        return x == _norm.x && y == _norm.y && z == _norm.z;
    }
    bool operator!=(const Normal& _norm) const 
    {
        return x != _norm.x || y != _norm.y || z == _norm.z;
    }
    Normal operator*(float _f) const 
    {
        return Normal(_f * x, _f * y, _f * z);
    }   
    Normal &operator*=(float _f) 
    {
        x *= _f; 
        y *= _f; 
        z *= _f;
        return *this;
    }
    Normal operator/(float _f) const 
    {
        // One division, 3 multiplications ^^
        float _inverse = 1.f / _f;
        return Normal(x * _inverse, y * _inverse, z * _inverse);
    }
    Normal &operator/=(float _f) 
    {
        float _inverse = 1.f / _f;
        x *= _inverse; 
        y *= _inverse; 
        z *= _inverse;
        return *this;
    }
    Normal operator-() const 
    {
        return Normal(-x, -y, -z);
    }
    float operator[](int _i) const 
    {
        // Return x, y or z
        return (&x)[_i];
    }   
    float &operator[](int _i) 
    {
        return (&x)[_i];
    }
    float LengthSquared() const 
    { 
        return x * x + y * y + z * z; 
    }
    float Length() const 
    { 
        return sqrtf(LengthSquared()); 
    }
    // Public Variables
    float x, y, z;
}; // 12 Byte

第2版:文件打开(在创建并销毁Loader对象时完成)

bool AbstractStream::OpenFile()
{
    bool _return = false;
    // We should open file to write or to read
    if(isWriter)
    {
        // Check if the file is already open
        if(!writeStream->is_open())
            writeStream->open(fileName, ios::in | ios::binary);
        if(!writeStream->fail())
            _return = true; // File could be opened
    }
    else
    {
        // Check if the file is already open
        if(!readStream->is_open())
            readStream->open(fileName, ios::in | ios::binary);
        if(!readStream->fail())
            _return = true; // File could be opened
    }
    return _return; // File could be opened
}

如果我忘了什么,请告诉我,因为这是我在stackerflow上的第一个问题

我真的希望有人能帮助我,因为我暂时回避了这个问题(处理渲染器的其他部分)。

提前感谢!

除了你以前的new/delete(避免指针),我想你的问题是写数据。我的建议是:试着写一个非(!)二进制文件,在编辑器中查看一下。

最新更新