无法将短片复制到动态数组C ++中

时间:2013-04-03 13:19:22

标签: c++ pointers binary fstream short

我一直在尝试将3D模型格式加载到C ++中,但我遇到了问题。一切顺利,除了短裤的指标数组......当我尝试加载一个模型时,阵列中的前2个空格是...如果我加载第二个模型;更多的短裤进一步错误进入模型数据......

这是我的头文件:

#ifndef MODELDATA_H
#define MODELDATA_H

#include <GL/glu.h>
#include <string>

using namespace std;


class modelData
{
    public:
    modelData();
    virtual ~modelData();

    short m_vert_count;
    short m_ind_count;
    short m_tid;
    GLfloat *m_verts;
    GLfloat *m_tex;
    GLint *m_ind;

    void loadModel(string input);
    void draw();

    protected:
    private:
};

#endif // MODELDATA_H

这是我的cpp文件:

#include "modelData.h"
#include <GL/glu.h>
#include <fstream>
#include <string.h>
#include <iostream>

using namespace std;

modelData::modelData()
{
    //ctor
    m_verts = NULL;
    m_tex = NULL;
    m_ind = NULL;
    m_ind_count = 0;
    m_vert_count = 0;
}

modelData::~modelData()
{
    //dtor
    delete[] m_verts;
    delete[] m_tex;
    delete[] m_ind;
    m_ind_count = 0;
    m_vert_count = 0;
}

void modelData::loadModel(string input)
{
    short temp;
    char *newInput = new char[input.size()+1];
    newInput[input.size()]=0;
    memcpy(newInput,input.c_str(),input.size());
    ifstream a_file( newInput,  ios::binary | ios::in );
    delete newInput;

    a_file.seekg( 0, ios::beg );
    if ( ! a_file.read( reinterpret_cast<char*>( & m_vert_count ), sizeof( short ) ) )
    {
        cout << "Error reading from file: can't attain vertex buffer size" << endl;
        return;
    }

    if ( ! a_file.read( reinterpret_cast<char*>( & m_ind_count ), sizeof( short ) ) )
    {
        cout << "Error reading from file: can't attain index buffer size" << endl;
        return;
    }

    m_verts = new GLfloat[m_vert_count];
    m_ind = new GLint[m_ind_count];
    m_tex = new GLfloat[m_vert_count];

    for (int i = 0; i < m_vert_count; i++)
    {
        if ( ! a_file.read( reinterpret_cast<char*>( & m_verts[i] ), sizeof( float ) ) )
        {
            cout << "Error reading from file: can't attain vertex buffer" << endl;
            return;
        }
    }

    for (int i = 0; i < ((m_vert_count)/3)*2; i++)
    {
        if ( ! a_file.read( reinterpret_cast<char*>( & m_tex[i] ), sizeof( float ) ) )
        {
            cout << "Error reading from file: can't attain texcoord buffer" << endl;
            return;
        }
    }
    for (int i = 0; i < m_ind_count; i++)
    {
        if ( ! a_file.read( reinterpret_cast<char*>( & m_ind[i] ), sizeof( short ) ) )
        {
            cout << "Error reading from file: can't attain index buffer" << endl;
            return;
        }
    }


    cout << m_vert_count << "   " << m_ind_count << endl;
    cout << "Model successfully loaded from " << input << "  ...Jolly good." << endl;

}

void modelData::draw()
{
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3,GL_FLOAT,0,m_verts);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2,GL_FLOAT,0,m_tex);
    glDrawElements(GL_TRIANGLES,m_ind_count,GL_UNSIGNED_INT,m_ind);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);

}

我希望代码不会太长或太乱,我真的不知道发生了什么......据我所知; m_verts []和m_tex []看起来很好,但是m_ind []每隔一段时间就会弹出错误的数字...我是不是错误地分配了数组?我不清理我应该做的事情吗?

哦,我不认为这会导致问题,但你会注意到我没有添加任何东西来进行大端或小端转换...这是因为我使用的是一台看似英特尔的机器总是假设小端,我的安卓游戏引擎使用小端...所以我不认为这是问题......

任何帮助或建议都会很棒。 谢谢你的阅读。

1 个答案:

答案 0 :(得分:1)

如果我能看到正确,m_ind被输入为GLint的数组,但您正在读它,好像它是short的数组一样。在大多数平台上,这意味着值的高两个字节几乎是随机的。

如果您的数据流实际上包含短片,这将是阅读它们的安全方式:

for (int i = 0; i < m_ind_count; i++)
{
    short val;
    if ( ! a_file.read( reinterpret_cast<char*>( & val ), sizeof( short ) ) )
    {
        cout << "Error reading from file: can't attain index buffer" << endl;
        return;
    }
    m_ind[i] = val;
}

一些不相关的说明:

o这个:

char *newInput = new char[input.size()+1];
newInput[input.size()]=0;
memcpy(newInput,input.c_str(),input.size());
ifstream a_file( newInput,  ios::binary | ios::in );
delete newInput;

只是一种非常复杂的说法:

ifstream a_file(input.c_str(), ios::binary | ios::in);

o我强烈建议您使用std::vector而不是手动动态分配和取消分配数组。