⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 meshrender.cpp

📁 virtual reality project. This algorithm for building large image to Volume rendering. Using directx
💻 CPP
字号:
// MeshRender.cpp: implementation of the CMeshRender class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "mvolumerender.h"
#include "MeshRender.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

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

CMeshRender::CMeshRender()
{
	m_pMPRVertexDecl = NULL;
//	m_pMPRVertexShader = NULL;
//	m_pMPRPixelShader = NULL;
	m_pMPRVB = NULL;
//	m_pMPRVSConstantTable = NULL;

//	m_pMeshVertexDecl = NULL;
//	m_pMeshVertexShader = NULL;
//	m_pMeshPixelShader = NULL;
	m_pMesh	= NULL;
	m_pMeshMaterials = NULL;
	m_pMeshTextures = NULL;
}

CMeshRender::~CMeshRender()
{

}

void CMeshRender::DeleteDeviceObjects()
{
	// MPR object
	
	SAFE_RELEASE( m_pMPRVB );
//	SAFE_RELEASE( m_pMPRVertexShader);
	SAFE_RELEASE( m_pMPRVertexDecl);
//	SAFE_RELEASE( m_pMPRPixelShader);
//	SAFE_RELEASE( m_pMPRVSConstantTable);


	// MESH object甸阑 力芭茄促.
	

//	SAFE_RELEASE( m_pMeshVertexShader);
//	SAFE_RELEASE( m_pMeshVertexDecl);
//	SAFE_RELEASE( m_pMeshPixelShader);
//	SAFE_RELEASE( m_pMeshVSConstantTable);

	SAFE_RELEASE( m_pMesh );

    if( m_pMeshMaterials != NULL ) 
        delete[] m_pMeshMaterials;

    if( m_pMeshTextures )
    {
        for( DWORD i = 0; i < m_dwNumMaterials; i++ )
        {
            if( m_pMeshTextures[i] )
                m_pMeshTextures[i]->Release();
        }
        delete[] m_pMeshTextures;
    }
}

HRESULT CMeshRender::InitDeviceObjects(LPDIRECT3DDEVICE9 m_pd3dDevice)
{
	HRESULT hr;
	////////////////// 杭俘俊 火涝且 MESH甫 LOAD 茄促. /////////////////////////////

	// initialize mesh

	LPD3DXBUFFER pD3DXMtrlBuffer;

	// Load the mesh from the specified file
	if( FAILED( D3DXLoadMeshFromX( "arrow.x", D3DXMESH_SYSTEMMEM, 
			m_pd3dDevice, NULL, &pD3DXMtrlBuffer, NULL,
			&m_dwNumMaterials, &m_pMesh ) ) )
	{
		// If model is not in current folder, try parent folder
		if( FAILED( D3DXLoadMeshFromX( "..\\arrow.x",      
				D3DXMESH_SYSTEMMEM, m_pd3dDevice, NULL, 
				&pD3DXMtrlBuffer, NULL, &m_dwNumMaterials, 
				&m_pMesh ) ) )
		{
			AfxMessageBox("Could not find arrow.x");
			return E_FAIL;
		}
	}
	
	D3DXMATERIAL *d3dxMaterials;
	d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
    m_pMeshMaterials = new D3DMATERIAL9[m_dwNumMaterials];
    m_pMeshTextures  = new LPDIRECT3DTEXTURE9[m_dwNumMaterials];


    for(int i=0; i<(int)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;

        m_pMeshTextures[i] = NULL;
        if( d3dxMaterials[i].pTextureFilename != NULL && 
            lstrlen(d3dxMaterials[i].pTextureFilename) > 0 )
        {
            // Create the texture
            if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, 
                                                d3dxMaterials[i].pTextureFilename, 
                                                &m_pMeshTextures[i] ) ) )
            {
                // If texture is not in current folder, try parent folder
                const TCHAR* strPrefix = TEXT("..\\");
                const int lenPrefix = lstrlen( strPrefix );
                TCHAR strTexture[MAX_PATH];
                lstrcpyn( strTexture, strPrefix, MAX_PATH );
                lstrcpyn( strTexture + lenPrefix, d3dxMaterials[i].pTextureFilename, MAX_PATH - lenPrefix );
                // If texture is not in current folder, try parent folder
                if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, 
                                                    strTexture, 
                                                    &m_pMeshTextures[i] ) ) )
                {
                    AfxMessageBox("Could not find texture map");
                }
            }
        }
    }
	
	SAFE_RELEASE(pD3DXMtrlBuffer);


	//float halfSliceRange = SLICEWIDTH / 2.0f;    
	//float halfTexRange = TEXRANGE / 2.0f;
	float halfSliceRange = SLICEWIDTH / TEXRANGE / 2.0f;    
	float halfTexRange = 0.5f;

	// MPR PLANE Vertex Buffer 

	if( FAILED( hr = m_pd3dDevice->CreateVertexBuffer( 3*4*sizeof(MPRVERTEX),
                                   D3DUSAGE_WRITEONLY,
                                   0,
                                   D3DPOOL_MANAGED, &m_pMPRVB, NULL ) ) )
    return hr;

    MPRVERTEX* pMPRVertices;
    m_pMPRVB->Lock( 0, 0, (void**)&pMPRVertices, 0 );
	
	pMPRVertices[0].x = -halfSliceRange;
	pMPRVertices[0].y = halfSliceRange;
	pMPRVertices[0].z = 0;
	pMPRVertices[0].tu = -halfTexRange + 0.5f;
	pMPRVertices[0].tv = -halfTexRange + 0.5f;
	pMPRVertices[0].tw = 0.5f;

	pMPRVertices[1].x = -halfSliceRange;
	pMPRVertices[1].y = -halfSliceRange;
	pMPRVertices[1].z = 0;
	pMPRVertices[1].tu = -halfTexRange + 0.5f;
	pMPRVertices[1].tv = halfTexRange + 0.5f;
	pMPRVertices[1].tw = 0.5f;

	pMPRVertices[2].x = halfSliceRange;
	pMPRVertices[2].y = -halfSliceRange;
	pMPRVertices[2].z = 0;
	pMPRVertices[2].tu = halfTexRange + 0.5f;
	pMPRVertices[2].tv = halfTexRange + 0.5f;
	pMPRVertices[2].tw = 0.5f;

	pMPRVertices[3].x = halfSliceRange;
	pMPRVertices[3].y = halfSliceRange;
	pMPRVertices[3].z = 0;
	pMPRVertices[3].tu = halfTexRange + 0.5f;
	pMPRVertices[3].tv = -halfTexRange + 0.5f;
	pMPRVertices[3].tw = 0.5f;


	pMPRVertices[4].x = -halfSliceRange;
	pMPRVertices[4].y = 0;
	pMPRVertices[4].z = halfSliceRange;
	pMPRVertices[4].tu = -halfTexRange + 0.5f;
	pMPRVertices[4].tv = 0.5f;
	pMPRVertices[4].tw = halfTexRange + 0.5f;

	pMPRVertices[5].x = -halfSliceRange;
	pMPRVertices[5].y = 0;
	pMPRVertices[5].z = -halfSliceRange;
	pMPRVertices[5].tu = -halfTexRange + 0.5f;
	pMPRVertices[5].tv = 0.5f;
	pMPRVertices[5].tw = -halfTexRange + 0.5f;

	pMPRVertices[6].x = halfSliceRange;
	pMPRVertices[6].y = 0;
	pMPRVertices[6].z = -halfSliceRange;
	pMPRVertices[6].tu = halfTexRange + 0.5f;
	pMPRVertices[6].tv = 0.5f;
	pMPRVertices[6].tw = -halfTexRange + 0.5f;

	pMPRVertices[7].x = halfSliceRange;
	pMPRVertices[7].y = 0;
	pMPRVertices[7].z = halfSliceRange;
	pMPRVertices[7].tu = halfTexRange + 0.5f;
	pMPRVertices[7].tv = 0.5f;
	pMPRVertices[7].tw = halfTexRange + 0.5f;


	pMPRVertices[8].x = 0;
	pMPRVertices[8].y = halfSliceRange;
	pMPRVertices[8].z = -halfSliceRange;
	pMPRVertices[8].tu = 0.5f;
	pMPRVertices[8].tv = -halfTexRange + 0.5f;
	pMPRVertices[8].tw = -halfTexRange + 0.5f;

	pMPRVertices[9].x = 0;
	pMPRVertices[9].y = -halfSliceRange;
	pMPRVertices[9].z = -halfSliceRange;
	pMPRVertices[9].tu = 0.5f;
	pMPRVertices[9].tv = halfTexRange + 0.5f;
	pMPRVertices[9].tw = -halfTexRange + 0.5f;

	pMPRVertices[10].x = 0;
	pMPRVertices[10].y = -halfSliceRange;
	pMPRVertices[10].z = halfSliceRange;
	pMPRVertices[10].tu = 0.5f;
	pMPRVertices[10].tv = halfTexRange + 0.5f;
	pMPRVertices[10].tw = halfTexRange + 0.5f;

	pMPRVertices[11].x = 0;
	pMPRVertices[11].y = halfSliceRange;
	pMPRVertices[11].z = halfSliceRange;
	pMPRVertices[11].tu = 0.5f;
	pMPRVertices[11].tv = -halfTexRange + 0.5f;
	pMPRVertices[11].tw = halfTexRange + 0.5f;


	m_pMPRVB->Unlock();

/*
	ID3DXBuffer* pShaderBuffer;
	ID3DXBuffer* pShaderErrors;
	ID3DXBuffer* pShaderVBuffer;
	ID3DXBuffer* pShaderVErrors;
*/
	//4. ----- "ps_passMPR.phl" ---------------
/*
	if (FAILED(D3DXCompileShaderFromFile("ps_passMPR.phl", NULL, NULL, "main", "ps_2_0", NULL, &pShaderBuffer, &pShaderErrors, NULL)))
	{
		AfxMessageBox("-_-. Quitting...");
		return E_FAIL;
	}

	if (FAILED(m_pd3dDevice->CreatePixelShader(
		                           (DWORD *)pShaderBuffer->GetBufferPointer(),
								   &m_pMPRPixelShader)))
	{
		AfxMessageBox("The pixel shader could not be created. Quitting...");
		return E_FAIL;
	}
*/
	//4. ----- "ps_passMesh.phl" ---------------
	/*
	if (FAILED(D3DXCompileShaderFromFile("ps_passMesh.phl", NULL, NULL, "main", "ps_2_0", NULL, &pShaderBuffer, &pShaderErrors, NULL)))
	{
		AfxMessageBox("-_-. Quitting...");
		return E_FAIL;
	}

	if (FAILED(m_pd3dDevice->CreatePixelShader(
		                           (DWORD *)pShaderBuffer->GetBufferPointer(),
								   &m_pMeshPixelShader)))
	{
		AfxMessageBox("The pixel shader could not be created. Quitting...");
		return E_FAIL;
	}
*/

	//------------- vertex shader for MPR rendering --------------------------------
	// Create the shader declaration.
	D3DVERTEXELEMENT9 declMPRProxy[] = 
	{
		{ 0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
		{ 0, 12, D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		D3DDECL_END()
	};	
	
	if( FAILED( hr = m_pd3dDevice->CreateVertexDeclaration( declMPRProxy, &m_pMPRVertexDecl ) ) )
        return hr;
	// D3DXSHADER_PACKMATRIX_COLUMNMAJOR (DEFAULT) 荤侩.

	/*
	if (FAILED(D3DXCompileShaderFromFile("vs_passMPR.vhl", NULL, NULL, "main", "vs_2_0", NULL/*D3DXSHADER_PACKMATRIX_COLUMNMAJOR*//*, &pShaderVBuffer, &pShaderVErrors, &m_pMPRVSConstantTable)))
/*
	{
		AfxMessageBox("Couldn't find vertex shader file...");
		return E_FAIL;
	}

	if (FAILED(m_pd3dDevice->CreateVertexShader(
		                           (DWORD *)pShaderVBuffer->GetBufferPointer(),
								   &m_pMPRVertexShader)))
	{
		AfxMessageBox("The vertex shader could not be created. Quitting...");
		return E_FAIL;
	}
*/

	//------------- vertex shader for Mesh rendering --------------------------------
	// Create the shader declaration.
/*
	D3DVERTEXELEMENT9 declMeshProxy[] = 
	{
		{ 0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
		{ 0, 12, D3DDECLTYPE_FLOAT2,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		D3DDECL_END()
	};	
	
	if( FAILED( hr = m_pd3dDevice->CreateVertexDeclaration( declMeshProxy, &m_pMeshVertexDecl ) ) )
        return hr;
	// D3DXSHADER_PACKMATRIX_COLUMNMAJOR (DEFAULT) 荤侩.
	if (FAILED(D3DXCompileShaderFromFile("vs_passMesh.vhl", NULL, NULL, "main", "vs_2_0", NULL/*D3DXSHADER_PACKMATRIX_COLUMNMAJOR*///, &pShaderVBuffer, &pShaderVErrors, &m_pMeshVSConstantTable)))
	/*
	{
		AfxMessageBox("Couldn't find vertex shader file...");
		return E_FAIL;
	}

	if (FAILED(m_pd3dDevice->CreateVertexShader(
		                           (DWORD *)pShaderVBuffer->GetBufferPointer(),
								   &m_pMeshVertexShader)))
	{
		AfxMessageBox("The vertex shader could not be created. Quitting...");
		return E_FAIL;
	}

	pShaderBuffer->Release();
	//pShaderErrors->Release();
	pShaderVBuffer->Release();
	//pShaderVErrors->Release();
*/
	return S_OK;
}


void CMeshRender::RenderMesh(LPDIRECT3DDEVICE9 m_pd3dDevice, LPD3DXEFFECT m_pEffect)
{

	m_pEffect->Pass(5);
    for(DWORD i=0; i<m_dwNumMaterials; i++ )
    {
        // Set the material and texture for this subset
        m_pd3dDevice->SetMaterial( &m_pMeshMaterials[i] );
        m_pd3dDevice->SetTexture( 7, m_pMeshTextures[i] );
		
        // Draw the mesh subset
        m_pMesh->DrawSubset( i );
    }

}


void CMeshRender::RenderMPR(LPDIRECT3DDEVICE9 m_pd3dDevice, LPD3DXEFFECT m_pEffect, LPDIRECT3DVOLUMETEXTURE9 pVolumeTexture)
{
    m_pd3dDevice->SetTexture( 0, pVolumeTexture);
	m_pd3dDevice->SetVertexDeclaration( m_pMPRVertexDecl );
	m_pd3dDevice->SetStreamSource( 0, m_pMPRVB, 0, sizeof(MPRVERTEX) );
	m_pEffect->Pass(6);
	m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );	
	m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 4, 2 );	
	m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 8, 2 );	
}

void CMeshRender::RestoreDeviceObjects(LPDIRECT3DDEVICE9 m_pd3dDevice)
{
    m_pd3dDevice->SetSamplerState( 7, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
    m_pd3dDevice->SetSamplerState( 7, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
	m_pd3dDevice->SetSamplerState( 7, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
	m_pd3dDevice->SetSamplerState( 7, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);
	m_pd3dDevice->SetSamplerState( 7, D3DSAMP_ADDRESSW, D3DTADDRESS_WRAP);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -