Beispiel #1
0
void SnowParticle::Render()
{
	if (m_device)
	{
		//设置状态
		m_device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
		//设置alpha混合因子
		m_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
		m_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
		m_device->SetRenderState(D3DRS_POINTSPRITEENABLE, true);
		m_device->SetRenderState(D3DRS_POINTSCALEENABLE, true);

		//m_device->SetRenderState(D3DRS_LIGHTING, false);
		//设置粒子大小
		//设置
		m_device->SetRenderState(D3DRS_POINTSIZE, FloatToDword(1.0f));
		m_device->SetRenderState(D3DRS_POINTSIZE_MIN, FloatToDword(0.00f));
		m_device->SetRenderState(D3DRS_POINTSCALE_A, FloatToDword(0.00f));
		m_device->SetRenderState(D3DRS_POINTSCALE_B, FloatToDword(0.00f));
		m_device->SetRenderState(D3DRS_POINTSCALE_C, FloatToDword(1.00f));

		//设置数据源和纹理
		m_device->SetStreamSource(0, m_vb, 0, sizeof(POINTVERTEX));
		m_device->SetFVF(D3DFVF_POINTVERTEX);
		

		POINTVERTEX *vertex = 0;
		//锁住顶点
		m_vb->Lock(0, 0, (void **)&vertex, 0);

		int numToRender = 0;
		for (std::vector<Particle>::iterator it = m_snowPartices.begin();
			it != m_snowPartices.end(); 
			it++)
		{
			if (it->isLive)
			{
				vertex->pos = it->position;
				vertex->color = it->color;
				numToRender++;
				vertex++;
			}
		}
		m_vb->Unlock();

		int numParticlePerTex = numToRender / m_snowTextures.size();

		for (int i = 0; i < m_snowTextures.size(); i++)
		{
			m_device->SetTexture(0, m_snowTextures[i]);
			m_device->DrawPrimitive(D3DPT_POINTLIST, numParticlePerTex * i, numParticlePerTex);
		}
		int remainToRender = numToRender - numParticlePerTex * m_snowTextures.size();
		if (remainToRender > 0)
			m_device->DrawPrimitive(D3DPT_POINTLIST, 0, remainToRender);

		m_device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
	}
}
Beispiel #2
0
void Balls::Render()
{
	// Set render state
	device->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
	device->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ONE );
	device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
	device->SetRenderState( D3DRS_POINTSPRITEENABLE, TRUE) ;
	device->SetRenderState( D3DRS_POINTSCALEENABLE, TRUE) ;
	device->SetRenderState( D3DRS_POINTSIZE,	 FloatToDword(0.5f) );
	device->SetRenderState( D3DRS_POINTSIZE_MIN, FloatToDword(0.00f) );
	device->SetRenderState( D3DRS_POINTSCALE_A,  FloatToDword(0.00f) );
	device->SetRenderState( D3DRS_POINTSCALE_B,  FloatToDword(0.00f) );
	device->SetRenderState( D3DRS_POINTSCALE_C,  FloatToDword(1.00f) );

	// Set texture
	device->SetTexture(0, texture) ;
	device->SetStreamSource( 0, pVB, 0, sizeof(POINTVERTEX));
	device->SetFVF(D3DFVF_POINTVERTEX) ;

	// Start at beginning if we reach the end of vb
	if(vbOffset >= vbSize)
		vbOffset = 0 ;

	POINTVERTEX* v ;

	pVB->Lock(
		vbOffset * sizeof(POINTVERTEX),
		vbBatchSize * sizeof(POINTVERTEX),
		(void**)&v,
		vbOffset ? D3DLOCK_NOOVERWRITE : D3DLOCK_DISCARD
		) ;

	DWORD numParticleinBatch = 0 ;

	for (vector<Particle>::iterator citor = buffer.begin(); citor != buffer.end(); ++citor)
	{
		if (citor->isLive) // Only draw live particles
		{
			v->pos = citor->position ;
			v->color = 0xff00ff00 ;
			v++ ;

			numParticleinBatch++ ;

			if (numParticleinBatch == vbBatchSize)
			{
				pVB->Unlock() ;
				device->DrawPrimitive( D3DPT_POINTLIST, vbOffset, vbBatchSize) ;
				
				vbOffset += vbBatchSize ;

				if (vbOffset >= vbSize)
					vbOffset = 0 ;

				pVB->Lock(
					vbOffset * sizeof(POINTVERTEX),
					vbBatchSize * sizeof(POINTVERTEX),
					(void**)&v,
					vbOffset ? D3DLOCK_NOOVERWRITE : D3DLOCK_DISCARD
					) ;

				numParticleinBatch = 0 ;
			}
		}
	}

	pVB->Unlock() ;

	// Render the left particles
	if (numParticleinBatch)
	{
		device->DrawPrimitive(
			D3DPT_POINTLIST,
			vbOffset,
			numParticleinBatch
			) ;
	}

	// Restore state
	device->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
}