コード例 #1
0
void GenerateLOD::Init_Buffer(fbxsdk::FbxMesh *pMesh, Model &model)
{
	model.SetVertexCount(static_cast<int>(ControlP->size()));
	model.SetPolygonCount(static_cast<int>(Triangles->size()));

	// Create the VertexBuffer
	model.CreateVertextBuffer();

	IDirect3DVertexBuffer9 *g_pVB = model.GetVertexBuffer();

	CUSTOMVERTEX *pVertices;
	g_pVB->Lock(0, 0, (void **)&pVertices, 0);
	FbxLayerElementArrayTemplate<FbxVector2> *pUVarray = NULL;
	bool hasUV = pMesh->GetTextureUV(&pUVarray);
	if (!hasUV) {
		printf("Has No Textures!\n");
	}

	WORD count = 0;
	std::unordered_map<int, WORD> RemainPoints;
	FbxVector4 *pControlPoints = pMesh->GetControlPoints();
	for (std::unordered_map<int, Point>::iterator it = (*ControlP).begin(); it != (*ControlP).end(); ++it) {
		const FbxVector4 &P = pControlPoints[it->first];
		pVertices[count].x = static_cast<FLOAT>(P[0]);
		pVertices[count].y = static_cast<FLOAT>(P[1]);
		pVertices[count].z = static_cast<FLOAT>(P[2]);

		// uv coordinate
		pVertices[count].tu = static_cast<FLOAT>((pUVarray->GetAt(*it->second.uvSet.begin()))[0]);
		pVertices[count].tv = static_cast<FLOAT>((pUVarray->GetAt(*it->second.uvSet.begin()))[1]);

		RemainPoints[it->first] = count;
		++count;
	}
	g_pVB->Unlock();

	
	// Create the IndexBuffer
	model.CreateIndexBuffer();

	IDirect3DIndexBuffer9 *g_pIB = model.GetIndexBuffer();

	WORD *index;
	int cnt = 0;
	g_pIB->Lock(0, 0, (void **)&index, 0);
	for (std::unordered_map<int, Face>::iterator it = (*Triangles).begin(); it != (*Triangles).end(); ++it) {
		index[cnt++] = RemainPoints[it->second.points[0]];
		index[cnt++] = RemainPoints[it->second.points[1]];
		index[cnt++] = RemainPoints[it->second.points[2]];
	}
	g_pIB->Unlock();
}
コード例 #2
0
	Model * CreateModelFromObjFile( const std::string & filename ) {
		File *file = FileOpenForRead( filename );
		ParseState ps;
		while ( file->AtEnd() == false && ps.mode != Mode_Failed ) {
			string line = file->ReadLine();
			vector< Token > tokens = TokenizeString( line.c_str() );
			ps.ProcessLine( tokens );
		}
		delete file;
		if ( ps.mode == Mode_Failed ) {
			return NULL;
		}
		Model *m = new Model( filename );
		m->SetPrimitive( Primitive_Triangles );
		VertexBuffer & vb = m->GetVertexBuffer();
		vb.SetVarying( ps.varying );
		//Output( "model %s varying = %d", filename.c_str(), ps.varying );
		vb.SetData( (int)ps.vbdata.size() * sizeof( float ), &ps.vbdata[0] );
		m->GetIndexBuffer().SetData( (int)ps.ibdata.size() * sizeof( ushort ), &ps.ibdata[0] );
		return m;
	}