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

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

?? psystem.cpp

?? Introduction to directx9 3d game programming 一書的源代碼
?? CPP
字號:
//////////////////////////////////////////////////////////////////////////////////////////////////
// 
// 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 geneal 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
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美视频你懂的| 国产精品99久久久久久久女警| 久久久久久久久久久久电影| 欧美精品粉嫩高潮一区二区| 欧美美女黄视频| 欧美一区二区黄| 91精品国产综合久久久久久| 欧美福利视频一区| 91精品国产一区二区三区| 日韩一区二区免费在线观看| 日韩欧美中文一区| 2023国产精品| 国产亚洲污的网站| 亚洲人亚洲人成电影网站色| 亚洲精品成a人| 五月婷婷激情综合网| 美女看a上一区| 国产成人精品一区二| 91丨porny丨户外露出| 欧美三级日韩三级| 日韩午夜在线观看视频| 久久伊人蜜桃av一区二区| 国产精品毛片久久久久久久| 亚洲码国产岛国毛片在线| 亚洲香肠在线观看| 免费高清在线视频一区·| 国产91丝袜在线观看| 在线视频国内自拍亚洲视频| 日韩欧美在线综合网| 亚洲天堂a在线| 日本亚洲欧美天堂免费| 成人不卡免费av| 91精品一区二区三区在线观看| 欧美精品一区二区三区蜜桃视频 | 精品国产sm最大网站免费看| 国产亚洲欧美日韩在线一区| 亚洲精品成人天堂一二三| 美女视频一区二区| www..com久久爱| 日韩精品一区二区三区四区视频 | 欧美精品一区二区三区四区| 自拍偷拍国产亚洲| 久久99精品久久久久久久久久久久| 国产夫妻精品视频| 欧美色视频在线观看| 国产精品天天摸av网| 天天综合色天天| av电影在线不卡| 欧美精品一区二区三区很污很色的| 亚洲自拍偷拍欧美| 国产成人亚洲综合色影视| 欧美一区二区三区四区五区| 亚洲欧美日韩在线| 国产激情一区二区三区| 欧美一区二区三区视频免费| 亚洲人成影院在线观看| 国产精品18久久久| 精品欧美黑人一区二区三区| 亚洲一区影音先锋| 99久久99久久精品国产片果冻| 欧美成人一区二区三区在线观看| 亚洲不卡一区二区三区| 色婷婷亚洲婷婷| 国产精品伦理一区二区| 国产高清精品在线| 欧美成人在线直播| 精品在线播放免费| 欧美一区二区二区| 美国毛片一区二区三区| 91精品国产入口| 日韩高清中文字幕一区| 欧美一区二区啪啪| 免费看日韩精品| 日韩一区二区在线观看视频| 午夜a成v人精品| 欧美精品一二三区| 亚洲在线中文字幕| 欧美视频一区二区三区| 亚洲成人免费视频| 欧美精品aⅴ在线视频| 天天综合网天天综合色| 555www色欧美视频| 免费精品99久久国产综合精品| 欧美一区欧美二区| 久久aⅴ国产欧美74aaa| 欧美岛国在线观看| 国产一区二区精品久久| 国产精品视频在线看| 在线欧美日韩国产| 人人超碰91尤物精品国产| 精品日韩一区二区三区| 国产999精品久久久久久| 最新欧美精品一区二区三区| 在线一区二区三区四区五区 | 亚洲一区二区三区四区在线免费观看 | 久久蜜桃av一区二区天堂| 国产一区二区三区四| 国产精品午夜电影| 欧美性色综合网| 精品影院一区二区久久久| 国产拍欧美日韩视频二区| 99久精品国产| 久久精品国产亚洲a| 国产色一区二区| 一本色道综合亚洲| 日本视频一区二区| 国产精品久久久一本精品| 在线视频综合导航| 国内精品伊人久久久久av影院| 国产精品三级在线观看| 欧美性一二三区| 国产传媒一区在线| 亚洲第一电影网| 国产区在线观看成人精品| 欧美巨大另类极品videosbest | 麻豆精品蜜桃视频网站| 亚洲国产高清在线观看视频| 欧美写真视频网站| 国产精品资源在线| 性久久久久久久久久久久| 国产喷白浆一区二区三区| 欧美精品久久久久久久多人混战 | 精品视频免费看| 成人开心网精品视频| 麻豆91免费观看| 亚洲综合激情另类小说区| 国产精品免费人成网站| 6080yy午夜一二三区久久| 91蝌蚪国产九色| 国产suv一区二区三区88区| 日日噜噜夜夜狠狠视频欧美人 | 水野朝阳av一区二区三区| 国产精品久线在线观看| 日韩精品一区二区三区四区| 在线观看一区日韩| 99久久久精品| 风流少妇一区二区| 国产一区二区在线视频| 日韩国产一区二| 亚洲一区二区三区视频在线播放| 中文字幕欧美国产| 久久久欧美精品sm网站| 精品日韩99亚洲| 日韩免费看网站| 欧美成人艳星乳罩| 日韩欧美中文一区| 日韩欧美不卡一区| 欧美一区二区黄色| 欧美一级高清片在线观看| 欧美日韩电影一区| 欧美视频精品在线观看| 欧美午夜影院一区| 欧美日韩国产欧美日美国产精品| 在线观看视频91| 在线看国产日韩| 欧美女孩性生活视频| 欧美美女直播网站| 欧美一区二区久久久| 欧美精品一区二区三区一线天视频| 欧美一区二区三区视频免费 | 国产91综合网| 99久久综合国产精品| 91在线视频官网| 欧洲av一区二区嗯嗯嗯啊| 欧美日韩国产综合一区二区三区 | 中文字幕va一区二区三区| 国产精品无圣光一区二区| 亚洲日本青草视频在线怡红院| 国产精品第五页| 亚洲裸体xxx| 日本在线播放一区二区三区| 久久国产人妖系列| 成人高清免费观看| 色综合天天综合给合国产| 欧美日韩一区在线观看| 日韩欧美国产小视频| 久久久影视传媒| 一区二区三区免费观看| 蜜臀av一级做a爰片久久| 国产一区二区三区不卡在线观看| 成人一区二区三区| 欧美中文字幕久久| 日韩欧美资源站| 成人免费在线观看入口| 日韩精品每日更新| 成人精品高清在线| 欧美丰满嫩嫩电影| 日本一区二区电影| 亚洲午夜精品久久久久久久久| 精品亚洲国内自在自线福利| a亚洲天堂av| 日韩一区二区三区三四区视频在线观看 | 亚洲bdsm女犯bdsm网站| 国内一区二区在线| 欧美日韩免费电影| 久久久久国产精品麻豆| 亚洲福利一区二区| 成人av电影在线播放| 欧美一区二区三区思思人| 亚洲人成网站在线|