//----------------------------------------------------------------------------
void CollisionsBoundTree::Response (CRecord& record0, int t0,
    CRecord& record1, int t1, Intersector<float,Vector3f>*)
{
    CollisionsBoundTree* app = (CollisionsBoundTree*)TheApplication;

    // Mesh0 triangles that are intersecting change from blue to cyan.
    TriMesh* mesh = record0.GetMesh();
    VertexBufferAccessor vba(mesh);
    const int* indices = (int*)mesh->GetIndexBuffer()->GetData();
    int i0 = indices[3*t0];
    int i1 = indices[3*t0 + 1];
    int i2 = indices[3*t0 + 2];
    vba.TCoord<Float2>(0, i0) = app->mCyanUV;
    vba.TCoord<Float2>(0, i1) = app->mCyanUV;
    vba.TCoord<Float2>(0, i2) = app->mCyanUV;
    app->mRenderer->Update(mesh->GetVertexBuffer());

    // Mesh1 triangles that are intersecting change from red to yellow.
    mesh = record1.GetMesh();
    vba.ApplyTo(mesh);
    indices = (int*)mesh->GetIndexBuffer()->GetData();
    i0 = indices[3*t1];
    i1 = indices[3*t1 + 1];
    i2 = indices[3*t1 + 2];
    vba.TCoord<Float2>(0 ,i0) = app->mYellowUV;
    vba.TCoord<Float2>(0, i1) = app->mYellowUV;
    vba.TCoord<Float2>(0, i2) = app->mYellowUV;
    app->mRenderer->Update(mesh->GetVertexBuffer());

    // NOTE: See the comments in Wm5CollisionGroup.h about information that
    // is available from the Intersector<float,Vector3f> object.
}
//----------------------------------------------------------------------------
RawTerrainPage::RawTerrainPage (VertexFormat* vformat, int size,
						  float* heights, const Float2& origin, float spacing)
						  :
mSize(size),
mSizeM1(size - 1),
mHeights(heights),
mOrigin(origin),
mSpacing(spacing)
{
	// size = 2^p + 1, p <= 7
	assertion(size ==  3 || size ==  5 || size ==   9 || size == 17
		|| size == 33 || size == 65 || size == 129, "Invalid page size\n");

	mInvSpacing = 1.0f/mSpacing;

	// 创建地形页网格
	float ext = mSpacing*mSizeM1;
	TriMesh* mesh = StandardMesh(vformat).Rectangle(mSize, mSize, ext, ext);
	mVFormat = vformat;
	mVBuffer = mesh->GetVertexBuffer();
	mIBuffer = mesh->GetIndexBuffer();
	delete0(mesh);

	// 修改地形顶点数据
	VertexBufferAccessor vba(mVFormat, mVBuffer);
	int numVertices = mVBuffer->GetNumElements();
	for (int i = 0; i < numVertices; ++i)
	{
		int x = i % mSize;
		int y = i / mSize;
		vba.Position<Float3>(i) = Float3(GetX(x), GetY(y), GetHeight(i));
		vba.Normal<Float3>(i) = Float3(0.0f, 0.0f, 1.0f);
	}

	UpdateModelSpace(Renderable::GU_NORMALS);

	mUV01 = Float4(8.0f, 8.0f, 8.0f, 8.0f);
	mUV23 = Float4(8.0f, 8.0f, 8.0f, 8.0f);
	mUV4 = Float4(8.0f, 8.0f, 8.0f, 8.0f);

	mUV01Float = new0 ShaderFloat(1);
	mUV23Float = new0 ShaderFloat(1);
	mUV4Float = new0 ShaderFloat(1);

	SetUV0(Float2(mUV01[0], mUV01[1]));
	SetUV1(Float2(mUV01[2], mUV01[3]));
	SetUV2(Float2(mUV23[0], mUV23[1]));
	SetUV3(Float2(mUV23[2], mUV23[3]));
	SetUV4(Float2(mUV4[0], mUV4[1]));

	mTextureAlpha = new0 Texture2D(Texture::TF_A8R8G8B8, mSize, mSize, 1);
	mTextureDefault = DynamicCast<Texture2D>(ResourceManager::GetSingleton()
		.BlockLoad("Data/Images/Terrain/NiTu.dds"));
	assertion(mTextureDefault!=0, "Load texture %s failed.",
		"Data/Images/Terrain/NiTu.dds");
}
//----------------------------------------------------------------------------
RevolutionSurface::RevolutionSurface (Curve2f* curve, float xCenter,
    TopologyType topology, int numCurveSamples, int numRadialSamples,
    bool sampleByArcLength, bool outsideView, VertexFormat* vformat)
    :
    mCurve(curve),
    mXCenter(xCenter),
    mTopology(topology),
    mNumCurveSamples(numCurveSamples),
    mNumRadialSamples(numRadialSamples),
    mSin(0),
    mCos(0),
    mSamples(0),
    mSampleByArcLength(sampleByArcLength)
{
    ComputeSampleData();

    // The topology of the meshes is all that matters.  The vertices will be
    // modified later based on the curve of revolution.
    StandardMesh stdmesh(vformat, !outsideView);
    TriMesh* mesh = 0;
    switch (mTopology)
    {
    case REV_DISK_TOPOLOGY:
        mesh = stdmesh.Disk(mNumCurveSamples, mNumRadialSamples, 1.0f);
        break;
    case REV_CYLINDER_TOPOLOGY:
        mesh = stdmesh.Cylinder(mNumCurveSamples, mNumRadialSamples, 1.0f,
            1.0f, true);
        break;
    case REV_SPHERE_TOPOLOGY:
        mesh = stdmesh.Sphere(mNumCurveSamples, mNumRadialSamples, 1.0f);
        break;
    case REV_TORUS_TOPOLOGY:
        mesh = stdmesh.Torus(mNumCurveSamples, mNumRadialSamples, 1.0f,
            0.25f);
        break;
    default:
        assertion(false, "Unexpected condition\n");
        break;
    }
    assertion(mesh != 0, "Failed to construct mesh\n");

    mVFormat = vformat;
    mVBuffer = mesh->GetVertexBuffer();

    // Generate the actual surface by replacing the vertex values.  NOTE:
    // Setting mIBuffer to zero acts as a flag that tells UpdateSurface
    // *not* to call Renderer::UpdateVertexBuffer(mVBuffer).  Only when the
    // application has constructed a RevolutionSurface wlil the update occur.
    mIBuffer = 0;
    UpdateSurface();

    mIBuffer = mesh->GetIndexBuffer();
    delete0(mesh);
}
//----------------------------------------------------------------------------
RawTerrainPage::RawTerrainPage (VertexFormat* vformat, int size,
	float* heights, const Float2& origin, float spacing)
	:
TerrainPage(size, heights, origin, spacing)
{
	float ext = mSpacing*mSizeM1;
	TriMesh* mesh = StandardMesh(vformat).Rectangle(mSize, mSize, ext, ext);
	mVFormat = vformat;
	mVBuffer = mesh->GetVertexBuffer();
	mIBuffer = mesh->GetIndexBuffer();
	delete0(mesh);

	VertexBufferAccessor vba(mVFormat, mVBuffer);
	int numVertices = mVBuffer->GetNumElements();
	for (int i = 0; i < numVertices; ++i)
	{
		int x = i % mSize;
		int y = i / mSize;
		vba.Position<Float3>(i) = Float3(GetX(x), GetY(y), GetHeight(i));
		vba.Normal<Float3>(i) = Float3(0.0f, 0.0f, 1.0f);
		vba.Color<Float3>(0, i) = Float3(0.0f, 0.0f, 0.0f);
	}

	UpdateModelSpace(Renderable::GU_NORMALS);

	mUV01 = Float4(8.0f, 8.0f, 8.0f, 8.0f);
	mUV23 = Float4(8.0f, 8.0f, 8.0f, 8.0f);
	mUV4 = Float4(8.0f, 8.0f, 8.0f, 8.0f);

	mUV01Float = new0 ShaderFloat(1);
	mUV23Float = new0 ShaderFloat(1);
	mUV4Float = new0 ShaderFloat(1);

	SetUV0(Float2(mUV01[0], mUV01[1]));
	SetUV1(Float2(mUV01[2], mUV01[3]));
	SetUV2(Float2(mUV23[0], mUV23[1]));
	SetUV3(Float2(mUV23[2], mUV23[3]));
	SetUV4(Float2(mUV4[0], mUV4[1]));

	mTextureAlpha = new0 Texture2D(Texture::TF_A8R8G8B8, mSize, mSize, 1);
	mTextureDefaultFilename = "Data/engine/grass.png";
}