Exemple #1
0
//--------------------------------------------------------------------------------------
void RenderSubset( UINT iSubset )
{
    HRESULT hr;
    UINT iPass, cPasses;

    // Retrieve the ID3DXMesh pointer and current material from the MeshLoader helper
    ID3DXMesh* pMesh = g_MeshLoader.GetMesh();
    Material* pMaterial = g_MeshLoader.GetMaterial( iSubset );

    // Set the lighting variables and texture for the current material
    V( g_pEffect->SetValue( g_hAmbient, pMaterial->vAmbient, sizeof( D3DXVECTOR3 ) ) );
    V( g_pEffect->SetValue( g_hDiffuse, pMaterial->vDiffuse, sizeof( D3DXVECTOR3 ) ) );
    V( g_pEffect->SetValue( g_hSpecular, pMaterial->vSpecular, sizeof( D3DXVECTOR3 ) ) );
    V( g_pEffect->SetTexture( g_hTexture, pMaterial->pTexture ) );
    V( g_pEffect->SetFloat( g_hOpacity, pMaterial->fAlpha ) );
    V( g_pEffect->SetInt( g_hSpecularPower, pMaterial->nShininess ) );

    V( g_pEffect->SetTechnique( pMaterial->hTechnique ) );
    V( g_pEffect->Begin( &cPasses, 0 ) );

    for( iPass = 0; iPass < cPasses; iPass++ )
    {
        V( g_pEffect->BeginPass( iPass ) );

        // The effect interface queues up the changes and performs them 
        // with the CommitChanges call. You do not need to call CommitChanges if 
        // you are not setting any parameters between the BeginPass and EndPass.
        // V( g_pEffect->CommitChanges() );

        // Render the mesh with the applied technique
        V( pMesh->DrawSubset( iSubset ) );

        V( g_pEffect->EndPass() );
    }
    V( g_pEffect->End() );
}
Exemple #2
0
//--------------------------------------------------------------------------------------
// Saves the mesh to X-file
//--------------------------------------------------------------------------------------
void SaveMeshToXFile()
{
    HRESULT hr;

    // Fill out D3DXMATERIAL structures
    UINT numMaterials = g_MeshLoader.GetNumMaterials();
    D3DXMATERIAL* pMaterials = new D3DXMATERIAL[numMaterials];
    char* pStrTexture = new char[MAX_PATH * numMaterials];
    if( ( pMaterials != NULL ) && ( pStrTexture != NULL ) )
    {
        for( UINT i = 0; i < g_MeshLoader.GetNumMaterials(); i++ )
        {
            Material* pMat = g_MeshLoader.GetMaterial( i );
            if( pMat != NULL )
            {
                pMaterials[i].MatD3D.Ambient.r = pMat->vAmbient.x;
                pMaterials[i].MatD3D.Ambient.g = pMat->vAmbient.y;
                pMaterials[i].MatD3D.Ambient.b = pMat->vAmbient.z;
                pMaterials[i].MatD3D.Ambient.a = pMat->fAlpha;

                pMaterials[i].MatD3D.Diffuse.r = pMat->vDiffuse.x;
                pMaterials[i].MatD3D.Diffuse.g = pMat->vDiffuse.y;
                pMaterials[i].MatD3D.Diffuse.b = pMat->vDiffuse.z;
                pMaterials[i].MatD3D.Diffuse.a = pMat->fAlpha;

                pMaterials[i].MatD3D.Specular.r = pMat->vSpecular.x;
                pMaterials[i].MatD3D.Specular.g = pMat->vSpecular.y;
                pMaterials[i].MatD3D.Specular.b = pMat->vSpecular.z;
                pMaterials[i].MatD3D.Specular.a = pMat->fAlpha;

                pMaterials[i].MatD3D.Emissive.r = 0;
                pMaterials[i].MatD3D.Emissive.g = 0;
                pMaterials[i].MatD3D.Emissive.b = 0;
                pMaterials[i].MatD3D.Emissive.a = 0;

                pMaterials[i].MatD3D.Power = ( float )pMat->nShininess;

                WideCharToMultiByte( CP_ACP, 0, pMat->strTexture, -1, ( pStrTexture + i * MAX_PATH ), MAX_PATH, NULL,
                                     NULL );
                pMaterials[i].pTextureFilename = ( pStrTexture + i * MAX_PATH );
            }

        }

        // Write to file in same directory where the .obj file was found
        WCHAR strBuf[ MAX_PATH ];
        swprintf_s( strBuf, MAX_PATH - 1, L"%s\\%s", g_MeshLoader.GetMediaDirectory(), L"MeshFromOBJ.x" );
        hr = D3DXSaveMeshToX( strBuf, g_MeshLoader.GetMesh(), NULL,
                              pMaterials, NULL, numMaterials,
                              D3DXF_FILEFORMAT_TEXT );

        if( SUCCEEDED( hr ) )
        {
            swprintf_s( g_strFileSaveMessage, MAX_PATH - 1, L"Created %s", strBuf );
        }
        else
        {
            DXTRACE_ERR( L"SaveMeshToXFile()::D3DXSaveMeshToX", hr );
            swprintf_s( g_strFileSaveMessage, MAX_PATH - 1, L"Error creating %s, check debug output", strBuf );
        }
    }

    SAFE_DELETE_ARRAY( pMaterials );
    SAFE_DELETE_ARRAY( pStrTexture );
}