Beispiel #1
0
void								CreateScreenQuad(
	MeshData&							Quad)
{
	AttribData<vec3> Vertices(4);
	Vertices.Set(0, vec3(-1.0f, -1.0f,  0.0f));
	Vertices.Set(1, vec3( 1.0f, -1.0f,  0.0f));
	Vertices.Set(2, vec3( 1.0f,  1.0f,  0.0f));
	Vertices.Set(3, vec3(-1.0f,  1.0f,  0.0f));

	AttribData<vec2> TexCoords(4);
	TexCoords.Set(0, vec2(0.0f, 0.0f));
	TexCoords.Set(1, vec2(1.0f, 0.0f));
	TexCoords.Set(2, vec2(1.0f, 1.0f));
	TexCoords.Set(3, vec2(0.0f, 1.0f));

	AttribData<GLFace16> Faces(2);
	Faces.Set(0, GLFace16(0, 1, 2) );
	Faces.Set(1, GLFace16(2, 3, 0) );
	
	Polygons Polylist1;
	Polylist1.SetMaterial(0);
	Polylist1.SetFaces(Faces);

	AttribData<poly> Polygons(1);
	Polygons.Set(0, Polylist1);

	Quad.SetVertexArray(Vertices);
	Quad.SetUVArray(TexCoords);
	Quad.SetPolygonList(Polygons);
	Quad.GenerateNormals(false);
	Quad.GenerateTangents();
}
Beispiel #2
0
void KRipper9::RipDIP(
    IDirect3DDevice9 *pDev, 
    D3DPRIMITIVETYPE Type, 
    INT BaseVertexIndex, 
    UINT MinIndex, 
    UINT NumVertices, 
    UINT StartIndex, 
    UINT PrimitiveCount
    )
{
    do
    {
        HRESULT hr;
        KRipHeader RipHeader;
        KFACES Faces( (DWORD)PrimitiveCount );
        KDumper::KInputVertexDeclaration  InputVertDecl;
        KDumper::KOutputVertexDeclaration OutputVertDecl;
        KMeshTextures2 MeshTextures;
        KMeshShaders2  MeshShaders;


        //////////////////////////////////////////////////////////////////////////
        // Primitive topology
        //////////////////////////////////////////////////////////////////////////
        KDumper::EPrimitiveType PrimType = D3DPRIMITIVETYPE_to_EPrimitiveType( Type );
        if( !KDumper::IsPrimitiveTypeSupported( PrimType ) )
        {
            g_pLog->Write( L"Input primitive topology not supported\n" );
            break;
        }
        g_pLog->Write( L"Input primitive topology: %s\n", KDumper::PrimTopology2Str( PrimType ) );


        //////////////////////////////////////////////////////////////////////////
        // Input/Output vertex declaration
        //////////////////////////////////////////////////////////////////////////
        hr = GetVertexDeclarations( pDev, InputVertDecl, OutputVertDecl );
        if( FAILED( hr ) )
        {
            g_pLog->Write( L"Input vertex declaration error: 0x%08X\n", hr );
            break;
        }
        KDumper::DumpInputVertexDeclaration2Log( InputVertDecl );
        KDumper::DumpOutputVertexDeclaration2Log( OutputVertDecl );

        //////////////////////////////////////////////////////////////////////////
        // Index Buffer
        //////////////////////////////////////////////////////////////////////////
        hr = DumpIndexBuffer( 
            pDev, 
            &Faces, 
            PrimType, 
            StartIndex
            );
        if( FAILED( hr ) )
        {
            g_pLog->Write( L"Index buffer dump error: 0x%08X\n", hr );
            break;
        }


        //////////////////////////////////////////////////////////////////////////
        // Vertices
        //////////////////////////////////////////////////////////////////////////
        DWORD VertCnt = Faces.GetVertexCount();
        DWORD VertSize= OutputVertDecl.GetVertexSize();

        g_pLog->Write( L"Min/Max Index: %d %d\nVertSize=%d\n", Faces.MinIdx, Faces.MaxIdx, VertSize );

        KVERTICES Vertices( VertCnt, VertSize );


        hr = DumpVertexBuffer(
            pDev, 
            &Vertices,
            &Faces,
            InputVertDecl,
            OutputVertDecl,
            BaseVertexIndex
            );
        if( FAILED ( hr ) )
        {
            g_pLog->Write( L"Vertex buffer dump error: 0x%08X\n", hr );
            break;
        }


        SaveMeshTextures( pDev, MeshTextures );
        //SaveMeshShaders()

        KDumper::PrepareHeader(
            RipHeader, 
            InputVertDecl,
            Faces,
            Vertices, 
            MeshTextures, 
            MeshShaders 
            );


        //Save RIP file
        std::wstring RipFilePath = g_pIntruder->GetFrameMeshSavePath();

        hr = KDumper::SaveMeshToRIPFile2(
            RipFilePath, 
            RipHeader, 
            OutputVertDecl,
            MeshTextures, 
            MeshShaders, 
            Faces.pFaces, 
            Vertices.pVertices
            );

        if( SUCCEEDED( hr ) )
        {
            g_pLog->Write( L"Mesh saved: %s\n\n\n", RipFilePath.c_str() );
        }
        else
        {
            g_pLog->Write( L"Mesh save error: %s\n\n\n", RipFilePath.c_str() );
        }

        g_pIntruder->IncFrameMeshIdx();

    }
    while( FALSE );

}
Beispiel #3
0
Shape* StlMesh::CreateShape(const Transform &o2w,
							bool reverseOrientation,
							const ParamSet &params)
{
	string name = params.FindOneString("name", "'stlmesh'");
	string FileName = params.FindOneString("filename", "none");

	string subdivscheme = params.FindOneString("subdivscheme", "loop");

	int nsubdivlevels = params.FindOneInt("nsubdivlevels", 0);

	bool recenterMesh = params.FindOneBool("recenter_mesh", false);

	Mesh::MeshSubdivType subdivType;

	if (subdivscheme == "loop")
		subdivType = Mesh::SUBDIV_LOOP;
	else if (subdivscheme == "microdisplacement")
		subdivType = Mesh::SUBDIV_MICRODISPLACEMENT;
	else
	{
		SHAPE_LOG(name, LUX_WARNING,LUX_BADTOKEN) << "Subdivision type  '" << subdivscheme << "' unknown. Using \"loop\".";
		subdivType = Mesh::SUBDIV_LOOP;
	}

	SHAPE_LOG(name, LUX_INFO, LUX_NOERROR) << "Loading STL mesh file: '" << FileName << "'...";

	FILE* pFile = fopen(FileName.c_str(), "rb");

	if(!pFile)
	{
		SHAPE_LOG(name, LUX_ERROR, LUX_SYSTEM) << "Unable to read STL mesh file '" << FileName << "'";
		return NULL;
	}

	uint32_t uNFaces;

	std::vector<Point> Vertices;

	// Checking file format (ASCII or binary)
	bool bIsBinary = false;	

	do
	{
		fseek(pFile, 0, SEEK_END);

		uint32_t uLength = ftell(pFile);

		if (80 + sizeof(uint32_t) >= uLength)
			break;

		fseek(pFile, 80, SEEK_SET);		

		if (fread(&uNFaces, sizeof(uNFaces), 1, pFile) != 1)
			break;

		if (!uNFaces || uNFaces > MAX_STL_FILE_FACES)
			break;

		if (80 + sizeof(uint32_t) + uNFaces * (sizeof(float) * 3 * 4 + sizeof(uint16_t)) != uLength)
			break;

		bIsBinary = true;

	}while(false);	

	if(bIsBinary) // binary mode loader
	{
		fseek(pFile, 80, SEEK_SET);

		if(fread(&uNFaces, sizeof(uNFaces), 1, pFile) != 1)
		{
			fclose(pFile);
			SHAPE_LOG(name, LUX_ERROR, LUX_SYSTEM) << "Invalid STL mesh file '" << FileName << "'";
			return NULL;
		}

		Vertices.resize(uNFaces * 3);

		const size_t szFaceSize = sizeof(float) * 3 * 4 + sizeof(uint16_t);

		// Preloading file
		std::vector<uint8_t> Data(uNFaces * szFaceSize);

		if(fread(&Data[0], 1, Data.size(), pFile) != Data.size())
		{
			fclose(pFile);
			SHAPE_LOG(name, LUX_ERROR, LUX_SYSTEM) << "Invalid STL mesh file '" << FileName << "'";
			return NULL;
		}

		for(uint32_t i = 0 ; i < uNFaces ; i++)
		{
			const float* pBaseData = reinterpret_cast<float*>(&Data[0] + i * szFaceSize);

			Vertices[i*3 + 0].x = pBaseData[3];
			Vertices[i*3 + 0].y = pBaseData[4];
			Vertices[i*3 + 0].z = pBaseData[5];

			Vertices[i*3 + 1].x = pBaseData[6];
			Vertices[i*3 + 1].y = pBaseData[7];
			Vertices[i*3 + 1].z = pBaseData[8];

			Vertices[i*3 + 2].x = pBaseData[9];
			Vertices[i*3 + 2].y = pBaseData[10];
			Vertices[i*3 + 2].z = pBaseData[11];
		}

		fclose(pFile);
	}
	else // ASCII mode loader
	{
		// Reopening in text mode
		fclose(pFile);

		if(!(pFile = fopen(FileName.c_str(), "rt")))
		{
			SHAPE_LOG(name, LUX_ERROR, LUX_SYSTEM) << "Unable to read STL mesh file '" << FileName << "'";
			return NULL;
		}

		// Reading file
		char Token[128];

		uNFaces = 0;

		while(!feof(pFile))
		{
			if(fscanf(pFile, "%127s", Token) != 1)
			{
				fclose(pFile);
				SHAPE_LOG(name, LUX_ERROR, LUX_SYSTEM) << "Invalid STL mesh file '" << FileName << "'";
				return NULL;
			}
			
			Token[127] = 0;

			if(!stricmp(Token, "endsolid"))
				break;

			if(!stricmp(Token, "facet"))
				uNFaces++;
		}

		Vertices.resize(uNFaces * 3);

		// Rewinding file and skipping 'solid ...' header
		fseek(pFile, 0, SEEK_SET);
		fgets(Token, sizeof(Token), pFile);

		// Reading faces
		for(uint32_t i = 0 ; i < uNFaces ; i++)
		{
			fscanf(pFile, "%*s %*s %*f %*f %*f %*s %*s"); // facet normal [nx] [ny] [nz] outer loop

			if(	fscanf(	pFile,
						"%*s %f %f %f",
							&Vertices[i*3 + 0].x,
							&Vertices[i*3 + 0].y,
							&Vertices[i*3 + 0].z) != 3 || // vertex [v1] [v2] [v3]

				fscanf(	pFile,
						"%*s %f %f %f",
							&Vertices[i*3 + 1].x,
							&Vertices[i*3 + 1].y,
							&Vertices[i*3 + 1].z) != 3 || // vertex [v1] [v2] [v3]

				fscanf(	pFile,
						"%*s %f %f %f",
							&Vertices[i*3 + 2].x,
							&Vertices[i*3 + 2].y,
							&Vertices[i*3 + 2].z) != 3) // vertex [v1] [v2] [v3]
			{
				fclose(pFile);
				SHAPE_LOG(name, LUX_ERROR, LUX_SYSTEM) << "Invalid STL mesh file '" << FileName << "'";
				return NULL;
			}

			fscanf(pFile, "%*s %*s"); // endloop endfacet
		}

		fclose(pFile);
	}

	if (recenterMesh)
	{
		// Bringing bounding box center to (0,0,0)
		BBox bbox;

		for(size_t i = 0 ; i < Vertices.size() ; i++)
			bbox = Union(bbox, BBox(Vertices[i]));

		Vector centerV((bbox.pMin + bbox.pMax) * 0.5f);

		for(size_t i = 0 ; i < Vertices.size() ; i++)
			Vertices[i] -= centerV;
	}

	// Filling face indices
	vector<int> Faces(uNFaces * 3);

	for(uint32_t i = 0 ; i < uNFaces ; i++)
	{
		Faces[i*3 + 0] = i*3 + 0;
		Faces[i*3 + 1] = i*3 + 1;
		Faces[i*3 + 2] = i*3 + 2;
	}

	boost::shared_ptr<Texture<float> > displacementMap;

	return new Mesh(o2w, reverseOrientation, name, Mesh::ACCEL_AUTO,
					Vertices.size(), &Vertices[0], NULL, NULL, NULL, NULL, 1.f,
					Mesh::TRI_AUTO, uNFaces, &Faces[0],
					Mesh::QUAD_QUADRILATERAL, 0, NULL,
					subdivType, nsubdivlevels, displacementMap, 0.1f, 0.0f, true, false,
					false, false);
}