Exemplo n.º 1
0
Mesh::Mesh(char *catalog, char *filename)
{
	char filepath[222];
	LPD3DXBUFFER pMtrlBuffer = NULL;
	pAdjBuffer = 0;
	strcpy(filepath, catalog);
	strcat(filepath, filename);
	if (FAILED(D3DXLoadMeshFromX(filepath, D3DXMESH_MANAGED, gdevice,
		&pAdjBuffer, &pMtrlBuffer, NULL, &Mtnum, &m_pmesh))) {
		MessageBox(0, "load mesh fail ", 0, 0);
		return;
	}

	//二,读取材质和纹理数据  
	D3DXMATERIAL*pMtrls = (D3DXMATERIAL*)pMtrlBuffer->GetBufferPointer(); //创建一个D3DXMATERIAL结构体用于读取材质和纹理信息  
	pMaterial = new D3DMATERIAL9[Mtnum];
	pTexture = new LPDIRECT3DTEXTURE9[Mtnum];

	for (DWORD i = 0; i<Mtnum; i++) {
		//获取材质,并设置环境光的颜色值  
		pMaterial[i] = pMtrls[i].MatD3D;
		pMaterial[i].Ambient = pMaterial[i].Diffuse;
		strcpy(filepath, catalog);
		strcat(filepath, pMtrls[i].pTextureFilename);
		if(FAILED(D3DXCreateTextureFromFileA(gdevice, filepath, &pTexture[i]))) 
			MessageBox(0, "load mesh texture fail ", 0, 0);
	}

	LoadData();			// load vertex and face 

	RELEASE(pMtrlBuffer);
}
Exemplo n.º 2
0
HRESULT initGeometry(){
	LPD3DXBUFFER pD3DXMtrlBuffer;
	if(FAILED(D3DXLoadMeshFromX(L"Heli.x", D3DXMESH_MANAGED,
		g_pDevice, NULL, &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials, &g_pMesh)))
		return E_FAIL;
	//Extract material & texture
	D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
	g_pMeshMaterials = new D3DMATERIAL9[g_dwNumMaterials];

	if(g_pMeshMaterials == NULL)
		return E_OUTOFMEMORY;

	g_pMeshTextures = new LPDIRECT3DTEXTURE9[g_dwNumMaterials];
	if(g_pMeshTextures == NULL)
		return E_OUTOFMEMORY;
	//Extract
	for(DWORD i=0; i<g_dwNumMaterials; ++i){
		g_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;
		g_pMeshMaterials[i].Ambient = g_pMeshMaterials[i].Diffuse;

		g_pMeshTextures[i] = NULL;
		if(d3dxMaterials[i].pTextureFilename != NULL && strlen(d3dxMaterials[i].pTextureFilename) > 0){
			WCHAR name[256];
			removePathFromFileName(d3dxMaterials[i].pTextureFilename, name);

			if(FAILED(D3DXCreateTextureFromFile(g_pDevice, name, &g_pMeshTextures[i]))){
				MessageBox(NULL, L"Cound not find texture file", L"initGeometry()", MB_OK);
			}
		}
	}
	pD3DXMtrlBuffer->Release();

	return S_OK;
};
Exemplo n.º 3
0
HRESULT Mesh::LoadMesh(WCHAR* directory, WCHAR* name, WCHAR* extension)
{
  HRESULT hr;
  
  CleanUpMesh();
    
  SetDirectory( directory );
  SetName( name );
  WCHAR meshfile[120];
  WCHAR meshpathrel[120];
  WCHAR meshpath[120];
  Concat(meshfile, name, extension );
  Concat(meshpathrel, directory, meshfile );
  AppendToRootDir(meshpath, meshpathrel);

  hr = D3DXLoadMeshFromX( meshpath, 
                          D3DXMESH_MANAGED | D3DXMESH_32BIT, mDevice, NULL, 
                          &mMaterialBuffer, NULL, &mNumMaterials, &mMesh );
  
  PD(hr, L"load mesh from file");
  if(FAILED(hr)) return hr;

  mMaterials = (D3DXMATERIAL*)mMaterialBuffer->GetBufferPointer();
    
  PD( AdjustMeshDecl(), L"adjust mesh delaration" );
  PD( AttribSortMesh(), L"attribute sort mesh" );
  PD( LoadTextures(), L"load textures" );
	PD( CreateTopologyFromMesh(), L"create topology from mesh");
  return D3D_OK;
}
Exemplo n.º 4
0
void x_mesh_t::load( LPCWSTR file_name, LPDIRECT3DDEVICE9 device )
{
    ID3DXBuffer *materials_buf = NULL;

    HRESULT hr = D3DXLoadMeshFromX(file_name, 0, device, NULL, &materials_buf, NULL, &m_materials_count, &m_mesh);
    if (hr != ERROR_SUCCESS)
        return;

    D3DXMATERIAL *materials_array = (D3DXMATERIAL *)materials_buf->GetBufferPointer();
    m_materials = new D3DMATERIAL9[m_materials_count];

    m_textures = new texture_t[m_materials_count];

    bool result;
    for (DWORD i = 0; i < m_materials_count; ++i)
    {
        m_materials[i] = materials_array[i].MatD3D;
        m_materials[i].Ambient = m_materials[i].Diffuse;

        std::wstring str;
        A2W(str, std::string(materials_array[i].pTextureFilename));

        result = m_textures[i].load(device, str.c_str());
    }
    if (materials_buf)
        materials_buf->Release();

    m_mesh->OptimizeInplace(D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT, NULL, NULL, NULL, NULL);
}
Exemplo n.º 5
0
bool ObjectInit(HWND hwnd)
{
	srand(unsigned(time(nullptr)));
	PlaySound(_T("コミネリサ - Resuscitated Hope.wav"), nullptr, SND_ASYNC | SND_FILENAME | SND_LOOP);
	D3DXCreateFont(gPD3DDevice, 30, 0, 0, 0, 0, 0, 0, 0, 0, _T("微软雅黑"), &gPTextAdapterFont);
	D3DXCreateFont(gPD3DDevice, 20, 0, 0, 0, 0, 0, 0, 0, 0, _T("华文中宋"), &gPTextHelperFont);
	D3DXCreateFont(gPD3DDevice, 30, 0, 0, 0, 0, 0, 0, 0, 0, _T("黑体"), &gPTextInfoFont);
	D3DXCreateFont(gPD3DDevice, 36, 0, 0, 0, 0, 0, 0, 0, 0, _T("楷体"), &gPTextFPSFont);

	//////////////////////////////////////////////////////////////////////////
	// Load mesh and materials here  
	//////////////////////////////////////////////////////////////////////////
	LPD3DXBUFFER pAdjBuffer;
	LPD3DXBUFFER pMtrlBuffer;
	D3DXLoadMeshFromX(_T("loli.X"), D3DXMESH_MANAGED, gPD3DDevice, &pAdjBuffer, &pMtrlBuffer, nullptr, &gDwNumMtrl, &gPCharacter);
	D3DXMATERIAL* pMaterial = (D3DXMATERIAL*)(pMtrlBuffer->GetBufferPointer());
	gPMaterial = new D3DMATERIAL9[gDwNumMtrl];
	gPTexture = new LPDIRECT3DTEXTURE9[gDwNumMtrl];
	for (DWORD i = 0; i < gDwNumMtrl; i++)
	{
		gPMaterial[i] = pMaterial->MatD3D;
		gPMaterial[i].Ambient = gPMaterial[i].Diffuse;
		gPTexture[i] = nullptr;
		D3DXCreateTextureFromFileA(gPD3DDevice, pMaterial[i].pTextureFilename, &gPTexture[i]);
	}

	pAdjBuffer->Release();
	SAFE_RELEASE(pMtrlBuffer);
	// 设置渲染状态
	gPD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);   //开启背面消隐
	gPD3DDevice->SetRenderState(D3DRS_AMBIENT, D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f)); //设置环境光

	return true;
}
Exemplo n.º 6
0
void OnInitial(DEVICEINSTANCE *device)
{
	XInputEnable(true);

	D3DXMATRIX projection;
	D3DXMatrixPerspectiveFovLH(&projection, D3DX_PI / 4, 800 / 600.0f, 1.0f, 10000.0f);
	device->d3dDevice->SetTransform(D3DTS_PROJECTION, &projection);

	D3DXLoadMeshFromX(L"skybox.x", 0, device->d3dDevice, nullptr, nullptr, nullptr, nullptr, &skybox);

	D3DXCreateTextureFromFile(device->d3dDevice, L"sky copy.png", &texture);

	D3DXCreateTextureFromFile(device->d3dDevice, L"terrain_01.jpg", &heightMap);
	D3DXCreateTextureFromFile(device->d3dDevice, L"terrain_02.jpg", &heightMapTexture);

	D3DXIMAGE_INFO heightMapInfo = { 0, };
	D3DXGetImageInfoFromFile(L"terrain_01.jpg", &heightMapInfo);
	terrainWidth = heightMapInfo.Width; terrainHeight = heightMapInfo.Height;

	D3DCOLOR * colors;
	D3DLOCKED_RECT lockedRect = { 0, };
	RECT rect = { 0, 0, terrainWidth, terrainHeight };
	terrainVertices = new TerrainVertex[numOfVertices = terrainWidth * terrainHeight];
	heightMap->LockRect(0, &lockedRect, &rect, 0);
	colors = (D3DCOLOR*)lockedRect.pBits;
	TerrainVertex * tempVertices = terrainVertices;
	for (int x = 0; x < terrainHeight; x++)
	{
		for (int z = 0; z < terrainWidth; z++)
		{
			int location = x * terrainWidth + z;
			*tempVertices = TerrainVertex(
				(x - terrainWidth / 2) * 5.0f, (colors[location] & 0xff) * 5.0f / 2,
				(z - terrainWidth / 2) * 5.0f,
				z / (float)terrainWidth, x / (float)terrainHeight
				);
			tempVertices++;
		}
	}
	heightMap->UnlockRect(0);
	heightMap->Release();

	terrainIndices = new int[numOfIndices = (terrainWidth - 1) * (terrainHeight - 1) * 2 * 3];
	struct Polygon { int index[3]; } *tempIndices = (Polygon*)terrainIndices;

	for (int z = 0; z < terrainHeight - 1; z++)
	{
		for (int x = 0; x < terrainWidth - 1; x++)
		{
			(*tempIndices).index[0] = z * terrainWidth + x;
			(*tempIndices).index[1] = z * terrainWidth + (x + 1);
			(*tempIndices).index[2] = (z + 1) * terrainWidth + x;
			tempIndices++;
			(*tempIndices).index[0] = (z + 1) * terrainWidth + x;
			(*tempIndices).index[1] = z * terrainWidth + (x + 1);
			(*tempIndices).index[2] = (z + 1) * terrainWidth + (x + 1);
			tempIndices++;
		}
	}
}
Exemplo n.º 7
0
BOOL DMeshRender::CreateMeshFromFileX(LPCWSTR pFileName, DWORD options)
{

	LPD3DXBUFFER ppAdjacency;
	LPD3DXBUFFER ppMaterials;
	LPD3DXBUFFER ppEffectInstances;
	DWORD NumMaterials;

	if (FAILED(D3DXLoadMeshFromX(pFileName, options, DDEInitialize::gRootDevice, 
		&ppAdjacency, &ppMaterials, &ppEffectInstances, &NumMaterials, &m_pMess)))
		return FALSE;

	D3DXMATERIAL *pMtrls = (D3DXMATERIAL*)ppMaterials->GetBufferPointer();
	LPDIRECT3DTEXTURE9 tmpTexture = nullptr;
	for (DWORD matRenderIndex = 0; matRenderIndex < NumMaterials; matRenderIndex++)
	{
		if (!FAILED(D3DXCreateTextureFromFileA(DDEInitialize::gRootDevice, pMtrls[matRenderIndex].pTextureFilename, &tmpTexture)))
		{
			DMaterialRender* matRender = new DMaterialRender(m_gameObj, 0, pMtrls[matRenderIndex].MatD3D, tmpTexture);
			m_matRenders.push_back(matRender);
		}
	}
	m_isEnabled = TRUE;
	return TRUE;
}
Exemplo n.º 8
0
DummyFace::DummyFace() {
    m_pMesh = NULL;
    m_pTexture = NULL;

    D3DXLoadMeshFromX("resources/face01.x", D3DXMESH_MANAGED, g_pDevice, NULL, NULL, NULL, NULL, &m_pMesh);
    D3DXCreateTextureFromFile(g_pDevice, "resources/face.jpg", &m_pTexture);
}
Exemplo n.º 9
0
void Mesh::loadMesh(LPCWSTR filename, pBuffer adjBuffer)
{
	pDevice device = Engine::DX::instance()->getDevice();
	D3DXLoadMeshFromX(filename, 
						D3DXMESH_MANAGED, 
						device, 
						&adjBuffer,
						&mat_buffer,
						&effect, 
						&numMaterials, 
						&mesh);

	// Pull material (including texture) information from loaded .x file
	D3DXMATERIAL *d3dxMaterials = (D3DXMATERIAL*)mat_buffer->GetBufferPointer();

	D3DMATERIAL9* mesh_material = new D3DMATERIAL9[numMaterials];

	for(DWORD i = 0; i < numMaterials; ++i)
	{
		//Copy the material
		mesh_material[i] = d3dxMaterials[i].MatD3D;

		//Set the ambient color (if needed) for the material (D3DX doesn't do this)
		mesh_material[i].Ambient = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f);
				
	}

	for(unsigned int i = 0; i < numMaterials; i++){
		materials.push_back(mesh_material[i]);
	}

	delete [] mesh_material;
}
Exemplo n.º 10
0
//=======================================================================
HRESULT init_geometry()
{
    LPD3DXBUFFER pD3DXMtrlBuffer;

    { // try load mesh from x-file
        HRESULT hr = D3DXLoadMeshFromX(g_xFname, D3DXMESH_SYSTEMMEM,
                                        g_pd3dDevice, NULL,
                                        &pD3DXMtrlBuffer, NULL, &g_materialNums,
                                        &g_pMesh);
        if ( FAILED( hr ) ) return E_FAIL;
    }

    // init material & texture
    D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
    g_pMeshMaterials            = new D3DMATERIAL9[g_materialNums];
    g_pMeshTextures             = new LPDIRECT3DTEXTURE9[g_materialNums];

    for (int i = 0; i < g_materialNums; ++i) {
        g_pMeshMaterials[i]         = d3dxMaterials[i].MatD3D;
        g_pMeshMaterials[i].Ambient = g_pMeshMaterials[i].Diffuse;
        g_pMeshTextures[i] = NULL;

        if (d3dxMaterials[i].pTextureFilename != NULL
            && lstrlen(d3dxMaterials[i].pTextureFilename) > 0 ) {
            HRESULT hr = D3DXCreateTextureFromFile( g_pd3dDevice, 
                                                d3dxMaterials[i].pTextureFilename, 
                                                &g_pMeshTextures[i]);
            if ( FAILED( hr ) ) return E_FAIL;
        }
    }

    pD3DXMtrlBuffer->Release();
    return S_OK;
}
Exemplo n.º 11
0
bool Mesh::Load(LPDIRECT3DDEVICE9 device, const char* filename, TextureManager* textureManager)
{
	if(FAILED(D3DXLoadMeshFromX(filename, D3DXMESH_SYSTEMMEM, device, NULL, &m_materialBuffer, NULL, &m_numberOfMaterials, &m_mesh)))
	{
		return false;
	}

	m_filename = filename;
	m_materials = new D3DMATERIAL9[m_numberOfMaterials];
	m_meshTextures = new Texture*[m_numberOfMaterials];

	D3DXMATERIAL* loadedMaterials = (D3DXMATERIAL*)m_materialBuffer->GetBufferPointer();

	for(DWORD i = 0; i < m_numberOfMaterials; i++) 
	{
		m_materials[i] = loadedMaterials[i].MatD3D;
		m_materials[i].Ambient = m_materials[i].Diffuse;

		char* textureFilename = loadedMaterials[i].pTextureFilename;
		if(textureManager->Load(device, textureFilename))
		{
			m_meshTextures[i] = textureManager->GetTexture(textureFilename);
		}
		else 
		{
			m_meshTextures[i] = NULL;
		}
	}

	return true;


}
Exemplo n.º 12
0
HRESULT LoadMesh(IDirect3DDevice9* pd3dDevice, WCHAR* strFileName, ID3DXMesh** ppMesh)
{
    ID3DXMesh* pMesh = NULL;
    WCHAR str[MAX_PATH];
    HRESULT hr;

    V_RETURN(DXUTFindDXSDKMediaFileCch(str, MAX_PATH, strFileName));

    V_RETURN(D3DXLoadMeshFromX(str, D3DXMESH_MANAGED, pd3dDevice, NULL, NULL, NULL, NULL, &pMesh));

    DWORD* rgdwAdjacency = NULL;

    if(!(pMesh->GetFVF() & D3DFVF_NORMAL))
    {
        ID3DXMesh* pTempMesh;
        V(pMesh->CloneMeshFVF(pMesh->GetOptions(),
                                pMesh->GetFVF() | D3DFVF_NORMAL,
                                pd3dDevice, &pTempMesh));
        V(D3DXComputeNormals(pTempMesh, NULL));

        SAFE_RELEASE(pMesh);
        pMesh = pTempMesh;
    }
   
    rgdwAdjacency = new DWORD[pMesh->GetNumFaces() * 3];
    if(rgdwAdjacency == NULL)
        return E_OUTOFMEMORY;
    V(pMesh->GenerateAdjacency(1e-6f, rgdwAdjacency));
    V(pMesh->OptimizeInplace(D3DXMESHOPT_VERTEXCACHE, rgdwAdjacency, NULL, NULL, NULL));
    delete []rgdwAdjacency;

    *ppMesh = pMesh;

    return S_OK;
}
Exemplo n.º 13
0
//------------------------------------------------------------------------------------------------
// Name:  obtainSourceGeometry
// Desc:  Loads geometry from the source file into the output subset geometry
//------------------------------------------------------------------------------------------------
bool XMesh::obtainSourceGeometry(LPDIRECT3DDEVICE9 pd3dDevice, SubsetGeometry* subsetGeometry) const
{
    // Fail without a device or if something is wrong with the output pointer
    if (APP_ERROR(!pd3dDevice || !subsetGeometry)("Invalid paramter to obtainSourceGeometry") ||
		APP_ERROR(!subsetGeometry->empty())("Provided geometry subset for obtainSourceGeometry must be empty"))
		return false;

    // Check to make sure a file exists (if not, just exit)
    if (mySourceFile.getValue().empty()) return true;

    // Keeps track of how many subsets this mesh contains
    DWORD subsets = 0;

    // Stores the mesh that was loaded from the file
    LPD3DXMESH pXMesh = NULL;

    // Load the mesh from the specified file
    if (APP_ERROR(D3DXLoadMeshFromX(mySourceFile.getValue().c_str(), D3DXMESH_SYSTEMMEM, 
                                   pd3dDevice, NULL, NULL, NULL, &subsets, 
                                   &pXMesh))("XMesh couldn't load \"%s\"", mySourceFile.getValue().c_str()))
		return false;

    // Convert the mesh
    bool succeeded = buildGeometryFromD3DXMesh(pXMesh, subsetGeometry, subsets);

    // Release the mesh
    pXMesh->Release();

    // Return an error code if an error occurred
    if (APP_ERROR(!succeeded)("Unable to build geometry for mesh from \"%s\"", mySourceFile.getValue().c_str()))
        return false;

    // Success
    return true;
}
Exemplo n.º 14
0
void GameObject::InitMesh(LPDIRECT3DDEVICE9& d3dDevice)
{
	LPD3DXBUFFER pD3DXMtrlBuffer = nullptr;
	if ( d3dDevice )

	D3DXLoadMeshFromX( L"spaceMan.x", D3DXMESH_SYSTEMMEM,
		d3dDevice, NULL,
		&pD3DXMtrlBuffer, NULL, &m_NumMaterial,
		&m_pMesh );

	D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();

	// skip error handling -.-;;
	m_pMeshMaterials = new D3DMATERIAL9[m_NumMaterial];
	m_pMeshTextures = new LPDIRECT3DTEXTURE9[m_NumMaterial];

	for ( DWORD i = 0; i < m_NumMaterial; ++i )
	{
		m_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;

		m_pMeshMaterials[i].Ambient = m_pMeshMaterials[i].Diffuse;

		m_pMeshTextures[i] = NULL;
		
		D3DXCreateTextureFromFileA( d3dDevice,
			d3dxMaterials[i].pTextureFilename,
			&m_pMeshTextures[i] );
	}

	pD3DXMtrlBuffer->Release();
}
Exemplo n.º 15
0
void Meshes::load_meshes(LPCSTR pFilename, LPDIRECT3DDEVICE9 pD3DDevice)
{
	ID3DXBuffer* buff_Material = NULL;
	ID3DXBuffer* buff_Adjacency = NULL;

	D3DXLoadMeshFromX(pFilename, D3DXMESH_MANAGED, pD3DDevice, &buff_Adjacency, &buff_Material, NULL, &numMaterials, &Model); 

	
	D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)buff_Material->GetBufferPointer();


	material = new D3DMATERIAL9[numMaterials];
	texture = new LPDIRECT3DTEXTURE9[numMaterials];

	for(DWORD i = 0; i < numMaterials; i++) 
	{
		material[i] = tempMaterials[i].MatD3D; 
		material[i].Ambient = material[i].Diffuse; 

		if(FAILED(D3DXCreateTextureFromFileA(pD3DDevice,tempMaterials[i].pTextureFilename,&texture[i])))
			texture[i] = NULL; 
	}
	

	return;
}
Exemplo n.º 16
0
	MeshSP Mesh::CreateFromX(const std::string& path)
	{
		std::unordered_map<std::string, MeshWP>::iterator it = _meshes.find(path);

		if(it != _meshes.end()) 
			return MeshSP((*it).second.lock());
		// 存在しなければ新しくロード
		MeshSP mesh = MeshSP(new Mesh);
		// ファイルパスを取得
		mesh->_path = path;
		// 文字列変換
		WCHAR wcbuff[255] = {0};
		DXconvAnsiToWide(wcbuff, path.c_str(), 255);
		// メッシュのロード
		D3DXLoadMeshFromX(wcbuff,
			D3DXMESH_MANAGED,
			GraphicsManager::_device,
			NULL,
			&mesh->_meterialBuffer,
			NULL,
			&mesh->_materialNumber,
			&mesh->_mesh);
		// マップに追加
		_meshes.insert(std::make_pair(path, MeshWP(mesh)));

		return mesh ;
	}
Exemplo n.º 17
0
Mesh::Mesh(LPCWSTR file)
: _mesh(NULL)
, _meshMaterials(NULL)
, _meshTextures(NULL)
, _meshMaterialCount(0)
{
	ID3DXBuffer * meshMaterialBuffer = NULL;
	HRESULT hr = D3DXLoadMeshFromX(file, 0, dxr->device, NULL, &meshMaterialBuffer, NULL, &_meshMaterialCount, &_mesh);
	_ASSERT(SUCCEEDED(hr));
	if (meshMaterialBuffer && _meshMaterialCount > 0)
	{
		D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)meshMaterialBuffer->GetBufferPointer();
		_meshMaterials = new D3DMATERIAL9[_meshMaterialCount];
		_meshTextures = new IDirect3DTexture9 * [_meshMaterialCount];
		for (unsigned long i = 0; i < _meshMaterialCount; i++)
		{
			_meshMaterials[i] = d3dxMaterials[i].MatD3D;
			if (D3DXCOLOR(0xff000000) == _meshMaterials[i].Ambient)
				_meshMaterials[i].Ambient = D3DXCOLOR(0.75,0.75,0.75,0.75);
			_meshTextures[i] = NULL;
			if (d3dxMaterials[i].pTextureFilename)
			{
				QString texturePath = native(QFileInfo(QString::fromUtf16((const ushort *)file)).dir() / QFileInfo(d3dxMaterials[i].pTextureFilename).fileName());
				hr = D3DXCreateTextureFromFileW(dxr->device, (LPCWSTR)texturePath.utf16(), &_meshTextures[i]);
				_ASSERT(SUCCEEDED(hr));
			}
		}
	}
	meshMaterialBuffer->Release();	
}
Exemplo n.º 18
0
//初期化。
void Model::Init(LPDIRECT3DDEVICE9 pd3dDevice, const char* fileName)
{
	Release();
	//Xファイルをロード。
	LPD3DXBUFFER pD3DXMtrlBuffer;
	//Xファイルのロード。
	D3DXLoadMeshFromX(fileName, D3DXMESH_SYSTEMMEM,
		pd3dDevice, NULL,
		&pD3DXMtrlBuffer, NULL, &numMaterial,
		&mesh);
	//法線が存在するか調べる。
	if ((mesh->GetFVF() & D3DFVF_NORMAL) == 0) {
		//法線がないので作成する。
		ID3DXMesh* pTempMesh = NULL;

		mesh->CloneMeshFVF(mesh->GetOptions(),
			mesh->GetFVF() | D3DFVF_NORMAL, g_pd3dDevice, &pTempMesh);

		D3DXComputeNormals(pTempMesh, NULL);
		mesh->Release();
		mesh = pTempMesh;

	}

	// マテリアルバッファを取得。
	D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();

	//テクスチャをロード。
	textures = new LPDIRECT3DTEXTURE9[numMaterial];
	for (DWORD i = 0; i < numMaterial; i++)
	{
		textures[i] = NULL;
		//テクスチャを作成する。
		D3DXCreateTextureFromFileA(pd3dDevice,
			d3dxMaterials[i].pTextureFilename,
			&textures[i]);
	}
	// マテリアルバッファを解放。
	pD3DXMtrlBuffer->Release();

	//シェーダーをコンパイル。
	LPD3DXBUFFER  compileErrorBuffer = NULL;
	//シェーダーをコンパイル。
	HRESULT hr = D3DXCreateEffectFromFile(
		pd3dDevice,
		"basic.fx",
		NULL,
		NULL,
		D3DXSHADER_SKIPVALIDATION,
		NULL,
		&effect,
		&compileErrorBuffer
		);
	if (hr != S_OK) {
		MessageBox(NULL, (char*)(compileErrorBuffer->GetBufferPointer()), "error", MB_OK);
		std::abort();
	}
}
Exemplo n.º 19
0
//=============================================================================
// 初期化処理
//=============================================================================
HRESULT InitEnemy(void)
{

	LPDIRECT3DDEVICE9 pDevice = GetDevice();
	
	for(int nCntEnemy = 0; nCntEnemy < MAX_ENEMY; nCntEnemy++)
	{
		// 位置・回転・スケールの初期設定
		g_enemy[nCntEnemy].pos = D3DXVECTOR3( 0.0f, 0.0f, 0.0f);
		g_enemy[nCntEnemy].rot = D3DXVECTOR3( 0.0f, 0.0f, 0.0f);
		g_enemy[nCntEnemy].scl = D3DXVECTOR3( 2.0f, 2.0f, 2.0f);
		g_enemy[nCntEnemy].move = D3DXVECTOR3( VALUE_MOVE, VALUE_MOVE, 0.0f);
		g_enemy[nCntEnemy].rotAngle = D3DXVECTOR3( 0.0f, 0.0f, 0.0f);
		g_enemy[nCntEnemy].rotTarget = D3DXVECTOR3( 0.0f, 0.0f, 0.0f);
		g_enemy[nCntEnemy].nCoolDown = -1;
		g_enemy[nCntEnemy].bUse = false;
		g_enemy[nCntEnemy].bShot = false;
		g_enemy[nCntEnemy].bSeePlayer = false;
		g_enemy[nCntEnemy].nSeePFrame = 0;
		g_enemy[nCntEnemy].bPatrol = true;
		g_enemy[nCntEnemy].posPatrol = D3DXVECTOR3( 0.0f, 0.0f, 0.0f);
		g_enemy[nCntEnemy].rotPatrol = D3DXVECTOR3( 0.0f, 0.0f, 0.0f);
		g_enemy[nCntEnemy].bEscaping = false;
		g_enemy[nCntEnemy].type = ENEMY_WHITE;

	}

	//モデルに関する変数の初期化
	strcpy( g_modelData[ENEMY_WHITE].filePath, MODEL_ENEMY_WHITE);
	strcpy( g_modelData[ENEMY_BLUE].filePath, MODEL_ENEMY_BLUE);
	strcpy( g_modelData[ENEMY_RED].filePath, MODEL_ENEMY_RED);

	//モデルに関する変数の初期化
	for(int nCntType = 0; nCntType < ENEMY_TYPE_MAX; nCntType++)
	{
		g_modelData[nCntType].pD3DTexture = NULL;
		g_modelData[nCntType].pD3DXMesh = NULL;
		g_modelData[nCntType].pD3DXBuffMat = NULL;
		g_modelData[nCntType].nNumMat = 0;

		// Xファイルの読み込み
		if(FAILED(D3DXLoadMeshFromX(
			g_modelData[nCntType].filePath,			// 読み込むモデルファイル名(Xファイル)
			D3DXMESH_SYSTEMMEM,						// メッシュの作成オプションを指定
			pDevice,								// IDirect3DDevice9インターフェイスへのポインタ
			NULL,									// 隣接性データを含むバッファへのポインタ
			&g_modelData[nCntType].pD3DXBuffMat,	// マテリアルデータを含むバッファへのポインタ
			NULL,									// エフェクトインスタンスの配列を含むバッファへのポインタ
			&g_modelData[nCntType].nNumMat,			// D3DXMATERIAL構造体の数
			&g_modelData[nCntType].pD3DXMesh		// ID3DXMeshインターフェイスへのポインタのアドレス
			)))
		{
			return E_FAIL;
		}	
	}
														
	return S_OK;
}
Exemplo n.º 20
0
HRESULT CMainFrame::SetupXFileMesh(LPCTSTR pszXFilePath)
{
    /*****************************************************************************************
    * 绘制时应该使用如下方式进行绘制 -- 分别绘制每个子 Material 的 Material 和 Texture
    for( DWORD i = 0 ,i < m_dwNumMaterials,i++ )
    {
        m_pD3dDevice->SetMaterial( &m_pMeshMaterials[i] );
        m_pD3dDevice->SetTexture( 0, m_ppMeshTextures[i] );
        // Draw the mesh subset
        m_pD3dMesh->DrawSubset( i );  //在 D3DXLoadMeshFromX 时,m_pD3dMesh 已经和 m_pD3dDevice 建立起关联了
    }
    *****************************************************************************************/

    HRESULT hr= E_FAIL;
    CComPtr<ID3DXBuffer> pD3DXMtrlBuffer;

    //从指定的XFile(Tiger.x)中加载Mesh
    DX_VERIFY(D3DXLoadMeshFromX( pszXFilePath, D3DXMESH_SYSTEMMEM, 
        m_pD3dDevice, NULL, 
        &pD3DXMtrlBuffer, NULL, &m_dwNumMaterials, 
        &m_pD3dMesh ));

    // We need to extract the material properties and texture names from the 
    // pD3DXMtrlBuffer

    //此处使用了强制类型转换,始终都是 D3DXMATERIAL 类型吗?能够获取到其中的 MeshMaterialList 信息,但其他信息无法获取?
    D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();

    m_pMeshMaterials = new D3DMATERIAL9[m_dwNumMaterials];
    if( m_pMeshMaterials == NULL )
        return E_OUTOFMEMORY;
    m_ppMeshTextures  = new IDirect3DTexture9*[m_dwNumMaterials];
    if( m_ppMeshTextures == NULL )
        return E_OUTOFMEMORY;

    for( DWORD i=0; i<m_dwNumMaterials; i++ )
    {
        // Copy the material
        m_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;

        // Set the ambient color for the material (D3DX does not do this)
        m_pMeshMaterials[i].Ambient = m_pMeshMaterials[i].Diffuse;

        //如果有子Texture的文件,对应加载?
        m_ppMeshTextures[i] = NULL;
        if( d3dxMaterials[i].pTextureFilename != NULL && 
            strlen(d3dxMaterials[i].pTextureFilename) > 0 )
        {
            // Create the texture
            DX_VERIFY(D3DXCreateTextureFromFile( m_pD3dDevice, 
                CA2T(d3dxMaterials[i].pTextureFilename), 
                &m_ppMeshTextures[i] ));
        }
    }

    //SAFE_RELEASE(pD3DXMtrlBuffer);
    return hr;
}
Exemplo n.º 21
0
shap* InitShap(string smodpath)
{
	shap *pShap=new shap;
	

	LPD3DXBUFFER pD3DXMtrlBuffer;
	wstring s=s2ws(smodpath);
    // 装载.x文件,初始化shap的mesh和dwNumMaterials
	if( FAILED( D3DXLoadMeshFromX( s.c_str(), D3DXMESH_SYSTEMMEM,
                                   g_pd3dDevice, NULL,
								   &pD3DXMtrlBuffer, NULL, &pShap->dwNumMaterials,
								   &pShap->mesh ) ) )
    {        
            return NULL;
     
    }



    // 取出materials和textures
    D3DXMATERIAL* d3dxMaterials = ( D3DXMATERIAL* )pD3DXMtrlBuffer->GetBufferPointer();
	pShap->pMeshMaterials = new D3DMATERIAL9[pShap->dwNumMaterials];
    if( pShap->pMeshMaterials == NULL )
        return NULL;
	pShap->pMeshTextures = new LPDIRECT3DTEXTURE9[pShap->dwNumMaterials];
    if( pShap->pMeshTextures == NULL )
        return NULL;

    for( DWORD i = 0; i < pShap->dwNumMaterials; i++ )
    {
        // Copy the material
        pShap->pMeshMaterials[i] = d3dxMaterials[i].MatD3D;

        // Set the ambient color for the material (D3DX does not do this)
        pShap->pMeshMaterials[i].Ambient = pShap->pMeshMaterials[i].Diffuse;
		///////////////////////////////////////////////////////////////////////////////////
        pShap->pMeshTextures[i] = NULL;
        if( d3dxMaterials[i].pTextureFilename != NULL &&
            lstrlenA( d3dxMaterials[i].pTextureFilename ) > 0 )
        {
            // Create the texture
            if( FAILED( D3DXCreateTextureFromFileA( g_pd3dDevice,
                                                    d3dxMaterials[i].pTextureFilename,
                                                    &pShap->pMeshTextures[i] ) ) )
            {
                ;
            }
        }
    }

    // Done with the material buffer
    pD3DXMtrlBuffer->Release();

    return pShap;
	
}
Exemplo n.º 22
0
//-----------------------------------------------------------------------------
HRESULT CDXUTMesh::Create( LPDIRECT3DDEVICE9 pd3dDevice, LPCWSTR strFilename )
{
    WCHAR        strPath[MAX_PATH];
    LPD3DXBUFFER pAdjacencyBuffer = NULL;
    LPD3DXBUFFER pMtrlBuffer = NULL;
    HRESULT      hr;

    // Cleanup previous mesh if any
    Destroy();

    // Find the path for the file, and convert it to ANSI (for the D3DX API)
    DXUTFindDXSDKMediaFileCch( strPath, sizeof(strPath) / sizeof(WCHAR), strFilename );

    // Load the mesh
    if( FAILED( hr = D3DXLoadMeshFromX( strPath, D3DXMESH_MANAGED, pd3dDevice, 
                                        &pAdjacencyBuffer, &pMtrlBuffer, NULL,
                                        &m_dwNumMaterials, &m_pMesh ) ) )
    {
        return hr;
    }

    // Optimize the mesh for performance
    if( FAILED( hr = m_pMesh->OptimizeInplace(
                        D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,
                        (DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL, NULL, NULL ) ) )
    {
        SAFE_RELEASE( pAdjacencyBuffer );
        SAFE_RELEASE( pMtrlBuffer );
        return hr;
    }

    // Set strPath to the path of the mesh file
    WCHAR *pLastBSlash = wcsrchr( strPath, L'\\' );
    if( pLastBSlash )
        *(pLastBSlash + 1) = L'\0';
    else
        *strPath = L'\0';

    D3DXMATERIAL* d3dxMtrls = (D3DXMATERIAL*)pMtrlBuffer->GetBufferPointer();
    hr = CreateMaterials( strPath, pd3dDevice, d3dxMtrls, m_dwNumMaterials );

    SAFE_RELEASE( pAdjacencyBuffer );
    SAFE_RELEASE( pMtrlBuffer );

    // Extract data from m_pMesh for easy access
    D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE];
    m_dwNumVertices    = m_pMesh->GetNumVertices();
    m_dwNumFaces       = m_pMesh->GetNumFaces();
    m_dwBytesPerVertex = m_pMesh->GetNumBytesPerVertex();
    m_pMesh->GetIndexBuffer( &m_pIB );
    m_pMesh->GetVertexBuffer( &m_pVB );
    m_pMesh->GetDeclaration( decl );
    pd3dDevice->CreateVertexDeclaration( decl, &m_pDecl );

    return hr;
}
Exemplo n.º 23
0
//メッシュを読み込む
bool Model::LoadMesh(const TCHAR *meshFileName)
{
	if (FAILED(D3DXLoadMeshFromX(meshFileName, D3DXMESH_SYSTEMMEM, d3dDevice, nullptr, &buffer, nullptr, &numMaterial, &mesh)))
	{
		DebugAlert("%sを\n読み込めませんでした。\nファイル名やディレクトリを間違えていないかご確認ください。", meshFileName);
		return false;
	}

	return true;
}
Exemplo n.º 24
0
//
// MeshNode::VOnRestore				-  3rd Edition, Chapter 14, page 506
//
// This function loads the Mesh and ensures the Mesh has normals; it also optimizes the 
// Mesh for the graphics card's vertex cache, which improves performance by organizing 
// the internal triangle list for less cache misses.
//
HRESULT D3DMeshNode9::VOnRestore(Scene *pScene)
{
	if (m_XFileName.empty())
	{
		SetRadius(CalcBoundingSphere());
		return D3DSceneNode9::VOnRestore(pScene);
	}

	// Change post press - release the Mesh only if we have a valid Mesh file name to load.
	// Otherwise we likely created it on our own, and needs to be kept.
	SAFE_RELEASE(m_pMesh);

    WCHAR str[MAX_PATH];
    HRESULT hr;

    // Load the Mesh with D3DX and get back a ID3DXMesh*.  For this
    // sample we'll ignore the X file's embedded materials since we know 
    // exactly the model we're loading.  See the Mesh samples such as
    // "OptimizedMesh" for a more generic Mesh loading example.
	V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, m_XFileName.c_str() ) );

    V_RETURN( D3DXLoadMeshFromX(str, D3DXMESH_MANAGED, DXUTGetD3D9Device(), NULL, NULL, NULL, NULL, &m_pMesh) );

    DWORD *rgdwAdjacency = NULL;

    // Make sure there are normals which are required for lighting
    if( !(m_pMesh->GetFVF() & D3DFVF_NORMAL) )
    {
        ID3DXMesh* pTempMesh;
        V( m_pMesh->CloneMeshFVF( m_pMesh->GetOptions(), 
                                  m_pMesh->GetFVF() | D3DFVF_NORMAL, 
                                  DXUTGetD3D9Device(), &pTempMesh ) );
        V( D3DXComputeNormals( pTempMesh, NULL ) );

        SAFE_RELEASE( m_pMesh );
        m_pMesh = pTempMesh;
    }

    // Optimize the Mesh for this graphics card's vertex cache 
    // so when rendering the Mesh's triangle list the vertices will 
    // cache hit more often so it won't have to re-execute the vertex shader 
    // on those vertices so it will improve perf.     

    rgdwAdjacency = GCC_NEW DWORD[m_pMesh->GetNumFaces() * 3];
    if( rgdwAdjacency == NULL )
        return E_OUTOFMEMORY;
    V( m_pMesh->ConvertPointRepsToAdjacency(NULL, rgdwAdjacency) );
    V( m_pMesh->OptimizeInplace(D3DXMESHOPT_VERTEXCACHE, rgdwAdjacency, NULL, NULL, NULL) );
    
	SAFE_DELETE_ARRAY(rgdwAdjacency);

	SetRadius(CalcBoundingSphere());

    return D3DSceneNode9::VOnRestore(pScene);
}
Exemplo n.º 25
0
//-----------------------------------------------------------------------------
// Desc: 创建场景图形
//-----------------------------------------------------------------------------
HRESULT InitGeometry()
{
	LPD3DXBUFFER pD3DXMtrlBuffer;  //存储网格模型材质的缓冲区对象

	//从磁盘文件加载网格模型
	if( FAILED( D3DXLoadMeshFromX( L"airplane.x", D3DXMESH_MANAGED, 
		g_pd3dDevice, NULL, 
		&pD3DXMtrlBuffer, NULL, &g_dwNumMaterials, 
		&g_pMesh ) ) )
	{
		MessageBox(NULL, L"Could not find airplane.x", L"Mesh", MB_OK);
		return E_FAIL;
	}

	//从网格模型中提取材质属性和纹理文件名 
	D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
	g_pMeshMaterials = new D3DMATERIAL9[g_dwNumMaterials];

	if( g_pMeshMaterials == NULL )
		return E_OUTOFMEMORY;

	g_pMeshTextures  = new LPDIRECT3DTEXTURE9[g_dwNumMaterials];
	if( g_pMeshTextures == NULL )
		return E_OUTOFMEMORY;

	//逐块提取网格模型材质属性和纹理文件名
	for( DWORD i=0; i<g_dwNumMaterials; i++ )
	{
		//材料属性
		g_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;
		//设置模型材料的环境光反射系数, 因为模型材料本身没有设置环境光反射系数
		g_pMeshMaterials[i].Ambient = g_pMeshMaterials[i].Diffuse;

		g_pMeshTextures[i] = NULL;
		if( d3dxMaterials[i].pTextureFilename != NULL && 
			strlen(d3dxMaterials[i].pTextureFilename) > 0 )
		{
			//获取纹理文件名
			WCHAR filename[256];
			RemovePathFromFileName(d3dxMaterials[i].pTextureFilename, filename);

			//创建纹理
			if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, filename, 
				&g_pMeshTextures[i] ) ) )
			{
				MessageBox(NULL, L"Could not find texture file", L"Mesh", MB_OK);
			}
		}
	}

	//释放在加载模型文件时创建的保存模型材质和纹理数据的缓冲区对象
	pD3DXMtrlBuffer->Release();

	return S_OK;
}
Exemplo n.º 26
0
HRESULT initGeometry(){
	LPD3DXBUFFER pD3DXMtrlBuffer;
	if(FAILED(D3DXLoadMeshFromX(L"tank.x", D3DXMESH_MANAGED,
		g_pDevice, NULL, &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials, &g_pMesh)))
		return E_FAIL;
	//Extract material & texture
	D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
	g_pMeshMaterials = new D3DMATERIAL9[g_dwNumMaterials];

	if(g_pMeshMaterials == NULL)
		return E_OUTOFMEMORY;

	g_pMeshTextures = new LPDIRECT3DTEXTURE9[g_dwNumMaterials];
	if(g_pMeshTextures == NULL)
		return E_OUTOFMEMORY;
	//Extract
	for(DWORD i=0; i<g_dwNumMaterials; ++i){
		g_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;
		g_pMeshMaterials[i].Ambient = g_pMeshMaterials[i].Diffuse;

		g_pMeshTextures[i] = NULL;
		if(d3dxMaterials[i].pTextureFilename != NULL && strlen(d3dxMaterials[i].pTextureFilename) > 0){
			WCHAR name[256];
			removePathFromFileName(d3dxMaterials[i].pTextureFilename, name);

			if(FAILED(D3DXCreateTextureFromFile(g_pDevice, name, &g_pMeshTextures[i]))){
				MessageBox(NULL, L"Cound not find texture file", L"initGeometry()", MB_OK);
			}
		}
	}
	pD3DXMtrlBuffer->Release();

	//////////////////////////////////////////////////////////////////////////

	//Shutter data
	CUSTOMVERTEX vertices[] = {
		{ -8,   -2,  -10.0f,   0xff808080},     
		{ -8,    6,  -10.0f,   0xff808080},	
		{  8,   -2,  -10.0f,   0xff808080},	
		{  8,    6,  -10.0f,   0xff808080}
	};

	if(FAILED(g_pDevice->CreateVertexBuffer(4* sizeof(CUSTOMVERTEX),0,
		D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pShutter, 0)))
		return E_FAIL;

	//Fill the vertex buffer
	VOID* pVertices;
	if(FAILED(g_pShutter->Lock(0, sizeof(vertices), (void**)&pVertices,0)))
		return E_FAIL;
	memcpy(pVertices, vertices, sizeof(vertices));
	g_pShutter->Unlock();

	return S_OK;
};
Exemplo n.º 27
0
void Mesh::loadTexturedMesh(LPCWSTR filename, pBuffer adjBuffer)
{
	pDevice device = Engine::DX::instance()->getDevice();
	D3DXLoadMeshFromX( filename, 
						D3DXMESH_MANAGED, 
						device, 
						&adjBuffer, 
						&mat_buffer, 
						&effect,
						&numMaterials, 
						&mesh);


	//Pull material (including texture) information from loaded .x file
	D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)mat_buffer->GetBufferPointer();

	D3DMATERIAL9* mesh_material = new D3DMATERIAL9[numMaterials];
	LPDIRECT3DTEXTURE9* meshTextures = new LPDIRECT3DTEXTURE9[numMaterials];

	for(DWORD i = 0; i <numMaterials; ++i)
	{
		//Copy the material
		mesh_material[i] = d3dxMaterials[i].MatD3D;

		//Set the ambient color (if needed) for the material (D3DX doesn't do this)
		mesh_material[i].Ambient = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f);

		//Create the texture if it exists - it may not
		meshTextures[i] = NULL;

		if(d3dxMaterials[i].pTextureFilename)
		{
			//Need to convert the texture filenames to Unicode string
			int len = 0;
			len = (int)strlen(d3dxMaterials[i].pTextureFilename) + 1;
			wchar_t *ucString = new wchar_t[len];
			mbstowcs(ucString, d3dxMaterials[i].pTextureFilename, len);
			LPCWSTR newfilename = (LPCWSTR)ucString;

			//Load the texture now that we have a valid filename

			D3DXCreateTextureFromFile(device, newfilename, &meshTextures[i]);
		
			delete[]ucString;
		}
	}
	for(unsigned int i = 0; i < numMaterials; i++){
		materials.push_back(mesh_material[i]);
	}
	for(unsigned int i = 0; i < numMaterials; i++){
		textures.push_back(meshTextures[i]);
	}
	delete [] mesh_material;
	delete [] meshTextures;
}
Exemplo n.º 28
0
int Model::load(const std::string& xfile)
{
	LPD3DXBUFFER pD3DXMtrlBuff;

	if ( FAILED( D3DXLoadMeshFromX( xfile.c_str(), D3DXMESH_SYSTEMMEM,
		Graphics::inst()->Device(), NULL,
		&pD3DXMtrlBuff, NULL, &m_numMaterials,
		&m_pD3DMesh ) ) )
	{
		return -1;
	}

	D3DXMATERIAL* pD3DXMats = ( D3DXMATERIAL* ) pD3DXMtrlBuff->GetBufferPointer();
	m_pD3DMaterials = new D3DMATERIAL9[m_numMaterials];
	if ( m_pD3DMaterials == 0 )
		return -1;

	m_pD3DTexture = new LPDIRECT3DTEXTURE9[m_numMaterials];
	if ( m_pD3DTexture == 0 )
		return -1;

	for ( DWORD i = 0; i < m_numMaterials; i++ )
	{
		m_pD3DMaterials[i] = pD3DXMats[i].MatD3D;

		m_pD3DMaterials[i].Ambient = m_pD3DMaterials[i].Diffuse;

		m_pD3DTexture[i] = 0;
		if ( pD3DXMats[i].pTextureFilename != 0 
			&& lstrlenA( pD3DXMats[i].pTextureFilename ) > 0 )
		{
			D3DXCreateTextureFromFileExA(
				Graphics::inst()->Device(),
				pD3DXMats[i].pTextureFilename,
				0, 0, 0, 0,
				D3DFMT_A8R8G8B8,
				D3DPOOL_MANAGED,
				D3DX_FILTER_LINEAR,
				D3DX_FILTER_LINEAR,
				D3DCOLOR_XRGB(0, 0, 0),
				0, 0, &m_pD3DTexture[i]);
//			if ( FAILED( D3DXCreateTextureFromFileA( Graphics::inst()->Device(),
//				pD3DXMats[i].pTextureFilename,
//				&m_pD3DTexture[i] ) ) )
//			{
//				return -1;
//			}
		}
	}

	pD3DXMtrlBuff->Release();

	return S_OK;
}
Exemplo n.º 29
0
	bool Mesh::Load(char* filename)
	{
		HRESULT result;

		//load mesh from the specified file
		result = D3DXLoadMeshFromX(
			filename,               //filename
			D3DXMESH_SYSTEMMEM,     //mesh options
			g_engine->getDevice(),  //Direct3D device
			NULL,                   //adjacency buffer
			&matbuffer,             //material buffer
			NULL,                   //special effects
			&material_count, 		//number of materials
			&mesh);          		//resulting mesh

		if (result != D3D_OK)  {
			return false;
		}

		//extract material properties and texture names from material buffer
		d3dxMaterials = (LPD3DXMATERIAL)matbuffer->GetBufferPointer();
		materials = new D3DMATERIAL9[material_count];
		textures  = new LPDIRECT3DTEXTURE9[material_count];

		//create the materials and textures
		for(DWORD i=0; i < material_count; i++)
		{
			//grab the material
			materials[i] = d3dxMaterials[i].MatD3D;

			//set ambient color for material 
			materials[i].Ambient = materials[i].Diffuse;
			//materials[i].Emissive = materials[i].Diffuse;
			materials[i].Power = 0.5f;
			//materials[i].Specular = materials[i].Diffuse;

			textures[i] = NULL;
			if( d3dxMaterials[i].pTextureFilename != NULL && 
				lstrlen(d3dxMaterials[i].pTextureFilename) > 0 )
			{
				//load texture file specified in .x file
				result = D3DXCreateTextureFromFile(g_engine->getDevice(), d3dxMaterials[i].pTextureFilename, &textures[i]);

				if (result != D3D_OK) {
					return false;
				}
			}
		}

		//done using material buffer
		matbuffer->Release();

		return true;
	}
Exemplo n.º 30
0
// 모델 로딩
LPD3DXMESH LoadModel(const char * filename)
{
	LPD3DXMESH ret = NULL;
	if (FAILED(D3DXLoadMeshFromX(filename, D3DXMESH_SYSTEMMEM, gpD3DDevice, NULL, NULL, NULL, NULL, &ret)))
	{
		OutputDebugString("모델 로딩 실패: ");
		OutputDebugString(filename);
		OutputDebugString("\n");
	};

	return ret;
}