【发布时间】:2016-02-21 02:37:04
【问题描述】:
我正在尝试从已预先格式化的文件中读取数据,以便将其直接传递到顶点缓冲区。当读取超过一定大小的文件(高于 1152 字节,但小于 92,160 字节)时,它会失败。它将文件中的一些值写入分配的内存,然后不设置其余值,将它们保留为值 -431602080,这似乎是表示干净内存的 CRT 值(SO Source)。它成功写入的值的数量似乎因文件大小而异。
我已调查我是否超出了分配限制,但似乎并非如此。我已检查以确保分配没有因其他原因而失败。我已将数据读入一个之前填充了值的向量,以查看它是否手动写入值 -431602080,但是之前的值仍然表明它只是无法写入内存。
void Mesh::LoadMeshFromFile(DXManager* dxPtr, LPCSTR fileName, int ID)
{
std::fstream f(fileName, std::ios_base::in);
short vertexStride = sizeof(Vertex); //Gets the stride per vertex
long fileLength = f.seekg(0, std::ios::end).tellg(); //Gets the byte count
float vertexCount = (float)fileLength / (float)vertexStride; //Calculates the vertex count
if (vertexCount == (int)vertexCount) //Ensures there are no incomplete vertices
{
if (vertexCount > 134217727 || vertexCount * vertexStride > 2147483647)
{
//Alloc will definitely fail.
//Throw an error
}
else
{
char* vertices = new __nothrow char[(int)fileLength];
if (vertices == NULL)
{
//Allocation failed
//Throw an error
}
else
{
//Read all vertices directly into the array and build the buffer
f.seekg(0, std::ios::beg);
f.read(vertices, fileLength);
BuildBuffer(dxPtr, (Vertex*)vertices, vertexCount);
}
}
}
else
{
//1 or more incomplete vertices.
}
}
下面是一个示例输出。某些文件不会出现此问题,并且似乎与文件大小有关。发生数据丢失的位置不是恒定的,而且似乎也因文件大小而异。
如您所见,数据在第 4 个条目之前有效,部分通过该行。 希望有人能够阐明正在发生的事情。
【问题讨论】:
-
您正在使用浮点数...为什么?在大多数情况下,当然长的更好......
-
数据需要小数,用于基于 FLOAT typedef 的 XMMATRIX 操作。
-
您使用的是 float 而不是 FLOAT...这值得检查吗?
标签: c++ directx directx-11