如果您遇到问题,请参考以下示例代码(注意:顶点是交错的,而不是像您的那样分开):
矢量二维:
#ifndef VECTOR2D_H_
#define VECTOR2D_H_
struct Vector2D
{
union
{
struct { float x,y; };
struct { float s,t; };
struct { float u,v; };
float e[2];
};
Vector2D(): x(0.0f),y(0.0f) {}
Vector2D(const float _x,const float _y): x(_x),y(_y) {}
};
#endif
矢量3D:
#ifndef VECTOR3D_H_
#define VECTOR3D_H_
struct Vector3D
{
union
{
struct { float x,y,z; };
struct { float s,t,r; };
float e[3];
};
Vector3D() :x(0.0f),y(0.0f),z(0.0f) {}
Vector3D(const float _x,const float _y,const float _z): x(_x),y(_y),z(_z) {}
};
#endif
顶点:
#ifndef VERTEX_H_
#define VERTEX_H_
#include "Vector2D.h"
#include "Vector3D.h"
struct Vertex
{
Vector3D pos;
Vector3D nrm;
Vector2D tex;
Vertex() {}
Vertex(const Vector3D& _pos,const Vector3D& _nrm,const Vector2D& _tex)
:pos(_pos),nrm(_nrm),tex(_tex) {}
};
#endif
网:
#ifndef MESH_H_
#define MESH_H_
#include <vector>
#include "Vertex.h"
#include <sstream>
struct MyMesh
{
std::vector<Vertex> verts;
unsigned numVerts;
void WriteOut(std::ostringstream& oss)
{
numVerts = verts.size();
oss.write((const char*)&numVerts,sizeof(numVerts));
unsigned totalSize = numVerts * sizeof(Vertex);
oss.write((const char*)verts.data(),totalSize);
}
void ReadIn(std::istringstream& iss)
{
iss.read((char*)&numVerts,sizeof(numVerts));
verts.resize(numVerts);
iss.read((char*)verts.data(),numVerts*sizeof(Vertex));
}
};
#endif
和一个测试 main.cpp:
#include "Mesh.h"
#include <sstream>
#include <fstream>
#include <iostream>
void PopulateMesh(MyMesh& mesh)
{
// Fill the mesh with some vertices
for(int i=0; i<3; ++i)
{
Vector3D tempVec(0.0f,i,0.0f);
Vector2D tempTex(0.0f,i);
Vertex temp(tempVec,tempVec,tempTex);
mesh.verts.push_back(temp);
}
}
void PrintMesh(const MyMesh& mesh)
{
for(auto i=0u; i<mesh.verts.size(); ++i)
{
std::cout << "Position: " << mesh.verts[i].pos.x << ' ' << mesh.verts[i].pos.y << ' ' << mesh.verts[i].pos.z << '\n';
std::cout << "Normal: " << mesh.verts[i].nrm.x << ' ' << mesh.verts[i].nrm.y << ' ' << mesh.verts[i].nrm.z << '\n';
std::cout << "Tex Coords: " << mesh.verts[i].tex.u << ' ' << mesh.verts[i].tex.v << "\n\n";
}
}
void WriteStreamToFile(std::ostringstream& oss)
{
std::ofstream fout;
fout.open("test.bin",std::ios_base::binary | std::ios_base::out);
if(fout.is_open())
{
fout.write(oss.str().c_str(),oss.str().size());
fout.close();
}
}
void ReadStreamFromFile(std::istringstream& iss)
{
std::string file;
std::ifstream fin;
fin.open("test.bin",std::ios_base::binary | std::ios_base::in | std::ios_base::_Nocreate);
if(fin.is_open())
{
std::getline(fin,file,'\x1A');
fin.close();
}
iss.str(file);
}
int main()
{
MyMesh outMesh;
unsigned numMeshes = 1;
PopulateMesh(outMesh);
PrintMesh(outMesh);
// Write to the stream
std::ostringstream oss(std::ios_base::binary | std::ios_base::out);
oss.write((const char*)&numMeshes,sizeof(numMeshes));
outMesh.WriteOut(oss);
WriteStreamToFile(oss);
std::istringstream iss(std::ios_base::binary | std::ios_base::in);
ReadStreamFromFile(iss);
// Read from the stream
iss.read((char*)&numMeshes,sizeof(numMeshes));
MyMesh inMesh;
inMesh.ReadIn(iss);
PrintMesh(inMesh);
return 0;
}