Beispiel #1
0
	void ParticlePool::BuildVertexData()
	{
		Matrix4 transform = matViewInv;
		Matrix3 localRot = transform.GetRotationMatrix();

		// 计算顶点法线信息
		Vector3f normal = localRot * Vector3f(0.0f, 0.0f, 1.0f);

		// 每帧构造新的顶点缓冲,用于渲染
		std::vector<Particle>::iterator iter = m_Particles.begin();
		for (unsigned int i=0; i<m_ActiveParticleCount; i++, iter++)
		{
			// 更新顶点颜色
			float new_color[16];
			for (int j=0; j<4; j++)
				memcpy(&(new_color[j*4]), iter->m_Color.GetArray(), sizeof(float) * 4);

			m_VertexBuffer->ModifyVertexData(VFormat_Color, 16 * i/*iter->m_VertexOffset*/, sizeof(float) * 16, new_color);

			Vector3f points[4] = { Vector3f(-0.5f, 0.5f, 0.0f),
				Vector3f(0.5f, 0.5f, 0.0f),
				Vector3f(-0.5f, -0.5f, 0.0f),
				Vector3f(0.5f, -0.5f, 0.0f) };

			float new_pos[12];
			float new_normal[12];

			// 生成billboard顶点的方向
			transform.SetPosition(iter->m_Position);

			Matrix3 rotMat = Matrix3::BuildRollRotationMatrix(iter->spin);
			for (int j=0; j<4; j++)
			{
				// 根据缩放等属性计算顶点位置信息
				Vector3f point = rotMat * (points[j] * iter->scale);
				//point.z = iter->m_ZOffset;
				point.x *= iter->m_ScreenScaleX;
				point.y *= iter->m_ScreenScaleY;

				point = transform * point;

				//point += iter->m_Position;
				memcpy(&new_pos[j*3], point.GetArray(), sizeof(float) * 3);

				memcpy(&new_normal[j*3], normal.GetArray(), sizeof(float) * 3);
			}

			m_VertexBuffer->ModifyVertexData(VFormat_Position, 12 * i/*iter->m_VertexOffset*/, sizeof(float) * 12, new_pos);
			m_VertexBuffer->ModifyVertexData(VFormat_Normal, 12 * i, sizeof(float) * 12, new_normal);

			float uv[] = { 0.0f, 1.0f,
				1.0f, 1.0f,
				0.0f, 0.0f,
				1.0f, 0.0f };
			m_VertexBuffer->ModifyVertexData(VFormat_Texcoord0, 8 * i, sizeof(float) * 8, uv);

		}

		// 更新索引缓冲数据,处理有效粒子数目变化的情况
		m_IndexBuffer->SetIndexSize(m_ActiveParticleCount * 2);
		iter = m_Particles.begin();
		for (unsigned int i=0; i<m_ActiveParticleCount; i++, iter++)
		{
			//unsigned int offset = iter->m_IndexOffset;
			unsigned int index[6] = { i * 4, 2 + i * 4, 1 + i * 4,
				2 + i * 4, 3 + i * 4, 1 + i * 4 };
			m_IndexBuffer->ModifyIndexData(6 * i, sizeof(unsigned int) * 6, index);
		}
	}