我一直在尝试将 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[] 经常在数组中弹出错误的数字......我是否错误地分配了数组?我不是在清理我应该清理的东西吗?
哦,我不认为这是导致问题的原因,但你会注意到我没有添加任何东西来处理大端或小端转换......这是因为我使用的是英特尔机器,看起来总是假设小端和我的 android 游戏引擎使用小端......所以我不认为这是问题......
任何帮助或建议都会很棒。 感谢阅读。
最佳答案
如果我没看错的话,m_ind
的类型是 GLint
的数组,但您读入它时就好像它是 的数组一样短
。在大多数平台上,这意味着值的高两个字节几乎是随机的。
如果您的数据流实际上包含短裤,这将是读取它们的安全方式:
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
而不是手动动态分配和释放数组。
关于c++ - 无法将短裤复制到动态数组 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15788469/