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

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? terrain.cpp

?? DirectX 8 教程 第八章源代碼下載 可以運行
?? CPP
字號:
// Terrain.cpp: implementation of the CTerrain class.
//
//////////////////////////////////////////////////////////////////////

#include "Terrain.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTerrain::CTerrain(LPDIRECT3DDEVICE8 pD3DDevice, WORD wRows, WORD wCols, float rTileSize, WORD wMaxHeight)
{
	m_pD3DDevice = pD3DDevice;
	m_pVertexBuffer = NULL;
	m_pIndexBuffer = NULL;
	m_pTexture = NULL;

	//Set a default size and position
	m_wRows = wRows;
	m_wCols = wCols;
	
	//Set the tile size for the terrain
	m_rTileSize = rTileSize;

	//Set the max height for any vertex
	m_wMaxHeight = wMaxHeight;

	//Setup counts for this object
	m_dwNumOfVertices = (m_wCols + 1) * (m_wRows + 1);
	m_dwNumOfPolygons = m_wRows * m_wCols * 2;
	m_dwNumOfIndices  = m_dwNumOfPolygons * 3;

	//Set material default values (R, G, B, A)
	D3DCOLORVALUE rgbaDiffuse  = {1.0, 1.0, 1.0, 0.0,};
	D3DCOLORVALUE rgbaAmbient  = {1.0, 1.0, 1.0, 0.0,};
	D3DCOLORVALUE rgbaSpecular = {0.0, 0.0, 0.0, 0.0,};
	D3DCOLORVALUE rgbaEmissive = {0.0, 0.0, 0.0, 0.0,};
	
	SetMaterial(rgbaDiffuse, rgbaAmbient, rgbaSpecular, rgbaEmissive, 0);

	//Initialize Vertex Buffer
    if(SUCCEEDED(CreateVertexBuffer()))
	{
		if(CreateIndexBuffer())
		{
			UpdateVertices();
		}
	}
}

CTerrain::~CTerrain()
{
	SafeRelease(m_pTexture);
	SafeRelease(m_pIndexBuffer);
	SafeRelease(m_pVertexBuffer);
}

DWORD CTerrain::Render()
{
	m_pD3DDevice->SetStreamSource(0, m_pVertexBuffer, sizeof(TERRAIN_CUSTOMVERTEX));
	m_pD3DDevice->SetVertexShader(TERRAIN_D3DFVF_CUSTOMVERTEX);
	
	if(m_pTexture != NULL)
	{
		//A texture has been set. We want our texture to be shaded based
		//on the current light levels, so used D3DTOP_MODULATE.
		m_pD3DDevice->SetTexture(0, m_pTexture);
		m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
		m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
		m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT);
	}
	else
	{
		//No texture has been set
		m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
		m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
		m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT);
	}

	//Select the material to use
	m_pD3DDevice->SetMaterial(&m_matMaterial);

	//Select index buffer
	m_pD3DDevice->SetIndices(m_pIndexBuffer, 0);

	//Render polygons from index buffer
	m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, m_dwNumOfVertices, 0, m_dwNumOfPolygons);

	//Return the number of polygons rendered
	return m_dwNumOfPolygons;
}

HRESULT CTerrain::CreateVertexBuffer()
{
    //Create the vertex buffer from our device.
    if(FAILED(m_pD3DDevice->CreateVertexBuffer(m_dwNumOfVertices * sizeof(TERRAIN_CUSTOMVERTEX),
                                               0, TERRAIN_D3DFVF_CUSTOMVERTEX,
                                               D3DPOOL_DEFAULT, &m_pVertexBuffer)))
    {
        return E_FAIL;
    }

    return S_OK;
}

bool CTerrain::CreateIndexBuffer()
{
	VOID* pBufferIndices;

	//Create the index buffer from our device
	if(FAILED(m_pD3DDevice->CreateIndexBuffer(m_dwNumOfIndices * sizeof(WORD), 
											  0, D3DFMT_INDEX16, D3DPOOL_MANAGED,
											  &m_pIndexBuffer)))
	{
	    return false;
	}
	
	
	//Set values for the index buffer
	WORD* pIndices = new WORD[m_dwNumOfIndices];	//Array holds the indices
	
	WORD a = m_wCols + 1;
	WORD b = 0;
	WORD c = a + 1;

	WORD x, z, i = 0;
	for(z = 0; z < m_wRows; z++)
	{
		for(x = 0; x < m_wCols; x++)
		{
			pIndices[i]	= a;
			pIndices[i + 1]	= b;
			pIndices[i + 2]	= c;
			pIndices[i + 3]	= b + 1;
			pIndices[i + 4]	= c;
			pIndices[i + 5]	= b;

			a++;
			b++;
			c++;
			i += 6;
		}

		a = c;
		b++;
		c++;
	}

	//Get a pointer to the index buffer indices and lock the index buffer    
	m_pIndexBuffer->Lock(0, m_dwNumOfIndices * sizeof(WORD), (BYTE**)&pBufferIndices, 0);

	//Copy our stored indices values into the index buffer
	memcpy(pBufferIndices, pIndices, m_dwNumOfIndices * sizeof(WORD));
	
	//Unlock the index buffer
	m_pIndexBuffer->Unlock();

	//Clean up
	delete pIndices;
	pIndices = NULL;

	return true;
}

D3DXVECTOR3 CTerrain::GetTriangeNormal(D3DXVECTOR3* vVertex1, D3DXVECTOR3* vVertex2, D3DXVECTOR3* vVertex3)
{
	D3DXVECTOR3 vNormal;
	D3DXVECTOR3 v1;
	D3DXVECTOR3 v2;

	D3DXVec3Subtract(&v1, vVertex2, vVertex1);
	D3DXVec3Subtract(&v2, vVertex3, vVertex1);

	D3DXVec3Cross(&vNormal, &v1, &v2);

	D3DXVec3Normalize(&vNormal, &vNormal);

	return vNormal;
}

bool CTerrain::UpdateVertices()
{
	DWORD i = 0;
	VOID* pVertices;
	WORD* pBufferIndices;
	D3DXVECTOR3 vNormal;
	DWORD dwVertex1;
	DWORD dwVertex2;
	DWORD dwVertex3;

	WORD* pNumOfSharedPolygons = new WORD[m_dwNumOfVertices];							//Array holds how many times this vertex is shared
	D3DVECTOR* pSumVertexNormal = new D3DVECTOR[m_dwNumOfVertices];						//Array holds sum of all face normals for shared vertex
	TERRAIN_CUSTOMVERTEX* pcvVertices = new TERRAIN_CUSTOMVERTEX[m_dwNumOfVertices];	//Array holds the veritces																				

	float x, z;

	//Centre terrain around the origin
	float zStart	= (float)(0.0 - (m_wRows/2.0));
	float zEnd		= (float)(m_wRows/2.0);
	float xStart	= (float)(0.0 - (m_wCols/2.0));
	float xEnd		= (float)(m_wCols/2.0);

	//Initialise the random number generator
	srand(timeGetTime());  

	//Clear memory and setup vertices for terrain
	for(z = zStart; z <= zEnd; z++)
	{
		for(x = xStart; x <= xEnd; x++)
		{
			pNumOfSharedPolygons[i] = 0;
			pSumVertexNormal[i] = D3DXVECTOR3(0,0,0);

			pcvVertices[i].x = x * m_rTileSize;
			
			//Make sure that the edges are all the same level
			if((z == zStart) || (z == zEnd) || (x == xStart) || (x == xEnd))
			{
				pcvVertices[i].y = 0.0;
			}
			else
			{
				//Set a random height for y
				pcvVertices[i].y = (float)(rand() % m_wMaxHeight);
			}
			

			pcvVertices[i].z = -z * m_rTileSize;	
			pcvVertices[i].nx = 0.0;
			pcvVertices[i].ny = 0.0;
			pcvVertices[i].nz = 0.0;
			
			if(((int)z % 2) == 0)
			{
				if(((int)x % 2) == 0)
				{
					pcvVertices[i].tu = 0.0;
					pcvVertices[i].tv = 0.0;
				}
				else
				{
					pcvVertices[i].tu = 1.0;
					pcvVertices[i].tv = 0.0;
				}
			}
			else
			{
				if(((int)x % 2) == 0)
				{
					pcvVertices[i].tu = 0.0;
					pcvVertices[i].tv = 1.0;
				}
				else
				{
					pcvVertices[i].tu = 1.0;
					pcvVertices[i].tv = 1.0;
				}
			}

			i++;
		}
	}


	//Get a pointer to the index buffer indices and lock the index buffer    
	m_pIndexBuffer->Lock(0, m_dwNumOfIndices * sizeof(WORD), (BYTE**)&pBufferIndices, D3DLOCK_READONLY);

	//For each triangle, count the number of times each vertex is used and
	//add together the normals of faces that share a vertex
	for(i = 0; i < m_dwNumOfIndices; i += 3)
	{
		dwVertex1 = pBufferIndices[i];
		dwVertex2 = pBufferIndices[i + 1];
		dwVertex3 = pBufferIndices[i + 2];

		vNormal = GetTriangeNormal(&D3DXVECTOR3(pcvVertices[dwVertex1].x, pcvVertices[dwVertex1].y, pcvVertices[dwVertex1].z), 
								   &D3DXVECTOR3(pcvVertices[dwVertex2].x, pcvVertices[dwVertex2].y, pcvVertices[dwVertex2].z), 
								   &D3DXVECTOR3(pcvVertices[dwVertex3].x, pcvVertices[dwVertex3].y, pcvVertices[dwVertex3].z));

		
		pNumOfSharedPolygons[dwVertex1]++;
		pNumOfSharedPolygons[dwVertex2]++;
		pNumOfSharedPolygons[dwVertex3]++;

		pSumVertexNormal[dwVertex1].x += vNormal.x;
		pSumVertexNormal[dwVertex1].y += vNormal.y;
		pSumVertexNormal[dwVertex1].z += vNormal.z;
		
		pSumVertexNormal[dwVertex2].x += vNormal.x;
		pSumVertexNormal[dwVertex2].y += vNormal.y;
		pSumVertexNormal[dwVertex2].z += vNormal.z;

		pSumVertexNormal[dwVertex3].x += vNormal.x;
		pSumVertexNormal[dwVertex3].y += vNormal.y;
		pSumVertexNormal[dwVertex3].z += vNormal.z;
	}


	//Unlock the index buffer
	m_pIndexBuffer->Unlock();

	//For each vertex, calculate the average normal
	//CHAR DEBUG[255];
	for(i = 0; i < m_dwNumOfVertices; i++)
	{
		vNormal.x = pSumVertexNormal[i].x / pNumOfSharedPolygons[i];
		vNormal.y = pSumVertexNormal[i].y / pNumOfSharedPolygons[i];
		vNormal.z = pSumVertexNormal[i].z / pNumOfSharedPolygons[i];

		D3DXVec3Normalize(&vNormal, &vNormal);

		pcvVertices[i].nx = vNormal.x;
		pcvVertices[i].ny = vNormal.y;
		pcvVertices[i].nz = vNormal.z;

		//sprintf(DEBUG, "Vertex Data %d: x = %f, y = %f, z = %f, nx = %f, ny = %f, nz = %f, tu = %f, tv = %f\n", i, pcvVertices[i].x, pcvVertices[i].y, pcvVertices[i].z, pcvVertices[i].nx, pcvVertices[i].ny, pcvVertices[i].nz, pcvVertices[i].tu, pcvVertices[i].tv);
		//OutputDebugString(DEBUG);
	}


	//Get a pointer to the vertex buffer vertices and lock the vertex buffer
    if(FAILED(m_pVertexBuffer->Lock(0, m_dwNumOfVertices * sizeof(TERRAIN_CUSTOMVERTEX), (BYTE**)&pVertices, 0)))
    {
        OutputDebugString("Lock Vertex Buffer Failed!\n");
		return false;
    }

    //Copy our stored vertices values into the vertex buffer
    memcpy(pVertices, pcvVertices, m_dwNumOfVertices * sizeof(TERRAIN_CUSTOMVERTEX));

    //Unlock the vertex buffer
    m_pVertexBuffer->Unlock();
	

	//Clean up
	delete pNumOfSharedPolygons;
	delete pSumVertexNormal;
	delete pcvVertices;

	pNumOfSharedPolygons = NULL;
	pSumVertexNormal = NULL;
	pcvVertices = NULL;

	return true;
}

bool CTerrain::SetSize(WORD wRows, WORD wCols, float rTileSize, WORD wMaxHeight)
{
	//Set size
	m_wRows = wRows;
	m_wCols = wCols;
	
	//Set the tile size for the terrain
	m_rTileSize = rTileSize;

	//Set the max height for any vertex
	m_wMaxHeight = wMaxHeight;

	//Setup counts for this object
	m_dwNumOfVertices = (m_wCols + 1)  * (m_wRows + 1);	
	m_dwNumOfPolygons = m_wRows * m_wCols * 2;
	m_dwNumOfIndices  = m_dwNumOfPolygons * 3;

	UpdateVertices();

	return true;
}

bool CTerrain::SetTexture(const char *szTextureFilePath)
{
	if(FAILED(D3DXCreateTextureFromFile(m_pD3DDevice, szTextureFilePath, &m_pTexture)))
	{
		OutputDebugString("Failed to load texture.");
		return false;
	}

	return true;
}

bool CTerrain::SetMaterial(D3DCOLORVALUE rgbaDiffuse, D3DCOLORVALUE rgbaAmbient, D3DCOLORVALUE rgbaSpecular, D3DCOLORVALUE rgbaEmissive, float rPower)
{
	//Set the RGBA for diffuse light reflected from this material. 
	m_matMaterial.Diffuse = rgbaDiffuse; 

	//Set the RGBA for ambient light reflected from this material. 
	m_matMaterial.Ambient = rgbaAmbient; 

	//Set the color and sharpness of specular highlights for the material. 
	m_matMaterial.Specular = rgbaSpecular; 
	m_matMaterial.Power = rPower;

	//Set the RGBA for light emitted from this material. 
	m_matMaterial.Emissive = rgbaEmissive;

	return true;
}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
日韩欧美一卡二卡| 日韩亚洲欧美一区| 国产成人精品综合在线观看 | 天天综合网天天综合色| 亚洲免费av高清| 中文字幕一区二区三区在线播放| 国产偷国产偷亚洲高清人白洁| 欧美变态tickle挠乳网站| 91精品国产色综合久久久蜜香臀| 欧美一区二区性放荡片| 欧美一级欧美三级在线观看| 欧美高清www午色夜在线视频| 8x8x8国产精品| 日韩亚洲欧美成人一区| 久久精品亚洲国产奇米99| 欧美激情在线一区二区三区| 国产精品福利影院| 亚洲成a人v欧美综合天堂| 秋霞成人午夜伦在线观看| 久久精品久久99精品久久| 粉嫩av亚洲一区二区图片| 99视频一区二区| 欧美精品少妇一区二区三区| 精品电影一区二区| 亚洲欧洲av另类| 午夜久久久久久久久久一区二区| 久久99国产精品麻豆| 99久久精品国产毛片| 欧美精品日韩综合在线| 久久久久久麻豆| 亚洲成av人片一区二区| 国产成人免费在线观看不卡| 欧美午夜在线观看| 26uuu精品一区二区在线观看| 亚洲精品高清在线| 精品一区二区三区影院在线午夜| 99re成人精品视频| 日韩精品一区二区在线| 亚洲影视在线播放| 成人黄色片在线观看| 欧美一卡2卡3卡4卡| 亚洲欧美在线视频| 久久www免费人成看片高清| 91蜜桃网址入口| 久久嫩草精品久久久精品一| 午夜精品在线看| 99久久婷婷国产精品综合| 欧美精品一区二区三区蜜桃| 亚洲va欧美va天堂v国产综合| 国产成人午夜精品5599| 欧美一区二区精美| 午夜视频一区在线观看| 日本乱码高清不卡字幕| 国产精品伦一区二区三级视频| 久久精品99国产国产精| 欧美性色aⅴ视频一区日韩精品| 国产欧美日韩综合精品一区二区| 蜜臀久久99精品久久久久宅男| 色狠狠av一区二区三区| 最新日韩av在线| www.日韩精品| 国产精品久久久久永久免费观看 | 99久久er热在这里只有精品15| 日韩欧美色综合网站| 五月婷婷另类国产| 欧美偷拍一区二区| 一区二区三区中文字幕精品精品| 国产精品伊人色| 国产视频一区不卡| 国模娜娜一区二区三区| 精品成人在线观看| 国产尤物一区二区| 国产日韩欧美不卡| 粉嫩一区二区三区性色av| 国产亲近乱来精品视频| 粉嫩蜜臀av国产精品网站| 国产免费久久精品| 99久久久精品免费观看国产蜜| 国产精品久久久久影院老司| 成人动漫视频在线| 一区二区三区中文字幕在线观看| 日本久久一区二区| 日韩和欧美一区二区三区| 日韩一区二区在线观看视频 | 国产精品的网站| 97精品电影院| 亚洲夂夂婷婷色拍ww47| 欧美日韩中字一区| 日本三级韩国三级欧美三级| 欧美一级一区二区| 国产一区二区精品久久| 中文字幕在线一区| 欧美男生操女生| 韩国在线一区二区| 中文字幕在线不卡视频| 欧美图区在线视频| 国内精品伊人久久久久av一坑| 国产色综合一区| 在线视频欧美区| 免费精品视频在线| 中文字幕一区av| 欧美性极品少妇| 国产精品一色哟哟哟| 夜夜精品视频一区二区| 欧美一区午夜视频在线观看| 国产精品99久久久久久有的能看| 亚洲精品免费在线观看| 日韩精品在线看片z| 99在线热播精品免费| 青青草国产精品亚洲专区无| 欧美国产日本视频| 51精品国自产在线| av成人免费在线观看| 日韩国产欧美三级| 亚洲四区在线观看| 欧美大白屁股肥臀xxxxxx| 成人激情午夜影院| 全部av―极品视觉盛宴亚洲| 亚洲天堂免费看| 久久精品夜色噜噜亚洲a∨| 欧美在线一区二区三区| 国产成人av资源| 日本视频在线一区| 亚洲女人的天堂| 国产日韩欧美精品一区| 日韩视频免费观看高清完整版在线观看 | 久久精品夜色噜噜亚洲a∨| 欧美日韩国产首页| 91视频国产观看| 盗摄精品av一区二区三区| 日本欧美一区二区三区乱码| 一区二区三区在线影院| 中文一区在线播放| 久久久久久电影| www国产精品av| 日韩视频免费观看高清完整版| 欧美色欧美亚洲另类二区| 91免费观看视频在线| www.久久精品| 成人av电影免费观看| 成人午夜在线视频| 国产成人三级在线观看| 国产一区二区视频在线播放| 久久99国内精品| 国产在线播放一区三区四| 麻豆精品国产传媒mv男同| 日韩国产欧美一区二区三区| 亚洲福利视频一区| 亚州成人在线电影| 亚洲va天堂va国产va久| 亚洲第一在线综合网站| 午夜精品福利一区二区蜜股av| 亚洲精品国产一区二区精华液| 日韩毛片在线免费观看| 亚洲男人的天堂一区二区| 亚洲精品午夜久久久| 亚洲国产一区在线观看| 亚洲成a人片在线不卡一二三区| 亚洲已满18点击进入久久| 性欧美疯狂xxxxbbbb| 蜜桃精品视频在线| 国产黄色精品网站| 不卡电影一区二区三区| 91香蕉视频mp4| 欧美高清www午色夜在线视频| 欧美一区二区二区| 国产色91在线| 一区二区三区四区不卡视频| 亚洲一卡二卡三卡四卡五卡| 图片区日韩欧美亚洲| 国产一区欧美日韩| 99久久精品国产导航| 欧美日韩精品系列| 精品99一区二区| 18成人在线观看| 免费黄网站欧美| 波多野结衣亚洲| 欧美片在线播放| 国产精品你懂的在线| 亚洲午夜私人影院| 激情成人午夜视频| 91免费精品国自产拍在线不卡| 欧美人与性动xxxx| 国产精品乱码人人做人人爱| 一区二区三区中文字幕| 久久精品国产99国产| 色综合天天综合色综合av| 欧美精品v国产精品v日韩精品| 欧美精品一区二区蜜臀亚洲| 综合婷婷亚洲小说| 免费的国产精品| 在线这里只有精品| 久久久一区二区三区捆绑**| 一区二区在线观看视频在线观看| 日本欧美加勒比视频| 99精品1区2区| 精品国产青草久久久久福利| 一区二区成人在线视频| 国产a视频精品免费观看| 日韩视频永久免费|