Exemple #1
0
void DestroyMesh(struct Mesh *mesh)
{
	if (mesh != NULL)
	{
		if (mesh->vertices != NULL)
			free(mesh->vertices);

		if (mesh->triangles != NULL)
			free(mesh->triangles);

		if (mesh->normals != NULL)
			free(mesh->normals);

		if (mesh->uvcoords != NULL)
			free(mesh->uvcoords);

		if (mesh->materials != NULL)
		{
			for (int i = 0; i < mesh->num_materials; i++)
				DestroyMaterial(&mesh->materials[i]);

			free(mesh->materials);
		}

		free(mesh);
		mesh = NULL;
	}
}
//-----------------------------------------------------------------------------
// Tests dynamic buffers
//-----------------------------------------------------------------------------
void CMaterialSystemTestApp::TestDynamicBuffers( IMatRenderContext *pMatRenderContext, bool bBuffered )
{
	CreateWireframeMaterial();

	g_pMaterialSystem->BeginFrame( 0 );

	pMatRenderContext->Bind( m_pMaterial );
	IMesh *pMesh = pMatRenderContext->GetDynamicMesh( bBuffered );

	// clear (so that we can make sure that we aren't getting results from the previous quad)
	pMatRenderContext->ClearColor3ub( RandomInt( 0, 100 ), RandomInt( 0, 100 ), RandomInt( 190, 255 ) );
	pMatRenderContext->ClearBuffers( true, true );

	static unsigned char s_pColors[4][4] = 
	{
		{ 255,   0,   0, 255 },
		{   0, 255,   0, 255 },
		{   0,   0, 255, 255 },
		{ 255, 255, 255, 255 },
	};

	static int nCount = 0;

	const int nLoopCount = 8;
	float flWidth = 2.0f / nLoopCount;
	for ( int i = 0; i < nLoopCount; ++i )
	{
		CMeshBuilder mb;
		mb.Begin( pMesh, MATERIAL_TRIANGLES, 4, 6 );

		mb.Position3f( -1.0f + i * flWidth, -1.0f, 0.5f );
		mb.Normal3f( 0.0f, 0.0f, 1.0f );
		mb.Color4ubv( s_pColors[nCount++ % 4] );
		mb.AdvanceVertex();

		mb.Position3f( -1.0f + i * flWidth + flWidth, -1.0f, 0.5f );
		mb.Normal3f( 0.0f, 0.0f, 1.0f );
		mb.Color4ubv( s_pColors[nCount++ % 4] );
		mb.AdvanceVertex();

		mb.Position3f( -1.0f + i * flWidth + flWidth,  1.0f, 0.5f );
		mb.Normal3f( 0.0f, 0.0f, 1.0f );
		mb.Color4ubv( s_pColors[nCount++ % 4] );
		mb.AdvanceVertex();

		mb.Position3f( -1.0f + i * flWidth,  1.0f, 0.5f );
		mb.Normal3f( 0.0f, 0.0f, 1.0f );
		mb.Color4ubv( s_pColors[nCount++ % 4] );
		mb.AdvanceVertex();

		++nCount;

		mb.FastIndex( 0 );
		mb.FastIndex( 2 );
		mb.FastIndex( 1 );
		mb.FastIndex( 0 );
		mb.FastIndex( 3 );
		mb.FastIndex( 2 );

		mb.End( true );

		pMesh->Draw( );
	}

	++nCount;

	g_pMaterialSystem->EndFrame();
	g_pMaterialSystem->SwapBuffers();

	DestroyMaterial();
}