Esempio n. 1
0
		LPDIRECT3DINDEXBUFFER9 CreateIndexBuffer( LPDIRECT3DDEVICE9 device, OvByte* buffer, OvSize bufsize )
		{
			if ( device && buffer && bufsize )
			{
				LPDIRECT3DINDEXBUFFER9	streamBuffer = NULL;
				HRESULT hr = device->CreateIndexBuffer
					( bufsize
					, 0
					, D3DFMT_INDEX16
					, D3DPOOL_MANAGED
					, &streamBuffer
					, NULL
					);

				if ( SUCCEEDED(hr) && streamBuffer )
				{
					void* copyDest = NULL;
					if ( SUCCEEDED(streamBuffer->Lock( 0, bufsize, &copyDest, 0)) && copyDest)
					{
						memcpy( copyDest, buffer, bufsize );
						streamBuffer->Unlock();
						return streamBuffer;
					}
				}
			}
			return NULL;
		}
/**-----------------------------------------------------------------------------
 * 인덱스 버퍼 초기화
 *------------------------------------------------------------------------------
 */
HRESULT InitIB()
{
    if( FAILED( g_pd3dDevice->CreateIndexBuffer( (g_cxHeight-1)*(g_czHeight-1)*2 * sizeof(MYINDEX), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_pIB, NULL ) ) )
    {
        return E_FAIL;
    }

    MYINDEX		i;
    MYINDEX*	pI;
    if( FAILED( g_pIB->Lock( 0, (g_cxHeight-1)*(g_czHeight-1)*2 * sizeof(MYINDEX), (void**)&pI, 0 ) ) )
        return E_FAIL;

    for ( DWORD z = 0; z < g_czHeight - 1; z++ )
    {
        for ( DWORD x = 0; x < g_cxHeight - 1; x++ )
        {
            i._0 = static_cast<WORD>( z*g_cxHeight + x );
            i._1 = static_cast<WORD>( z*g_cxHeight + x + 1 );
            i._2 = static_cast<WORD>( ( z + 1 )*g_cxHeight + x );
            *pI++ = i;
            i._0 = static_cast<WORD>( ( z + 1 )*g_cxHeight + x );
            i._1 = static_cast<WORD>( z*g_cxHeight + x + 1 );
            i._2 = static_cast<WORD>( ( z + 1 )*g_cxHeight + x + 1 );
            *pI++ = i;
        }
    }
    g_pIB->Unlock();

    return S_OK;
}
Esempio n. 3
0
LPDIRECT3DINDEXBUFFER9 OvRenderer::CreateIndexStream( void* buffer, UINT stride, UINT count )
{
	OvDevice device = GetDevice();
	if ( device )
	{
		LPDIRECT3DINDEXBUFFER9	streamBuffer = NULL;
		UINT streamSize = count * stride;
		HRESULT hr = device->CreateIndexBuffer
			( streamSize
			, 0
			, D3DFMT_INDEX16
			, D3DPOOL_MANAGED
			, &streamBuffer
			, NULL
			);

		if ( SUCCEEDED(hr) && streamBuffer )
		{
			void* copyDest = NULL;
			if ( SUCCEEDED(streamBuffer->Lock( 0, streamSize, &copyDest, 0)) && copyDest)
			{
				memcpy( copyDest, buffer, streamSize );
				streamBuffer->Unlock();
				return streamBuffer;
			}
		}
	}
	return NULL;

}
Esempio n. 4
0
bool CEntity::ComputeNormalVector(
	LPD3DXMESH _pMesh, D3DXVECTOR3& _vNormal, D3DXVECTOR3& _vCol, CEntity* target)
{
	BOOL isHit = false;
	DWORD dwFaceIndex = 0;
	float fDist = 0;
	LPD3DXBUFFER ppAllhit;
	DWORD pCountOfHits;
	
	D3DXVECTOR3 vPos = m_vPos - target->GetPos();
	D3DXVECTOR3 vtar = (-vPos);
	D3DXVec3Normalize( &vtar, &vtar);

	D3DXIntersect( _pMesh, &vPos, &vtar, &isHit, 
		&dwFaceIndex, NULL, NULL, &fDist, &ppAllhit, &pCountOfHits );

	if ( !isHit || fDist > GetSize()  )
		return false;// Ãæµ¹ÀÌ ¾È‰ç°Å³ª °Å¸®°¡ ¸Ö´Ù¸é ¸®ÅÏ;

	LPDIRECT3DVERTEXBUFFER9 pVB; 
	LPDIRECT3DINDEXBUFFER9 pIB; 

	_pMesh->GetVertexBuffer(&pVB); 
	_pMesh->GetIndexBuffer( &pIB ); 

	WORD* pIndices; 
	D3DVERTEX* pVertices; 

	pIB->Lock( 0, 0, (void**)&pIndices, 0 ); 
	pVB->Lock( 0, 0,(void**)&pVertices, 0); 

	D3DXVECTOR3 v0 = pVertices[pIndices[3*dwFaceIndex+0]].vPos; 
	D3DXVECTOR3 v1 = pVertices[pIndices[3*dwFaceIndex+1]].vPos; 
	D3DXVECTOR3 v2 = pVertices[pIndices[3*dwFaceIndex+2]].vPos; 
	
	D3DXPLANE plane;
	
	D3DXPlaneFromPoints( &plane, &v0, &v1, &v2);
	
	_vCol = (v0 + v1 + v2)/3.f;
	_vCol += target->GetPos();

	_vNormal.x = plane.a;
	_vNormal.y = plane.b;
	_vNormal.z = plane.c;
	
#ifdef _DEBUG
	//Ãæµ¹ÁöÁ¡ Ç¥½Ã
	_SINGLE(CDebug)->AddPosMark( _vCol, COLOR_BLACK);
#endif
	
	pVB->Unlock(); 
	pIB->Unlock(); 
	Safe_Release(pVB); 
	Safe_Release(pIB); 

	return true;
}
Esempio n. 5
0
void GdsNode::CreateOctree()
{
	RENDER_OBJECT_CONTAINER::iterator it = m_list_RenderObject.begin();
	for ( ; it != m_list_RenderObject.end() ; ++it )
	{
		VOID* pVB;
		GdsRenderObject* rendertoken = it->first;
		LPDIRECT3DVERTEXBUFFER9 vb = rendertoken->GetVertexBuffer();
		if (  SUCCEEDED( vb->Lock( 0 , rendertoken->GetVertexMaxCount() * sizeof( GDSVERTEX ) , (void**)&pVB , 0 ) ) )
		{
			D3DXVECTOR3 minPos( 0,0,0 );
			D3DXVECTOR3 maxPos( 0,0,0 );
			for ( int i=0 ; i < rendertoken->GetVertexMaxCount() ; i++ )
			{				
				GDSVERTEX* v = (GDSVERTEX*)pVB + i;

				if ( minPos.x > v->p.x )
					minPos.x = v->p.x;

				if ( minPos.y > v->p.y )
					minPos.y = v->p.y;

				if ( minPos.z > v->p.z )
					minPos.z = v->p.z;

				if ( maxPos.x < v->p.x )
					maxPos.x = v->p.x;
				if ( maxPos.y < v->p.y )
					maxPos.y = v->p.y;
				if ( maxPos.z < v->p.z )
					maxPos.z = v->p.z;
			}

			m_pOctreeRootNode = NULL;
			m_pVert = (GDSVERTEX*)pVB;
			if ( m_pOctreeRootNode == NULL )
				m_pOctreeRootNode = new Node( rendertoken->GetIndexMaxCount() , minPos , maxPos );

			VOID* pI;
			LPDIRECT3DINDEXBUFFER9 pIB = rendertoken->GetIndexBuffer();
			pIB->Lock( 0 , rendertoken->GetIndexMaxCount() * sizeof( GDSINDEX ) , (void**)&pI , 0 );
			memcpy( m_pOctreeRootNode->m_pFace , pI , sizeof( GDSINDEX ) * rendertoken->GetIndexMaxCount() );
			pIB->Unlock();
			m_pOctreeRootNode->m_iNumOfChild = 0;	

			build( m_pOctreeRootNode );
			
		}		
		vb->Unlock();
	}

	m_bUseOctree = true;
	m_iCountOfOctreeNode = 0;
}
Esempio n. 6
0
void ComputeNormals(LPDIRECT3DVERTEXBUFFER9 vtxBuff, int vtxSize,  LPDIRECT3DINDEXBUFFER9 idxBuff, int faceSize)
{
	Vertex* vertices;
	vtxBuff->Lock( 0, sizeof(Vertex), (void**)&vertices, 0);
	WORD *indices = NULL;
	idxBuff->Lock(0, 0, (void**)&indices, 0);

	for (int i=0; i < faceSize*3; i+=3)
	{
		Vector3 p1 = vertices[ indices[ i]].p;
		Vector3 p2 = vertices[ indices[ i+1]].p;
		Vector3 p3 = vertices[ indices[ i+2]].p;

		Vector3 v1 = p2 - p1;
		Vector3 v2 = p3 - p1;
		v1.Normalize();
		v2.Normalize();
		Vector3 n = v1.CrossProduct(v2);
		n.Normalize();

		if (vertices[ indices[ i]].n.IsEmpty())
		{
			vertices[ indices[ i]].n = n;
		}
		else
		{
			vertices[ indices[ i]].n += n;
			vertices[ indices[ i]].n /= 2.f;
		}

		if (vertices[ indices[ i+1]].n.IsEmpty())
		{
			vertices[ indices[ i+1]].n = n;
		}
		else
		{
			vertices[ indices[ i+1]].n += n;
			vertices[ indices[ i+1]].n /= 2.f;
		}

		if (vertices[ indices[ i+2]].n.IsEmpty())
		{
			vertices[ indices[ i+2]].n = n;
		}
		else
		{
			vertices[ indices[ i+2]].n += n;
			vertices[ indices[ i+2]].n /= 2.f;
		}
	}

	vtxBuff->Unlock();
	idxBuff->Unlock();
}
Esempio n. 7
0
void cSubDivSurf::Draw( matrix4& mat )
{
	LPDIRECT3DDEVICE9 lpDevice = Graphics()->GetDevice();
	Graphics()->SetWorldMatrix( mat );

	HRESULT hr;

	// The index buffer
	LPDIRECT3DINDEXBUFFER9 pIndexBuffer = 0;

	// Create the index buffer
	lpDevice->CreateIndexBuffer(
		m_nTris * 3 * sizeof( WORD ),	// Size in bytes of buffer
		D3DUSAGE_WRITEONLY,				// Will only be writing to the buffer
		D3DFMT_INDEX16,					// 16 bit indices
		D3DPOOL_DEFAULT,				// Default memory pooling
		&pIndexBuffer,					// Address of the buffer pointer
		NULL );							// Reserved. set to NULL

	// Pointer to the index buffer data
	WORD* pData = 0;

	// Lock the index buffer
	pIndexBuffer->Lock( 0, 0, (void**)&pData, 0 );

	// Copy the index data into the index buffer
	CopyMemory( pData, m_d3dTriList, m_nTris * 3 * sizeof( WORD ) );

	// Unlock the index buffer
	pIndexBuffer->Unlock();

	// Tell Direct3D to use the index buffer
	lpDevice->SetIndices( pIndexBuffer );
	
	// Attach the vertex buffer to rendering stream 0
	lpDevice->SetStreamSource( 0, m_pVertexBuffer, 0, sizeof( sVertex ) );

	// Draw the primitive
	hr = lpDevice->DrawIndexedPrimitive(
		D3DPT_TRIANGLELIST,
		0,
		0,
		m_nVerts,
		0,
		m_nTris );

	if( FAILED( hr ) )
	{
		DP0("[cSubDivSurf::Draw]: DrawIndexedPrimitive failed!\n");
	}

	pIndexBuffer->Release();
}
Esempio n. 8
0
/************************************************************************
* Objects such as vertex buffer, index buffer, fonts are initialized here
************************************************************************/
bool ObjectInit(HWND hwnd)
{

	PlaySound(_T("阿兰 - 浴火重生.wav"), nullptr, SND_ASYNC | SND_FILENAME | SND_LOOP);
	srand(unsigned(time(NULL)));

	D3DXCreateFont(gPD3DDevice, 40, 0, 0, 0, 0, 0, 0, 0, 0, _T("楷体"), &gPFont);

	//////////////////////////////////////////////////////////////////////////
	// Fill in the vertex struct  
	//////////////////////////////////////////////////////////////////////////
	CUSTOM_VERTEX vertices[17];
	vertices[0].x = 400.0f;
	vertices[0].y = 300.0f;
	vertices[0].z = 0.0f;
	vertices[0].rhw = 1.0f;
	vertices[0].color = D3DCOLOR_XRGB(rand() % 256, rand() % 256, rand() % 256);
	for (int i = 1; i < 17; i++)
	{
		vertices[i].x = gRadius*cos(PI*(i - 1)/ 8) + vertices[0].x;
		vertices[i].y = gRadius*sin(PI*(i - 1)/ 8) + vertices[0].y;
		vertices[i].z = 0.0f;
		vertices[i].rhw = 1.0f;
		vertices[i].color = D3DCOLOR_XRGB(rand() % 256, rand() % 256, rand() % 256);
	}

	gPD3DDevice->CreateVertexBuffer(17 * sizeof(CUSTOM_VERTEX), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_DEFAULT,
		&gPVertexBuffer, nullptr);
	void* pVertices{0};
	gPVertexBuffer->Lock(0, 17 * sizeof(CUSTOM_VERTEX), (void**)&pVertices, 0);
	memcpy(pVertices, vertices, 17 * sizeof(CUSTOM_VERTEX));
	gPVertexBuffer->Unlock();

    //////////////////////////////////////////////////////////////////////////
    // Fill in the index struct  
    //////////////////////////////////////////////////////////////////////////	
	int indexes[48];
	for (int i = 0; i < 16; i++)
	{
		indexes[i * 3] = 0;
		indexes[i * 3 + 1] = i + 1;
		indexes[i * 3 + 2] = i + 2;
	}
	indexes[47] = 1;
	
	gPD3DDevice->CreateIndexBuffer(48 * sizeof(int), 0, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &gPIndexBuffer, nullptr);
	int* pIndexes{0};
	gPIndexBuffer->Lock(0, 0, (void**)&pIndexes, 0);
	memcpy(pIndexes, indexes, 48 * sizeof(int));
	gPIndexBuffer->Unlock();

	return true;
}
/**-----------------------------------------------------------------------------
 * Frustum Culling을 사용해서 그려질 인덱스 생성
 *------------------------------------------------------------------------------
 */
HRESULT ProcessFrustumCull()
{
	DWORD		i[4];	// 임시로 저장할 인덱스 정보
	BOOL		b[4];	// 임시로 저장할 frustum culling결과값
	MYINDEX		idx;
    MYINDEX*	pI;

	if ( FAILED( g_pIB->Lock( 0, ( g_cxHeight - 1 )*( g_czHeight - 1 ) * 2 * sizeof( MYINDEX ), (void**)&pI, 0 ) ) )
	{
		return E_FAIL;
	}

	g_nTriangles = 0;

	for( DWORD z = 0 ; z < g_czHeight-1 ; z++ )
	{
		for( DWORD x = 0 ; x < g_cxHeight-1 ; x++ )
		{
			i[0] = (z*g_cxHeight+x);			// 좌측 상단
			i[1] = (z*g_cxHeight+x+1);			// 우측 상단
			i[2] = ((z+1)*g_cxHeight+x);		// 좌측 하단
			i[3] = ((z+1)*g_cxHeight+x+1);		// 우측 하단

			b[0] = g_pFrustum->IsIn( &g_pvHeightMap[ i[0] ] );	// 좌측상단 정점이 Frustum안에 있는가?
			b[1] = g_pFrustum->IsIn( &g_pvHeightMap[ i[1] ] );	// 우측상단 정점이 Frustum안에 있는가?
			b[2] = g_pFrustum->IsIn( &g_pvHeightMap[ i[2] ] );	// 좌측하단 정점이 Frustum안에 있는가?
			if ( b[0] | b[1] | b[2] )	// 셋중에 하나라도 frustum안에 있으면 렌더링한다.
			{
				idx._0 = static_cast<WORD>( i[0] );
				idx._1 = static_cast<WORD>( i[1] );
				idx._2 = static_cast<WORD>( i[2] );
				*pI++ = idx;
				g_nTriangles++;			// 렌더링할 삼각형 개수 증가
			}

			b[2] = g_pFrustum->IsIn( &g_pvHeightMap[ i[2] ] );	// 좌측하단 정점이 Frustum안에 있는가?
			b[1] = g_pFrustum->IsIn( &g_pvHeightMap[ i[1] ] );	// 우측상단 정점이 Frustum안에 있는가?
			b[3] = g_pFrustum->IsIn( &g_pvHeightMap[ i[3] ] );	// 우측하단 정점이 Frustum안에 있는가?
			if ( b[2] | b[1] | b[3] )	// 셋중에 하나라도 frustum안에 있으면 렌더링한다.
			{
				idx._0 = static_cast<WORD>( i[2] );
				idx._1 = static_cast<WORD>( i[1] );
				idx._2 = static_cast<WORD>( i[3] );
				*pI++ = idx;
				g_nTriangles++;
			}
		}
	}
    g_pIB->Unlock();

    return S_OK;
}
Esempio n. 10
0
// this is the function that puts the 3D models into video RAM
void init_graphics(void)
{   
    // create a vertex buffer interface called v_buffer
    d3ddev->CreateVertexBuffer(g_Sim.numPoints()*sizeof(CUSTOMVERTEX),
                               D3DUSAGE_WRITEONLY,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &v_buffer,
                               NULL);
	d3ddev->CreateVertexBuffer(5*sizeof(CUSTOMVERTEX),
		                       D3DUSAGE_WRITEONLY,
							   CUSTOMFVF,
							   D3DPOOL_MANAGED,
							   &v_bordbuffer,
							   NULL);
	d3ddev->CreateIndexBuffer(g_Sim.numConstraints() * 2 * sizeof(short),
							  D3DUSAGE_WRITEONLY,
							  D3DFMT_INDEX16,
							  D3DPOOL_MANAGED,
							  &i_buffer,
							  NULL);


	HRESULT hr = D3DXCreateFont(d3ddev, 17, 0, FW_NORMAL, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Courier New"), &font );

	CUSTOMVERTEX* verts;
	v_bordbuffer->Lock(0, 0, (void**)&verts, 0);
	verts[0].X = MINX;
	verts[0].Y = MINY;
	verts[1].X = MINX;
	verts[1].Y = MAXY;
	verts[2].X = MAXX;
	verts[2].Y = MAXY;
	verts[3].X = MAXX;
	verts[3].Y = MINY;
	verts[4].X = MINX;
	verts[4].Y = MINY;

	verts[0].Z = verts[1].Z = verts[2].Z = verts[3].Z = verts[4].Z = 0.0f;
	verts[0].COLOR = verts[1].COLOR = verts[2].COLOR = verts[3].COLOR = verts[4].COLOR = D3DCOLOR_XRGB(127, 127, 127);
	v_bordbuffer->Unlock();

    short* indices;
    i_buffer->Lock(0, 0, (void**)&indices, 0);
	for (unsigned i = 0; i < g_Sim.numConstraints(); i++) {
		const std::pair<unsigned, unsigned>& p = g_Sim.constraint(i);
		indices[2 * i] = p.first;
		indices[2 * i + 1] = p.second;
	}
    i_buffer->Unlock();

}
Esempio n. 11
0
HRESULT	CameraWorkBase::SetIB( LPDIRECT3DINDEXBUFFER9 _pIB, LPVOID _indices, INT _nIndex, INT _Size )
{
	LPVOID pIndices;
	if( FAILED( _pIB->Lock( 0, _nIndex * _Size, (VOID**)&pIndices, 0 ) ) )
	{
		MessageBox( NULL, L"CameraWorkBase::m_pIB->Lock() failed.", NULL, MB_OK );
		return E_FAIL;
	}
	
		memcpy( pIndices, _indices, _nIndex * _Size );

	_pIB->Unlock();

	return S_OK;
}
Esempio n. 12
0
    void LoadMesh(const PMDLoader::DATA *_data)
    {
        LPDIRECT3DDEVICE9 device = DirectX9::Instance().Device;
        HRESULT hr;

        // 頂点バッファの作成
        m_MaxVertex = _data->vert_count.u;
        hr = device->CreateVertexBuffer(
            sizeof(Vertex3D::VERTEX) * _data->vert_count.u,
            D3DUSAGE_WRITEONLY,                // 頂点バッファの使用方法
            Vertex3D::FVF,                     // 使用する頂点フォーマット
            D3DPOOL_MANAGED,                   // バッファを保持するメモリクエストを指定
            &m_VertexBuffer,                   // 頂点のバッファの先頭アドレス
            NULL );

        Vertex3D::VERTEX *vtx;
        hr = m_VertexBuffer->Lock(0,0,(void**)&vtx,0);
        for( unsigned int i = 0; i < _data->vert_count.u ; i++)
        {
            vtx[i].pos.x =   _data->vertex[i].pos[0].f;
            vtx[i].pos.y =   _data->vertex[i].pos[1].f;
            vtx[i].pos.z =  -_data->vertex[i].pos[2].f;
            vtx[i].nor.x =  _data->vertex[i].normal_vec[0].f;
            vtx[i].nor.y =  _data->vertex[i].normal_vec[1].f;
            vtx[i].nor.z = -_data->vertex[i].normal_vec[2].f;
            vtx[i].tex.x =  _data->vertex[i].uv[0].f;
            vtx[i].tex.y =  _data->vertex[i].uv[1].f;
        }
        hr = m_VertexBuffer->Unlock();

        // インデックスバッファの作成
        m_MaxIndex = _data->face_vert_count.u;
        hr = device->CreateIndexBuffer(
            sizeof( WORD ) * m_MaxIndex,    // バッファサイズ
            D3DUSAGE_WRITEONLY,             // 頂点バッファの使用方法
            D3DFMT_INDEX16,                 // 使用する頂点フォーマット
            D3DPOOL_MANAGED,                // バッファを保持するメモリクエストを指定
            &m_IndexBuffer,                 // 頂点インデックスのバッファの先頭アドレス
            NULL );

        WORD *idx;
        hr = m_IndexBuffer->Lock(0,0,(void**)&idx,0);
        for( unsigned int i=0; i < _data->face_vert_count.u ; i++)
        {
            idx[i] = _data->face_vert_index[i].u;
        }
        hr = m_IndexBuffer->Unlock();
    }
Esempio n. 13
0
//===============================================
//頂点情報のコンバート
//===============================================
//[input]
//	pD3DX9:Direct3Dデバイス
//[return]
//	HREULT値
//===============================================
bool CXMesh::ConvertVertex(LPDIRECT3DDEVICE9 pD3DX9)
{
    LPD3DXBUFFER pD3DXMtrlBuffer = NULL;

	/*Vertex Bufferにコピーする*/
	D3DVERTEX* pSrc;
	D3DVERTEX* pDest;
	LPDIRECT3DINDEXBUFFER9 pSrcIndex;
	WORD* pISrc;
	WORD* pIDest;

	/*VertexBuffer情報取得*/
	LPDIRECT3DVERTEXBUFFER9 pVB;
	MeshData.pMesh->GetVertexBuffer(&pVB);

	D3DVERTEXBUFFER_DESC	Desc;
	pVB->GetDesc( &Desc );

	DWORD nMeshVertices	= MeshData.pMesh->GetNumVertices();
	DWORD nMeshFaces	= MeshData.pMesh->GetNumFaces();

	/*頂点バッファを作成*/
	pD3DX9->CreateVertexBuffer( Desc.Size, 0, MeshData.pMesh->GetFVF(), D3DPOOL_MANAGED, &m_pMeshVB, NULL );

	/*インデックスバッファを作成*/
	pD3DX9->CreateIndexBuffer( nMeshFaces * 3 * sizeof(WORD), 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_pMeshIndex, NULL );

	/*頂点バッファをコピー*/
	pVB->Lock(0,0,(void**)&pSrc,0);
	m_pMeshVB->Lock(0,0,(void**)&pDest,0);
	CopyMemory( pDest, pSrc, Desc.Size );

	pVB->Unlock();
	pVB->Release();
	m_pMeshVB->Unlock();

	/*インデックスのコピー*/
	MeshData.pMesh->GetIndexBuffer( &pSrcIndex );
	pSrcIndex->Lock( 0, 0, (void**)&pISrc, 0 );
	m_pMeshIndex->Lock( 0, 0, (void**)&pIDest, 0 );
	CopyMemory( pIDest, pISrc, nMeshFaces * 3 * sizeof( WORD ) );

	pSrcIndex->Unlock();
	m_pMeshIndex->Unlock();
	pSrcIndex->Release();

    return true;
}
Esempio n. 14
0
//------------------------------------------------------------------------------
VOID Cleanup()
{
    if ( g_pIndexBuff )  { g_pIndexBuff->Release();  g_pIndexBuff = NULL; }
    if ( g_pVertexBuff ) { g_pVertexBuff->Release(); g_pVertexBuff = NULL; }
    if ( g_pd3dDevice )  { g_pd3dDevice->Release();  g_pd3dDevice = NULL; }
    if ( g_pD3D )        { g_pD3D->Release();        g_pD3D = NULL; }
}
/**-----------------------------------------------------------------------------
 * 초기화 객체들 소거
 *------------------------------------------------------------------------------
 */
VOID Cleanup()
{
	if ( g_pTexHeight != NULL )
	{
		g_pTexHeight->Release();
	}

	if ( g_pTexDiffuse != NULL )
	{
		g_pTexDiffuse->Release();
	}

	if ( g_pIB != NULL )
	{
		g_pIB->Release();
	}

	if ( g_pVB != NULL )
	{
		g_pVB->Release();
	}

	if ( g_pd3dDevice != NULL )
	{
		g_pd3dDevice->Release();
	}

	if ( g_pD3D != NULL )
	{
		g_pD3D->Release();
	}

	delete[] g_pvHeightMap;
}
/**-----------------------------------------------------------------------------
 * 초기화 객체들 소거
 *------------------------------------------------------------------------------
 */
VOID Cleanup()
{
    if ( g_pTexNormal != NULL )
    {
        g_pTexNormal->Release();
    }
    if ( g_pTexHeight != NULL )
    {
        g_pTexHeight->Release();
    }
    if ( g_pTexDiffuse != NULL )
    {
        g_pTexDiffuse->Release();
    }
    if ( g_pIB != NULL )
    {
        g_pIB->Release();
    }
    if ( g_pVB != NULL )
    {
        g_pVB->Release();
    }
    if ( g_pd3dDevice != NULL )
    {
        g_pd3dDevice->Release();
    }
    if ( g_pD3D != NULL )
    {
        g_pD3D->Release();
    }
}
Esempio n. 17
0
void D3DQuads_OnRecover (void)
{
	if (!d3d_QuadBuffer)
	{
		D3DMain_CreateVertexBuffer (D3D_MAX_QUADS * 4 * sizeof (quadvert_t), D3DUSAGE_DYNAMIC, &d3d_QuadBuffer);
		d3d_NumQuads = 0;
	}

	if (!d3d_QuadIndexes)
	{
		unsigned short *ndx = NULL;

		D3DMain_CreateIndexBuffer16 (D3D_MAX_QUADS * 6, 0, &d3d_QuadIndexes);
		d3d_QuadIndexes->Lock (0, 0, (void **) &ndx, d3d_GlobalCaps.DefaultLock);

		// strips?  go hang yer bollocks on them.  with a 4-entry vertex cache this is just fine
		for (int i = 0, v = 0; i < D3D_MAX_QUADS; i++, v += 4, ndx += 6)
		{
			ndx[0] = v + 0;
			ndx[1] = v + 1;
			ndx[2] = v + 2;
			ndx[3] = v + 0;
			ndx[4] = v + 2;
			ndx[5] = v + 3;
		}

		d3d_QuadIndexes->Unlock ();
		d3d_RenderDef.numlock++;
	}

	if (!d3d_QuadDecl)
	{
		D3DVERTEXELEMENT9 d3d_quadlayout[] =
		{
			VDECL (0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0),
			VDECL (0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0),
			VDECL (0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0),
			D3DDECL_END ()
		};

		hr = d3d_Device->CreateVertexDeclaration (d3d_quadlayout, &d3d_QuadDecl);
		if (FAILED (hr)) Sys_Error ("D3DQuads_OnRecover: d3d_Device->CreateVertexDeclaration failed");
	}
}
Esempio n. 18
0
void ChangeObject(void)
{
	//顶点数据  
	CUSTOMVERTEX vertices[] =
	{
		{ -20.0f, 20.0f, -20.0f, D3DCOLOR_XRGB(rand() % 256, rand() % 256, rand() % 256) },
		{ -20.0f, 20.0f, 20.0f, D3DCOLOR_XRGB(rand() % 256, rand() % 256, rand() % 256) },
		{ 20.0f, 20.0f, 20.0f, D3DCOLOR_XRGB(rand() % 256, rand() % 256, rand() % 256) },
		{ 20.0f, 20.0f, -20.0f, D3DCOLOR_XRGB(rand() % 256, rand() % 256, rand() % 256) },
		{ -20.0f, -20.0f, -20.0f, D3DCOLOR_XRGB(rand() % 256, rand() % 256, rand() % 256) },
		{ -20.0f, -20.0f, 20.0f, D3DCOLOR_XRGB(rand() % 256, rand() % 256, rand() % 256) },
		{ 20.0f, -20.0f, 20.0f, D3DCOLOR_XRGB(rand() % 256, rand() % 256, rand() % 256) },
		{ 20.0f, -20.0f, -20.0f, D3DCOLOR_XRGB(rand() % 256, rand() % 256, rand() % 256) }
	};

	VOID * pVertics = 0;
	g_pVertexBuffer->Lock(0, sizeof(vertices), (void**)&pVertics, 0);
	memcpy(pVertics, vertices, sizeof(vertices));
	g_pVertexBuffer->Unlock();

	// 填充索引数据  
	WORD *pIndices = NULL;
	g_pIndexBuffer->Lock(0, 0, (void**)&pIndices, 0);
	// 顶面  
	pIndices[0] = 0, pIndices[1] = 1, pIndices[2] = 2;
	pIndices[3] = 0, pIndices[4] = 2, pIndices[5] = 3;
	// 正面  
	pIndices[6] = 0, pIndices[7] = 3, pIndices[8] = 7;
	pIndices[9] = 0, pIndices[10] = 7, pIndices[11] = 4;
	// 左侧面  
	pIndices[12] = 0, pIndices[13] = 4, pIndices[14] = 5;
	pIndices[15] = 0, pIndices[16] = 5, pIndices[17] = 1;
	// 右侧面  
	pIndices[18] = 2, pIndices[19] = 6, pIndices[20] = 7;
	pIndices[21] = 2, pIndices[22] = 7, pIndices[23] = 3;
	// 背面  
	pIndices[24] = 2, pIndices[25] = 5, pIndices[26] = 6;
	pIndices[27] = 2, pIndices[28] = 1, pIndices[29] = 5;
	// 底面  
	pIndices[30] = 4, pIndices[31] = 6, pIndices[32] = 5;
	pIndices[33] = 4, pIndices[34] = 7, pIndices[35] = 6;
	g_pIndexBuffer->Unlock();
}
Esempio n. 19
0
VOID cleanup(){
	if(g_pVB != NULL)
		g_pVB->Release();
	if(g_pIB != NULL)
		g_pIB->Release();
	if(g_pDevice != NULL)
		g_pDevice->Release();
	if(g_pD3D != NULL)
		g_pD3D->Release();
}
Esempio n. 20
0
void GdsNode::GenOctreeFaceIndex()
{
	if ( !m_bUseOctree )
		return;

	RENDER_OBJECT_CONTAINER::iterator it = m_list_RenderObject.begin();
	for ( ; it != m_list_RenderObject.end() ; ++it )
	{
		LPDWORD pIB;
		GdsRenderObject* rendertoken = it->first;
		LPDIRECT3DINDEXBUFFER9 pI = rendertoken->GetIndexBuffer();
		if( SUCCEEDED( pI->Lock( 0 , 0 , (void**)&pIB , 0 ) ) )
		{
			int iMaxCountIndex = genTriIndex( m_pOctreeRootNode , pIB , 0 );
			rendertoken->SetIndexMaxCount( iMaxCountIndex );
		}		
		pI->Unlock();
	}
}
Esempio n. 21
0
																																					//
// Creates a DirectX mesh
void DirectX::Mesh::create( void* vertexBuffer, void* indexBuffer, unsigned long FVF, unsigned int nVertices, unsigned int nFaces, unsigned long options )
{
	// Calculate vertex stride
	int vertexStride =
		((FVF|D3DFVF_XYZ)      == FVF) * 12 +
		((FVF|D3DFVF_XYZRHW)   == FVF) * 12 +
		((FVF|D3DFVF_XYZB1)    == FVF) * 12 +
		((FVF|D3DFVF_XYZB2)    == FVF) * 12 +
		((FVF|D3DFVF_XYZB3)    == FVF) * 12 +
		((FVF|D3DFVF_XYZB4)    == FVF) * 12 +
		((FVF|D3DFVF_XYZB5)    == FVF) * 12 +
		((FVF|D3DFVF_XYZW)     == FVF) * 12 +
		((FVF|D3DFVF_NORMAL)   == FVF) * 12 +
		((FVF|D3DFVF_DIFFUSE)  == FVF) * 4  +
		((FVF|D3DFVF_SPECULAR) == FVF) * 4  +
		((FVF|D3DFVF_TEX1)     == FVF) * 8  +
		((FVF|D3DFVF_TEX2)     == FVF) * 8  +
		((FVF|D3DFVF_TEX3)     == FVF) * 8  +
		((FVF|D3DFVF_TEX4)     == FVF) * 8  +
		((FVF|D3DFVF_TEX5)     == FVF) * 8  +
		((FVF|D3DFVF_TEX6)     == FVF) * 8  +
		((FVF|D3DFVF_TEX7)     == FVF) * 8  +
		((FVF|D3DFVF_TEX8)     == FVF) * 8;

	// Create mesh  of sufficient size to store vertex/index buffer data
	HRESULT hr = D3DXCreateMeshFVF( nFaces, nVertices, options, FVF, DirectX::Manager::instance( )->getD3DDev( ), &m_mesh );
	if( hr ) MessageBox( NULL, L"Call to D3DXCreateMeshFVF has failed.", L"DirectX Engine", NULL );
	LPDIRECT3DVERTEXBUFFER9 vb; LPDIRECT3DINDEXBUFFER9 ib; m_mesh->GetVertexBuffer( &vb ); m_mesh->GetIndexBuffer( &ib );
	
	// Lock the ibuffer and load the indices
	char* pVertices; vb->Lock( NULL, NULL, (void**)&pVertices, D3DLOCK_DISCARD );
	memcpy( pVertices, vertexBuffer, nVertices*vertexStride ); vb->Unlock( );

	// Lock the vbuffer and load the vertices
	char* pIndices; ib->Lock( NULL, NULL, (void**)&pIndices, D3DLOCK_DISCARD );
	memcpy( pIndices, indexBuffer, nFaces*3*sizeof(short) ); ib->Unlock( );

	// Register with the manager
	if( !(options&D3DXMESH_SYSTEMMEM) ) 
		DirectX::Manager::instance( )->addResource( this );
}
Esempio n. 22
0
///Create Vertex Buffer
HRESULT initVB(){
	CUSTOMVERTEX vertices[9];
	vertices[0].x = 300;
	vertices[0].y = 250;
	vertices[0].z = 0.5f;
	vertices[0].rhw = 1.0f;
	vertices[0].color = 0xffff0000;
	for(int i=0; i<8; ++i){
		vertices[i+1].x = (float)(200*sin(i*3.14159/4.0)) + 300;
		vertices[i+1].y = -(float)(200*cos(i*3.14159/4.0)) + 250;
		vertices[i+1].z = 0.5f;
		vertices[i+1].rhw = 1.0f;
		vertices[i+1].color = 0xff00ff00;
	}
	WORD indices[] = { 0,1,2, 0,2,3, 0,3,4, 0,4,5, 0,5,6, 0,6,7, 0,7,8, 0,8,1 };
	//Vertex Buffer
	if(FAILED(g_pDevice->CreateVertexBuffer(9 * sizeof(CUSTOMVERTEX),
		0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL)))
		return E_FAIL;

	VOID* pVertices;
	if(FAILED(g_pVB->Lock(0, sizeof(vertices), (void**)&pVertices, 0)))
		return E_FAIL;
	memcpy(pVertices, vertices, sizeof(vertices));
	g_pVB->Unlock();
	///Index Buffer
	if(FAILED(g_pDevice->CreateIndexBuffer(24* sizeof(WORD), 0, D3DFMT_INDEX16,
		D3DPOOL_DEFAULT, &g_pIB, NULL)))
		return E_FAIL;

	VOID* pIndices;
	if(FAILED(g_pIB->Lock(0, sizeof(indices), (void**)&pIndices, 0)))
		return E_FAIL;
	memcpy(pIndices, indices, sizeof(indices));
	g_pIB->Unlock();


	return S_OK;
}
Esempio n. 23
0
HRESULT init_index_buffer()
{
    CUSTOMINDEX	indices[] = {
		{ 0, 1, 2 }, { 0, 2, 3 },	/// 윗면
		{ 4, 6, 5 }, { 4, 7, 6 },	/// 아랫면
		{ 0, 3, 7 }, { 0, 7, 4 },	/// 왼면
		{ 1, 5, 6 }, { 1, 6, 2 },	/// 오른면
		{ 3, 2, 6 }, { 3, 6, 7 },	/// 앞면
		{ 0, 4, 5 }, { 0, 5, 1 }	/// 뒷면
    };

    if (FAILED( g_pd3dDevice->CreateIndexBuffer( 12 * sizeof(CUSTOMINDEX), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_pIndexBuff, NULL )))
        return E_FAIL;

    VOID* pIndices;
    if (FAILED( g_pIndexBuff->Lock( 0, sizeof(indices), (void**)&pIndices, 0 )))
        return E_FAIL;
    memcpy( pIndices, indices, sizeof(indices) );
    g_pIndexBuff->Unlock();

    return S_OK;
}
void ImGui_ImplDX9_InvalidateDeviceObjects()
{
    if (!g_pd3dDevice)
        return;
    if (g_pVB)
    {
        g_pVB->Release();
        g_pVB = NULL;
    }
    if (g_pIB)
    {
        g_pIB->Release();
        g_pIB = NULL;
    }

    // At this point note that we set ImGui::GetIO().Fonts->TexID to be == g_FontTexture, so clear both.
    ImGuiIO& io = ImGui::GetIO();
    IM_ASSERT(g_FontTexture == io.Fonts->TexID);
    if (g_FontTexture)
        g_FontTexture->Release();
    g_FontTexture = NULL;
    io.Fonts->TexID = NULL;
}
Esempio n. 25
0
void SetObject(void)
{
	//顶点数据  
	CUSTOMVERTEX vertices[] =
	{
		// 正面顶点数据
		{ -10.0f, 10.0f, -10.0f, 0.0f, 0.0f },
		{ 10.0f, 10.0f, -10.0f, 1.0f, 0.0f },
		{ 10.0f, -10.0f, -10.0f, 1.0f, 1.0f },
		{ -10.0f, -10.0f, -10.0f, 0.0f, 1.0f },

		// 背面顶点数据
		{ 10.0f, 10.0f, 10.0f, 0.0f, 0.0f },
		{ -10.0f, 10.0f, 10.0f, 1.0f, 0.0f },
		{ -10.0f, -10.0f, 10.0f, 1.0f, 1.0f },
		{ 10.0f, -10.0f, 10.0f, 0.0f, 1.0f },

		// 顶面顶点数据  
		{ -10.0f, 10.0f, 10.0f, 0.0f, 0.0f },
		{ 10.0f, 10.0f, 10.0f, 1.0f, 0.0f },
		{ 10.0f, 10.0f, -10.0f, 1.0f, 1.0f },
		{ -10.0f, 10.0f, -10.0f, 0.0f, 1.0f },

		// 底面顶点数据  
		{ -10.0f, -10.0f, -10.0f, 0.0f, 0.0f },
		{ 10.0f, -10.0f, -10.0f, 1.0f, 0.0f },
		{ 10.0f, -10.0f, 10.0f, 1.0f, 1.0f },
		{ -10.0f, -10.0f, 10.0f, 0.0f, 1.0f },

		// 左侧面顶点数据  
		{ -10.0f, 10.0f, 10.0f, 0.0f, 0.0f },
		{ -10.0f, 10.0f, -10.0f, 1.0f, 0.0f },
		{ -10.0f, -10.0f, -10.0f, 1.0f, 1.0f },
		{ -10.0f, -10.0f, 10.0f, 0.0f, 1.0f },

		// 右侧面顶点数据  
		{ 10.0f, 10.0f, -10.0f, 0.0f, 0.0f },
		{ 10.0f, 10.0f, 10.0f, 1.0f, 0.0f },
		{ 10.0f, -10.0f, 10.0f, 1.0f, 1.0f },
		{ 10.0f, -10.0f, -10.0f, 0.0f, 1.0f },
	};

	VOID * pVertics = 0;
	g_pVertexBuffer->Lock(0, sizeof(vertices), (void**)&pVertics, 0);
	memcpy(pVertics, vertices, sizeof(vertices));
	g_pVertexBuffer->Unlock();

	// 填充索引数据  
	WORD *pIndices = NULL;
	g_pIndexBuffer->Lock(0, 0, (void**)&pIndices, 0);
	// 正面索引数据  
	pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
	pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3;

	// 背面索引数据  
	pIndices[6] = 4; pIndices[7] = 5; pIndices[8] = 6;
	pIndices[9] = 4; pIndices[10] = 6; pIndices[11] = 7;

	// 顶面索引数据  
	pIndices[12] = 8; pIndices[13] = 9; pIndices[14] = 10;
	pIndices[15] = 8; pIndices[16] = 10; pIndices[17] = 11;

	// 底面索引数据  
	pIndices[18] = 12; pIndices[19] = 13; pIndices[20] = 14;
	pIndices[21] = 12; pIndices[22] = 14; pIndices[23] = 15;

	// 左侧面索引数据  
	pIndices[24] = 16; pIndices[25] = 17; pIndices[26] = 18;
	pIndices[27] = 16; pIndices[28] = 18; pIndices[29] = 19;

	// 右侧面索引数据  
	pIndices[30] = 20; pIndices[31] = 21; pIndices[32] = 22;
	pIndices[33] = 20; pIndices[34] = 22; pIndices[35] = 23;

	g_pIndexBuffer->Unlock();
}
Esempio n. 26
0
 ~PMDDataDx9()
 {
     if(m_Material) delete[] m_Material;
     if(m_IndexBuffer)  m_IndexBuffer->Release();
     if(m_VertexBuffer) m_VertexBuffer->Release();
 }
HRESULT CreateCube()
{
    // The vertex order in each face is in the following order when you
    // look at the face perpendicular against it's normal
    // bottom left -> bottom right -> top right -> top left
    // So you need to set the cull mode as D3DCULL_CW, since the default mode is D3DCULL_CCW
    Vertex vertices[] =
    {
        // Front face
        { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f }, // 0
        { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f }, // 1
        { 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f }, // 2
        { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f }, // 3

        // Back face
        { 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f },
        { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f },
        { 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f },
        { 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },

        // Left face
        { 0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f },
        { 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f },
        { 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f },
        { 0.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f },

        // Right face
        { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f },
        { 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f },
        { 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f },
        { 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },

        // Top face
        { 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f },
        { 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f },
        { 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f },
        { 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },

        // Bottom face
        { 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f },
        { 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f },
        { 0.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f },
        { 1.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f },
    } ;

    // Index buffer
    WORD indices[] =
    {
        // Front side
        0, 1, 2, 0, 2, 3,

        // Back side
        4, 5, 6, 4, 6, 7,

        // Top side
        8, 9, 10, 8, 10, 11,

        // Bottom side
        12, 13, 14, 12, 14, 15,

        // Left side
        16, 17, 18, 16, 18, 19,

        // Right side
        20, 21, 22, 20, 22, 23,
    } ;

    // Create vertex buffer
    if( FAILED( g_pd3dDevice->CreateVertexBuffer( sizeof(vertices) * sizeof(Vertex),
                D3DUSAGE_WRITEONLY,
                VERTEX_FVF,
                D3DPOOL_MANAGED,
                &g_pVB,
                NULL ) ) )
    {
        return E_FAIL;
    }

    // Copy vertex data
    VOID* pVertices;
    if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices, vertices, sizeof(vertices) );
    g_pVB->Unlock();

    // Create index buffer
    if( FAILED( g_pd3dDevice->CreateIndexBuffer( sizeof(indices) * sizeof(WORD),
                D3DUSAGE_WRITEONLY,
                D3DFMT_INDEX16,
                D3DPOOL_MANAGED,
                &g_pIB,
                0) ) )
    {
        return E_FAIL ;
    }

    // Copy index data
    VOID *pIndices;
    if( FAILED( g_pIB->Lock( 0, sizeof(indices), (void **)&pIndices, 0) ) )
        return E_FAIL;
    memcpy(pIndices, indices, sizeof(indices) );
    g_pIB->Unlock() ;
}
Esempio n. 28
0
//-----------------------------------------------------------------------------
//Desc: 生成蒙皮网格模型(含有每个顶点的混合权重、索引和一个骨骼组合表)
//-----------------------------------------------------------------------------
HRESULT DexAllocateHierarchy::GenerateGameSkinMesh(LPDIRECT3DDEVICE9 device, stDexMeshContainerEx *pMeshContainer)
{
	getLog()->BeginLog();
	HRESULT hr = S_OK;

	if (pMeshContainer->pSkinInfo == NULL)
		return hr;

	_SafeRelease( pMeshContainer->MeshData.pMesh );
	_SafeRelease( pMeshContainer->pBoneCombinationBuf );

	D3DCAPS9 d3dCaps;
	device->GetDeviceCaps( &d3dCaps );

	UINT MaxMatrices = 26; 
	pMeshContainer->NumPaletteEntries = min(MaxMatrices, pMeshContainer->pSkinInfo->GetNumBones());

	DWORD Flags = D3DXMESHOPT_VERTEXCACHE;
	if (d3dCaps.VertexShaderVersion >= D3DVS_VERSION(1, 1))
	{
		Flags |= D3DXMESH_MANAGED;
	}
	else
	{
		getLog()->Log(log_allert, ".X Animation file shader 錯誤!");
	}


	//_SafeRelease(pMeshContainer->MeshData.pMesh);

	if(1)
	{
		DWORD NumMaxFaceInfl;
		DWORD Flags = D3DXMESHOPT_VERTEXCACHE;

		LPDIRECT3DINDEXBUFFER9 pIB;
		hr = pMeshContainer->pOrigMesh->GetIndexBuffer( &pIB );
		if( FAILED( hr ) )
			return hr;

		hr = pMeshContainer->pSkinInfo->GetMaxFaceInfluences( pIB,
			pMeshContainer->pOrigMesh->GetNumFaces(),
			&NumMaxFaceInfl );
		pIB->Release();
		if( FAILED( hr ) )
			return hr;

		// 12 entry palette guarantees that any triangle (4 independent influences per vertex of a tri)
		// can be handled
		NumMaxFaceInfl = min( NumMaxFaceInfl, 12 );

		if( d3dCaps.MaxVertexBlendMatrixIndex + 1 < NumMaxFaceInfl )
		{
			// HW does not support indexed vertex blending. Use SW instead
			pMeshContainer->NumPaletteEntries = min( 256, pMeshContainer->pSkinInfo->GetNumBones() );
			Flags |= D3DXMESH_SYSTEMMEM;
		}
		else
		{
			// using hardware - determine palette size from caps and number of bones
			// If normals are present in the vertex data that needs to be blended for lighting, then 
			// the number of matrices is half the number specified by MaxVertexBlendMatrixIndex.
			pMeshContainer->NumPaletteEntries = min( ( d3dCaps.MaxVertexBlendMatrixIndex + 1 ) / 2,
				pMeshContainer->pSkinInfo->GetNumBones() );
			Flags |= D3DXMESH_MANAGED;
		}

		hr = pMeshContainer->pSkinInfo->ConvertToIndexedBlendedMesh
			(
			pMeshContainer->pOrigMesh,
			Flags,
			pMeshContainer->NumPaletteEntries,
			pMeshContainer->pAdjacency,
			NULL, NULL, NULL,
			&pMeshContainer->NumInfl,
			&pMeshContainer->NumAttributeGroups,
			&pMeshContainer->pBoneCombinationBuf,
			&pMeshContainer->MeshData.pMesh );
		if( FAILED( hr ) )
			return hr;
		return 1;
	}

	//生成蒙皮网格模型
	hr = pMeshContainer->pSkinInfo->ConvertToIndexedBlendedMesh
		(
		pMeshContainer->pOrigMesh,
		Flags, 
		pMeshContainer->NumPaletteEntries, 
		pMeshContainer->pAdjacency, 
		NULL, NULL, NULL,             
		&pMeshContainer->NumInfl,
		&pMeshContainer->NumAttributeGroups, 
		&pMeshContainer->pBoneCombinationBuf, 
		&pMeshContainer->MeshData.pMesh);
	if (FAILED(hr))
		return hr;


	// FVF has to match our declarator. Vertex shaders are not as forgiving as FF pipeline
	DWORD NewFVF = (pMeshContainer->MeshData.pMesh->GetFVF() & D3DFVF_POSITION_MASK) | D3DFVF_NORMAL | D3DFVF_TEX1 | D3DFVF_LASTBETA_UBYTE4;
	if (NewFVF != pMeshContainer->MeshData.pMesh->GetFVF())
	{
		LPD3DXMESH pMesh;
		hr = pMeshContainer->MeshData.pMesh->CloneMeshFVF(pMeshContainer->MeshData.pMesh->GetOptions(), NewFVF, device, &pMesh);
		if (!FAILED(hr))
		{
			pMeshContainer->MeshData.pMesh->Release();
			pMeshContainer->MeshData.pMesh = pMesh;
			pMesh = NULL;
		}
	}

	D3DVERTEXELEMENT9 pDecl[MAX_FVF_DECL_SIZE];
	LPD3DVERTEXELEMENT9 pDeclCur;
	hr = pMeshContainer->MeshData.pMesh->GetDeclaration(pDecl);
	if (FAILED(hr))
		return hr;

	// the vertex shader is expecting to interpret the UBYTE4 as a D3DCOLOR, so update the type 
	//   NOTE: this cannot be done with CloneMesh, that would convert the UBYTE4 data to float and then to D3DCOLOR
	//          this is more of a "cast" operation
	pDeclCur = pDecl;
	while (pDeclCur->Stream != 0xff)
	{
		if ((pDeclCur->Usage == D3DDECLUSAGE_BLENDINDICES) && (pDeclCur->UsageIndex == 0))
			pDeclCur->Type = D3DDECLTYPE_D3DCOLOR;
		pDeclCur++;
	}

	hr = pMeshContainer->MeshData.pMesh->UpdateSemantics(pDecl);
	if (FAILED(hr))
		return hr;

	// allocate a buffer for bone matrices, but only if another mesh has not allocated one of the same size or larger
	if( m_NumBoneMatricesMax < pMeshContainer->pSkinInfo->GetNumBones() )
	{
		m_NumBoneMatricesMax = pMeshContainer->pSkinInfo->GetNumBones();

		// Allocate space for blend matrices
		delete[] m_pBoneMatrices; 
		m_pBoneMatrices  = new D3DXMATRIX[m_NumBoneMatricesMax];
		if( m_pBoneMatrices == NULL )
		{
			hr = E_OUTOFMEMORY;
			return hr;
		}
	}
	return hr;
}
Esempio n. 29
0
bool ReadModelFile( const string &fileName, LPDIRECT3DVERTEXBUFFER9 &vtxBuff, int &vtxSize,  LPDIRECT3DINDEXBUFFER9 &idxBuff, int &faceSize )
{
	using namespace std;
	ifstream fin(fileName.c_str());
	if (!fin.is_open())
		return false;

	string vtx, vtx_eq;
	int numVertices;
	fin >> vtx >> vtx_eq >> numVertices;

	if (numVertices <= 0)
		return  false;

	vtxSize = numVertices;

	// 버텍스 버퍼 생성.
	if (FAILED(g_pDevice->CreateVertexBuffer( numVertices * sizeof(Vertex),
		D3DUSAGE_WRITEONLY, Vertex::FVF,
		D3DPOOL_MANAGED, &vtxBuff, NULL)))
	{
		return false;
	}

	// 버텍스 버퍼 초기화.
	Vertex* vertices;
	if (FAILED(vtxBuff->Lock( 0, sizeof(Vertex), (void**)&vertices, 0)))
		return false;
	float num1, num2, num3;
	for (int i = 0; i < numVertices; i++)
	{
		fin >> num1 >> num2 >> num3;
		vertices[i] = Vertex(num1, num2, num3);
	}
	vtxBuff->Unlock();


	string idx, idx_eq;
	int numIndices;
	fin >> idx >> idx_eq >> numIndices;

	if (numIndices <= 0)
		return false;

	faceSize = numIndices;

	if (FAILED(g_pDevice->CreateIndexBuffer(numIndices*3*sizeof(WORD), 
		D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX16,
		D3DPOOL_MANAGED,
		&idxBuff, NULL)))
	{
		return false;
	}

	WORD *indices = NULL;
	idxBuff->Lock(0, 0, (void**)&indices, 0);
	int num4, num5, num6;
	for (int i = 0; i < numIndices*3; i+=3)
	{
		fin >> num4 >> num5 >> num6;
		indices[ i] = num4;
		indices[ i+1] = num5;
		indices[ i+2] = num6;	
	}
	idxBuff->Unlock();

	ComputeNormals(vtxBuff, vtxSize, idxBuff, faceSize);
	return true;
}
Esempio n. 30
0
int APIENTRY WinMain(HINSTANCE hInstance, 
					 HINSTANCE hPrevInstance, 
					 LPSTR lpCmdLine, 
					 int nCmdShow)
{
	wchar_t className[32] = L"Sample";
	wchar_t windowName[32] = L"Sample";

	//윈도우 클레스 정보 생성
	//내가 이러한 윈도를 만들겠다 라는 정보
	WNDCLASS WndClass;
	WndClass.cbClsExtra = 0;			//윈도우에서 사용하는 여분의 메모리설정( 그냥 0 이다  신경쓰지말자 )
	WndClass.cbWndExtra = 0;			//윈도우에서 사용하는 여분의 메모리설정( 그냥 0 이다  신경쓰지말자 )
	WndClass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);		//윈도우 배경색상
	WndClass.hCursor = LoadCursor( NULL, IDC_ARROW );			//윈도우의 커서모양 결정
	WndClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );		//윈도우아이콘모양 결정
	WndClass.hInstance = hInstance;				//프로그램인스턴스핸들 
	WndClass.lpfnWndProc = (WNDPROC)WndProc;			//윈도우 프로시져 함수 포인터
	WndClass.lpszMenuName = NULL;						//메뉴이름 없으면 NULL
	WndClass.lpszClassName = className;				//지금 작성하고 있는 윈도우 클레스의 이름
	WndClass.style	 = CS_HREDRAW | CS_VREDRAW;	//윈도우 그리기 방식 설정 ( 사이즈가 변경될때 화면갱신 CS_HREDRAW | CS_VREDRAW )

	//위에서 작성한 윈도우 클레스정보 등록
	RegisterClass( &WndClass );

	//윈도우 생성
	//생성된 윈도우 핸들을 전역변수 g_hWnd 가 받는다.
	HWND hWnd = CreateWindow(
		className,				//생성되는 윈도우의 클래스이름
		windowName,				//윈도우 타이틀바에 출력되는 이름
		WS_OVERLAPPEDWINDOW,	//윈도우 스타일 WS_OVERLAPPEDWINDOW
		WINPOS_X,				//윈도우 시작 위치 X 
		WINPOS_Y,				//윈도우 시작 위치 Y
		WINSIZE_X,				//윈도우 가로 크기 ( 작업영역의 크기가 아님 )
		WINSIZE_Y,				//윈도우 세로 크기 ( 작업영역의 크기가 아님 )
		GetDesktopWindow(),		//부모 윈도우 핸들 ( 프로그램에서 최상위 윈도우면 NULL 또는 GetDesktopWindow() )
		NULL,					//메뉴 ID ( 자신의 컨트롤 객체의 윈도우인경우 컨트롤 ID 가 된	
		hInstance,				//이 윈도우가 물릴 프로그램 인스턴스 핸들
		NULL					//추가 정보 NULL ( 신경끄자 )
		);

	//윈도우를 정확한 작업영역 크기로 맞춘다
	RECT rcClient = { 0, 0, WINSIZE_X, WINSIZE_Y };
	AdjustWindowRect( &rcClient, WS_OVERLAPPEDWINDOW, FALSE );	//rcClient 크기를 작업 영영으로 할 윈도우 크기를 rcClient 에 대입되어 나온다.

	//윈도우 크기와 윈도우 위치를 바꾸어준다.
	SetWindowPos( hWnd, NULL, 0, 0, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, 
		SWP_NOZORDER | SWP_NOMOVE );

	if (!InitDirectX(hWnd))
	{
		return 0;
	}

	InitVertexBuffer();

	ShowWindow( hWnd, nCmdShow );

	//메시지 구조체
	MSG msg;		
	ZeroMemory( &msg, sizeof( MSG ) );

	int oldT = GetTickCount();
	while (msg.message != WM_QUIT)
	{
		//PeekMessage 는 메시지 큐에 메시지가 없어도 프로그램이 멈추기 않고 진행이 된다.
		//이때 메시지큐에 메시지가 없으면 false 가 리턴되고 메시지가 있으면 true 가 리턴이된다.
		if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
		{
			TranslateMessage( &msg ); //눌린 키보드 의 문자를 번역하여 WM_CHAR 메시지를 발생시킨다.
			DispatchMessage( &msg );  //받아온 메시지 정보로 윈도우 프로시져 함수를 실행시킨다.
		}
		else
		{
			const int curT = GetTickCount();
			const int elapseT = curT - oldT;
			if (elapseT > 15)
			{
				oldT = curT;
				Render(elapseT);
			}
		}
	}

	if (g_pDevice)
		g_pDevice->Release();
	if (g_pVB)
		g_pVB->Release();
	if (g_pIB)
		g_pIB->Release();
	if (g_pMesh)
		g_pMesh->Release();
	return 0;
}