亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? psystem.cpp

?? Introduction to directx9 3d game programming 一書的源代碼
?? CPP
字號(hào):
//////////////////////////////////////////////////////////////////////////////////////////////////
// 
// File: pSystem.cpp
// 
// Author: Frank Luna (C) All Rights Reserved
//
// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0 
//
// Desc: Represents a general particle system.
//          
//////////////////////////////////////////////////////////////////////////////////////////////////

#include <cstdlib>
#include "pSystem.h"

using namespace psys;

const DWORD Particle::FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;

PSystem::PSystem()
{
	_device = 0;
	_vb     = 0;
	_tex    = 0;
}

PSystem::~PSystem()
{
	d3d::Release<IDirect3DVertexBuffer9*>(_vb);
	d3d::Release<IDirect3DTexture9*>(_tex);
}

bool PSystem::init(IDirect3DDevice9* device, char* texFileName)
{
	// vertex buffer's size does not equal the number of particles in our system.  We
	// use the vertex buffer to draw a portion of our particles at a time.  The arbitrary
	// size we choose for the vertex buffer is specified by the _vbSize variable.

	_device = device; // save a ptr to the device

	HRESULT hr = 0;

	hr = device->CreateVertexBuffer(
		_vbSize * sizeof(Particle),
		D3DUSAGE_DYNAMIC | D3DUSAGE_POINTS | D3DUSAGE_WRITEONLY,
		Particle::FVF,
		D3DPOOL_DEFAULT, // D3DPOOL_MANAGED can't be used with D3DUSAGE_DYNAMIC 
		&_vb,
		0);
	
	if(FAILED(hr))
	{
		::MessageBox(0, "CreateVertexBuffer() - FAILED", "PSystem", 0);
		return false;
	}

	hr = D3DXCreateTextureFromFile(
		device,
		texFileName,
		&_tex);

	if(FAILED(hr))
	{
		::MessageBox(0, "D3DXCreateTextureFromFile() - FAILED", "PSystem", 0);
		return false;
	}

	return true;
}

void PSystem::reset()
{
	std::list<Attribute>::iterator i;
	for(i = _particles.begin(); i != _particles.end(); i++)
	{
		resetParticle( &(*i) );
	}
}

void PSystem::addParticle()
{
	Attribute attribute;

	resetParticle(&attribute);

	_particles.push_back(attribute);
}

void PSystem::preRender()
{
	_device->SetRenderState(D3DRS_LIGHTING, false);
	_device->SetRenderState(D3DRS_POINTSPRITEENABLE, true);
	_device->SetRenderState(D3DRS_POINTSCALEENABLE, true); 
	_device->SetRenderState(D3DRS_POINTSIZE, d3d::FtoDw(_size));
	_device->SetRenderState(D3DRS_POINTSIZE_MIN, d3d::FtoDw(0.0f));

	// control the size of the particle relative to distance
	_device->SetRenderState(D3DRS_POINTSCALE_A, d3d::FtoDw(0.0f));
	_device->SetRenderState(D3DRS_POINTSCALE_B, d3d::FtoDw(0.0f));
	_device->SetRenderState(D3DRS_POINTSCALE_C, d3d::FtoDw(1.0f));
		
	// use alpha from texture
	_device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
	_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);

	_device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
	_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    _device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
}

void PSystem::postRender()
{
	_device->SetRenderState(D3DRS_LIGHTING,          true);
	_device->SetRenderState(D3DRS_POINTSPRITEENABLE, false);
	_device->SetRenderState(D3DRS_POINTSCALEENABLE,  false);
	_device->SetRenderState(D3DRS_ALPHABLENDENABLE,  false);
}

void PSystem::render()
{
	//
	// Remarks:  The render method works by filling a section of the vertex buffer with data,
	//           then we render that section.  While that section is rendering we lock a new
	//           section and begin to fill that section.  Once that sections filled we render it.
	//           This process continues until all the particles have been drawn.  The benifit
	//           of this method is that we keep the video card and the CPU busy.  

	if( !_particles.empty() )
	{
		//
		// set render states
		//

		preRender();
		
		_device->SetTexture(0, _tex);
		_device->SetFVF(Particle::FVF);
		_device->SetStreamSource(0, _vb, 0, sizeof(Particle));

		//
		// render batches one by one
		//

		// start at beginning if we're at the end of the vb
		if(_vbOffset >= _vbSize)
			_vbOffset = 0;

		Particle* v = 0;

		_vb->Lock(
			_vbOffset    * sizeof( Particle ),
			_vbBatchSize * sizeof( Particle ),
			(void**)&v,
			_vbOffset ? D3DLOCK_NOOVERWRITE : D3DLOCK_DISCARD);

		DWORD numParticlesInBatch = 0;

		//
		// Until all particles have been rendered.
		//
		std::list<Attribute>::iterator i;
		for(i = _particles.begin(); i != _particles.end(); i++)
		{
			if( i->_isAlive )
			{
				//
				// Copy a batch of the living particles to the
				// next vertex buffer segment
				//
				v->_position = i->_position;
				v->_color    = (D3DCOLOR)i->_color;
				v++; // next element;

				numParticlesInBatch++; //increase batch counter

				// if this batch full?
				if(numParticlesInBatch == _vbBatchSize) 
				{
					//
					// Draw the last batch of particles that was
					// copied to the vertex buffer. 
					//
					_vb->Unlock();

					_device->DrawPrimitive(
						D3DPT_POINTLIST,
						_vbOffset,
						_vbBatchSize);

					//
					// While that batch is drawing, start filling the
					// next batch with particles.
					//

					// move the offset to the start of the next batch
					_vbOffset += _vbBatchSize; 

					// don't offset into memory thats outside the vb's range.
					// If we're at the end, start at the beginning.
					if(_vbOffset >= _vbSize) 
						_vbOffset = 0;       

					_vb->Lock(
						_vbOffset    * sizeof( Particle ),
						_vbBatchSize * sizeof( Particle ),
						(void**)&v,
						_vbOffset ? D3DLOCK_NOOVERWRITE : D3DLOCK_DISCARD);

					numParticlesInBatch = 0; // reset for new batch
				}	
			}
		}

		_vb->Unlock();

		// its possible that the LAST batch being filled never 
		// got rendered because the condition 
		// (numParticlesInBatch == _vbBatchSize) would not have
		// been satisfied.  We draw the last partially filled batch now.
		
		if( numParticlesInBatch )
		{
			_device->DrawPrimitive(
				D3DPT_POINTLIST,
				_vbOffset,
				numParticlesInBatch);
		}

		// next block
		_vbOffset += _vbBatchSize; 

		//
		// reset render states
		//

		postRender();
	}
}

bool PSystem::isEmpty()
{
	return _particles.empty();
}

bool PSystem::isDead()
{
	std::list<Attribute>::iterator i;
	for(i = _particles.begin(); i != _particles.end(); i++)
	{
		// is there at least one living particle?  If yes,
		// the system is not dead.
		if( i->_isAlive )
			return false;
	}
	// no living particles found, the system must be dead.
	return true;
}

void PSystem::removeDeadParticles()
{
	std::list<Attribute>::iterator i;

	i = _particles.begin();

	while( i != _particles.end() )
	{
		if( i->_isAlive == false )
		{
			// erase returns the next iterator, so no need to
		    // incrememnt to the next one ourselves.
			i = _particles.erase(i); 
		}
		else
		{
			i++; // next in list
		}
	}
}

//*****************************************************************************
// Snow System
//***************

Snow::Snow(d3d::BoundingBox* boundingBox, int numParticles)
{
	_boundingBox   = *boundingBox;
	_size          = 0.25f;
	_vbSize        = 2048;
	_vbOffset      = 0; 
	_vbBatchSize   = 512; 
	
	for(int i = 0; i < numParticles; i++)
		addParticle();
}

void Snow::resetParticle(Attribute* attribute)
{
	attribute->_isAlive  = true;

	// get random x, z coordinate for the position of the snow flake.
	d3d::GetRandomVector(
		&attribute->_position,
		&_boundingBox._min,
		&_boundingBox._max);

	// no randomness for height (y-coordinate).  Snow flake
	// always starts at the top of bounding box.
	attribute->_position.y = _boundingBox._max.y; 

	// snow flakes fall downwards and slightly to the left
	attribute->_velocity.x = d3d::GetRandomFloat(0.0f, 1.0f) * -3.0f;
	attribute->_velocity.y = d3d::GetRandomFloat(0.0f, 1.0f) * -10.0f;
	attribute->_velocity.z = 0.0f;

	// white snow flake
	attribute->_color = d3d::WHITE;
}

void Snow::update(float timeDelta)
{
	std::list<Attribute>::iterator i;
	for(i = _particles.begin(); i != _particles.end(); i++)
	{
		i->_position += i->_velocity * timeDelta;

		// is the point outside bounds?
		if( _boundingBox.isPointInside( i->_position ) == false ) 
		{
			// nope so kill it, but we want to recycle dead 
			// particles, so respawn it instead.
			resetParticle( &(*i) );
		}
	}
}

//*****************************************************************************
// Explosion System
//********************

Firework::Firework(D3DXVECTOR3* origin, int numParticles)
{
	_origin        = *origin;
	_size          = 0.9f;
	_vbSize        = 2048;
	_vbOffset      = 0;   
	_vbBatchSize   = 512; 

	for(int i = 0; i < numParticles; i++)
		addParticle();
}

void Firework::resetParticle(Attribute* attribute)
{
	attribute->_isAlive  = true;
	attribute->_position = _origin;

	D3DXVECTOR3 min = D3DXVECTOR3(-1.0f, -1.0f, -1.0f);
	D3DXVECTOR3 max = D3DXVECTOR3( 1.0f,  1.0f,  1.0f);

	d3d::GetRandomVector(
		&attribute->_velocity,
		&min,
		&max);

	// normalize to make spherical
	D3DXVec3Normalize(
		&attribute->_velocity,
		&attribute->_velocity);

	attribute->_velocity *= 100.0f;

	attribute->_color = D3DXCOLOR(
		d3d::GetRandomFloat(0.0f, 1.0f),
		d3d::GetRandomFloat(0.0f, 1.0f),
		d3d::GetRandomFloat(0.0f, 1.0f),
		1.0f);

	attribute->_age      = 0.0f;
	attribute->_lifeTime = 2.0f; // lives for 2 seconds
}

void Firework::update(float timeDelta)
{
	std::list<Attribute>::iterator i;

	for(i = _particles.begin(); i != _particles.end(); i++)
	{
		// only update living particles
		if( i->_isAlive )
		{
			i->_position += i->_velocity * timeDelta;

			i->_age += timeDelta;

			if(i->_age > i->_lifeTime) // kill 
				i->_isAlive = false;
		}
	}
}

void Firework::preRender()
{
	PSystem::preRender();

	_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
    _device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

	// read, but don't write particles to z-buffer
	_device->SetRenderState(D3DRS_ZWRITEENABLE, false);
}

void Firework::postRender()
{
	PSystem::postRender();

	_device->SetRenderState(D3DRS_ZWRITEENABLE, true);
}

//*****************************************************************************
// Laser System
//****************

ParticleGun::ParticleGun(Camera* camera)
{
	_camera          = camera;
	_size            = 0.8f;
	_vbSize          = 2048;
	_vbOffset        = 0;  
	_vbBatchSize     = 512; 
}

void ParticleGun::resetParticle(Attribute* attribute)
{
	attribute->_isAlive  = true;

	D3DXVECTOR3 cameraPos;
	_camera->getPosition(&cameraPos);

	D3DXVECTOR3 cameraDir;
	_camera->getLook(&cameraDir);

	// change to camera position
	attribute->_position = cameraPos;
	attribute->_position.y -= 1.0f; // slightly below camera
	                         // so its like we're carrying a gun

	// travels in the direction the camera is looking
	attribute->_velocity = cameraDir * 100.0f;

	// green
	attribute->_color = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f);

	attribute->_age      = 0.0f; 
	attribute->_lifeTime = 1.0f; // lives for 1 seconds
}

void ParticleGun::update(float timeDelta)
{
	std::list<Attribute>::iterator i;

	for(i = _particles.begin(); i != _particles.end(); i++)
	{
		i->_position += i->_velocity * timeDelta;

		i->_age += timeDelta;

		if(i->_age > i->_lifeTime) // kill 
			i->_isAlive = false;
	}
	removeDeadParticles();
}

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美三级中文字幕| 不卡在线视频中文字幕| 91精品国产欧美一区二区| 日韩黄色一级片| 精品国产在天天线2019| 国产永久精品大片wwwapp | 欧美视频一区在线观看| 亚洲一级在线观看| 欧美一区二区高清| 国产精品一区二区91| 亚洲欧洲日韩av| 欧美性大战久久久久久久蜜臀 | 99国产欧美久久久精品| 一卡二卡三卡日韩欧美| 日韩一级欧美一级| 国产福利精品一区| 一区二区三区在线观看网站| 欧美日韩一二区| 国产精品影视在线| 国产精品电影院| 7777精品伊人久久久大香线蕉完整版| 九色综合狠狠综合久久| 国产精品电影一区二区三区| 欧美午夜精品久久久久久孕妇 | 香蕉久久一区二区不卡无毒影院| 在线91免费看| 成人av在线影院| 日韩电影一二三区| 欧美国产视频在线| 欧美精品自拍偷拍动漫精品| 狠狠色丁香久久婷婷综合丁香| 最新日韩在线视频| 日韩你懂的在线播放| 99久久久无码国产精品| 久久av资源网| 亚洲国产精品久久久久秋霞影院 | 欧美日韩不卡一区二区| 国产精品影音先锋| 日韩福利视频网| 中文字幕一区二区三区乱码在线| 欧美精品vⅰdeose4hd| 成人福利视频网站| 久久精品二区亚洲w码| 亚洲青青青在线视频| 久久久精品免费网站| 3d成人h动漫网站入口| 色婷婷综合五月| 成人av网站免费观看| 精品一区二区三区日韩| 一级日本不卡的影视| 国产精品短视频| 国产欧美在线观看一区| 欧美一区二区三区电影| 欧美亚洲一区二区三区四区| www.久久精品| 丁香一区二区三区| 国产精品资源在线看| 日本欧美一区二区在线观看| 亚洲一区二区视频| 亚洲蜜臀av乱码久久精品| 欧美国产精品中文字幕| 国产网站一区二区| 久久久影院官网| 亚洲精品在线网站| 欧美大肚乱孕交hd孕妇| 91精品国产黑色紧身裤美女| 欧美视频三区在线播放| 在线视频综合导航| 欧洲色大大久久| 欧美中文一区二区三区| 色婷婷亚洲精品| 色猫猫国产区一区二在线视频| 99久久综合狠狠综合久久| 成人黄色777网| 99久久综合国产精品| 99re这里都是精品| 色呦呦国产精品| 欧美日韩亚洲综合| 欧美电影影音先锋| 91精品国产综合久久婷婷香蕉| 欧美精品在线观看播放| 7777精品伊人久久久大香线蕉最新版 | 亚洲成人激情av| 图片区小说区国产精品视频| 亚洲www啪成人一区二区麻豆| 亚洲午夜免费福利视频| 日韩精品一二三区| 蜜桃av一区二区三区电影| 捆绑调教美女网站视频一区| 免费在线欧美视频| 国产一区在线观看麻豆| 丁香婷婷综合激情五月色| 成人午夜激情片| 在线视频观看一区| 在线综合视频播放| 精品国产乱码久久久久久牛牛| 国产欧美精品日韩区二区麻豆天美| 国产精品毛片高清在线完整版| 亚洲免费av在线| 日韩电影一区二区三区| 国产suv精品一区二区三区| 波多野结衣视频一区| 在线看国产日韩| 精品久久久久久亚洲综合网| 久久久久国产精品厨房| 1024亚洲合集| 麻豆一区二区三| 岛国av在线一区| 欧美三日本三级三级在线播放| 日韩欧美一级二级三级久久久| 久久久久久久久97黄色工厂| 日韩理论电影院| 奇米一区二区三区| 99在线精品免费| 日韩欧美你懂的| 亚洲精品一二三| 激情五月婷婷综合| 在线这里只有精品| 久久免费看少妇高潮| 亚洲制服丝袜av| 国产精品一品二品| 欧美久久久久免费| 最新国产成人在线观看| 免费成人av在线播放| 99在线视频精品| www国产精品av| 亚洲一区二区视频在线| 高清国产一区二区| 日韩欧美在线一区二区三区| ●精品国产综合乱码久久久久 | 亚洲成a人v欧美综合天堂下载| 国产美女精品在线| 678五月天丁香亚洲综合网| 国产精品水嫩水嫩| 国内精品免费在线观看| 欧美精品粉嫩高潮一区二区| 亚洲三级视频在线观看| 国产精品538一区二区在线| 欧美精品一二三| 一区二区三区欧美视频| 粉嫩高潮美女一区二区三区| 日韩亚洲欧美高清| 亚洲第一搞黄网站| 91美女在线看| 国产精品卡一卡二卡三| 国产福利一区二区三区视频在线| 91精品一区二区三区久久久久久| 亚洲黄色免费网站| www.欧美日韩| 国产精品传媒在线| 国产成人亚洲综合a∨婷婷| 精品国产一区二区三区不卡| 日韩精品一级二级 | 在线视频欧美区| 亚洲人妖av一区二区| 春色校园综合激情亚洲| 久久天堂av综合合色蜜桃网| 日韩黄色小视频| 88在线观看91蜜桃国自产| 亚洲第一狼人社区| 欧美日韩中文另类| 亚洲一区二区在线免费观看视频 | 日韩亚洲欧美成人一区| 青青草成人在线观看| 69堂成人精品免费视频| 五月激情综合网| 日韩欧美国产高清| 玖玖九九国产精品| 日韩免费高清av| 精品亚洲成a人| 久久久久久久久久久久久女国产乱| 极品少妇xxxx偷拍精品少妇| 久久视频一区二区| 成人丝袜高跟foot| 亚洲欧美日韩小说| 欧美在线高清视频| 日韩国产欧美在线观看| 欧美电视剧在线看免费| 国内精品久久久久影院一蜜桃| 久久久三级国产网站| 成人免费视频一区二区| 亚洲色图色小说| 欧美日韩一区精品| 久久精品国产亚洲高清剧情介绍| 精品久久久网站| jizzjizzjizz欧美| 亚洲一区二区精品3399| 日韩一区二区三| 国产成人精品网址| 亚洲欧美日韩久久精品| 7878成人国产在线观看| 国产一区二区导航在线播放| 国产精品第四页| 欧美精品xxxxbbbb| 成人性色生活片| 亚洲成av人片在www色猫咪| 精品久久久久久久人人人人传媒 | 欧美一级一区二区| 国产成人精品免费网站| 一区二区在线观看视频|