Ejemplo n.º 1
0
HRESULT InitD3D( HWND hWnd ) 
{ 
	// Create the D3D object, which is needed to create the D3DDevice. 
	if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) 
		return E_FAIL; 

	D3DPRESENT_PARAMETERS d3dpp;  
	ZeroMemory( &d3dpp, sizeof(d3dpp) ); 
	d3dpp.Windowed = TRUE; 
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 
	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; 

	// Create device 
	if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, 
		D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
		&d3dpp, &g_pd3dDevice ) ) ) 
	{ 
		return E_FAIL; 
	} 

	// Turn off culling, so we see the front and back of the triangle    
	g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );   

	// Wire frame mode
	g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);

	//g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);    
	g_pd3dDevice->SetRenderState( D3DRS_LIGHTING , FALSE );    

	// Create a torus
	D3DXCreateTorus(g_pd3dDevice, 1.0f, 2.0f, 20, 20, &g_pTorusMesh, NULL);

	return S_OK; 
} 
Ejemplo n.º 2
0
bool ObjectInit(HWND hwnd)
{
	srand(unsigned(time(nullptr)));
	PlaySound(_T("コミネリサ - Resuscitated Hope.wav"), nullptr, SND_ASYNC | SND_FILENAME | SND_LOOP);

	if (FAILED(D3DXCreateFont(gPD3DDevice, 38, 0, 0, 0, 0, 0, 0, 0, 0, _T("楷体"), &gPFont)))
	{
		return false;
	}
	D3DXCreateTeapot(gPD3DDevice, &gPTeapot, 0);
	D3DXCreateBox(gPD3DDevice, 2, 2, 2, &gPBox, 0);
	D3DXCreateTorus(gPD3DDevice, 1.0f, 2.0f, 25, 25, &gPTorus, 0);
	D3DXCreateSphere(gPD3DDevice, 2.0f, 25, 25, &gPSphere, 0);

	//D3DMATERIAL9 is a struct with diffuse, specular, ambient, and emissive;
	//There is also power typed float, however its usage not known yet.
	D3DMATERIAL9 material;
	ZeroMemory(&material, sizeof(material));
	material.Ambient = D3DXCOLOR(0.5f, 0.5f, 0.7f, 1.0f);
	material.Diffuse = D3DXCOLOR(0.4f, 0.6f, 0.6f, 1.0f);
	material.Specular = D3DXCOLOR(0.3f, 0.3f, 0.3f, 1.0f);
	material.Emissive = D3DXCOLOR(0.3f, 0.0f, 0.1f, 1.0f);
	gPD3DDevice->SetMaterial(&material);	

	return true;
}
Ejemplo n.º 3
0
bool init(HWND hWnd)
{
    g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
    if(!g_pD3D) {
        return false;
    }

    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = true;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

    g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
        D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice);

    if(!g_pd3dDevice) {
        return false;
    }

    g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);

    D3DXCreateSphere(g_pd3dDevice, OBJECT_SIZE, OBJECT_PRECESSION, OBJECT_PRECESSION, &g_pSphereMesh, 0);
    D3DXCreateBox(g_pd3dDevice, OBJECT_SIZE, OBJECT_SIZE, OBJECT_SIZE, &g_pCubeMesh, 0);
    D3DXCreateTorus(g_pd3dDevice, OBJECT_SIZE / OBJECT_SIZE, OBJECT_SIZE, OBJECT_PRECESSION / (UINT) OBJECT_SIZE, OBJECT_PRECESSION, &g_pTorusMesh, 0);

    return true;
}
HRESULT InitD3D( HWND hWnd )
{
	// Create the D3D object, which is needed to create the D3DDevice.
	if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
		return E_FAIL;

	D3DPRESENT_PARAMETERS d3dpp; 
	ZeroMemory( &d3dpp, sizeof(d3dpp) );
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

	// Create device
	if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp, &g_pd3dDevice ) ) )
	{
		return E_FAIL;
	}

	// Wire frame mode.
	g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME) ;

	// Disable lighting.
	g_pd3dDevice->SetRenderState( D3DRS_LIGHTING , FALSE );   

	// Create geometries.
	D3DXCreateTeapot(g_pd3dDevice, &g_pMeshes[0], NULL) ; // Teapot
	D3DXCreateSphere(g_pd3dDevice, 1.2f, 20, 20, &g_pMeshes[1], NULL) ; // Sphere
	D3DXCreateCylinder(g_pd3dDevice, 1.0f, 1.0f, 2.0f, 20, 5, &g_pMeshes[2], NULL) ; // Cylinder
	D3DXCreateTorus(g_pd3dDevice, 0.5f, 1.0f, 15, 15, &g_pMeshes[3], NULL) ; // Torus

	return S_OK;
}
Ejemplo n.º 5
0
HRESULT InitObject(void)
{
	srand(unsigned(time(NULL)));

	if (FAILED(D3DXCreateFont(g_pDevice, 30, 0, 0, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, TEXT("宋体"), &g_pFont)))
	{
		return E_FAIL;
	}

	if (FAILED(D3DXCreateTeapot(g_pDevice, &g_pTeapot, NULL))) return E_FAIL;
	if (FAILED(D3DXCreateBox(g_pDevice, 2.0f, 2.0f, 2.0f, &g_pCube, NULL))) return E_FAIL;
	if (FAILED(D3DXCreateSphere(g_pDevice, 1.5f, 25, 25, &g_pSphere, NULL))) return E_FAIL;
	if (FAILED(D3DXCreateTorus(g_pDevice, 0.5f, 1.2f, 25, 25, &g_pTorus, NULL))) return E_FAIL;

	//设置材质
	D3DMATERIAL9 material;
	ZeroMemory(&material, sizeof(D3DMATERIAL9));
	material.Ambient = D3DXCOLOR(0.5f, 0.5f, 0.7f, 1.0f);	//环境光
	material.Diffuse = D3DXCOLOR(0.6f, 0.6f, 0.6f, 1.0f);	//漫反射
	material.Specular = D3DXCOLOR(0.3f, 0.3f, 0.3f, 0.3f);	//镜面反射
	material.Emissive = D3DXCOLOR(0.3f, 0.0f, 0.1f, 1.0f);
	g_pDevice->SetMaterial(&material);

	//设置光照
	SetLight(g_pDevice, 1);
	g_pDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
	g_pDevice->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
	g_pDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
	//开启背面消隐  
	g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);

	return S_OK;
}
//-----------------------------------【Object_Init( )函数】--------------------------------------
//	描述:渲染资源初始化函数,在此函数中进行要被渲染的物体的资源的初始化
//--------------------------------------------------------------------------------------------------
HRESULT Objects_Init(HWND hwnd)
{
	//创建字体
	if(FAILED(D3DXCreateFont(g_pd3dDevice, 36, 0, 0, 1, false, DEFAULT_CHARSET, 
		OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, _T("微软雅黑"), &g_pFont)))
		return E_FAIL;
	srand(timeGetTime());      //用系统时间初始化随机种子 

	// 物体的创建
	if(FAILED(D3DXCreateBox(g_pd3dDevice, 2, 2, 2, &g_cube, NULL)))	//立方体的创建
		return false;
	if(FAILED(D3DXCreateTeapot(g_pd3dDevice, &g_teapot, NULL)))		//茶壶的创建
		return false;	
	if(FAILED(D3DXCreateSphere(g_pd3dDevice, 1.5, 25, 25,					//球面体的创建
		&g_sphere, NULL))) return false;
	if(FAILED(D3DXCreateTorus(g_pd3dDevice, 0.5f, 1.2f, 25, 25,				//圆环体的创建
		&g_torus, NULL))) return false;

	// 设置渲染状态
	g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);   //关闭光照
	g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);   //开启背面消隐
	g_pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);  //设置线框填充模式

	return S_OK;
}
Ejemplo n.º 7
0
bool Setup()
{
    //create objects
    D3DXCreateTeapot(Device, &Objects[0], 0);
    D3DXCreateSphere(Device, 1.0f, 20, 20, &Objects[1], 0);
    D3DXCreateTorus(Device, 0.5f, 1.0f, 20, 20, &Objects[2], 0);
    D3DXCreateCylinder(Device, 0.5f, 0.5f, 2.0f, 20, 20, &Objects[3], 0);

    //build world martices - position the objects in world space.
    D3DXMatrixTranslation(&Worlds[0], 0.0f, 2.0f, 0.0f);
    D3DXMatrixTranslation(&Worlds[1], 0.0f, 2.0f, -3.0f);
    D3DXMatrixTranslation(&Worlds[2], -3.0f, 0.0f, 0.0f);
    D3DXMatrixTranslation(&Worlds[3], 3.0f, 0.0f, 0.0f);

    //setup the object's materials
    Mtrls[0] = d3d::RED_MTRL;
    Mtrls[1] = d3d::BLUE_MTRL;
    Mtrls[2] = d3d::GREEN_MTRL;
    Mtrls[3] = d3d::YELLOW_MTRL;

    // Setup a directional light.
    /*D3DXVECTOR3 dir(1.0f, -0.0f, 0.25f);
    D3DXCOLOR   c = d3d::WHITE;
    D3DLIGHT9 dirLight = d3d::InitDirectionalLight(&dir, &c);*/
    D3DXVECTOR3 pos(5.0f, 50.0f, 50.0f);
    D3DXCOLOR   c = d3d::WHITE;
    D3DLIGHT9 point = d3d::InitPointLight(&pos, &c);

    //D3DXVECTOR3 pos(0.0f, 0.0f, -8.0f);
    //D3DXVECTOR3 dir(0.0f, 0.0f, 1.0f);
    //D3DXCOLOR   c = d3d::WHITE;
    //D3DLIGHT9 Spot = d3d::InitSpotLight(&pos, &dir, &c);

    // Set and Enable the light.
    Device->SetLight(0, &point);
    Device->LightEnable(0, true);

    // Set lighting related render states.
    Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
    Device->SetRenderState(D3DRS_SPECULARENABLE, false);

    // Set the projection matrix.
    D3DXMATRIX proj;
    D3DXMatrixPerspectiveFovLH(
        &proj,
        D3DX_PI * 0.25f, // 45 - degree
        (float)Width / (float)Height,
        1.0f,
        1000.0f);
    Device->SetTransform(D3DTS_PROJECTION, &proj);

    return true;
}
Ejemplo n.º 8
0
	virtual bool Setup(HINSTANCE hInstance, int width, int height, bool windowed, D3DDEVTYPE deviceType)
	{
		CallBaseSetup;
		// create objects
		D3DXCreateTeapot(pDevice_, &objects_[0], 0);
		D3DXCreateSphere(pDevice_, 1.f, 20, 20, &objects_[1], 0);
		D3DXCreateTorus(pDevice_, .5f, 1.f, 20, 20, &objects_[2], 0);
		D3DXCreateCylinder(pDevice_, .5f, .5f, 2.f, 20, 20, &objects_[3], 0);

		// build world matrices --position the objects in the world space
		D3DXMatrixTranslation(&worlds_[0], 0.f, 2.f, 0.f);
		D3DXMatrixTranslation(&worlds_[1], 0.f, -2.f, -0.f);
		D3DXMatrixTranslation(&worlds_[2], -3.f, 0.f, 0.f);
		D3DXMatrixTranslation(&worlds_[3], 3.f, 0.f, 0.f);

		// setup the object's materials
		mtrls_[0] = RED_MTRL;
		mtrls_[1] = BLUE_MTRL;
		mtrls_[2] = GREEN_MTRL;
		mtrls_[3] = YELLOW_MTRL;

		// setup a directional light
		D3DXVECTOR3 dir(1.f, -0.f, 0.25f);
		D3DXCOLOR c = WHITE;
		D3DLIGHT9 dirLight = InitDirectionalLight(&dir, &c);
		
		// set and enable the light
		pDevice_->SetLight(0, &dirLight);
		pDevice_->LightEnable(0, true);

		// set lighting related render states
		pDevice_->SetRenderState(D3DRS_NORMALIZENORMALS, true);
		pDevice_->SetRenderState(D3DRS_SPECULARENABLE, false);

		// set the projection matrix

		D3DXMATRIX proj;
		D3DXMatrixPerspectiveFovLH(
			&proj, D3DX_PI * 0.25f, (float)width / (float)height,
			1.f, 1000.f);
		pDevice_->SetTransform(D3DTS_PROJECTION, &proj);

		return true;
	}
Ejemplo n.º 9
0
bool Setup()
{
	//与网格关联的设备,接收网格指针,LPD3DXBUFFER*
	D3DXCreateTeapot(Device, &Objects[0], 0);
	//设备,半径,切片,堆叠,接收,LPD3DXBUFFER*
	D3DXCreateSphere(Device, 1.0f, 20, 20, &Objects[1], 0);
	//设备,内半径,外半径,边,吊环,接收,LPD3DXBUFFER*
	D3DXCreateTorus(Device, 0.5f, 1.0f, 20, 20, &Objects[2], 0);
	//设备,radius at negative z end,radius at positive z end,
	//length of cylinder, slices, stacks,接收,LPD3DXBUFFER*
	D3DXCreateCylinder(Device, 0.5f, 0.5f, 2.0f, 20, 20, &Objects[3], 0);

	D3DXMatrixTranslation(&Worlds[0], 0.0f, 2.0f, 0.0f);
	D3DXMatrixTranslation(&Worlds[1], 0.0f, -2.0f, 0.0f);
	D3DXMatrixTranslation(&Worlds[2], -3.0f, 0.0f, 0.0f);
	D3DXMatrixTranslation(&Worlds[3], 3.0f, 0.0f, 0.0f);

	Mtrls[0] = d3d::RED_MTRL;
	Mtrls[1] = d3d::BLUE_MTRL;
	Mtrls[2] = d3d::GREEN_MTRL;
	Mtrls[3] = d3d::YELLOW_MTRL;

	D3DXVECTOR3 pos(0.0f, 0.0f, 0.0f);
	D3DXCOLOR c = d3d::WHITE;
	D3DLIGHT9 point = d3d::InitPointLight(&pos, &c);

	Device->SetLight(0, &point);
	Device->LightEnable(0, true);

	Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
	Device->SetRenderState(D3DRS_SPECULARENABLE, false);

	D3DXMATRIX proj;
	D3DXMatrixPerspectiveFovLH(
		&proj,
		D3DX_PI * 0.25f, // 45 - degree
		(float)Width / (float)Height,
		1.0f,
		1000.0f);
	Device->SetTransform(D3DTS_PROJECTION, &proj);

	return true;
}
Ejemplo n.º 10
0
bool Setup()
{
	D3DXCreateTeapot(Device, &Objects[0], 0);
	D3DXCreateSphere(Device, 1.0f, 20, 20, &Objects[1], 0);
	D3DXCreateTorus(Device, 0.5f, 1.0f, 20, 20, &Objects[2], 0);
	D3DXCreateCylinder(Device, 0.5f, 0.5f, 2.0f, 20, 20, &Objects[3], 0);

	D3DXMatrixTranslation(&Worlds[0],  0.0f,  2.0f, 0.0f);
	D3DXMatrixTranslation(&Worlds[1],  0.0f, -2.0f, 0.0f);
	D3DXMatrixTranslation(&Worlds[2], -3.0f,  0.0f, 0.0f);
	D3DXMatrixTranslation(&Worlds[3],  3.0f,  0.0f, 0.0f);

	Mtrls[0] = byhj::RED_MTRL;
	Mtrls[1] = byhj::BLUE_MTRL;
	Mtrls[2] = byhj::GREEN_MTRL;
	Mtrls[3] = byhj::YELLOW_MTRL;

	D3DXVECTOR3 dir(1.0f, -0.0f, 0.25f);
	D3DXCOLOR color = byhj::WHITE;
	D3DLIGHT9 dirLight = byhj::InitDirectionalLight(&dir, &color);
	Device->SetLight(0, &dirLight);
	Device->LightEnable(0, true);
	Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
	Device->SetRenderState(D3DRS_SPECULARENABLE, false);

	//set the projection matrix
	D3DXMATRIX proj;
	D3DXMatrixPerspectiveFovLH(
		&proj,
		D3DX_PI * 0.5f,
		(float)Width / (float)Height,
		1.0f,
		1000.0f);
	Device->SetTransform(D3DTS_PROJECTION, &proj);

	return true;
}
Ejemplo n.º 11
0
HRESULT STDMETHODCALLTYPE KG3DRotationCoordinateSolid::Init()
{
	RepresentData rpData;
	
	HRESULT hr = D3DXCreateTorus(g_pd3dDevice, em_mesh_inner_radius, em_mesh_range, em_mesh_sides, em_mesh_rings
		, &rpData.MeshForPlane[2], NULL);	//创建出来的圈圈是Z轴正向的
	KG_COM_PROCESS_ERROR(hr);
	{
		//复制出另外两个圈圈

		D3DXMATRIX matRotation;
		D3DXMatrixRotationAxis(&matRotation, &D3DXVECTOR3(1,0,0), -D3DX_PI/2);	

		hr = D3DXMeshClone(rpData.MeshForPlane[2], &rpData.MeshForPlane[1]);
		KG_COM_PROCESS_ERROR(hr);
		hr = D3DXMeshTransformation(rpData.MeshForPlane[1], &matRotation, NULL);
		KG_COM_PROCESS_ERROR(hr);
		
		D3DXMatrixRotationAxis(&matRotation, &D3DXVECTOR3(0,1,0), D3DX_PI/2);

		hr = D3DXMeshClone(rpData.MeshForPlane[2], &rpData.MeshForPlane[0]);
		KG_COM_PROCESS_ERROR(hr);
		hr = D3DXMeshTransformation(rpData.MeshForPlane[0], &matRotation, NULL);
		KG_COM_PROCESS_ERROR(hr);
	}
	m_RepresentData = rpData;
	ZeroMemory(&rpData, sizeof(rpData));
	m_RepresentData.bFilterInter = em_filter_back;	//把后半的碰撞结果过滤掉

	hr = KG3DBaseCoordImp::Init();
	KG_COM_PROCESS_ERROR(hr);
	return S_OK;
Exit0:
	rpData.ReleaseMeshes();
	return E_FAIL;
}
Ejemplo n.º 12
0
//----------------------------------
//機能:メッシュを作成する関数
//引数:なし
//戻値:成功or失敗
//----------------------------------
HRESULT BasicMesh::Create(DWORD type)
{
	/*------立方体のメッシュを作成する----------
	if (FAILED(D3DXCreateBox(g_pDevice, 1, 1, 1, &pMesh, NULL)))
	------------------------------------------*/


	/*------------スフィア(○)を作る----------
	第二引数は半径、正面から見た面の数、上から見た分割数、となる。
	これらを設定して球体の滑らかさを作る
	if (FAILED(D3DXCreateSphere(g_pDevice, 1, 24, 24, &pMesh, NULL)))
	------------------------------------------*/

	/*---------------円柱を作る-----------------
	if (FAILED(D3DXCreateCylinder(g_pDevice, 1, 1, 3, 24, 2, &pMesh, NULL)))
	------------------------------------------*/


	/*--------トーラス(ドーナツ型)を作る------
	第二引数はドーナツの太さ、円の大きさ、横から見た分割数、正面から見た分割数
	if (FAILED(D3DXCreateTorus(g_pDevice, 0.5, 1, 12, 24, &pMesh, NULL)))
	------------------------------------------*/
	
	//オマケ。DirectXには光の当たり具合、角度、影等を試せる関数が存在する
	//それがティーポットである。
	//if (FAILED(D3DXCreateTeapot(g_pDevice, &pMesh, NULL)))
	//{
	//	MessageBox(0, "メッシュの作成に失敗しました", "BasicMesh", MB_OK);
	//	return E_FAIL;
	//}

	//エラー処理用、一つにまとめる事で余計な手間を省ける
	HRESULT hr;


	//各モデルを詰め合わせたスイッチ文
	switch (type)
	{
	case BMESH_BOX:
		hr = D3DXCreateBox(g_pDevice, 1, 1, 1, &pMesh, NULL);
		break;

	case BMESH_SPHERE:
		hr = D3DXCreateSphere(g_pDevice, 1, 24, 24, &pMesh, NULL);
		break;

	case BMESH_CYLINDER:
		hr = D3DXCreateCylinder(g_pDevice, 1, 1, 3, 24, 2, &pMesh, NULL);
		break;

	case BMESH_TORUS:
		hr = D3DXCreateTorus(g_pDevice, 0.5, 1, 12, 24, &pMesh, NULL);
		break;

	case BMESH_TEAPOT:
		hr = D3DXCreateTeapot(g_pDevice, &pMesh, NULL);
		break;
	}

	//どれが失敗してもhrにまとまっているので確認が楽
	if (FAILED(hr))
	{
		MessageBox(0, "メッシュの作成に失敗しました", "BasicMesh", MB_OK);
		return E_FAIL;
	}

	//下のほうで作ったInitMaterialを呼び出す。(呼び忘れ防止)
	InitMaterial();

	return S_OK;
}
Ejemplo n.º 13
0
// 进入循环之前的设置
bool SetUp()
{
	HRESULT hr = 0;

	hr = D3DXCreateTeapot(g_pDevice, &g_ppMesh[0], NULL);
	if (FAILED(hr))
		return false;
	hr = D3DXCreateSphere(g_pDevice, 1.0f, 20, 20, &g_ppMesh[1], NULL);
	if (FAILED(hr))
		return false;
	hr = D3DXCreateTorus(g_pDevice, 0.5f, 1.0f, 20, 20, &g_ppMesh[2], NULL);
	if (FAILED(hr))
		return false;
	hr = D3DXCreateCylinder(g_pDevice, 0.5f, 1.0f, 2.0f, 20, 20, &g_ppMesh[3], NULL);
	if (FAILED(hr))
		return false;

	D3DXMatrixTranslation(&g_WorldMatrix[0], 0.0f, 2.0f, 0.0f);
	D3DXMatrixTranslation(&g_WorldMatrix[1], 0.0f, -2.0f, 0.0f);
	D3DXMatrixTranslation(&g_WorldMatrix[2], -3.0f, 0.0f, 0.0f);
	D3DXMatrixTranslation(&g_WorldMatrix[3], 3.0f, 0.0f, 0.0f);

	g_Color[0] = D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f);
	g_Color[1] = D3DXVECTOR4(0.0f, 1.0f, 0.0f, 1.0f);
	g_Color[2] = D3DXVECTOR4(0.0f, 0.0f, 1.0f, 1.0f);
	g_Color[3] = D3DXVECTOR4(1.0f, 1.0f, 0.0f, 1.0f);


	if (!d3d9::CheckShaderVersion(g_pDevice))
	{
		MessageBox(0, "Shader version can not support!", 0, 0);
		return false;
	}
	
	ID3DXBuffer *pShaderCache;
	ID3DXBuffer *pErrorMsg;
	hr = D3DXCompileShaderFromFile(
		"Toon.txt",
		NULL,
		NULL,
		"Main",
		"vs_1_1",
		D3DXSHADER_DEBUG,
		&pShaderCache,
		&pErrorMsg,
		&g_pConstTable
		);
	if (pErrorMsg)
	{
		MessageBox(0, (char*)pErrorMsg->GetBufferPointer(), 0, 0);
		d3d9::Release(pErrorMsg);
		return false;
	}
	d3d9::Release(pErrorMsg);

	if (!g_pConstTable)
	{
		MessageBox(0, "Const table is NULL!", 0, 0);
		return false;
	}

	if (FAILED(hr))
	{
		MessageBox(0, "Failed to compile shader from file!", 0, 0);
		return false;
	}

	hr = g_pDevice->CreateVertexShader((DWORD*)pShaderCache->GetBufferPointer(), &g_pVertexShader);

	if (FAILED(hr))
	{
		MessageBox(0, "Failed to create vertex shader!", 0, 0);
		d3d9::Release(pShaderCache);
		return false;
	}
	d3d9::Release(pShaderCache);

	hr = D3DXCreateTextureFromFile(g_pDevice, "toonshade.bmp", &g_pShadeTex);
	if (FAILED(hr))
	{
		MessageBox(0, "Failed to create texture from file!", 0, 0);
		return false;
	}

	g_pDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
	g_pDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
	g_pDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);

	g_hWorldViewMatrix = g_pConstTable->GetConstantByName(NULL, "WorldViewMatrix");
	if (!g_hWorldViewMatrix)
	{
		MessageBox(0, "Get WorldViewMatrix failed!", 0, 0);
		return false;
	}

	g_hWorldViewProjMatrix = g_pConstTable->GetConstantByName(NULL, "WorldViewProjMatrix");
	if (!g_hWorldViewProjMatrix)
	{
		MessageBox(0, "Get WorldViewProjMatrix failed!", 0, 0);
		return false;
	}

	g_hColor = g_pConstTable->GetConstantByName(NULL, "Color");
	if (!g_hColor)
	{
		MessageBox(0, "Get Color failed!", 0, 0);
		return false;
	}

	g_hLightDir = g_pConstTable->GetConstantByName(NULL, "DirToLight");
	if (!g_hLightDir)
	{
		MessageBox(0, "Get DirToLight failed!", 0, 0);
		return false;
	}
	g_pConstTable->SetDefaults(g_pDevice);

	D3DXVECTOR4 dirtolight(-0.57f, 0.57f, -0.57f, 0.0f);
	g_pConstTable->SetVector(g_pDevice, g_hLightDir, &dirtolight);


	// 竖直视角,宽/高比,近裁剪面,远裁剪面
	D3DXMatrixPerspectiveFovLH(&g_ProjMatrix, D3DX_PI*0.25f, (float)Width / (float)Height, 1.0f, 100.0f);

	return true;
}
Ejemplo n.º 14
0
// Create a torus with the parameters specified
// Returns true for success, false otherwise
bool CreateTorus(float innerRad, float outerRad, int color, ID3DXMesh **mesh)
{
	assert(g3D->mDevice != NULL);
	assert(mesh != NULL);

	const unsigned int kSides = 16; // Number of divisions looking at the torus form the side
	const unsigned int kRings = 16; // Number of divisions looking at the torus from the top
									// so you can see the hole in the middle

	ID3DXMesh *temp = NULL; // Temp D3D mesh object

	// Create the torus
	// By paramter:
	// g3D->mDevice -- Pointer to the Direct3D device to be associated with the torus
	// innerRad -- Inner radius of the torus
	// outerRad -- Outside radius of the torus
	// kSides -- Number of sides in a cross-section of the torus 
	// kRings -- Number of rings in a cross-section of the torus
	// &temp -- A pointer to a ID3DXMesh*, it will get filled with the 
	//			the created mesh
	// NULL --  Optional pointer to a ID3DXBuffer, if a valid pointer was passed
	//			it would be filled with the adjacency information for each face in 
	//			the mesh.  By passing NULL, we say we don't want this information
	if(D3DXCreateTorus(g3D->mDevice, innerRad, outerRad, kSides, kRings, &temp, NULL) != D3D_OK)
		return false;

	// Next we clone the mesh.  This does two things.  First, it allows us to
	// specify the vertex format we want on the cloned mesh.  Second, it copies the 
	// current mesh data into the ID3DXMesh we passed to this function.
	// By parameter:
	// D3DXMESH_VB_MANAGED | D3DXMESH_IB_MANAGED -- Flags specifying how we want the mesh to be
	//												cloned.  This particular flag combo says
	//												"Have the vertex buffer and index buffer
	//												associated with this mesh be in pooled memory
	//												that DirectX manages for us."
	// SVertexType -- Flexible vertex format that we want the cloned mesh to be converted to
	// g3D->mDevice -- IDirect3DDevice9 to associate this mesh with
	// mesh -- A pointer to a ID3DXMesh* that will get filled with the cloned mesh   
	if(temp->CloneMeshFVF(D3DXMESH_VB_MANAGED | D3DXMESH_IB_MANAGED, SVertexType, 
						  g3D->mDevice, mesh) != D3D_OK)
	{
		return false;
	}

	// Okay so up to this point we've created a stock D3D torus, then converted 
	// it a torus with the FVF that we want.  Now were going to loop through each
	// vertex and set it to the color that we want it to be.

	SVertex *v;

	// Lock the vertex buffer
	if((*mesh)->LockVertexBuffer(0, (void**)&v) != D3D_OK)
	{	
		(*mesh)->Release();
		return false;
	}

	// Loop through all the verts in the mesh, setting each one's 
	// color to the color passed into the function
	for(unsigned int i = 0; i < (*mesh)->GetNumVertices(); ++i)
		v[i].color = color;

	// We're done with the vertex buffer, so unlock it so it may be used
	// by others
	(*mesh)->UnlockVertexBuffer();

	temp->Release(); // Last but not least, free up the temporary mesh
		return true;
}
Ejemplo n.º 15
0
//
// Framework Functions
//
bool Setup()
{
	//
	// Create the objects.
	//

	D3DXCreateTeapot(
		Device,
		&Objects[0],
		0);

	D3DXCreateBox(
		Device,
		2.0f, // width
		2.0f, // height
		2.0f, // depth
		&Objects[1],
		0);

	// cylinder is built aligned on z-axis
	D3DXCreateCylinder(
		Device,
		1.0f, // radius at negative z end
		1.0f, // radius at positive z end
		3.0f, // length of cylinder
		10,   // slices
		10,   // stacks
		&Objects[2],
		0);

	D3DXCreateTorus(
		Device,
		1.0f, // inner radius
		3.0f, // outer radius
		10,   // sides
		10,   // rings
		&Objects[3],
		0);

	D3DXCreateSphere(
		Device,
		1.0f, // radius
		10,   // slices
		10,   // stacks
		&Objects[4],
		0);

	//
	// Build world matrices - position the objects in world space.
	// For example, ObjWorldMatrices[1] will position Objects[1] at
	// (-5, 0, 5).  Likewise, ObjWorldMatrices[2] will position
	// Objects[2] at (5, 0, 5).
	//

	D3DXMatrixTranslation(&ObjWorldMatrices[0],  0.0f, 0.0f,  0.0f);
	D3DXMatrixTranslation(&ObjWorldMatrices[1], -5.0f, 0.0f,  5.0f);
	D3DXMatrixTranslation(&ObjWorldMatrices[2],  5.0f, 0.0f,  5.0f);
	D3DXMatrixTranslation(&ObjWorldMatrices[3], -5.0f, 0.0f, -5.0f);
	D3DXMatrixTranslation(&ObjWorldMatrices[4],  5.0f, 0.0f, -5.0f);

	//
	// Set the projection matrix.
	//

	D3DXMATRIX proj;
	D3DXMatrixPerspectiveFovLH(
			&proj,
			D3DX_PI * 0.5f, // 90 - degree
			(float)Width / (float)Height,
			1.0f,
			1000.0f);
	Device->SetTransform(D3DTS_PROJECTION, &proj);

	//
	// Switch to wireframe mode.
	//

	Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);

	return true;
}
Ejemplo n.º 16
0
//
// Framework Functions
//
bool Setup()
{
	//
	// Create objects.
	//

	D3DXCreateTeapot(Device, &Objects[0], 0);
	D3DXCreateSphere(Device, 1.0f, 20, 20, &Objects[1], 0);
	D3DXCreateTorus(Device, 0.5f, 1.0f, 20, 20, &Objects[2], 0);
	D3DXCreateCylinder(Device, 0.5f, 0.5f, 2.0f, 20, 20, &Objects[3], 0);

	//
	// Build world matrices - position the objects in world space.
	//

	D3DXMatrixTranslation(&Worlds[0],  0.0f,  2.0f, 0.0f);
	D3DXMatrixTranslation(&Worlds[1],  0.0f, -2.0f, 0.0f);
	D3DXMatrixTranslation(&Worlds[2], -3.0f,  0.0f, 0.0f);
	D3DXMatrixTranslation(&Worlds[3],  3.0f,  0.0f, 0.0f);
	D3DXMATRIX Rx;
	D3DXMatrixRotationX(&Rx, D3DX_PI * 0.5f);
	Worlds[3] = Rx * Worlds[3];

	//
	// Setup the object's materials.
	//

	Mtrls[0] = d3d::RED_MTRL;
	Mtrls[1] = d3d::BLUE_MTRL;
	Mtrls[2] = d3d::GREEN_MTRL;
	Mtrls[3] = d3d::YELLOW_MTRL;

	for(int i = 0; i < 4; i++)
		Mtrls[i].Power = 20.0f;

	//
	// Setup a spot light
	//

	D3DXVECTOR3 pos(0.0f, 0.0f, -5.0f);
	D3DXVECTOR3 dir(0.0f, 0.0f,  1.0f);
	D3DXCOLOR   c = d3d::WHITE;
	Spot = d3d::InitSpotLight(&pos, &dir, &c);

	//
	// Set and Enable spotlight.
	//

	Device->SetLight(0, &Spot);
	Device->LightEnable(0, true);

	//
	// Set light related render states.
	//

	Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
	Device->SetRenderState(D3DRS_SPECULARENABLE, true);

	//
	// Position and aim the camera.
	//
	D3DXVECTOR3 position( 0.0f, 0.0f, -5.0f );
	D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	D3DXMATRIX V;
	D3DXMatrixLookAtLH(&V, &position, &target, &up);
	Device->SetTransform(D3DTS_VIEW, &V);

	//
	// Set the projection matrix.
	//

	D3DXMATRIX proj;
	D3DXMatrixPerspectiveFovLH(
			&proj,
			D3DX_PI * 0.5f, // 90 - degree
			(float)Width / (float)Height,
			1.0f,
			1000.0f);
	Device->SetTransform(D3DTS_PROJECTION, &proj);

	return true;
}