Пример #1
0
int JRenderServer::AddDirLight( const Vec3& dir, DWORD diffuse, DWORD specular, bool bPerPixel )
{
    D3DLIGHT8 light;
    light.Type          = D3DLIGHT_DIRECTIONAL ;            
    light.Diffuse       = ConvertColor( diffuse  );
    light.Specular      = ConvertColor( specular );        
    light.Ambient       = ConvertColor( 0xFF000000 );   

    Vec3 ldir( dir.x, dir.y, dir.z );
    ldir.normalize();
    light.Direction.x   = ldir.x; 
    light.Direction.y   = ldir.y;
    light.Direction.z   = ldir.z;

    light.Range         = 100.0f;
    light.Position.x    = 0.0f; 
    light.Position.y    = 0.0f;
    light.Position.z    = 0.0f;

    light.Attenuation0  = 0.0f;
    light.Attenuation1  = 0.0f;
    light.Attenuation2  = 0.0f;

    light.Falloff       = 0.0f;
    light.Theta         = c_PI;
    light.Phi           = c_PI;

    IDirect3DDevice8* pDevice = GetDirect3DDevice8();
    pDevice->LightEnable( 0, TRUE );
    pDevice->SetLight   ( 0, &light );
    
    return 0;
} // JRenderServer::AddDirLight
Пример #2
0
bool DG8DynamicVB::Initialise(int nv, int ni, int nc_p, int nc_t)
{
	int result = 0;
	
	m_numvertex	= nv;
	m_numindex	= ni;

	IDirect3DDevice8 *dev = ((DG8Graphics *)fusion->Graphics)->m_RenderDevice;

	result = dev->CreateVertexBuffer(	nv*sizeof(CUSTOMVERTEX),
										D3DUSAGE_DYNAMIC,
										D3DFVF_CUSTOMVERTEX,
										D3DPOOL_DEFAULT,
										&m_vertexbuffer);

	if(result != D3D_OK) return false;

	result = dev->CreateIndexBuffer(	ni*sizeof(int),
										D3DUSAGE_DYNAMIC,
										D3DFMT_INDEX32,
										D3DPOOL_DEFAULT,
										&m_indexbuffer);

	CUSTOMVERTEX *ptr = NULL;

	result = m_vertexbuffer->Lock(0,0,(unsigned char **)&ptr,D3DLOCK_DISCARD);

	if(result == D3D_OK){
		for(int a=0;a<nv;a++){
			ptr[a].colour = DG8COLOUR(	(char)m_material.Diffuse.r*256,
																	(char)m_material.Diffuse.g*256,
																	(char)m_material.Diffuse.b*256,
																	(char)m_material.Diffuse.a*256);
		}
	}

	m_vertexbuffer->Unlock();

	if(result != D3D_OK) return false;

	return true;
}
Пример #3
0
void DG8DynamicVB::Render(void)
{
	DG8Graphics *g = reinterpret_cast<DG8Graphics *>(fusion->Graphics);
	IDirect3DDevice8 *dev = g->m_RenderDevice;

	if(dev != NULL && m_vertexbuffer != NULL){
		dev->SetMaterial(&m_material);
		dev->SetIndices(m_indexbuffer,0);
		dev->SetStreamSource( 0, m_vertexbuffer, sizeof(CUSTOMVERTEX) );
		dev->SetVertexShader( D3DFVF_CUSTOMVERTEX );
		dev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,m_numvertex,0,m_numvertex/3);
	}
}
Пример #4
0
void JRenderServer::SetAmbient( DWORD color )
{
    m_Ambience = color;
    IDirect3DDevice8* pDevice = GetDirect3DDevice8();
    pDevice->SetRenderState( D3DRS_AMBIENT, color );
} // JRenderServer::SetAmbient
Пример #5
0
HRESULT RacorX5::RestoreDeviceObjects()
{
	HRESULT hr;

	IDirect3DDevice8* device;
	hr = m_spD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, m_iVP, &m_dpps, &device);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreateDevice failed!", L"Error", 0);
		return E_FAIL;
	}
	m_spDevice.reset(device, [](IDirect3DDevice8* device) { device->Release(); });

	VERTICES cp[9];
	cp[0].vPosition = D3DXVECTOR3(-1, 1, 0);	 cp[0].uv = D3DXVECTOR2(0.0f, 0.0f);
	cp[1].vPosition = D3DXVECTOR3(0, 1, 0);      cp[1].uv = D3DXVECTOR2(0.5f, 0.0f);
	cp[2].vPosition = D3DXVECTOR3(1, 1, 0);      cp[2].uv = D3DXVECTOR2(1.0f, 0.0f);

	cp[3].vPosition = D3DXVECTOR3(-1, 0, 0);     cp[3].uv = D3DXVECTOR2(0.0f, 0.5f);
	cp[4].vPosition = D3DXVECTOR3(0, 0, -2);     cp[4].uv = D3DXVECTOR2(0.5f, 0.5f);
	cp[5].vPosition = D3DXVECTOR3(1, 0, 0);      cp[5].uv = D3DXVECTOR2(1.0f, 0.5f);

	cp[6].vPosition = D3DXVECTOR3(-1, -1, 0);    cp[6].uv = D3DXVECTOR2(0.0f, 1.0f);
	cp[7].vPosition = D3DXVECTOR3(0, -1, 0);     cp[7].uv = D3DXVECTOR2(0.5f, 1.0f);
	cp[8].vPosition = D3DXVECTOR3(1, -1, 0);     cp[8].uv = D3DXVECTOR2(1.0f, 1.0f);

	m_spBPatch->m_dwULevel = 4;
	m_spBPatch->m_dwVLevel = 4;
	m_spBPatch->Create(cp, 3, 3);
	m_spBPatch->RestoreDeviceObjects(m_spDevice.get());
	DWORD dwDecl[] = {
		D3DVSD_STREAM(0),
		D3DVSD_REG(0, D3DVSDT_FLOAT3),
		D3DVSD_REG(3, D3DVSDT_FLOAT3),
		D3DVSD_REG(7, D3DVSDT_FLOAT2),
		D3DVSD_END()
	};

	hr = CreateVSFromBinFile(m_spDevice.get(), dwDecl, L"point_light.vso", &m_dwVSH);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreateVSFromBinFile failed!", L"Error", 0);
		return E_FAIL;
	}
	
	IDirect3DTexture8* backgroud;
	hr = D3DXCreateTextureFromFile(m_spDevice.get(), _T("ShaderX.tga"), &backgroud);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"D3DXCreateTextureFromFile failed!", L"Error", 0);
		return E_FAIL;
	}
	m_spBackground.reset(backgroud, [](IDirect3DTexture8* background){ background->Release(); });

	m_spDevice->SetViewport(&m_Viewport);

	m_spDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE);
	m_spDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
	m_spDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
	m_spDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

	//m_spDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
	m_spDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE);

	m_spDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
	m_spDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	m_spDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
	m_spDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
	m_spDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
	m_spDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
	m_spDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
	m_spDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
	m_spDevice->SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
	m_spDevice->SetTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);

	m_spDevice->SetVertexShaderConstant(SPEC_POWER, D3DXVECTOR4(0,10,25,50),1);

	//D3DXVECTOR3 vLight(0, 0, 1);
	//m_spDevice->SetVertexShaderConstant(LIGHT_POSITION, vLight, 1);

	D3DXCOLOR matDiffuse(0.9f, 0.9f, 0.9f, 1.0f);
	m_spDevice->SetVertexShaderConstant(DIFFUSE_COLOR, &matDiffuse, 1);

	D3DXVECTOR4 Attenuation = { 1.0f, 0.0f, 0.0f, 0.0f };
	m_spDevice->SetVertexShaderConstant(LIGHT_ATTENUATION, Attenuation, 1);

	return S_OK;
}
Пример #6
0
HRESULT RacorX8::RestoreDeviceObjects()
{
	HRESULT hr;

	IDirect3DDevice8* device;
	hr = m_spD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, m_iVP, &m_dpps, &device);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreateDevice failed!", L"Error", 0);
		return E_FAIL;
	}
	m_spDevice.reset(device, [](IDirect3DDevice8* device) { device->Release(); });

	//CreateSphere();

	LoadXFile("sphere.x");

	IDirect3DVertexBuffer8* vbNormal;
	hr = m_spDevice->CreateVertexBuffer(m_iNumVertices * 2 * sizeof SimpleVertex, D3DUSAGE_WRITEONLY, D3DFVF_XYZ, D3DPOOL_MANAGED, &vbNormal);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreateVertexBuffer failed!", L"Error", 0);
		return E_FAIL;
	}
	hr = CreateNormal<ShaderVertex, SimpleVertex>(m_spVB.get(), vbNormal);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreateNormal failed!", L"Error", 0);
		return E_FAIL;
	}
	m_spVBNormal.reset(vbNormal, [](IDirect3DVertexBuffer8* vbNormal){vbNormal->Release(); });

	IDirect3DVertexBuffer8* vbTangent;
	hr = m_spDevice->CreateVertexBuffer(m_iNumVertices * 2 * sizeof SimpleVertex, D3DUSAGE_WRITEONLY, D3DFVF_XYZ, D3DPOOL_MANAGED, &vbTangent);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreateVertexBuffer failed!", L"Error", 0);
		return E_FAIL;
	}
	hr = CreateTangent<ShaderVertex, SimpleVertex>(m_spVB.get(), vbTangent);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreateTangent failed!", L"Error", 0);
		return E_FAIL;
	}
	m_spVBTangent.reset(vbTangent, [](IDirect3DVertexBuffer8* vbTangent){vbTangent->Release(); });

	//DWORD dwDecl[MAX_FVF_DECL_SIZE];
	//ZeroMemory(dwDecl, sizeof dwDecl);
	//pSphere->GetDeclaration(dwDecl);
	DWORD decl[] = {
		D3DVSD_STREAM(0),
		D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),
		D3DVSD_REG(D3DVSDE_NORMAL, D3DVSDT_FLOAT3),
		D3DVSD_REG(D3DVSDE_TEXCOORD0, D3DVSDT_FLOAT2),
		D3DVSD_REG(D3DVSDE_TEXCOORD1, D3DVSDT_FLOAT3),
		D3DVSD_END()
	};
	hr = CreateVSFromBinFile(m_spDevice.get(), decl, L"specular.vso", &m_dwVSH);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreateVSFromBinFile failed!", L"Error", 0);
		return E_FAIL;
	}

	hr = CreatePSFromBinFile(m_spDevice.get(), L"specular.pso", &m_dwPSH);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreatePSFromBinFile failed!", L"Error", 0);
		return E_FAIL;
	}

	hr = CreatePSFromBinFile(m_spDevice.get(), L"specularBump.pso", &m_dwPSHBump);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreatePSFromBinFile failed!", L"Error", 0);
		return E_FAIL;
	}

	if (m_bPS14Avaliable)
	{
		hr = CreatePSFromBinFile(m_spDevice.get(), L"specularBump14.pso", &m_dwPSHBump14);
		if (FAILED(hr))
		{
			MessageBox(m_hWnd, L"CreatePSFromBinFile failed!", L"Error", 0);
			return E_FAIL;
		}
	}
	DWORD declLine[] = {
		D3DVSD_STREAM(0),
		D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),
		D3DVSD_END()
	};
	hr = CreateVSFromBinFile(m_spDevice.get(), declLine, L"line.vso", &m_dwVSHLine);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreateVSFromBinFile failed!", L"Error", 0);
		return E_FAIL;
	}
	hr = CreatePSFromBinFile(m_spDevice.get(), L"line.pso", &m_dwVPHLine);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreatePSFromBinFile failed!", L"Error", 0);
		return E_FAIL;
	}

	IDirect3DTexture8* color_map;
	hr = D3DXCreateTextureFromFile(m_spDevice.get(), _T("earth.bmp"), &color_map);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"D3DXCreateTextureFromFile failed!", L"Error", 0);
		return E_FAIL;
	}
	m_spColorMap.reset(color_map, [](IDirect3DTexture8* color_map){ color_map->Release(); });

	IDirect3DTexture8* heightMap;
	hr = D3DXCreateTextureFromFile(m_spDevice.get(), _T("earthbump.bmp"), &heightMap);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"D3DXCreateTextureFromFile failed!", L"Error", 0);
		return E_FAIL;
	}
	m_spHeightMap.reset(heightMap, [](IDirect3DTexture8* heightMap) { heightMap->Release(); });

	D3DSURFACE_DESC desc;
	m_spHeightMap->GetLevelDesc(0, &desc);

	IDirect3DTexture8* normalMap;
	hr = D3DXCreateTexture(m_spDevice.get(), desc.Width, desc.Height, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &normalMap);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"D3DXCreateTexture failed!", L"Error", 0);
		return E_FAIL;
	}
	m_spNormalMap.reset(normalMap, [](IDirect3DTexture8* normalMap){ normalMap->Release(); });

	hr = D3DXComputeNormalMap(m_spNormalMap.get(), m_spHeightMap.get(), NULL, 0, D3DX_CHANNEL_RED, 10);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"D3DXComputeNormalMap failed!", L"Error", 0);
		return E_FAIL;
	}
	
	IDirect3DTexture8* power;
	hr = D3DXCreateTexture(m_spDevice.get(), 256, 256, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &power);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"D3DXCreateTexture failed!", L"Error", 0);
		return E_FAIL;
	}
	m_spPower.reset(power, [](IDirect3DTexture8* power){ power->Release();  });
	FLOAT fPower = 16.0f;
	hr = D3DXFillTexture(power, LightEval, &fPower);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"D3DXFillTexture failed!", L"Error", 0);
		return E_FAIL;
	}

	m_spDevice->SetViewport(&m_Viewport);

	m_spDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE);
	m_spDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
	
	m_spDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);

	m_spDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
	m_spDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);

	//m_spDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
	//m_spDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
	//m_spDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE);

	//m_spDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
	//m_spDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	//m_spDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
	//m_spDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
	//m_spDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
	m_spDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
	m_spDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
	//m_spDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
	//m_spDevice->SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
	//m_spDevice->SetTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);

	m_spDevice->SetTextureStageState(2, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
	m_spDevice->SetTextureStageState(2, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);

	D3DXVECTOR4 vLight(0.0f, 0.0f, 1.0f, 0.0f);
	D3DXVec4Normalize(&vLight, &vLight);
	m_spDevice->SetVertexShaderConstant(12, &vLight, 1);

	D3DXVECTOR4 half(0.5f, 0.5f, 0.5f, 0.5f);
	m_spDevice->SetVertexShaderConstant(33, &half, 1);

	return S_OK;
}
Пример #7
0
HRESULT HelloShadowVolume::RestoreDeviceObjects()
{
    HRESULT hr;
    IDirect3DDevice8* device;
    hr = m_spD3D->CreateDevice(
             D3DADAPTER_DEFAULT,
             D3DDEVTYPE_HAL,
             m_hWnd,
             D3DCREATE_HARDWARE_VERTEXPROCESSING,
             &m_dpps,
             &device);
    if (FAILED(hr))
    {
        MessageBox(0, L"CreateDevice failed", 0, 0);
        return E_FAIL;
    }
    m_spDevice.reset(device, [](IDirect3DDevice8* device) {
        device->Release();
    });

    m_spDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
    m_spDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
    m_spDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE);
    D3DVIEWPORT8 viewport = { 0, 0, m_iWidth, m_iHeight };
    m_spDevice->SetViewport(&viewport);

    D3DXVECTOR3 eye(0.0f, 0.0f, 30.0f);
    D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
    D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
    D3DXMatrixLookAtLH(&m_mtView, &eye, &target, &up);

    D3DXMatrixPerspectiveFovLH(&m_mtProj, 0.2*D3DX_PI, (float)m_iWidth / (float)m_iHeight, 1.0f, 100.f);

    m_cPlaneTint = { 0.7f, 0.6f, 0.4f, 1.0f };


    ID3DXMesh* plane;
    //D3DXCreatePolygon(m_spDevice.get(), 2.0f, 4, &plane, NULL);
    CreatePlane(m_spDevice.get(), 15.0f, 10, &plane);
    //D3DXCreateSphere(m_spDevice.get(), 1.0f,20,20, &plane, NULL);

    IDirect3DVertexBuffer8* vb;
    IDirect3DIndexBuffer8* ib;
    plane->GetVertexBuffer(&vb);
    plane->GetIndexBuffer(&ib);
    m_spPlaneVB.reset(vb, [](IDirect3DVertexBuffer8* vb) {
        vb->Release();
    });
    m_spPlaneIB.reset(ib, [](IDirect3DIndexBuffer8* ib) {
        ib->Release();
    });
    m_dwPlaneNumVertices = plane->GetNumVertices();
    m_dwPlaneNumFaces = plane->GetNumFaces();

    plane->Release();

    DWORD decl[] = {
        D3DVSD_STREAM(0),
        D3DVSD_REG(0, D3DVSDT_FLOAT3),
        D3DVSD_REG(3, D3DVSDT_FLOAT3),
        D3DVSD_END()
    };
    hr = CreateVSFromBinFile(m_spDevice.get(), decl, L"plane.vso", &m_dwPlaneVSH);
    if (FAILED(hr))
    {
        MessageBox(0, 0, L"CreateVSFromBinFile failed", 0);
        return E_FAIL;
    }
    hr = CreatePSFromBinFile(m_spDevice.get(), L"plane.pso", &m_dwPlanePSH);
    if (FAILED(hr))
    {
        MessageBox(0, 0, L"CreatePSFromBinFile failed", 0);
        return E_FAIL;
    }

    D3DXMATRIX Rx, Tz;
    D3DXMatrixRotationX(&Rx, D3DX_PI*0.5f);
    D3DXMatrixTranslation(&Tz, 0.0f, -3.0f, 0.0f);
    m_mtPlaneWorld = Rx * Tz;

    ID3DXMesh* occluder;
    CreateOccluder(m_spDevice.get(), &occluder);
    IDirect3DVertexBuffer8* vbOccluder;
    IDirect3DIndexBuffer8* ibOccluder;
    occluder->GetVertexBuffer(&vbOccluder);
    occluder->GetIndexBuffer(&ibOccluder);
    m_spOccluderVB.reset(vbOccluder, [](IDirect3DVertexBuffer8* vb) {
        vb->Release();
    });
    m_spOccluderIB.reset(ibOccluder, [](IDirect3DIndexBuffer8* ib) {
        ib->Release();
    });
    m_dwOccluderNumVertices = occluder->GetNumVertices();
    m_dwOccluderNumFaces = occluder->GetNumFaces();
    occluder->Release();

    hr = CreateVSFromBinFile(m_spDevice.get(), decl, L"occluder.vso", &m_dwOccluderVSH);
    if (FAILED(hr))
    {
        MessageBox(0, 0, L"CreateVSFromBinFile failed", 0);
        return E_FAIL;
    }
    hr = CreatePSFromBinFile(m_spDevice.get(), L"occluder.pso", &m_dwOccluderPSH);
    if (FAILED(hr))
    {
        MessageBox(0, 0, L"CreatePSFromBinFile failed", 0);
        return E_FAIL;
    }
    m_cOccluderTint = { 0.3f, 0.0f, 0.8f, 1.0f };
    D3DXMATRIX Rz, T;
    D3DXMatrixTranslation(&T, 5.1f * cosf(0.5), 0.0f, 5.1f * sinf(0.5));
    D3DXMatrixIdentity(&m_mtVolumeWorld);
    D3DXMatrixRotationZ(&Rz, 0.5f);
    m_mtOccluderWorld = T * Rz;

    ID3DXMesh* volume;
    CreateVolume(m_spDevice.get(), &volume);
    IDirect3DVertexBuffer8* vbVolume;
    IDirect3DIndexBuffer8* ibVolume;
    volume->GetVertexBuffer(&vbVolume);
    volume->GetIndexBuffer(&ibVolume);
    m_spVolumeVB.reset(vbVolume, [](IDirect3DVertexBuffer8* vb) {
        vb->Release();
    });
    m_spVolumeIB.reset(ibVolume, [](IDirect3DIndexBuffer8* ib) {
        ib->Release();
    });
    m_dwVolumeNumVertices = volume->GetNumVertices();
    m_dwVolumeNumFaces = volume->GetNumFaces();
    volume->Release();

    hr = CreateVSFromBinFile(m_spDevice.get(), decl, L"volume.vso", &m_dwVolumeVSH);
    if (FAILED(hr))
    {
        MessageBox(0, 0, L"CreateVSFromBinFile failed", 0);
        return E_FAIL;
    }
    hr = CreatePSFromBinFile(m_spDevice.get(), L"volume.pso", &m_dwVolumePSH);
    if (FAILED(hr))
    {
        MessageBox(0, 0, L"CreatePSFromBinFile failed", 0);
        return E_FAIL;
    }
    m_cVolumeTint = { 0.7f, 0.0f, 0.0f, 1.0f };

    D3DXMATRIX Sx;
    D3DXMatrixIdentity(&m_mtVolumeWorld);
    D3DXMatrixScaling(&Sx, 6.0f, 1.0f, 1.0f);
    D3DXMatrixRotationZ(&Rz, 0.5f);
    m_mtVolumeWorld = Sx * Rz;

    return S_OK;
}
Пример #8
0
HRESULT CRacorX::RestoreDeviceObjects()
{
	IDirect3DDevice8* device;
	HRESULT hr = m_spD3D->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		m_hWnd,
		m_iVP,
		&m_dpps,
		&device);
	if (FAILED(hr)) {
		OutputDebugString(L"Create Device Failed! Error:\n");
		switch (hr)
		{
		case D3DERR_DEVICELOST:
			OutputDebugString(L"D3DERR_DEVICELOST\n");
			break;
		case D3DERR_INVALIDCALL:
			OutputDebugString(L"D3DERR_INVALIDCALL\n");
			break;
		case D3DERR_NOTAVAILABLE:
			OutputDebugString(L"D3DERR_NOTAVAILABLE\n");
			break;
		case D3DERR_OUTOFVIDEOMEMORY:
			OutputDebugString(L"D3DERR_OUTOFVIDEOMEMORY\n");
			break;;
		default:
			OutputDebugString(L"Unknown\n");
			break;
		}
		return S_FALSE;
	}
	m_spDevice.reset(device, [](IDirect3DDevice8* device){ device->Release(); });
	DWORD dwDecl0[] = {
		D3DVSD_STREAM(0),
		D3DVSD_REG(0,D3DVSDT_FLOAT3),
		//D3DVSD_REG(5,D3DVSDT_D3DCOLOR),
		/*D3DVSD_CONST(0, 4),
		*(DWORD*)&m_mtWorld[0], *(DWORD*)&m_mtWorld[1], *(DWORD*)&m_mtWorld[2], *(DWORD*)&m_mtWorld[3],
		*(DWORD*)&m_mtWorld[4], *(DWORD*)&m_mtWorld[5], *(DWORD*)&m_mtWorld[6], *(DWORD*)&m_mtWorld[7],
		*(DWORD*)&m_mtWorld[8], *(DWORD*)&m_mtWorld[9], *(DWORD*)&m_mtWorld[10], *(DWORD*)&m_mtWorld[11],
		*(DWORD*)&m_mtWorld[12], *(DWORD*)&m_mtWorld[13], *(DWORD*)&m_mtWorld[14], *(DWORD*)&m_mtWorld[15],
		D3DVSD_CONST(0, 1),
		*(DWORD*)&m_fMaterial[0], *(DWORD*)&m_fMaterial[1], *(DWORD*)&m_fMaterial[2], *(DWORD*)&m_fMaterial[3],*/
		D3DVSD_END()
	};

	IDirect3DVertexBuffer8* vb;
	m_spDevice->CreateVertexBuffer(
		4 * sizeof (Vertex),
		D3DUSAGE_WRITEONLY,
		Vertex::FVF,
		D3DPOOL_MANAGED,
		&vb);
	m_spVB.reset(vb, [](IDirect3DVertexBuffer8* vb){ vb->Release(); });

	Vertex* vertices = 0;
	m_spVB->Lock(0, 0, reinterpret_cast<BYTE**>(&vertices), 0);
	vertices[0] = { -100.0f, -100.0f, 0.0f, };
	vertices[1] = { 100.0f, -100.0f, 0.0f, };
	vertices[2] = { 100.0f, 100.0f, 0.0f, };
	vertices[3] = { -100.0f, 100.0f, 0.0f, };
	/*
	vertices[0] = { -1.0f, -1.0f, 0.2f, };
	vertices[1] = { 1.0f, -1.0f, 0.2f, };
	vertices[2] = { 1.0f, 1.0f, 0.2f, };
	vertices[3] = { -1.0f, 1.0f, 0.2f, };
	*/
	m_spVB->Unlock();

	IDirect3DIndexBuffer8* ib;
	m_spDevice->CreateIndexBuffer(
		6 * sizeof(WORD),
		D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX16,
		D3DPOOL_MANAGED,
		&ib);
	m_spIB.reset(ib, [](IDirect3DIndexBuffer8* ib){ ib->Release(); });

	WORD *indices = 0;
	m_spIB->Lock(0, 0, reinterpret_cast<BYTE**>(&indices), 0);

	indices[0] = 0;
	indices[1] = 1;
	indices[2] = 2;
	indices[3] = 0;
	indices[4] = 2;
	indices[5] = 3;

	m_spIB->Unlock();
	
	const char vsh[] = 
		"vs.1.1 \n" \
		"dp4 oPos.x, v0, c0 \n"\
		"dp4 oPos.y, v0, c1 \n"\
		"dp4 oPos.z, v0, c2 \n"\
		"dp4 oPos.w, v0, c3 \n"\
		"mov oD0, c4\n";
	/*
	const char vsh[] = 
		"vs.1.1 \n" \
		"mov oPos, v0 \n" \
		"mov oD0, c4 \n";
		*/
	ID3DXBuffer* pVBuffer;
	ID3DXBuffer* pErrors;
	HRESULT rc = D3DXAssembleShader(reinterpret_cast<LPCVOID>(vsh), sizeof(vsh) - 1, 0, NULL, &pVBuffer, &pErrors);
	if (FAILED(rc))
	{
		OutputDebugString(L"Failed to assemble the vertex shader, error:\n");
		OutputDebugStringA(reinterpret_cast<CHAR*>(pErrors->GetBufferPointer()));
		OutputDebugString(L"\n");
	}

	rc = m_spDevice->CreateVertexShader(dwDecl0, (DWORD*)pVBuffer->GetBufferPointer(), &m_dwVertexShader, 0);
	if (FAILED(rc))
	{
		OutputDebugString(L"Failed to create vertex shader, error:\n");
		WCHAR szBuffer[512] = { 0 };
		D3DXGetErrorString(rc, szBuffer, sizeof(szBuffer));
		OutputDebugString(szBuffer);
		OutputDebugString(L"\n");
	}
	m_spDevice->SetViewport(&m_Viewport);
	//m_spDevice->SetTransform(D3DTS_VIEW, &m_mtView);
	//m_spDevice->SetTransform(D3DTS_PROJECTION, &m_mtProj);
	m_spDevice->SetRenderState(D3DRS_ZENABLE, true);
	m_spDevice->SetRenderState(D3DRS_LIGHTING, false);
	//m_spDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
	m_spDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	return S_OK;
}