luckie
asked on
Multiple Meshes/Sprites
Hi guys and shelias,
I have looked all the way down in the DXSDK folder, but there are no samples that illustrate the methods of creating multiple sprites (Z-buffer stuff) or meshes... ya know any?
anyone knows how to fix it by the way... I need to achieve 'Multi-sprite'... Bye
here is my code:
#define STRICT
#include <basetsd.h>
#include <stdio.h>
#include <math.h>
#include <D3DX8.h>
#include "D3DApp.h"
#include "D3DFile.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"
class CMyD3DApplication : public CD3DApplication
{
CD3DFont* m_pFont;
CD3DMesh* m_pForkLift;
CD3DMesh* m_pPallet;
CD3DMesh* m_pRack;
/////////////////// added
TCHAR m_strMeshFilename[512];
TCHAR m_strInitialDir[512];
LPD3DXMESH m_pMeshSysMem; // forklift
LPD3DXMESH m_pMeshEnhanced;
UINT m_dwNumSegs;
DWORD m_dwNumMaterials;
CD3DArcBall m_ArcBall;
D3DXMATERIAL*m_pMaterials;
LPDIRECT3DTEXTURE8 *m_ppTextures;
LPD3DXBUFFER m_pbufMaterials;
LPD3DXBUFFER m_pbufAdjacency;
D3DXVECTOR3 m_vObjectCenter;
FLOAT m_fObjectRadius;
///////////////////
D3DXMATRIX m_matBillboardMatrix;
HRESULT ConfirmDevice(D3DCAPS8*, DWORD, D3DFORMAT);
HRESULT GenerateEnhancedMesh(UINT cNewNumSegs);
protected:
HRESULT OneTimeSceneInit();
HRESULT InitDeviceObjects();
HRESULT RestoreDeviceObjects();
HRESULT InvalidDeviceObjects();
HRESULT DeleteDeviceObjects();
HRESULT FinalCleanup();
HRESULT Render();
HRESULT FrameMove();
public:
CMyD3DApplication();
};
inline FLOAT HeightField( FLOAT x, FLOAT y)
{
return 9*(cosf(x/20+0.2f)*cosf(y/ 15-0.2f)+1 .0f);
}
CMyD3DApplication g_d3dApp;
INT WINAPI WinMain (HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
if (FAILED (g_d3dApp.Create (hInst)))
return 0;
return g_d3dApp.Run();
}
CMyD3DApplication::CMyD3DA pplication ()
{
m_strWindowTitle = _T("B&P Demostration");
m_pFont = new CD3DFont (_T("Arial"),12, D3DFONT_BOLD);
m_pForkLift = new CD3DMesh();
m_pPallet = new CD3DMesh();
m_pRack = new CD3DMesh();
/////////////////// added
m_bUseDepthBuffer = TRUE;
m_bShowCursorWhenFullscree n = TRUE;
m_dwNumSegs = 1;
_tcscpy (m_strInitialDir, DXUtil_GetDXSDKMediaPath() );
_tcscpy (m_strMeshFilename, _T("car.x"));
///////////////////
}
HRESULT CMyD3DApplication::InitDev iceObjects ()
{
LPDIRECT3DVERTEXBUFFER8 pVB = NULL;
BYTE *pVertices = NULL;
LPD3DXMESH pTempMesh;
TCHAR strMeshPath[512];
m_pFont->InitDeviceObjects ( m_pd3dDevice );
m_pForkLift->Create( m_pd3dDevice, _T("car.x"));
m_pRack->Create (m_pd3dDevice, _T("rack.x"));
m_pPallet->Create (m_pd3dDevice, _T("pallet.x"));
//////////////////// added
DXUtil_FindMediaFile (strMeshPath, m_strMeshFilename );
D3DXLoadMeshFromX (strMeshPath, D3DXMESH_SYSTEMMEM, m_pd3dDevice,
&m_pbufAdjacency, &m_pbufMaterials, &m_dwNumMaterials,
&m_pMeshSysMem );
m_pMeshSysMem->GetVertexBu ffer (&pVB);
pVB->Lock (0,0,&pVertices,0);
D3DXComputeBoundingSphere (pVertices, m_pMeshSysMem->GetNumVerti ces(),
m_pMeshSysMem->GetFVF(), &m_vObjectCenter,
&m_fObjectRadius);
pVB->Unlock();
SAFE_RELEASE(pVB);
m_pMaterials = (D3DXMATERIAL*) m_pbufMaterials->GetBuffer Pointer();
m_ppTextures = new LPDIRECT3DTEXTURE8[m_dwNum Materials] ;
for (UINT i=0; i < m_dwNumMaterials; i++)
{
TCHAR strTexturePath[512] = _T("");
DXUtil_FindMediaFile (strTexturePath, m_pMaterials[i].pTextureFi lename);
D3DXCreateTextureFromFile( m_pd3dDevi ce, strTexturePath, &m_ppTextures[i]);
}
if (! (m_pMeshSysMem->GetFVF() & D3DFVF_NORMAL ))
{
m_pMeshSysMem->CloneMeshFV F (m_pMeshSysMem->GetOptions (),
m_pMeshSysMem->GetFVF() | D3DFVF_NORMAL,
m_pd3dDevice, &pTempMesh);
D3DXComputeNormals (pTempMesh, NULL);
SAFE_RELEASE (m_pMeshSysMem);
m_pMeshSysMem = pTempMesh;
}
return S_OK;
}
HRESULT CMyD3DApplication::OneTime SceneInit( )
{
return S_OK;
}
HRESULT CMyD3DApplication::Render( )
{
m_pd3dDevice->Clear (0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x000000ff, 1.0f, 0L);
m_pd3dDevice->BeginScene() ;
D3DXMATRIX matView, matViewSave;
m_pd3dDevice->GetTransform (D3DTS_VIEW, &matViewSave);
matView = matViewSave;
matView._41 = 0.0f; matView._42 = -0.3f; matView._43 = 0.0f;
m_pd3dDevice->SetTransform (D3DTS_VIEW, &matView);
m_pForkLift->Render(m_pd3d Device);
m_pRack->Render(m_pd3dDevi ce);
m_pPallet->Render(m_pd3dDe vice);
////////////////// added
for (UINT i = 0; i < m_dwNumMaterials; i++) {
m_pd3dDevice->SetMaterial( &m_pMateri als[i].Mat D3D);
m_pd3dDevice->SetTexture (0, m_ppTextures[i]);
m_pd3dDevice->SetRenderSta te (D3DRS_ZFUNC, D3DCMP_ALWAYS);
m_pMeshEnhanced->DrawSubse t (i);
m_pd3dDevice->SetRenderSta te (D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
}
m_pd3dDevice->EndScene();
return S_OK;
}
HRESULT CMyD3DApplication::FrameMo ve()
{
D3DXMATRIX matWorld;
// view for XMesh
D3DXMatrixTranslation (&matWorld, -m_vObjectCenter.x,
-m_vObjectCenter.y,
-m_vObjectCenter.z);
D3DXMatrixMultiply (&matWorld, &matWorld, m_ArcBall.GetRotationMatri x());
D3DXMatrixMultiply (&matWorld, &matWorld, m_ArcBall.GetTranslationMa trix());
m_pd3dDevice->SetTransform (D3DTS_WOR LD, &matWorld);
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matVie w, &D3DXVECTOR3 (0,0,-2*m_fObjectRadius),
&D3DXVECTOR3 (0, 0, 0),
&D3DXVECTOR3 (0, 1, 0) );
m_pd3dDevice->SetTransform (D3DTS_VIE W, &matView);
// view for CDCMesh
/* D3DXVECTOR3 vUpvec (0.0f, 1.0f, 0.0f);
D3DXVECTOR3 vEyePt;
D3DXVECTOR3 vLookatPt;
vEyePt.x = 30.0f*cosf (0.8f * (m_fTime));
vEyePt.z = 30.0f*sinf (0.8f * (m_fTime));
vEyePt.x = 4 + HeightField (vEyePt.x, vEyePt.z );
vLookatPt.x = 30.0f*cosf(0.8f * (m_fTime + 0.5f) );
vLookatPt.z = 30.0f*sinf(0.8f * (m_fTime + 0.5f) );
vLookatPt.y = vEyePt.y - 1.0f;
// D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpvec);
m_pd3dDevice->SetTransform (D3DTS_VIEW, &matView); */
return S_OK;
}
HRESULT CMyD3DApplication::Generat eEnhancedM esh(UINT dwNewNumSegs)
{
LPD3DXMESH pMeshTemp;
LPD3DXMESH pMeshEnhancedSysMem = NULL;
HRESULT hr;
////////////////// temp cancelled
// m_pMeshSysMem->CloneMeshFV F (D3DXMESH_WRITEONLY | D3DXMESH_NPATCHES |
// (m_pMeshSysMem->GetOptions () & D3DXMESH_32BIT),
// m_pMeshSysMem->GetFVF(), m_pd3dDevice, &pMeshTemp );
hr = D3DXTessellateNPatches( m_pMeshSysMem, (DWORD*)m_pbufAdjacency->G etBufferPo inter(),
(float)dwNewNumSegs, FALSE, &pMeshEnhancedSysMem, NULL );
if( FAILED(hr) )
{
// If the tessellate failed, there might have been more triangles or vertices
// than can fit into a 16bit mesh, so try cloning to 32bit before tessellation
hr = m_pMeshSysMem->CloneMeshFV F( D3DXMESH_SYSTEMMEM | D3DXMESH_32BIT,
m_pMeshSysMem->GetFVF(), m_pd3dDevice, &pMeshTemp );
if (FAILED(hr))
return hr;
hr = D3DXTessellateNPatches( pMeshTemp, (DWORD*)m_pbufAdjacency->G etBufferPo inter(),
(float)dwNewNumSegs, FALSE, &pMeshEnhancedSysMem, NULL );
if( FAILED(hr) )
{
pMeshTemp->Release();
return hr;
}
pMeshTemp->Release();
}
// Make a vid mem version of the mesh
// Only set WRITEONLY if it doesn't use 32bit indices, because those
// often need to be emulated, which means that D3DX needs read-access.
DWORD dwMeshEnhancedFlags = pMeshEnhancedSysMem->GetOp tions() & D3DXMESH_32BIT;
if( (dwMeshEnhancedFlags & D3DXMESH_32BIT) == 0)
dwMeshEnhancedFlags |= D3DXMESH_WRITEONLY;
hr = pMeshEnhancedSysMem->Clone MeshFVF( dwMeshEnhancedFlags, m_pMeshSysMem->GetFVF(),
m_pd3dDevice, &pMeshTemp );
if( FAILED(hr) )
{
SAFE_RELEASE( pMeshEnhancedSysMem );
return hr;
}
// Latch in the enhanced mesh
SAFE_RELEASE( pMeshEnhancedSysMem );
m_pMeshEnhanced = pMeshTemp;
m_dwNumSegs = dwNewNumSegs;
return S_OK;
}
HRESULT CMyD3DApplication::Restore DeviceObje cts()
{
m_pForkLift->RestoreDevice Objects( m_pd3dDevice );
m_pPallet->RestoreDeviceOb jects( m_pd3dDevice );
m_pRack->RestoreDeviceObje cts (m_pd3dDevice );
m_pFont->RestoreDeviceObje cts();
HRESULT hr;
// m_bUseHWNPatches = (m_d3dCaps.DevCaps & D3DDEVCAPS_NPATCHES);
hr = GenerateEnhancedMesh( m_dwNumSegs );
if( FAILED(hr) )
return hr;
// Setup render state
m_pd3dDevice->SetRenderSta te( D3DRS_LIGHTING, TRUE );
m_pd3dDevice->SetRenderSta te( D3DRS_DITHERENABLE, TRUE );
m_pd3dDevice->SetRenderSta te( D3DRS_ZENABLE, TRUE );
m_pd3dDevice->SetRenderSta te( D3DRS_AMBIENT, 0x33333333 );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
// Setup the light
D3DLIGHT8 light;
D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.0f,-1.0f, 1.0f );
m_pd3dDevice->SetLight(0, &light );
m_pd3dDevice->LightEnable( 0, TRUE );
// Setup the arcball parameters
m_ArcBall.SetWindow( m_d3dsdBackBuffer.Width, m_d3dsdBackBuffer.Height, 0.85f );
m_ArcBall.SetRadius( 1.0f );
// Setup the projection matrix
D3DXMATRIX matProj;
FLOAT fAspect = (FLOAT)m_d3dsdBackBuffer.W idth / (FLOAT)m_d3dsdBackBuffer.H eight;
D3DXMatrixPerspectiveFovLH ( &matProj, D3DX_PI/4, fAspect,
m_fObjectRadius/64.0f, m_fObjectRadius*200.0f );
m_pd3dDevice->SetTransform ( D3DTS_PROJECTION, &matProj );
// Restore the font
m_pFont->RestoreDeviceObje cts();
// for CDCMesh
/* D3DXMATRIX matProj;
FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT) m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH (&matProj, D3DX_PI/4, fAspect, 1.0f, 100.0f);
m_pd3dDevice->SetTransform (D3DTS_PROJECTION, &matProj);
m_pd3dDevice->SetTextureSt ageState(0 , D3DTSS_COLOROP, D3DTOP_SELECTARG1);
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP );
m_pd3dDevice->SetRenderSta te( D3DRS_DITHERENABLE, TRUE );
m_pd3dDevice->SetRenderSta te( D3DRS_ZENABLE, TRUE );
m_pd3dDevice->SetRenderSta te( D3DRS_LIGHTING, TRUE ); */
return S_OK;
}
HRESULT CMyD3DApplication::DeleteD eviceObjec ts()
{
m_pFont->DeleteDeviceObjec ts();
m_pForkLift->Destroy();
m_pRack->Destroy();
m_pPallet->Destroy();
return S_OK;
}
HRESULT CMyD3DApplication::FinalCl eanup()
{
SAFE_DELETE (m_pFont);
SAFE_DELETE (m_pForkLift);
SAFE_DELETE (m_pPallet);
SAFE_DELETE (m_pRack);
return S_OK;
}
HRESULT CMyD3DApplication::Confirm Device( D3DCAPS8 *pCaps, DWORD dwBehavior, D3DFORMAT Format)
{
if (dwBehavior & D3DCREATE_PUREDEVICE)
return E_FAIL;
if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHAPALET TE)
return S_OK;
if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHA)
return S_OK;
return E_FAIL;
}
Thanks in advance
Jacky
I have looked all the way down in the DXSDK folder, but there are no samples that illustrate the methods of creating multiple sprites (Z-buffer stuff) or meshes... ya know any?
anyone knows how to fix it by the way... I need to achieve 'Multi-sprite'... Bye
here is my code:
#define STRICT
#include <basetsd.h>
#include <stdio.h>
#include <math.h>
#include <D3DX8.h>
#include "D3DApp.h"
#include "D3DFile.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"
class CMyD3DApplication : public CD3DApplication
{
CD3DFont* m_pFont;
CD3DMesh* m_pForkLift;
CD3DMesh* m_pPallet;
CD3DMesh* m_pRack;
/////////////////// added
TCHAR m_strMeshFilename[512];
TCHAR m_strInitialDir[512];
LPD3DXMESH m_pMeshSysMem; // forklift
LPD3DXMESH m_pMeshEnhanced;
UINT m_dwNumSegs;
DWORD m_dwNumMaterials;
CD3DArcBall m_ArcBall;
D3DXMATERIAL*m_pMaterials;
LPDIRECT3DTEXTURE8 *m_ppTextures;
LPD3DXBUFFER m_pbufMaterials;
LPD3DXBUFFER m_pbufAdjacency;
D3DXVECTOR3 m_vObjectCenter;
FLOAT m_fObjectRadius;
///////////////////
D3DXMATRIX m_matBillboardMatrix;
HRESULT ConfirmDevice(D3DCAPS8*, DWORD, D3DFORMAT);
HRESULT GenerateEnhancedMesh(UINT cNewNumSegs);
protected:
HRESULT OneTimeSceneInit();
HRESULT InitDeviceObjects();
HRESULT RestoreDeviceObjects();
HRESULT InvalidDeviceObjects();
HRESULT DeleteDeviceObjects();
HRESULT FinalCleanup();
HRESULT Render();
HRESULT FrameMove();
public:
CMyD3DApplication();
};
inline FLOAT HeightField( FLOAT x, FLOAT y)
{
return 9*(cosf(x/20+0.2f)*cosf(y/
}
CMyD3DApplication g_d3dApp;
INT WINAPI WinMain (HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
if (FAILED (g_d3dApp.Create (hInst)))
return 0;
return g_d3dApp.Run();
}
CMyD3DApplication::CMyD3DA
{
m_strWindowTitle = _T("B&P Demostration");
m_pFont = new CD3DFont (_T("Arial"),12, D3DFONT_BOLD);
m_pForkLift = new CD3DMesh();
m_pPallet = new CD3DMesh();
m_pRack = new CD3DMesh();
/////////////////// added
m_bUseDepthBuffer = TRUE;
m_bShowCursorWhenFullscree
m_dwNumSegs = 1;
_tcscpy (m_strInitialDir, DXUtil_GetDXSDKMediaPath()
_tcscpy (m_strMeshFilename, _T("car.x"));
///////////////////
}
HRESULT CMyD3DApplication::InitDev
{
LPDIRECT3DVERTEXBUFFER8 pVB = NULL;
BYTE *pVertices = NULL;
LPD3DXMESH pTempMesh;
TCHAR strMeshPath[512];
m_pFont->InitDeviceObjects
m_pForkLift->Create( m_pd3dDevice, _T("car.x"));
m_pRack->Create (m_pd3dDevice, _T("rack.x"));
m_pPallet->Create (m_pd3dDevice, _T("pallet.x"));
//////////////////// added
DXUtil_FindMediaFile (strMeshPath, m_strMeshFilename );
D3DXLoadMeshFromX (strMeshPath, D3DXMESH_SYSTEMMEM, m_pd3dDevice,
&m_pbufAdjacency, &m_pbufMaterials, &m_dwNumMaterials,
&m_pMeshSysMem );
m_pMeshSysMem->GetVertexBu
pVB->Lock (0,0,&pVertices,0);
D3DXComputeBoundingSphere (pVertices, m_pMeshSysMem->GetNumVerti
m_pMeshSysMem->GetFVF(), &m_vObjectCenter,
&m_fObjectRadius);
pVB->Unlock();
SAFE_RELEASE(pVB);
m_pMaterials = (D3DXMATERIAL*) m_pbufMaterials->GetBuffer
m_ppTextures = new LPDIRECT3DTEXTURE8[m_dwNum
for (UINT i=0; i < m_dwNumMaterials; i++)
{
TCHAR strTexturePath[512] = _T("");
DXUtil_FindMediaFile (strTexturePath, m_pMaterials[i].pTextureFi
D3DXCreateTextureFromFile(
}
if (! (m_pMeshSysMem->GetFVF() & D3DFVF_NORMAL ))
{
m_pMeshSysMem->CloneMeshFV
m_pMeshSysMem->GetFVF() | D3DFVF_NORMAL,
m_pd3dDevice, &pTempMesh);
D3DXComputeNormals (pTempMesh, NULL);
SAFE_RELEASE (m_pMeshSysMem);
m_pMeshSysMem = pTempMesh;
}
return S_OK;
}
HRESULT CMyD3DApplication::OneTime
{
return S_OK;
}
HRESULT CMyD3DApplication::Render(
{
m_pd3dDevice->Clear (0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x000000ff, 1.0f, 0L);
m_pd3dDevice->BeginScene()
D3DXMATRIX matView, matViewSave;
m_pd3dDevice->GetTransform
matView = matViewSave;
matView._41 = 0.0f; matView._42 = -0.3f; matView._43 = 0.0f;
m_pd3dDevice->SetTransform
m_pForkLift->Render(m_pd3d
m_pRack->Render(m_pd3dDevi
m_pPallet->Render(m_pd3dDe
////////////////// added
for (UINT i = 0; i < m_dwNumMaterials; i++) {
m_pd3dDevice->SetMaterial(
m_pd3dDevice->SetTexture (0, m_ppTextures[i]);
m_pd3dDevice->SetRenderSta
m_pMeshEnhanced->DrawSubse
m_pd3dDevice->SetRenderSta
}
m_pd3dDevice->EndScene();
return S_OK;
}
HRESULT CMyD3DApplication::FrameMo
{
D3DXMATRIX matWorld;
// view for XMesh
D3DXMatrixTranslation (&matWorld, -m_vObjectCenter.x,
-m_vObjectCenter.y,
-m_vObjectCenter.z);
D3DXMatrixMultiply (&matWorld, &matWorld, m_ArcBall.GetRotationMatri
D3DXMatrixMultiply (&matWorld, &matWorld, m_ArcBall.GetTranslationMa
m_pd3dDevice->SetTransform
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matVie
&D3DXVECTOR3 (0, 0, 0),
&D3DXVECTOR3 (0, 1, 0) );
m_pd3dDevice->SetTransform
// view for CDCMesh
/* D3DXVECTOR3 vUpvec (0.0f, 1.0f, 0.0f);
D3DXVECTOR3 vEyePt;
D3DXVECTOR3 vLookatPt;
vEyePt.x = 30.0f*cosf (0.8f * (m_fTime));
vEyePt.z = 30.0f*sinf (0.8f * (m_fTime));
vEyePt.x = 4 + HeightField (vEyePt.x, vEyePt.z );
vLookatPt.x = 30.0f*cosf(0.8f * (m_fTime + 0.5f) );
vLookatPt.z = 30.0f*sinf(0.8f * (m_fTime + 0.5f) );
vLookatPt.y = vEyePt.y - 1.0f;
// D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpvec);
m_pd3dDevice->SetTransform
return S_OK;
}
HRESULT CMyD3DApplication::Generat
{
LPD3DXMESH pMeshTemp;
LPD3DXMESH pMeshEnhancedSysMem = NULL;
HRESULT hr;
////////////////// temp cancelled
// m_pMeshSysMem->CloneMeshFV
// (m_pMeshSysMem->GetOptions
// m_pMeshSysMem->GetFVF(), m_pd3dDevice, &pMeshTemp );
hr = D3DXTessellateNPatches( m_pMeshSysMem, (DWORD*)m_pbufAdjacency->G
(float)dwNewNumSegs, FALSE, &pMeshEnhancedSysMem, NULL );
if( FAILED(hr) )
{
// If the tessellate failed, there might have been more triangles or vertices
// than can fit into a 16bit mesh, so try cloning to 32bit before tessellation
hr = m_pMeshSysMem->CloneMeshFV
m_pMeshSysMem->GetFVF(), m_pd3dDevice, &pMeshTemp );
if (FAILED(hr))
return hr;
hr = D3DXTessellateNPatches( pMeshTemp, (DWORD*)m_pbufAdjacency->G
(float)dwNewNumSegs, FALSE, &pMeshEnhancedSysMem, NULL );
if( FAILED(hr) )
{
pMeshTemp->Release();
return hr;
}
pMeshTemp->Release();
}
// Make a vid mem version of the mesh
// Only set WRITEONLY if it doesn't use 32bit indices, because those
// often need to be emulated, which means that D3DX needs read-access.
DWORD dwMeshEnhancedFlags = pMeshEnhancedSysMem->GetOp
if( (dwMeshEnhancedFlags & D3DXMESH_32BIT) == 0)
dwMeshEnhancedFlags |= D3DXMESH_WRITEONLY;
hr = pMeshEnhancedSysMem->Clone
m_pd3dDevice, &pMeshTemp );
if( FAILED(hr) )
{
SAFE_RELEASE( pMeshEnhancedSysMem );
return hr;
}
// Latch in the enhanced mesh
SAFE_RELEASE( pMeshEnhancedSysMem );
m_pMeshEnhanced = pMeshTemp;
m_dwNumSegs = dwNewNumSegs;
return S_OK;
}
HRESULT CMyD3DApplication::Restore
{
m_pForkLift->RestoreDevice
m_pPallet->RestoreDeviceOb
m_pRack->RestoreDeviceObje
m_pFont->RestoreDeviceObje
HRESULT hr;
// m_bUseHWNPatches = (m_d3dCaps.DevCaps & D3DDEVCAPS_NPATCHES);
hr = GenerateEnhancedMesh( m_dwNumSegs );
if( FAILED(hr) )
return hr;
// Setup render state
m_pd3dDevice->SetRenderSta
m_pd3dDevice->SetRenderSta
m_pd3dDevice->SetRenderSta
m_pd3dDevice->SetRenderSta
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetTextureSt
// Setup the light
D3DLIGHT8 light;
D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.0f,-1.0f, 1.0f );
m_pd3dDevice->SetLight(0, &light );
m_pd3dDevice->LightEnable(
// Setup the arcball parameters
m_ArcBall.SetWindow( m_d3dsdBackBuffer.Width, m_d3dsdBackBuffer.Height, 0.85f );
m_ArcBall.SetRadius( 1.0f );
// Setup the projection matrix
D3DXMATRIX matProj;
FLOAT fAspect = (FLOAT)m_d3dsdBackBuffer.W
D3DXMatrixPerspectiveFovLH
m_fObjectRadius/64.0f, m_fObjectRadius*200.0f );
m_pd3dDevice->SetTransform
// Restore the font
m_pFont->RestoreDeviceObje
// for CDCMesh
/* D3DXMATRIX matProj;
FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT) m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH
m_pd3dDevice->SetTransform
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetRenderSta
m_pd3dDevice->SetRenderSta
m_pd3dDevice->SetRenderSta
return S_OK;
}
HRESULT CMyD3DApplication::DeleteD
{
m_pFont->DeleteDeviceObjec
m_pForkLift->Destroy();
m_pRack->Destroy();
m_pPallet->Destroy();
return S_OK;
}
HRESULT CMyD3DApplication::FinalCl
{
SAFE_DELETE (m_pFont);
SAFE_DELETE (m_pForkLift);
SAFE_DELETE (m_pPallet);
SAFE_DELETE (m_pRack);
return S_OK;
}
HRESULT CMyD3DApplication::Confirm
{
if (dwBehavior & D3DCREATE_PUREDEVICE)
return E_FAIL;
if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHAPALET
return S_OK;
if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHA)
return S_OK;
return E_FAIL;
}
Thanks in advance
Jacky
I don't understand the problem.
If you want to create another mesh of whatever just create an instance of the mesh class and do the same thing again. Then when rendering call both Render()
If this isn't the problem tell me what is the problem :)
/Joachim
If you want to create another mesh of whatever just create an instance of the mesh class and do the same thing again. Then when rendering call both Render()
If this isn't the problem tell me what is the problem :)
/Joachim
ASKER
in simpler words, is to animate/display multiple objects
That's about what I said before then.
Asume this I have a class called Xmesh see below.
In my init for my app I can do this
HRESULT App::Init()
{
XMesh* myFirstMesh;
XMesh* mySecondMesh;
myFirstMesh = new XMesh(ourD3Ddevice);
mySecondMesh = new XMesh(ourD3Ddevice);
myFirstMesh->CreateXMesh(" someXfile. x");
mySecondMesh->CreateXMesh( "someXfile 2.x");
//Don't forget to set the matrix and stuff
return S_OK;
}
then when rendering
VOID App::Render()
{
D3DXMATRIX matrix;
D3DXMatrixIdentity(&matrix );
myFirstMesh->ourLocation = matrix;
myFirstMesh->RenderMyself( );
matrix._41 = 2.0f; //move 2 steps to the right
mySecondMesh->ourLocation = matrix;
mySecondMesh->RenderMyself ();
}
class XMesh
{
public:
//LPDIRECT3D8 g_pD3D; // Used to create the D3DDevice
LPDIRECT3DDEVICE8 g_pd3dDevice; // Our rendering device
LPD3DXMESH g_pMesh; // Our mesh object in sysmem
D3DMATERIAL8* g_pMeshMaterials; // Materials for our mesh
LPDIRECT3DTEXTURE8* g_pMeshTextures; // Textures for our mesh
DWORD g_dwNumMaterials; // Number of mesh materials
D3DXMATRIX ourLocation;
XMesh(LPDIRECT3DDEVICE8 aD3DDevice); //Constructor
HRESULT CreateXMesh(char aFileName[]); //Mesh loader
HRESULT RenderMyself();
VOID Cleanup();
//VOID Move(D3DXVECTOR3 &aDir, float aMove);
private:
D3DXVECTOR3 ourPos, ourDirection;
float ourMove;
char buf[50];
};
Asume this I have a class called Xmesh see below.
In my init for my app I can do this
HRESULT App::Init()
{
XMesh* myFirstMesh;
XMesh* mySecondMesh;
myFirstMesh = new XMesh(ourD3Ddevice);
mySecondMesh = new XMesh(ourD3Ddevice);
myFirstMesh->CreateXMesh("
mySecondMesh->CreateXMesh(
//Don't forget to set the matrix and stuff
return S_OK;
}
then when rendering
VOID App::Render()
{
D3DXMATRIX matrix;
D3DXMatrixIdentity(&matrix
myFirstMesh->ourLocation = matrix;
myFirstMesh->RenderMyself(
matrix._41 = 2.0f; //move 2 steps to the right
mySecondMesh->ourLocation = matrix;
mySecondMesh->RenderMyself
}
class XMesh
{
public:
//LPDIRECT3D8 g_pD3D; // Used to create the D3DDevice
LPDIRECT3DDEVICE8 g_pd3dDevice; // Our rendering device
LPD3DXMESH g_pMesh; // Our mesh object in sysmem
D3DMATERIAL8* g_pMeshMaterials; // Materials for our mesh
LPDIRECT3DTEXTURE8* g_pMeshTextures; // Textures for our mesh
DWORD g_dwNumMaterials; // Number of mesh materials
D3DXMATRIX ourLocation;
XMesh(LPDIRECT3DDEVICE8 aD3DDevice); //Constructor
HRESULT CreateXMesh(char aFileName[]); //Mesh loader
HRESULT RenderMyself();
VOID Cleanup();
//VOID Move(D3DXVECTOR3 &aDir, float aMove);
private:
D3DXVECTOR3 ourPos, ourDirection;
float ourMove;
char buf[50];
};
ASKER
Now, I got a background image, and there is an object moves against it. The background has two beams, when the object goes between the beams. How do I use DirectDraw/3D to create a Z-buffer so that the closer beam covers part o of the object and the object covers part of the farther beam?
Jack
Jack
d3dpp.AutoDepthStencilForm at = D3DFMT_D24S8;
ASKER
how come myFirstMesh and mySecondMesh weren't declared in the class. How can Render() accesses them. :) Thanks
Bye
Jacky
Bye
Jacky
ASKER
And the two beams are fixed and 'locked in' in the background.Is it still possible?
Sorry, there are lots of questions and I'm just a newbie...
and I'm running out of points. My apologises...
Jack
Sorry, there are lots of questions and I'm just a newbie...
and I'm running out of points. My apologises...
Jack
ASKER
what is 'ourlocation'?
a matrix with the location of the object.
ASKER
How do I set it up with myFirstMesh?
A deeper explanation is preferred :)
pts increased to 200
Regards,
Jacky
A deeper explanation is preferred :)
pts increased to 200
Regards,
Jacky
ASKER
How do I set it up with myFirstMesh?
A deeper explanation is preferred :)
pts increased to 200
Regards,
Jacky
A deeper explanation is preferred :)
pts increased to 200
Regards,
Jacky
Just load the mesh with the function that you can cut from many examples in the d3dx samples.
Then render it as usual.
I gave you the code for setting it up.
XMesh::XMesh(LPDIRECT3DDEV ICE8 aD3DDevice)
{
g_pMesh = NULL; // Our mesh object in sysmem
g_pMeshMaterials = NULL; // Materials for our mesh
g_pMeshTextures = NULL; // Textures for our mesh
g_dwNumMaterials = 0L;
g_pd3dDevice = aD3DDevice;
D3DXMatrixIdentity(&ourLoc ation);
Util::WriteLogFile("Initia lized XMesh");
}
HRESULT XMesh::CreateXMesh(char aFileName[])
{
LPD3DXBUFFER pD3DXMtrlBuffer;
//So I am wasting a few extra byte to be sure just shoot me.
char *texturePath = new char[25+strlen(aFileName)] ;
char *modelPath = new char[17+strlen(aFileName)] ;
strcpy(modelPath, "d:\\models\\");
strcat(modelPath, aFileName);
// Load the mesh from the specified file
if( FAILED( D3DXLoadMeshFromX( modelPath, D3DXMESH_SYSTEMMEM,
g_pd3dDevice, NULL,
&pD3DXMtrlBuffer, &g_dwNumMaterials,
&g_pMesh ) ) )
{
Util::WriteLogFile(strcat( "ERROR: failed to read mesh : ", modelPath));
return E_FAIL;
}
// We need to extract the material properties and texture names from the
// pD3DXMtrlBuffer
D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBu ffer->GetB ufferPoint er();
g_pMeshMaterials = new D3DMATERIAL8[g_dwNumMateri als];
g_pMeshTextures = new LPDIRECT3DTEXTURE8[g_dwNum Materials] ;
for( DWORD i=0; i<g_dwNumMaterials; i++ )
{
// Copy the material
g_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;
// Set the ambient color for the material (D3DX does not do this)
g_pMeshMaterials[i].Ambien t = g_pMeshMaterials[i].Diffus e;
g_pMeshMaterials[i].Power = 35;
strcpy(texturePath, "d:\\models\\textures\\");
strcat(texturePath, d3dxMaterials[i].pTextureF ilename);
// Create the texture
if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice,
texturePath,
&g_pMeshTextures[i] ) ) )
{
strcpy(buf, "ERROR: failed to read texture");
Util::WriteLogFile(strcat( buf, texturePath));
g_pMeshTextures[i] = NULL;
}
}
// Done with the material buffer
pD3DXMtrlBuffer->Release() ;
delete []texturePath;
delete []modelPath;
texturePath = NULL;
modelPath = NULL;
Util::WriteLogFile("Create d XMesh OK!");
return S_OK;
}
HRESULT XMesh::RenderMyself()
{
// Meshes are divided into subsets, one for each material. Render them in
// a loop
g_pd3dDevice->SetTexture(1 , NULL);
g_pd3dDevice->SetTransform ( D3DTS_WORLD, &ourLocation );
for( DWORD i=0; i<g_dwNumMaterials; i++ )
{
// Set the material and texture for this subset
g_pd3dDevice->SetMaterial( &g_pMeshMaterials[i] );
g_pd3dDevice->SetTexture( 0, g_pMeshTextures[i] );
// Draw the mesh subset
g_pMesh->DrawSubset( i );
}
return S_OK;
}
Then render it as usual.
I gave you the code for setting it up.
XMesh::XMesh(LPDIRECT3DDEV
{
g_pMesh = NULL; // Our mesh object in sysmem
g_pMeshMaterials = NULL; // Materials for our mesh
g_pMeshTextures = NULL; // Textures for our mesh
g_dwNumMaterials = 0L;
g_pd3dDevice = aD3DDevice;
D3DXMatrixIdentity(&ourLoc
Util::WriteLogFile("Initia
}
HRESULT XMesh::CreateXMesh(char aFileName[])
{
LPD3DXBUFFER pD3DXMtrlBuffer;
//So I am wasting a few extra byte to be sure just shoot me.
char *texturePath = new char[25+strlen(aFileName)]
char *modelPath = new char[17+strlen(aFileName)]
strcpy(modelPath, "d:\\models\\");
strcat(modelPath, aFileName);
// Load the mesh from the specified file
if( FAILED( D3DXLoadMeshFromX( modelPath, D3DXMESH_SYSTEMMEM,
g_pd3dDevice, NULL,
&pD3DXMtrlBuffer, &g_dwNumMaterials,
&g_pMesh ) ) )
{
Util::WriteLogFile(strcat(
return E_FAIL;
}
// We need to extract the material properties and texture names from the
// pD3DXMtrlBuffer
D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBu
g_pMeshMaterials = new D3DMATERIAL8[g_dwNumMateri
g_pMeshTextures = new LPDIRECT3DTEXTURE8[g_dwNum
for( DWORD i=0; i<g_dwNumMaterials; i++ )
{
// Copy the material
g_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;
// Set the ambient color for the material (D3DX does not do this)
g_pMeshMaterials[i].Ambien
g_pMeshMaterials[i].Power = 35;
strcpy(texturePath, "d:\\models\\textures\\");
strcat(texturePath, d3dxMaterials[i].pTextureF
// Create the texture
if( FAILED( D3DXCreateTextureFromFile(
texturePath,
&g_pMeshTextures[i] ) ) )
{
strcpy(buf, "ERROR: failed to read texture");
Util::WriteLogFile(strcat(
g_pMeshTextures[i] = NULL;
}
}
// Done with the material buffer
pD3DXMtrlBuffer->Release()
delete []texturePath;
delete []modelPath;
texturePath = NULL;
modelPath = NULL;
Util::WriteLogFile("Create
return S_OK;
}
HRESULT XMesh::RenderMyself()
{
// Meshes are divided into subsets, one for each material. Render them in
// a loop
g_pd3dDevice->SetTexture(1
g_pd3dDevice->SetTransform
for( DWORD i=0; i<g_dwNumMaterials; i++ )
{
// Set the material and texture for this subset
g_pd3dDevice->SetMaterial(
g_pd3dDevice->SetTexture( 0, g_pMeshTextures[i] );
// Draw the mesh subset
g_pMesh->DrawSubset( i );
}
return S_OK;
}
ASKER
The example I shown you can only display one object! do you know why?
From what I can see you have
LPD3DXMESH m_pMeshSysMem; // forklift
LPD3DXMESH m_pMeshEnhanced;
Do
LPD3DXMESH m_pMeshSysMem[9]; // forklift
LPD3DXMESH m_pMeshEnhanced[9];
if you want to be able to create ten of them. Then just use the array to access them.
//Changed
HRESULT CMyD3DApplication::Generat eEnhancedM esh(UINT dwNewNumSegs, int aNumber) //aNumber is the m_pMeshEnhanced number
//in the end of the GenerateEnhancedMesh
m_pMeshEnhanced[aNumber] = pMeshTemp;
m_dwNumSegs[aNumber] = dwNewNumSegs;
/Joachim
LPD3DXMESH m_pMeshSysMem; // forklift
LPD3DXMESH m_pMeshEnhanced;
Do
LPD3DXMESH m_pMeshSysMem[9]; // forklift
LPD3DXMESH m_pMeshEnhanced[9];
if you want to be able to create ten of them. Then just use the array to access them.
//Changed
HRESULT CMyD3DApplication::Generat
//in the end of the GenerateEnhancedMesh
m_pMeshEnhanced[aNumber] = pMeshTemp;
m_dwNumSegs[aNumber] = dwNewNumSegs;
/Joachim
ASKER
Could you please send me a complete source code of at least 2 objects to me? then I will give you the pts..:)
my address is luckie@netvigator.com I'm extremely tired.. hv't got a holiday for 2 weeks...
Bye and thanks
Jacky
my address is luckie@netvigator.com I'm extremely tired.. hv't got a holiday for 2 weeks...
Bye and thanks
Jacky
ASKER
If you don't mind, please send it over about 5 hrs later.Thanks
ASKER
just holding out for a savior... my boss nearly complains about me ha ha Bye
ASKER
doesn't matter... sending immediately is okay!
ASKER
but it is best to send later .. going back and forth...
From what I can see you have created three objects already. But you seem to be rendering them on top of each other.
in your Render()
//Camera stuff.
matView._41 = 0.0f; matView._42 = -0.3f; matView._43 = 0.0f;
m_pd3dDevice->SetTransform (D3DTS_VIEW, &matView);
D3DXMATRIX objectWorldMatrix;
D3DXMatrixIdentity(&object WorldMatri x)
m_pForkLift->Render(m_pd3d Device);
objectWorldMatrix._41 = 2.0f; //Move it to the right
m_pRack->Render(m_pd3dDevi ce);
objectWorldMatrix._42 = 2.0f; //Move it up a little
m_pPallet->Render(m_pd3dDe vice);
/Joachim
in your Render()
//Camera stuff.
matView._41 = 0.0f; matView._42 = -0.3f; matView._43 = 0.0f;
m_pd3dDevice->SetTransform
D3DXMATRIX objectWorldMatrix;
D3DXMatrixIdentity(&object
m_pForkLift->Render(m_pd3d
objectWorldMatrix._41 = 2.0f; //Move it to the right
m_pRack->Render(m_pd3dDevi
objectWorldMatrix._42 = 2.0f; //Move it up a little
m_pPallet->Render(m_pd3dDe
/Joachim
ASKER
but it is best to send later .. going back and forth...
ASKER
I still insist a complete source code as a starting point. Could you please help me out?
ASKER
so much detail to handle!!! please
now you must be able to put it together.
ASKER
Alright, Let me digest it thoroughtly before I get back to you... Thanks a lot!
Jack
Jack
ASKER
oh no use array... still the same outcome...
here is the modified code:
#define STRICT
#include <basetsd.h>
#include <stdio.h>
#include <math.h>
#include <D3DX8.h>
#include "D3DApp.h"
#include "D3DFile.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"
class CMyD3DApplication : public CD3DApplication
{
CD3DFont* m_pFont;
CD3DMesh* m_pForkLift;
CD3DMesh* m_pPallet;
CD3DMesh* m_pRack;
/////////////////// added
TCHAR m_strMeshFilename[512];
TCHAR m_strMeshFilename2[512];
TCHAR m_strInitialDir[512];
LPD3DXMESH m_pMeshSysMem[3]; // forklift, pallet and rack
LPD3DXMESH m_pMeshEnhanced[3];
UINT m_dwNumSegs[3];
DWORD m_dwNumMaterials;
CD3DArcBall m_ArcBall;
D3DXMATERIAL*m_pMaterials;
LPDIRECT3DTEXTURE8 *m_ppTextures;
LPD3DXBUFFER m_pbufMaterials;
LPD3DXBUFFER m_pbufAdjacency;
LPD3DXMESH m_pMeshSysMem2; // pallet
LPD3DXMESH m_pMeshEnhanced2;
UINT m_dwNumSegs2;
DWORD m_dwNumMaterials2;
CD3DArcBall m_ArcBall2;
D3DXMATERIAL*m_pMaterials2 ;
LPDIRECT3DTEXTURE8 *m_ppTextures2;
LPD3DXBUFFER m_pbufMaterials2;
LPD3DXBUFFER m_pbufAdjacency2;
///////////////////
D3DXVECTOR3 m_vObjectCenter;
FLOAT m_fObjectRadius;
///////////////////
D3DXMATRIX m_matBillboardMatrix;
HRESULT ConfirmDevice(D3DCAPS8*, DWORD, D3DFORMAT);
HRESULT GenerateEnhancedMesh(UINT cNewNumSegs, int Num);
protected:
HRESULT OneTimeSceneInit();
HRESULT InitDeviceObjects();
HRESULT RestoreDeviceObjects();
HRESULT InvalidDeviceObjects();
HRESULT DeleteDeviceObjects();
HRESULT FinalCleanup();
HRESULT Render();
HRESULT FrameMove();
public:
CMyD3DApplication();
};
inline FLOAT HeightField( FLOAT x, FLOAT y)
{
return 9*(cosf(x/20+0.2f)*cosf(y/ 15-0.2f)+1 .0f);
}
CMyD3DApplication g_d3dApp;
INT WINAPI WinMain (HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
if (FAILED (g_d3dApp.Create (hInst)))
return 0;
return g_d3dApp.Run();
}
CMyD3DApplication::CMyD3DA pplication ()
{
m_strWindowTitle = _T("B&P Demostration");
m_pFont = new CD3DFont (_T("Arial"),12, D3DFONT_BOLD);
m_pForkLift = new CD3DMesh();
m_pPallet = new CD3DMesh();
m_pRack = new CD3DMesh();
/////////////////// added
m_bUseDepthBuffer = TRUE;
m_bShowCursorWhenFullscree n = TRUE;
// m_dwNumSegs = 1;
_tcscpy (m_strInitialDir, DXUtil_GetDXSDKMediaPath() );
_tcscpy (m_strMeshFilename, _T("car.x"));
_tcscpy (m_strMeshFilename2, _T("pallet.x"));
///////////////////
}
HRESULT CMyD3DApplication::InitDev iceObjects ()
{
LPDIRECT3DVERTEXBUFFER8 pVB = NULL;
LPDIRECT3DVERTEXBUFFER8 pVB2 = NULL;
BYTE *pVertices = NULL;
BYTE *pVertices2 = NULL;
LPD3DXMESH pTempMesh;
LPD3DXMESH pTempMesh2;
TCHAR strMeshPath[512];
TCHAR strMeshPathPallet[512];
m_pFont->InitDeviceObjects ( m_pd3dDevice );
m_pForkLift->Create( m_pd3dDevice, _T("car.x"));
m_pRack->Create (m_pd3dDevice, _T("rack.x"));
m_pPallet->Create (m_pd3dDevice, _T("pallet.x"));
//////////////////// added
DXUtil_FindMediaFile (strMeshPath, m_strMeshFilename );
DXUtil_FindMediaFile (strMeshPathPallet, m_strMeshFilename2);
D3DXLoadMeshFromX (strMeshPath, D3DXMESH_SYSTEMMEM, m_pd3dDevice,
&m_pbufAdjacency, &m_pbufMaterials, &m_dwNumMaterials,
&m_pMeshSysMem[0] );
m_pMeshSysMem[0]->GetVerte xBuffer (&pVB);
pVB->Lock (0,0,&pVertices,0);
D3DXComputeBoundingSphere (pVertices, m_pMeshSysMem[0]->GetNumVe rtices(),
m_pMeshSysMem[0]->GetFVF() , &m_vObjectCenter,
&m_fObjectRadius);
pVB->Unlock();
SAFE_RELEASE(pVB);
m_pMaterials = (D3DXMATERIAL*) m_pbufMaterials->GetBuffer Pointer();
m_ppTextures = new LPDIRECT3DTEXTURE8[m_dwNum Materials] ;
for (UINT i=0; i < m_dwNumMaterials; i++)
{
TCHAR strTexturePath[512] = _T("");
DXUtil_FindMediaFile (strTexturePath, m_pMaterials[i].pTextureFi lename);
D3DXCreateTextureFromFile( m_pd3dDevi ce, strTexturePath, &m_ppTextures[i]);
}
if (! (m_pMeshSysMem[0]->GetFVF( ) & D3DFVF_NORMAL ))
{
m_pMeshSysMem[0]->CloneMes hFVF (m_pMeshSysMem[0]->GetOpti ons(),
m_pMeshSysMem[0]->GetFVF() | D3DFVF_NORMAL,
m_pd3dDevice, &pTempMesh);
D3DXComputeNormals (pTempMesh, NULL);
// SAFE_RELEASE (m_pMeshSysMem);
m_pMeshSysMem[0] = pTempMesh;
}
//////////////////
D3DXLoadMeshFromX (strMeshPathPallet, D3DXMESH_SYSTEMMEM, m_pd3dDevice,
&m_pbufAdjacency2, &m_pbufMaterials2, &m_dwNumMaterials2,
&m_pMeshSysMem[1]);
m_pMeshSysMem[1]->GetVerte xBuffer (&pVB2);
pVB2->Lock (0,0,&pVertices2,0);
// D3DXComputeBoundingSphere (pVertices2, m_pMeshSysMem2->GetNumVert ices(),
// m_pMeshSysMem2->GetFVF(), &m_vObjectCenter,
// &m_fObjectRadius);
pVB2->Unlock();
SAFE_RELEASE(pVB2);
m_pMaterials2 = (D3DXMATERIAL*) m_pbufMaterials2->GetBuffe rPointer() ;
m_ppTextures2 = new LPDIRECT3DTEXTURE8[m_dwNum Materials2 ];
for (i=0; i < m_dwNumMaterials2; i++)
{
TCHAR strTexturePath[512] = _T("");
DXUtil_FindMediaFile (strTexturePath, m_pMaterials2[i].pTextureF ilename);
D3DXCreateTextureFromFile( m_pd3dDevi ce, strTexturePath, &m_ppTextures2[i]);
}
if (! (m_pMeshSysMem[1]->GetFVF( ) & D3DFVF_NORMAL ))
{
m_pMeshSysMem[1]->CloneMes hFVF (m_pMeshSysMem[1]->GetOpti ons(),
m_pMeshSysMem[1]->GetFVF() | D3DFVF_NORMAL,
m_pd3dDevice, &pTempMesh2);
D3DXComputeNormals (pTempMesh2, NULL);
// SAFE_RELEASE (m_pMeshSysMem);
m_pMeshSysMem[1] = pTempMesh2;
}
return S_OK;
}
HRESULT CMyD3DApplication::OneTime SceneInit( )
{
return S_OK;
}
HRESULT CMyD3DApplication::Render( )
{
D3DXMATRIX objectWorldMatrix;
m_pd3dDevice->Clear (0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x000000ff, 1.0f, 0L);
D3DXMatrixIdentity(&object WorldMatri x);
m_pd3dDevice->BeginScene() ;
// temp can.
/* D3DXMATRIX matView, matViewSave;
m_pd3dDevice->GetTransform (D3DTS_VIEW, &matViewSave);
matView = matViewSave;
matView._41 = 0.0f; matView._42 = -0.3f; matView._43 = 0.0f;
m_pd3dDevice->SetTransform (D3DTS_VIEW, &matView); */
objectWorldMatrix._41 = 2.0f;
m_pForkLift->Render(m_pd3d Device);
objectWorldMatrix._41 = 2.0f;
m_pRack->Render(m_pd3dDevi ce);
m_pd3dDevice->SetRenderSta te (D3DRS_ZFUNC, D3DCMP_ALWAYS);
objectWorldMatrix._41 = 2.0f;
m_pPallet->Render(m_pd3dDe vice);
m_pd3dDevice->SetRenderSta te (D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
////////////////// added
for (UINT k = 0 ; k < 2; k++) {
for (UINT i = 0; i < m_dwNumMaterials; i++) {
m_pd3dDevice->SetMaterial( &m_pMateri als[i].Mat D3D);
m_pd3dDevice->SetTexture (0, m_ppTextures[i]);
m_pd3dDevice->SetRenderSta te (D3DRS_ZFUNC, D3DCMP_ALWAYS);
m_pMeshEnhanced[k]->DrawSu bset (i);
m_pd3dDevice->SetRenderSta te (D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
}
}
m_pd3dDevice->EndScene();
return S_OK;
}
HRESULT CMyD3DApplication::FrameMo ve()
{
D3DXMATRIX matWorld;
// view for XMesh
D3DXMatrixTranslation (&matWorld, -m_vObjectCenter.x,
-m_vObjectCenter.y,
-m_vObjectCenter.z);
D3DXMatrixMultiply (&matWorld, &matWorld, m_ArcBall.GetRotationMatri x());
D3DXMatrixMultiply (&matWorld, &matWorld, m_ArcBall.GetTranslationMa trix());
m_pd3dDevice->SetTransform (D3DTS_WOR LD, &matWorld);
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matVie w, &D3DXVECTOR3 (0,0,-2*m_fObjectRadius),
&D3DXVECTOR3 (0, 0, 0),
&D3DXVECTOR3 (0, 1, 0) );
m_pd3dDevice->SetTransform (D3DTS_VIE W, &matView);
// view for CDCMesh
/* D3DXVECTOR3 vUpvec (0.0f, 1.0f, 0.0f);
D3DXVECTOR3 vEyePt;
D3DXVECTOR3 vLookatPt;
vEyePt.x = 30.0f*cosf (0.8f * (m_fTime));
vEyePt.z = 30.0f*sinf (0.8f * (m_fTime));
vEyePt.x = 4 + HeightField (vEyePt.x, vEyePt.z );
vLookatPt.x = 30.0f*cosf(0.8f * (m_fTime + 0.5f) );
vLookatPt.z = 30.0f*sinf(0.8f * (m_fTime + 0.5f) );
vLookatPt.y = vEyePt.y - 1.0f;
// D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpvec);
m_pd3dDevice->SetTransform (D3DTS_VIEW, &matView); */
return S_OK;
}
HRESULT CMyD3DApplication::Generat eEnhancedM esh(UINT dwNewNumSegs, int Num)
{
LPD3DXMESH pMeshTemp;
LPD3DXMESH pMeshEnhancedSysMem[3] = {NULL, NULL, NULL};
HRESULT hr;
////////////////// temp cancelled
// m_pMeshSysMem->CloneMeshFV F (D3DXMESH_WRITEONLY | D3DXMESH_NPATCHES |
// (m_pMeshSysMem->GetOptions () & D3DXMESH_32BIT),
// m_pMeshSysMem->GetFVF(), m_pd3dDevice, &pMeshTemp );
hr = D3DXTessellateNPatches( m_pMeshSysMem[Num], (DWORD*)m_pbufAdjacency->G etBufferPo inter(),
(float)dwNewNumSegs, FALSE, &pMeshEnhancedSysMem[Num], NULL );
if( FAILED(hr) )
{
// If the tessellate failed, there might have been more triangles or vertices
// than can fit into a 16bit mesh, so try cloning to 32bit before tessellation
hr = m_pMeshSysMem[Num]->CloneM eshFVF( D3DXMESH_SYSTEMMEM | D3DXMESH_32BIT,
m_pMeshSysMem[Num]->GetFVF (), m_pd3dDevice, &pMeshTemp );
if (FAILED(hr))
return hr;
hr = D3DXTessellateNPatches( pMeshTemp, (DWORD*)m_pbufAdjacency->G etBufferPo inter(),
(float)dwNewNumSegs, FALSE, &pMeshEnhancedSysMem[Num], NULL );
if( FAILED(hr) )
{
pMeshTemp->Release();
return hr;
}
pMeshTemp->Release();
}
// Make a vid mem version of the mesh
// Only set WRITEONLY if it doesn't use 32bit indices, because those
// often need to be emulated, which means that D3DX needs read-access.
DWORD dwMeshEnhancedFlags = pMeshEnhancedSysMem[Num]-> GetOptions () & D3DXMESH_32BIT;
if( (dwMeshEnhancedFlags & D3DXMESH_32BIT) == 0)
dwMeshEnhancedFlags |= D3DXMESH_WRITEONLY;
hr = pMeshEnhancedSysMem[Num]-> CloneMeshF VF( dwMeshEnhancedFlags, m_pMeshSysMem[Num]->GetFVF (),
m_pd3dDevice, &pMeshTemp );
/* if( FAILED(hr) )
{
SAFE_RELEASE( pMeshEnhancedSysMem[Num] );
return hr;
}*/
// Latch in the enhanced mesh
/* SAFE_RELEASE( pMeshEnhancedSysMem[Num] );*/
m_pMeshEnhanced[Num] = pMeshTemp;
m_dwNumSegs[Num] = dwNewNumSegs;
return S_OK;
}
HRESULT CMyD3DApplication::Restore DeviceObje cts()
{
m_pForkLift->RestoreDevice Objects( m_pd3dDevice );
m_pPallet->RestoreDeviceOb jects( m_pd3dDevice );
m_pRack->RestoreDeviceObje cts (m_pd3dDevice );
m_pFont->RestoreDeviceObje cts();
HRESULT hr;
// m_bUseHWNPatches = (m_d3dCaps.DevCaps & D3DDEVCAPS_NPATCHES);
hr = GenerateEnhancedMesh( m_dwNumSegs[0] ,0);
if( FAILED(hr) )
return hr;
hr = GenerateEnhancedMesh( m_dwNumSegs[1] , 1);
if ( FAILED (hr) )
return hr;
// Setup render state
m_pd3dDevice->SetRenderSta te( D3DRS_LIGHTING, TRUE );
m_pd3dDevice->SetRenderSta te( D3DRS_DITHERENABLE, TRUE );
m_pd3dDevice->SetRenderSta te( D3DRS_ZENABLE, TRUE );
m_pd3dDevice->SetRenderSta te( D3DRS_AMBIENT, 0x33333333 );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
// Setup the light
D3DLIGHT8 light;
D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.0f,-1.0f, 1.0f );
m_pd3dDevice->SetLight(0, &light );
m_pd3dDevice->LightEnable( 0, TRUE );
// Setup the arcball parameters
m_ArcBall.SetWindow( m_d3dsdBackBuffer.Width, m_d3dsdBackBuffer.Height, 0.85f );
m_ArcBall.SetRadius( 1.0f );
// Setup the projection matrix
D3DXMATRIX matProj;
FLOAT fAspect = (FLOAT)m_d3dsdBackBuffer.W idth / (FLOAT)m_d3dsdBackBuffer.H eight;
D3DXMatrixPerspectiveFovLH ( &matProj, D3DX_PI/4, fAspect,
m_fObjectRadius/64.0f, m_fObjectRadius*200.0f );
m_pd3dDevice->SetTransform ( D3DTS_PROJECTION, &matProj );
// Restore the font
m_pFont->RestoreDeviceObje cts();
// for CDCMesh
/* D3DXMATRIX matProj;
FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT) m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH (&matProj, D3DX_PI/4, fAspect, 1.0f, 100.0f);
m_pd3dDevice->SetTransform (D3DTS_PROJECTION, &matProj);
m_pd3dDevice->SetTextureSt ageState(0 , D3DTSS_COLOROP, D3DTOP_SELECTARG1);
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP );
m_pd3dDevice->SetRenderSta te( D3DRS_DITHERENABLE, TRUE );
m_pd3dDevice->SetRenderSta te( D3DRS_ZENABLE, TRUE );
m_pd3dDevice->SetRenderSta te( D3DRS_LIGHTING, TRUE ); */
return S_OK;
}
HRESULT CMyD3DApplication::DeleteD eviceObjec ts()
{
m_pFont->DeleteDeviceObjec ts();
m_pForkLift->Destroy();
m_pRack->Destroy();
m_pPallet->Destroy();
return S_OK;
}
HRESULT CMyD3DApplication::FinalCl eanup()
{
SAFE_DELETE (m_pFont);
SAFE_DELETE (m_pForkLift);
SAFE_DELETE (m_pPallet);
SAFE_DELETE (m_pRack);
return S_OK;
}
HRESULT CMyD3DApplication::Confirm Device( D3DCAPS8 *pCaps, DWORD dwBehavior, D3DFORMAT Format)
{
if (dwBehavior & D3DCREATE_PUREDEVICE)
return E_FAIL;
if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHAPALET TE)
return S_OK;
if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHA)
return S_OK;
return E_FAIL;
}
here is the modified code:
#define STRICT
#include <basetsd.h>
#include <stdio.h>
#include <math.h>
#include <D3DX8.h>
#include "D3DApp.h"
#include "D3DFile.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"
class CMyD3DApplication : public CD3DApplication
{
CD3DFont* m_pFont;
CD3DMesh* m_pForkLift;
CD3DMesh* m_pPallet;
CD3DMesh* m_pRack;
/////////////////// added
TCHAR m_strMeshFilename[512];
TCHAR m_strMeshFilename2[512];
TCHAR m_strInitialDir[512];
LPD3DXMESH m_pMeshSysMem[3]; // forklift, pallet and rack
LPD3DXMESH m_pMeshEnhanced[3];
UINT m_dwNumSegs[3];
DWORD m_dwNumMaterials;
CD3DArcBall m_ArcBall;
D3DXMATERIAL*m_pMaterials;
LPDIRECT3DTEXTURE8 *m_ppTextures;
LPD3DXBUFFER m_pbufMaterials;
LPD3DXBUFFER m_pbufAdjacency;
LPD3DXMESH m_pMeshSysMem2; // pallet
LPD3DXMESH m_pMeshEnhanced2;
UINT m_dwNumSegs2;
DWORD m_dwNumMaterials2;
CD3DArcBall m_ArcBall2;
D3DXMATERIAL*m_pMaterials2
LPDIRECT3DTEXTURE8 *m_ppTextures2;
LPD3DXBUFFER m_pbufMaterials2;
LPD3DXBUFFER m_pbufAdjacency2;
///////////////////
D3DXVECTOR3 m_vObjectCenter;
FLOAT m_fObjectRadius;
///////////////////
D3DXMATRIX m_matBillboardMatrix;
HRESULT ConfirmDevice(D3DCAPS8*, DWORD, D3DFORMAT);
HRESULT GenerateEnhancedMesh(UINT cNewNumSegs, int Num);
protected:
HRESULT OneTimeSceneInit();
HRESULT InitDeviceObjects();
HRESULT RestoreDeviceObjects();
HRESULT InvalidDeviceObjects();
HRESULT DeleteDeviceObjects();
HRESULT FinalCleanup();
HRESULT Render();
HRESULT FrameMove();
public:
CMyD3DApplication();
};
inline FLOAT HeightField( FLOAT x, FLOAT y)
{
return 9*(cosf(x/20+0.2f)*cosf(y/
}
CMyD3DApplication g_d3dApp;
INT WINAPI WinMain (HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
if (FAILED (g_d3dApp.Create (hInst)))
return 0;
return g_d3dApp.Run();
}
CMyD3DApplication::CMyD3DA
{
m_strWindowTitle = _T("B&P Demostration");
m_pFont = new CD3DFont (_T("Arial"),12, D3DFONT_BOLD);
m_pForkLift = new CD3DMesh();
m_pPallet = new CD3DMesh();
m_pRack = new CD3DMesh();
/////////////////// added
m_bUseDepthBuffer = TRUE;
m_bShowCursorWhenFullscree
// m_dwNumSegs = 1;
_tcscpy (m_strInitialDir, DXUtil_GetDXSDKMediaPath()
_tcscpy (m_strMeshFilename, _T("car.x"));
_tcscpy (m_strMeshFilename2, _T("pallet.x"));
///////////////////
}
HRESULT CMyD3DApplication::InitDev
{
LPDIRECT3DVERTEXBUFFER8 pVB = NULL;
LPDIRECT3DVERTEXBUFFER8 pVB2 = NULL;
BYTE *pVertices = NULL;
BYTE *pVertices2 = NULL;
LPD3DXMESH pTempMesh;
LPD3DXMESH pTempMesh2;
TCHAR strMeshPath[512];
TCHAR strMeshPathPallet[512];
m_pFont->InitDeviceObjects
m_pForkLift->Create( m_pd3dDevice, _T("car.x"));
m_pRack->Create (m_pd3dDevice, _T("rack.x"));
m_pPallet->Create (m_pd3dDevice, _T("pallet.x"));
//////////////////// added
DXUtil_FindMediaFile (strMeshPath, m_strMeshFilename );
DXUtil_FindMediaFile (strMeshPathPallet, m_strMeshFilename2);
D3DXLoadMeshFromX (strMeshPath, D3DXMESH_SYSTEMMEM, m_pd3dDevice,
&m_pbufAdjacency, &m_pbufMaterials, &m_dwNumMaterials,
&m_pMeshSysMem[0] );
m_pMeshSysMem[0]->GetVerte
pVB->Lock (0,0,&pVertices,0);
D3DXComputeBoundingSphere (pVertices, m_pMeshSysMem[0]->GetNumVe
m_pMeshSysMem[0]->GetFVF()
&m_fObjectRadius);
pVB->Unlock();
SAFE_RELEASE(pVB);
m_pMaterials = (D3DXMATERIAL*) m_pbufMaterials->GetBuffer
m_ppTextures = new LPDIRECT3DTEXTURE8[m_dwNum
for (UINT i=0; i < m_dwNumMaterials; i++)
{
TCHAR strTexturePath[512] = _T("");
DXUtil_FindMediaFile (strTexturePath, m_pMaterials[i].pTextureFi
D3DXCreateTextureFromFile(
}
if (! (m_pMeshSysMem[0]->GetFVF(
{
m_pMeshSysMem[0]->CloneMes
m_pMeshSysMem[0]->GetFVF()
m_pd3dDevice, &pTempMesh);
D3DXComputeNormals (pTempMesh, NULL);
// SAFE_RELEASE (m_pMeshSysMem);
m_pMeshSysMem[0] = pTempMesh;
}
//////////////////
D3DXLoadMeshFromX (strMeshPathPallet, D3DXMESH_SYSTEMMEM, m_pd3dDevice,
&m_pbufAdjacency2, &m_pbufMaterials2, &m_dwNumMaterials2,
&m_pMeshSysMem[1]);
m_pMeshSysMem[1]->GetVerte
pVB2->Lock (0,0,&pVertices2,0);
// D3DXComputeBoundingSphere (pVertices2, m_pMeshSysMem2->GetNumVert
// m_pMeshSysMem2->GetFVF(), &m_vObjectCenter,
// &m_fObjectRadius);
pVB2->Unlock();
SAFE_RELEASE(pVB2);
m_pMaterials2 = (D3DXMATERIAL*) m_pbufMaterials2->GetBuffe
m_ppTextures2 = new LPDIRECT3DTEXTURE8[m_dwNum
for (i=0; i < m_dwNumMaterials2; i++)
{
TCHAR strTexturePath[512] = _T("");
DXUtil_FindMediaFile (strTexturePath, m_pMaterials2[i].pTextureF
D3DXCreateTextureFromFile(
}
if (! (m_pMeshSysMem[1]->GetFVF(
{
m_pMeshSysMem[1]->CloneMes
m_pMeshSysMem[1]->GetFVF()
m_pd3dDevice, &pTempMesh2);
D3DXComputeNormals (pTempMesh2, NULL);
// SAFE_RELEASE (m_pMeshSysMem);
m_pMeshSysMem[1] = pTempMesh2;
}
return S_OK;
}
HRESULT CMyD3DApplication::OneTime
{
return S_OK;
}
HRESULT CMyD3DApplication::Render(
{
D3DXMATRIX objectWorldMatrix;
m_pd3dDevice->Clear (0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x000000ff, 1.0f, 0L);
D3DXMatrixIdentity(&object
m_pd3dDevice->BeginScene()
// temp can.
/* D3DXMATRIX matView, matViewSave;
m_pd3dDevice->GetTransform
matView = matViewSave;
matView._41 = 0.0f; matView._42 = -0.3f; matView._43 = 0.0f;
m_pd3dDevice->SetTransform
objectWorldMatrix._41 = 2.0f;
m_pForkLift->Render(m_pd3d
objectWorldMatrix._41 = 2.0f;
m_pRack->Render(m_pd3dDevi
m_pd3dDevice->SetRenderSta
objectWorldMatrix._41 = 2.0f;
m_pPallet->Render(m_pd3dDe
m_pd3dDevice->SetRenderSta
////////////////// added
for (UINT k = 0 ; k < 2; k++) {
for (UINT i = 0; i < m_dwNumMaterials; i++) {
m_pd3dDevice->SetMaterial(
m_pd3dDevice->SetTexture (0, m_ppTextures[i]);
m_pd3dDevice->SetRenderSta
m_pMeshEnhanced[k]->DrawSu
m_pd3dDevice->SetRenderSta
}
}
m_pd3dDevice->EndScene();
return S_OK;
}
HRESULT CMyD3DApplication::FrameMo
{
D3DXMATRIX matWorld;
// view for XMesh
D3DXMatrixTranslation (&matWorld, -m_vObjectCenter.x,
-m_vObjectCenter.y,
-m_vObjectCenter.z);
D3DXMatrixMultiply (&matWorld, &matWorld, m_ArcBall.GetRotationMatri
D3DXMatrixMultiply (&matWorld, &matWorld, m_ArcBall.GetTranslationMa
m_pd3dDevice->SetTransform
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matVie
&D3DXVECTOR3 (0, 0, 0),
&D3DXVECTOR3 (0, 1, 0) );
m_pd3dDevice->SetTransform
// view for CDCMesh
/* D3DXVECTOR3 vUpvec (0.0f, 1.0f, 0.0f);
D3DXVECTOR3 vEyePt;
D3DXVECTOR3 vLookatPt;
vEyePt.x = 30.0f*cosf (0.8f * (m_fTime));
vEyePt.z = 30.0f*sinf (0.8f * (m_fTime));
vEyePt.x = 4 + HeightField (vEyePt.x, vEyePt.z );
vLookatPt.x = 30.0f*cosf(0.8f * (m_fTime + 0.5f) );
vLookatPt.z = 30.0f*sinf(0.8f * (m_fTime + 0.5f) );
vLookatPt.y = vEyePt.y - 1.0f;
// D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpvec);
m_pd3dDevice->SetTransform
return S_OK;
}
HRESULT CMyD3DApplication::Generat
{
LPD3DXMESH pMeshTemp;
LPD3DXMESH pMeshEnhancedSysMem[3] = {NULL, NULL, NULL};
HRESULT hr;
////////////////// temp cancelled
// m_pMeshSysMem->CloneMeshFV
// (m_pMeshSysMem->GetOptions
// m_pMeshSysMem->GetFVF(), m_pd3dDevice, &pMeshTemp );
hr = D3DXTessellateNPatches( m_pMeshSysMem[Num], (DWORD*)m_pbufAdjacency->G
(float)dwNewNumSegs, FALSE, &pMeshEnhancedSysMem[Num],
if( FAILED(hr) )
{
// If the tessellate failed, there might have been more triangles or vertices
// than can fit into a 16bit mesh, so try cloning to 32bit before tessellation
hr = m_pMeshSysMem[Num]->CloneM
m_pMeshSysMem[Num]->GetFVF
if (FAILED(hr))
return hr;
hr = D3DXTessellateNPatches( pMeshTemp, (DWORD*)m_pbufAdjacency->G
(float)dwNewNumSegs, FALSE, &pMeshEnhancedSysMem[Num],
if( FAILED(hr) )
{
pMeshTemp->Release();
return hr;
}
pMeshTemp->Release();
}
// Make a vid mem version of the mesh
// Only set WRITEONLY if it doesn't use 32bit indices, because those
// often need to be emulated, which means that D3DX needs read-access.
DWORD dwMeshEnhancedFlags = pMeshEnhancedSysMem[Num]->
if( (dwMeshEnhancedFlags & D3DXMESH_32BIT) == 0)
dwMeshEnhancedFlags |= D3DXMESH_WRITEONLY;
hr = pMeshEnhancedSysMem[Num]->
m_pd3dDevice, &pMeshTemp );
/* if( FAILED(hr) )
{
SAFE_RELEASE( pMeshEnhancedSysMem[Num] );
return hr;
}*/
// Latch in the enhanced mesh
/* SAFE_RELEASE( pMeshEnhancedSysMem[Num] );*/
m_pMeshEnhanced[Num] = pMeshTemp;
m_dwNumSegs[Num] = dwNewNumSegs;
return S_OK;
}
HRESULT CMyD3DApplication::Restore
{
m_pForkLift->RestoreDevice
m_pPallet->RestoreDeviceOb
m_pRack->RestoreDeviceObje
m_pFont->RestoreDeviceObje
HRESULT hr;
// m_bUseHWNPatches = (m_d3dCaps.DevCaps & D3DDEVCAPS_NPATCHES);
hr = GenerateEnhancedMesh( m_dwNumSegs[0] ,0);
if( FAILED(hr) )
return hr;
hr = GenerateEnhancedMesh( m_dwNumSegs[1] , 1);
if ( FAILED (hr) )
return hr;
// Setup render state
m_pd3dDevice->SetRenderSta
m_pd3dDevice->SetRenderSta
m_pd3dDevice->SetRenderSta
m_pd3dDevice->SetRenderSta
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetTextureSt
// Setup the light
D3DLIGHT8 light;
D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.0f,-1.0f, 1.0f );
m_pd3dDevice->SetLight(0, &light );
m_pd3dDevice->LightEnable(
// Setup the arcball parameters
m_ArcBall.SetWindow( m_d3dsdBackBuffer.Width, m_d3dsdBackBuffer.Height, 0.85f );
m_ArcBall.SetRadius( 1.0f );
// Setup the projection matrix
D3DXMATRIX matProj;
FLOAT fAspect = (FLOAT)m_d3dsdBackBuffer.W
D3DXMatrixPerspectiveFovLH
m_fObjectRadius/64.0f, m_fObjectRadius*200.0f );
m_pd3dDevice->SetTransform
// Restore the font
m_pFont->RestoreDeviceObje
// for CDCMesh
/* D3DXMATRIX matProj;
FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT) m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH
m_pd3dDevice->SetTransform
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetRenderSta
m_pd3dDevice->SetRenderSta
m_pd3dDevice->SetRenderSta
return S_OK;
}
HRESULT CMyD3DApplication::DeleteD
{
m_pFont->DeleteDeviceObjec
m_pForkLift->Destroy();
m_pRack->Destroy();
m_pPallet->Destroy();
return S_OK;
}
HRESULT CMyD3DApplication::FinalCl
{
SAFE_DELETE (m_pFont);
SAFE_DELETE (m_pForkLift);
SAFE_DELETE (m_pPallet);
SAFE_DELETE (m_pRack);
return S_OK;
}
HRESULT CMyD3DApplication::Confirm
{
if (dwBehavior & D3DCREATE_PUREDEVICE)
return E_FAIL;
if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHAPALET
return S_OK;
if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHA)
return S_OK;
return E_FAIL;
}
Found an error in your code.
//This needs to be called for every mesh.
hr = GenerateEnhancedMesh( m_dwNumSegs );
//This needs to be called for every mesh.
hr = GenerateEnhancedMesh( m_dwNumSegs );
ASKER
No, doesn't make it... I will increase to 300 pts if you can provide the source code, come on This is my first job... I beg you I don't want to lose it... I just need a starting pt... want just this...
Thanks
Jack
Thanks
Jack
Your first job? Are you working on a games company ?
do you really need the enhanced mesh or can you use a normal mesh ?
ASKER
Normal Mesh willl do
ASKER
I want to make it simple by adopting retained mode....
ASKER
what about this? 2 objects easier?
// InputView.cpp : implementation file
//
#include "stdafx.h"
#include "ws.h"
#include "InputView.h"
#include "simpvw.h"
#include "splitter.h"
#include <d3drm.h>
#include <ddraw.h>
#include <d3drmwin.h>
//#include <string.h>
//#include <stdio.h>
#include <iostream.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define GO_LEFT 0
#define GO_RIGHT 1
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CInputView
IMPLEMENT_DYNCREATE(CInput View, CFormView)
BEGIN_MESSAGE_MAP(CInputVi ew, CFormView)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_DESTROY()
END_MESSAGE_MAP()
int CInputView::OnCreate(LPCRE ATESTRUCT)
{
// create Direct3D object
HRESULT result = Direct3DRMCreate(&m_pD3D);
if (result != D3DRM_OK)
return 0;
result = DirectDrawCreateClipper(0, &m_pClipper, 0);
if (result != DD_OK)
return 0;
result = m_pClipper->SetHWnd(0, m_hWnd);
if (result != DD_OK)
return 0;
RECT rect;
::GetClientRect(m_hWnd, &rect);
// result = m_pD3D->CreateDeviceFromCl ipper(m_pC lipper,
// NULL, rect.right, rect.bottom, &m_pDevice);
result = m_pD3D->CreateDeviceFromCl ipper(m_pC lipper,
NULL, rect.right, rect.bottom, &m_pDevice);
if (result != D3DRM_OK)
return 0;
m_pDevice->SetQuality(D3DR MRENDER_GO URAUD);
//create parent frame
m_pD3D->CreateFrame(0, &m_pParentFrame);
if (result != D3DRM_OK)
return 0;
BOOL succeeded = MakeScene();
if (!succeeded)
return 0;
m_D3DOK = TRUE;
SetTimer(1, 100, NULL);
return 0;
}
CInputView::CInputView()
: CFormView(CInputView::IDD)
{
m_D3DOK = FALSE;
m_pD3D = NULL;
m_pDevice = NULL;
m_pViewport = NULL;
m_pParentFrame = NULL;
m_pEye = NULL;
m_pRack = NULL;
m_pMeshRack = NULL;
m_pMeshBuilder = NULL;
m_pClipper = NULL;
// Create(NULL, "Direct3DApp");
//{{AFX_DATA_INIT(CInputVi ew)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
CInputView::~CInputView()
{
}
void CInputView::DoDataExchange (CDataExch ange* pDX)
{
CFormView::DoDataExchange( pDX);
//{{AFX_DATA_MAP(CInputVie w)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
////////////////////////// ////////// //// ok
BOOL CInputView::MakeScene()
{
BOOL succeeded = CreateMesh();
if (!succeeded)
return FALSE;
succeeded = CreateViewport();
if (!succeeded)
return FALSE;
succeeded = CreateLight();
if (!succeeded)
return FALSE;
return TRUE;
}
////////////////////////// ////////// /// ok
////////////////////////// ////////// /// ok
BOOL CInputView::CreateMesh()
{
HRESULT result =
m_pD3D->CreateMeshBuilder( &m_pMeshBu ilder);
result = m_pD3D->CreateMeshBuilder( &m_pMeshRa ck);
if (result != D3DRM_OK)
return FALSE;
result = m_pMeshBuilder->Load("mode l.x",
NULL, D3DRMLOAD_FROMFILE, NULL, NULL );
result = m_pMeshRack->Load("rack.x" ,
NULL, D3DRMLOAD_FROMFILE, NULL, NULL );
if (result != D3DRM_OK)
{
MessageBox("MODEL.X file missing. Please place\n\
a Direct3D X file with this name\nin the Direct3DApp folder.");
return FALSE;
}
LPDIRECT3DRMFRAME pFrame;
result = m_pD3D->CreateFrame(m_pPar entFrame, &pFrame);
if (result != D3DRM_OK)
return FALSE;
pFrame->AddVisual(m_pMeshB uilder);
pFrame->AddVisual(m_pMeshR ack);
// pFrame->SetRotation(m_pPar entFrame, D3DVALUE(0.0),
// D3DVALUE(1.0), D3DVALUE(0.0), D3DVALUE(0.05));
pFrame->Release();
pFrame = NULL;
return TRUE;
} ////////////////////////// ///// ok
BOOL CInputView::CreateViewport ()
{
LPD3DVECTOR abc = new D3DVECTOR();
char hello[30];
HRESULT result = m_pD3D->CreateFrame(m_pPar entFrame, &m_pEye);
// result = m_pD3D->CreateFrame(m_pPar entFrame,& m_pRack);
if (result != D3DRM_OK)
AfxMessageBox ("Hi!");
// abc->x = 0.0;
// abc->y = 0.0;
// abc->z = 0.0;
// m_pRack->GetPosition(m_pPa rentFrame, LPD3DVECTOR(abc));
// sprintf (hello, "%f %f %f", abc->x,abc->y, abc->z);
// strcat (hello, '\0');
// strcpy (hello, "hello");
// AfxMessageBox (hello);
// z ,y (smaller the lower) ,x (smaller the lefter)
// 0.0 5.0 -50.0
m_pEye->SetPosition(m_pPar entFrame, D3DVALUE(0.0),
D3DVALUE(5.0), D3DVALUE(-50.0));
// m_pRack->SetPosition(m_pPa rentFrame, D3DVALUE(0.0),
// D3DVALUE(10.0), D3DVALUE(-50.0));
result = m_pD3D->CreateViewport(m_p Device, m_pEye,
0, 0, m_pDevice->GetWidth(),
m_pDevice->GetHeight(), &m_pViewport);
//result = m_pD3D->CreateViewport(m_p Device, m_pRack,
// 0, 0, m_pDevice->GetWidth(),
// m_pDevice->GetHeight(), &m_pViewport);
// result = m_pD3D->CreateViewport(m_p Device, m_pEye,
// 0, 0, 640,
// 480, &m_pViewport);
if (result != D3DRM_OK)
return FALSE;
return TRUE;
}
////////////////////////// ////////// //// ok
BOOL CInputView::CreateLight()
{
LPDIRECT3DRMLIGHT pLight;
HRESULT result = m_pD3D->
CreateLightRGB(D3DRMLIGHT_ DIRECTIONA L,
D3DVALUE(1.0), D3DVALUE(1.0), D3DVALUE(1.0),
&pLight);
if (result != D3DRM_OK)
return FALSE;
LPDIRECT3DRMFRAME pLightFrame;
result = m_pD3D->CreateFrame(m_pPar entFrame,
&pLightFrame );
if (result != D3DRM_OK)
return FALSE;
pLightFrame->AddLight(pLig ht);
pLightFrame->SetOrientatio n(m_pParen tFrame,
D3DVALUE(0.0), D3DVALUE(-1.0), D3DVALUE(1.0),
D3DVALUE(0.0), D3DVALUE(1.0), D3DVALUE(0.0));
pLight->Release();
pLight = NULL;
pLightFrame->Release();
pLightFrame = NULL;
return TRUE;
}
////////////////////////// ////////// /// ok
void CInputView::OnPaint()
{
BOOL repaint = GetUpdateRect(NULL);
if (!repaint)
return;
// PAINTSTRUCT pstruct, sstruct;
PAINTSTRUCT pstruct;
BeginPaint(&pstruct);
// BeginPaint(&sstruct);
if (m_D3DOK)
{
LPDIRECT3DRMWINDEVICE pWndDevice;
m_pDevice->QueryInterface(
IID_IDirect3DRMWinDevice,
(void**)&pWndDevice);
pWndDevice->HandlePaint(ps truct.hdc) ;
pWndDevice->Release();
// LPDIRECT3DRMWINDEVICE pWndDeviceRack;
// m_pDeviceRack->QueryInterf ace(
// IID_IDirect3DRMWinDevice,
// (void**)&pWndDeviceRack);
// pWndDeviceRack->HandlePain t(sstruct. hdc);
// pWndDeviceRack->Release();
}
else
::TextOut(pstruct.hdc, 20, 20,
"Direct3D failed to start.", 25);
EndPaint(&pstruct);
// EndPaint(&sstruct);
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CInputView diagnostics
void CInputView::OnTimer(UINT nIDEvent)
{
static float aPoint = 0;
static float aPoint2 = 0;
static float aPoint3 = 0;
// char abc[10];
static int count = 2;
// static int Direc = GO_LEFT;
if (m_D3DOK)
m_pD3D->Tick(D3DVALUE(1));
//x,y,z
if (count <= 10 && count >= 2) {
m_pMeshBuilder->Translate( aPoint,aPo int,0);
aPoint -= 0.2;
} else {
m_pMeshBuilder->Translate( aPoint,0, 0);
aPoint += 0.2;
}
count++;
if (count >= 100)
count = 2;
// sprintf (abc, "%f", aPoint);
// AfxMessageBox (abc);
/*if (aPoint <= -1.0) {
aPoint += 0.2;
return;
} else if (aPoint >= 1.0) {
aPoint -= 0.2;
return;
} else {
aPoint -= 0.2;
return;
}*/
/* if (aPoint <= 0 && aPoint >= -12.0 && Direc == GO_LEFT) {
if (aPoint == -10.0)
Direc = GO_RIGHT;
aPoint -= 0.2;
} else if (aPoint >= -12.2 && aPoint <= 0 && Direc == GO_RIGHT) {
aPoint += 0.2;
} */
}
void CInputView::OnDestroy()
{
if (m_pMeshBuilder)
m_pMeshBuilder->Release();
if (m_pMeshRack)
m_pMeshRack->Release();
if (m_pEye)
m_pEye->Release();
if (m_pRack)
m_pRack->Release();
if (m_pViewport)
m_pViewport->Release();
if (m_pParentFrame)
m_pParentFrame->Release();
if (m_pDevice)
m_pDevice->Release();
if (m_pD3D)
m_pD3D->Release();
if (m_pClipper)
m_pClipper->Release();
}
#ifdef _DEBUG
void CInputView::AssertValid() const
{
CFormView::AssertValid();
}
void CInputView::Dump(CDumpCont ext& dc) const
{
CFormView::Dump(dc);
}
#endif //_DEBUG
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CInputView message handlers
// InputView.cpp : implementation file
//
#include "stdafx.h"
#include "ws.h"
#include "InputView.h"
#include "simpvw.h"
#include "splitter.h"
#include <d3drm.h>
#include <ddraw.h>
#include <d3drmwin.h>
//#include <string.h>
//#include <stdio.h>
#include <iostream.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define GO_LEFT 0
#define GO_RIGHT 1
//////////////////////////
// CInputView
IMPLEMENT_DYNCREATE(CInput
BEGIN_MESSAGE_MAP(CInputVi
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_DESTROY()
END_MESSAGE_MAP()
int CInputView::OnCreate(LPCRE
{
// create Direct3D object
HRESULT result = Direct3DRMCreate(&m_pD3D);
if (result != D3DRM_OK)
return 0;
result = DirectDrawCreateClipper(0,
if (result != DD_OK)
return 0;
result = m_pClipper->SetHWnd(0, m_hWnd);
if (result != DD_OK)
return 0;
RECT rect;
::GetClientRect(m_hWnd, &rect);
// result = m_pD3D->CreateDeviceFromCl
// NULL, rect.right, rect.bottom, &m_pDevice);
result = m_pD3D->CreateDeviceFromCl
NULL, rect.right, rect.bottom, &m_pDevice);
if (result != D3DRM_OK)
return 0;
m_pDevice->SetQuality(D3DR
//create parent frame
m_pD3D->CreateFrame(0, &m_pParentFrame);
if (result != D3DRM_OK)
return 0;
BOOL succeeded = MakeScene();
if (!succeeded)
return 0;
m_D3DOK = TRUE;
SetTimer(1, 100, NULL);
return 0;
}
CInputView::CInputView()
: CFormView(CInputView::IDD)
{
m_D3DOK = FALSE;
m_pD3D = NULL;
m_pDevice = NULL;
m_pViewport = NULL;
m_pParentFrame = NULL;
m_pEye = NULL;
m_pRack = NULL;
m_pMeshRack = NULL;
m_pMeshBuilder = NULL;
m_pClipper = NULL;
// Create(NULL, "Direct3DApp");
//{{AFX_DATA_INIT(CInputVi
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
CInputView::~CInputView()
{
}
void CInputView::DoDataExchange
{
CFormView::DoDataExchange(
//{{AFX_DATA_MAP(CInputVie
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
//////////////////////////
BOOL CInputView::MakeScene()
{
BOOL succeeded = CreateMesh();
if (!succeeded)
return FALSE;
succeeded = CreateViewport();
if (!succeeded)
return FALSE;
succeeded = CreateLight();
if (!succeeded)
return FALSE;
return TRUE;
}
//////////////////////////
//////////////////////////
BOOL CInputView::CreateMesh()
{
HRESULT result =
m_pD3D->CreateMeshBuilder(
result = m_pD3D->CreateMeshBuilder(
if (result != D3DRM_OK)
return FALSE;
result = m_pMeshBuilder->Load("mode
NULL, D3DRMLOAD_FROMFILE, NULL, NULL );
result = m_pMeshRack->Load("rack.x"
NULL, D3DRMLOAD_FROMFILE, NULL, NULL );
if (result != D3DRM_OK)
{
MessageBox("MODEL.X file missing. Please place\n\
a Direct3D X file with this name\nin the Direct3DApp folder.");
return FALSE;
}
LPDIRECT3DRMFRAME pFrame;
result = m_pD3D->CreateFrame(m_pPar
if (result != D3DRM_OK)
return FALSE;
pFrame->AddVisual(m_pMeshB
pFrame->AddVisual(m_pMeshR
// pFrame->SetRotation(m_pPar
// D3DVALUE(1.0), D3DVALUE(0.0), D3DVALUE(0.05));
pFrame->Release();
pFrame = NULL;
return TRUE;
} //////////////////////////
BOOL CInputView::CreateViewport
{
LPD3DVECTOR abc = new D3DVECTOR();
char hello[30];
HRESULT result = m_pD3D->CreateFrame(m_pPar
// result = m_pD3D->CreateFrame(m_pPar
if (result != D3DRM_OK)
AfxMessageBox ("Hi!");
// abc->x = 0.0;
// abc->y = 0.0;
// abc->z = 0.0;
// m_pRack->GetPosition(m_pPa
// sprintf (hello, "%f %f %f", abc->x,abc->y, abc->z);
// strcat (hello, '\0');
// strcpy (hello, "hello");
// AfxMessageBox (hello);
// z ,y (smaller the lower) ,x (smaller the lefter)
// 0.0 5.0 -50.0
m_pEye->SetPosition(m_pPar
D3DVALUE(5.0), D3DVALUE(-50.0));
// m_pRack->SetPosition(m_pPa
// D3DVALUE(10.0), D3DVALUE(-50.0));
result = m_pD3D->CreateViewport(m_p
0, 0, m_pDevice->GetWidth(),
m_pDevice->GetHeight(), &m_pViewport);
//result = m_pD3D->CreateViewport(m_p
// 0, 0, m_pDevice->GetWidth(),
// m_pDevice->GetHeight(), &m_pViewport);
// result = m_pD3D->CreateViewport(m_p
// 0, 0, 640,
// 480, &m_pViewport);
if (result != D3DRM_OK)
return FALSE;
return TRUE;
}
//////////////////////////
BOOL CInputView::CreateLight()
{
LPDIRECT3DRMLIGHT pLight;
HRESULT result = m_pD3D->
CreateLightRGB(D3DRMLIGHT_
D3DVALUE(1.0), D3DVALUE(1.0), D3DVALUE(1.0),
&pLight);
if (result != D3DRM_OK)
return FALSE;
LPDIRECT3DRMFRAME pLightFrame;
result = m_pD3D->CreateFrame(m_pPar
&pLightFrame );
if (result != D3DRM_OK)
return FALSE;
pLightFrame->AddLight(pLig
pLightFrame->SetOrientatio
D3DVALUE(0.0), D3DVALUE(-1.0), D3DVALUE(1.0),
D3DVALUE(0.0), D3DVALUE(1.0), D3DVALUE(0.0));
pLight->Release();
pLight = NULL;
pLightFrame->Release();
pLightFrame = NULL;
return TRUE;
}
//////////////////////////
void CInputView::OnPaint()
{
BOOL repaint = GetUpdateRect(NULL);
if (!repaint)
return;
// PAINTSTRUCT pstruct, sstruct;
PAINTSTRUCT pstruct;
BeginPaint(&pstruct);
// BeginPaint(&sstruct);
if (m_D3DOK)
{
LPDIRECT3DRMWINDEVICE pWndDevice;
m_pDevice->QueryInterface(
IID_IDirect3DRMWinDevice,
(void**)&pWndDevice);
pWndDevice->HandlePaint(ps
pWndDevice->Release();
// LPDIRECT3DRMWINDEVICE pWndDeviceRack;
// m_pDeviceRack->QueryInterf
// IID_IDirect3DRMWinDevice,
// (void**)&pWndDeviceRack);
// pWndDeviceRack->HandlePain
// pWndDeviceRack->Release();
}
else
::TextOut(pstruct.hdc, 20, 20,
"Direct3D failed to start.", 25);
EndPaint(&pstruct);
// EndPaint(&sstruct);
}
//////////////////////////
// CInputView diagnostics
void CInputView::OnTimer(UINT nIDEvent)
{
static float aPoint = 0;
static float aPoint2 = 0;
static float aPoint3 = 0;
// char abc[10];
static int count = 2;
// static int Direc = GO_LEFT;
if (m_D3DOK)
m_pD3D->Tick(D3DVALUE(1));
//x,y,z
if (count <= 10 && count >= 2) {
m_pMeshBuilder->Translate(
aPoint -= 0.2;
} else {
m_pMeshBuilder->Translate(
aPoint += 0.2;
}
count++;
if (count >= 100)
count = 2;
// sprintf (abc, "%f", aPoint);
// AfxMessageBox (abc);
/*if (aPoint <= -1.0) {
aPoint += 0.2;
return;
} else if (aPoint >= 1.0) {
aPoint -= 0.2;
return;
} else {
aPoint -= 0.2;
return;
}*/
/* if (aPoint <= 0 && aPoint >= -12.0 && Direc == GO_LEFT) {
if (aPoint == -10.0)
Direc = GO_RIGHT;
aPoint -= 0.2;
} else if (aPoint >= -12.2 && aPoint <= 0 && Direc == GO_RIGHT) {
aPoint += 0.2;
} */
}
void CInputView::OnDestroy()
{
if (m_pMeshBuilder)
m_pMeshBuilder->Release();
if (m_pMeshRack)
m_pMeshRack->Release();
if (m_pEye)
m_pEye->Release();
if (m_pRack)
m_pRack->Release();
if (m_pViewport)
m_pViewport->Release();
if (m_pParentFrame)
m_pParentFrame->Release();
if (m_pDevice)
m_pDevice->Release();
if (m_pD3D)
m_pD3D->Release();
if (m_pClipper)
m_pClipper->Release();
}
#ifdef _DEBUG
void CInputView::AssertValid() const
{
CFormView::AssertValid();
}
void CInputView::Dump(CDumpCont
{
CFormView::Dump(dc);
}
#endif //_DEBUG
//////////////////////////
// CInputView message handlers
ASKER
what about this? 2 objects easier?
// InputView.cpp : implementation file
//
#include "stdafx.h"
#include "ws.h"
#include "InputView.h"
#include "simpvw.h"
#include "splitter.h"
#include <d3drm.h>
#include <ddraw.h>
#include <d3drmwin.h>
//#include <string.h>
//#include <stdio.h>
#include <iostream.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define GO_LEFT 0
#define GO_RIGHT 1
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CInputView
IMPLEMENT_DYNCREATE(CInput View, CFormView)
BEGIN_MESSAGE_MAP(CInputVi ew, CFormView)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_DESTROY()
END_MESSAGE_MAP()
int CInputView::OnCreate(LPCRE ATESTRUCT)
{
// create Direct3D object
HRESULT result = Direct3DRMCreate(&m_pD3D);
if (result != D3DRM_OK)
return 0;
result = DirectDrawCreateClipper(0, &m_pClipper, 0);
if (result != DD_OK)
return 0;
result = m_pClipper->SetHWnd(0, m_hWnd);
if (result != DD_OK)
return 0;
RECT rect;
::GetClientRect(m_hWnd, &rect);
// result = m_pD3D->CreateDeviceFromCl ipper(m_pC lipper,
// NULL, rect.right, rect.bottom, &m_pDevice);
result = m_pD3D->CreateDeviceFromCl ipper(m_pC lipper,
NULL, rect.right, rect.bottom, &m_pDevice);
if (result != D3DRM_OK)
return 0;
m_pDevice->SetQuality(D3DR MRENDER_GO URAUD);
//create parent frame
m_pD3D->CreateFrame(0, &m_pParentFrame);
if (result != D3DRM_OK)
return 0;
BOOL succeeded = MakeScene();
if (!succeeded)
return 0;
m_D3DOK = TRUE;
SetTimer(1, 100, NULL);
return 0;
}
CInputView::CInputView()
: CFormView(CInputView::IDD)
{
m_D3DOK = FALSE;
m_pD3D = NULL;
m_pDevice = NULL;
m_pViewport = NULL;
m_pParentFrame = NULL;
m_pEye = NULL;
m_pRack = NULL;
m_pMeshRack = NULL;
m_pMeshBuilder = NULL;
m_pClipper = NULL;
// Create(NULL, "Direct3DApp");
//{{AFX_DATA_INIT(CInputVi ew)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
CInputView::~CInputView()
{
}
void CInputView::DoDataExchange (CDataExch ange* pDX)
{
CFormView::DoDataExchange( pDX);
//{{AFX_DATA_MAP(CInputVie w)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
////////////////////////// ////////// //// ok
BOOL CInputView::MakeScene()
{
BOOL succeeded = CreateMesh();
if (!succeeded)
return FALSE;
succeeded = CreateViewport();
if (!succeeded)
return FALSE;
succeeded = CreateLight();
if (!succeeded)
return FALSE;
return TRUE;
}
////////////////////////// ////////// /// ok
////////////////////////// ////////// /// ok
BOOL CInputView::CreateMesh()
{
HRESULT result =
m_pD3D->CreateMeshBuilder( &m_pMeshBu ilder);
result = m_pD3D->CreateMeshBuilder( &m_pMeshRa ck);
if (result != D3DRM_OK)
return FALSE;
result = m_pMeshBuilder->Load("mode l.x",
NULL, D3DRMLOAD_FROMFILE, NULL, NULL );
result = m_pMeshRack->Load("rack.x" ,
NULL, D3DRMLOAD_FROMFILE, NULL, NULL );
if (result != D3DRM_OK)
{
MessageBox("MODEL.X file missing. Please place\n\
a Direct3D X file with this name\nin the Direct3DApp folder.");
return FALSE;
}
LPDIRECT3DRMFRAME pFrame;
result = m_pD3D->CreateFrame(m_pPar entFrame, &pFrame);
if (result != D3DRM_OK)
return FALSE;
pFrame->AddVisual(m_pMeshB uilder);
pFrame->AddVisual(m_pMeshR ack);
// pFrame->SetRotation(m_pPar entFrame, D3DVALUE(0.0),
// D3DVALUE(1.0), D3DVALUE(0.0), D3DVALUE(0.05));
pFrame->Release();
pFrame = NULL;
return TRUE;
} ////////////////////////// ///// ok
BOOL CInputView::CreateViewport ()
{
LPD3DVECTOR abc = new D3DVECTOR();
char hello[30];
HRESULT result = m_pD3D->CreateFrame(m_pPar entFrame, &m_pEye);
// result = m_pD3D->CreateFrame(m_pPar entFrame,& m_pRack);
if (result != D3DRM_OK)
AfxMessageBox ("Hi!");
// abc->x = 0.0;
// abc->y = 0.0;
// abc->z = 0.0;
// m_pRack->GetPosition(m_pPa rentFrame, LPD3DVECTOR(abc));
// sprintf (hello, "%f %f %f", abc->x,abc->y, abc->z);
// strcat (hello, '\0');
// strcpy (hello, "hello");
// AfxMessageBox (hello);
// z ,y (smaller the lower) ,x (smaller the lefter)
// 0.0 5.0 -50.0
m_pEye->SetPosition(m_pPar entFrame, D3DVALUE(0.0),
D3DVALUE(5.0), D3DVALUE(-50.0));
// m_pRack->SetPosition(m_pPa rentFrame, D3DVALUE(0.0),
// D3DVALUE(10.0), D3DVALUE(-50.0));
result = m_pD3D->CreateViewport(m_p Device, m_pEye,
0, 0, m_pDevice->GetWidth(),
m_pDevice->GetHeight(), &m_pViewport);
//result = m_pD3D->CreateViewport(m_p Device, m_pRack,
// 0, 0, m_pDevice->GetWidth(),
// m_pDevice->GetHeight(), &m_pViewport);
// result = m_pD3D->CreateViewport(m_p Device, m_pEye,
// 0, 0, 640,
// 480, &m_pViewport);
if (result != D3DRM_OK)
return FALSE;
return TRUE;
}
////////////////////////// ////////// //// ok
BOOL CInputView::CreateLight()
{
LPDIRECT3DRMLIGHT pLight;
HRESULT result = m_pD3D->
CreateLightRGB(D3DRMLIGHT_ DIRECTIONA L,
D3DVALUE(1.0), D3DVALUE(1.0), D3DVALUE(1.0),
&pLight);
if (result != D3DRM_OK)
return FALSE;
LPDIRECT3DRMFRAME pLightFrame;
result = m_pD3D->CreateFrame(m_pPar entFrame,
&pLightFrame );
if (result != D3DRM_OK)
return FALSE;
pLightFrame->AddLight(pLig ht);
pLightFrame->SetOrientatio n(m_pParen tFrame,
D3DVALUE(0.0), D3DVALUE(-1.0), D3DVALUE(1.0),
D3DVALUE(0.0), D3DVALUE(1.0), D3DVALUE(0.0));
pLight->Release();
pLight = NULL;
pLightFrame->Release();
pLightFrame = NULL;
return TRUE;
}
////////////////////////// ////////// /// ok
void CInputView::OnPaint()
{
BOOL repaint = GetUpdateRect(NULL);
if (!repaint)
return;
// PAINTSTRUCT pstruct, sstruct;
PAINTSTRUCT pstruct;
BeginPaint(&pstruct);
// BeginPaint(&sstruct);
if (m_D3DOK)
{
LPDIRECT3DRMWINDEVICE pWndDevice;
m_pDevice->QueryInterface(
IID_IDirect3DRMWinDevice,
(void**)&pWndDevice);
pWndDevice->HandlePaint(ps truct.hdc) ;
pWndDevice->Release();
// LPDIRECT3DRMWINDEVICE pWndDeviceRack;
// m_pDeviceRack->QueryInterf ace(
// IID_IDirect3DRMWinDevice,
// (void**)&pWndDeviceRack);
// pWndDeviceRack->HandlePain t(sstruct. hdc);
// pWndDeviceRack->Release();
}
else
::TextOut(pstruct.hdc, 20, 20,
"Direct3D failed to start.", 25);
EndPaint(&pstruct);
// EndPaint(&sstruct);
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CInputView diagnostics
void CInputView::OnTimer(UINT nIDEvent)
{
static float aPoint = 0;
static float aPoint2 = 0;
static float aPoint3 = 0;
// char abc[10];
static int count = 2;
// static int Direc = GO_LEFT;
if (m_D3DOK)
m_pD3D->Tick(D3DVALUE(1));
//x,y,z
if (count <= 10 && count >= 2) {
m_pMeshBuilder->Translate( aPoint,aPo int,0);
aPoint -= 0.2;
} else {
m_pMeshBuilder->Translate( aPoint,0, 0);
aPoint += 0.2;
}
count++;
if (count >= 100)
count = 2;
// sprintf (abc, "%f", aPoint);
// AfxMessageBox (abc);
/*if (aPoint <= -1.0) {
aPoint += 0.2;
return;
} else if (aPoint >= 1.0) {
aPoint -= 0.2;
return;
} else {
aPoint -= 0.2;
return;
}*/
/* if (aPoint <= 0 && aPoint >= -12.0 && Direc == GO_LEFT) {
if (aPoint == -10.0)
Direc = GO_RIGHT;
aPoint -= 0.2;
} else if (aPoint >= -12.2 && aPoint <= 0 && Direc == GO_RIGHT) {
aPoint += 0.2;
} */
}
void CInputView::OnDestroy()
{
if (m_pMeshBuilder)
m_pMeshBuilder->Release();
if (m_pMeshRack)
m_pMeshRack->Release();
if (m_pEye)
m_pEye->Release();
if (m_pRack)
m_pRack->Release();
if (m_pViewport)
m_pViewport->Release();
if (m_pParentFrame)
m_pParentFrame->Release();
if (m_pDevice)
m_pDevice->Release();
if (m_pD3D)
m_pD3D->Release();
if (m_pClipper)
m_pClipper->Release();
}
#ifdef _DEBUG
void CInputView::AssertValid() const
{
CFormView::AssertValid();
}
void CInputView::Dump(CDumpCont ext& dc) const
{
CFormView::Dump(dc);
}
#endif //_DEBUG
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CInputView message handlers
// InputView.cpp : implementation file
//
#include "stdafx.h"
#include "ws.h"
#include "InputView.h"
#include "simpvw.h"
#include "splitter.h"
#include <d3drm.h>
#include <ddraw.h>
#include <d3drmwin.h>
//#include <string.h>
//#include <stdio.h>
#include <iostream.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define GO_LEFT 0
#define GO_RIGHT 1
//////////////////////////
// CInputView
IMPLEMENT_DYNCREATE(CInput
BEGIN_MESSAGE_MAP(CInputVi
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_DESTROY()
END_MESSAGE_MAP()
int CInputView::OnCreate(LPCRE
{
// create Direct3D object
HRESULT result = Direct3DRMCreate(&m_pD3D);
if (result != D3DRM_OK)
return 0;
result = DirectDrawCreateClipper(0,
if (result != DD_OK)
return 0;
result = m_pClipper->SetHWnd(0, m_hWnd);
if (result != DD_OK)
return 0;
RECT rect;
::GetClientRect(m_hWnd, &rect);
// result = m_pD3D->CreateDeviceFromCl
// NULL, rect.right, rect.bottom, &m_pDevice);
result = m_pD3D->CreateDeviceFromCl
NULL, rect.right, rect.bottom, &m_pDevice);
if (result != D3DRM_OK)
return 0;
m_pDevice->SetQuality(D3DR
//create parent frame
m_pD3D->CreateFrame(0, &m_pParentFrame);
if (result != D3DRM_OK)
return 0;
BOOL succeeded = MakeScene();
if (!succeeded)
return 0;
m_D3DOK = TRUE;
SetTimer(1, 100, NULL);
return 0;
}
CInputView::CInputView()
: CFormView(CInputView::IDD)
{
m_D3DOK = FALSE;
m_pD3D = NULL;
m_pDevice = NULL;
m_pViewport = NULL;
m_pParentFrame = NULL;
m_pEye = NULL;
m_pRack = NULL;
m_pMeshRack = NULL;
m_pMeshBuilder = NULL;
m_pClipper = NULL;
// Create(NULL, "Direct3DApp");
//{{AFX_DATA_INIT(CInputVi
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
CInputView::~CInputView()
{
}
void CInputView::DoDataExchange
{
CFormView::DoDataExchange(
//{{AFX_DATA_MAP(CInputVie
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
//////////////////////////
BOOL CInputView::MakeScene()
{
BOOL succeeded = CreateMesh();
if (!succeeded)
return FALSE;
succeeded = CreateViewport();
if (!succeeded)
return FALSE;
succeeded = CreateLight();
if (!succeeded)
return FALSE;
return TRUE;
}
//////////////////////////
//////////////////////////
BOOL CInputView::CreateMesh()
{
HRESULT result =
m_pD3D->CreateMeshBuilder(
result = m_pD3D->CreateMeshBuilder(
if (result != D3DRM_OK)
return FALSE;
result = m_pMeshBuilder->Load("mode
NULL, D3DRMLOAD_FROMFILE, NULL, NULL );
result = m_pMeshRack->Load("rack.x"
NULL, D3DRMLOAD_FROMFILE, NULL, NULL );
if (result != D3DRM_OK)
{
MessageBox("MODEL.X file missing. Please place\n\
a Direct3D X file with this name\nin the Direct3DApp folder.");
return FALSE;
}
LPDIRECT3DRMFRAME pFrame;
result = m_pD3D->CreateFrame(m_pPar
if (result != D3DRM_OK)
return FALSE;
pFrame->AddVisual(m_pMeshB
pFrame->AddVisual(m_pMeshR
// pFrame->SetRotation(m_pPar
// D3DVALUE(1.0), D3DVALUE(0.0), D3DVALUE(0.05));
pFrame->Release();
pFrame = NULL;
return TRUE;
} //////////////////////////
BOOL CInputView::CreateViewport
{
LPD3DVECTOR abc = new D3DVECTOR();
char hello[30];
HRESULT result = m_pD3D->CreateFrame(m_pPar
// result = m_pD3D->CreateFrame(m_pPar
if (result != D3DRM_OK)
AfxMessageBox ("Hi!");
// abc->x = 0.0;
// abc->y = 0.0;
// abc->z = 0.0;
// m_pRack->GetPosition(m_pPa
// sprintf (hello, "%f %f %f", abc->x,abc->y, abc->z);
// strcat (hello, '\0');
// strcpy (hello, "hello");
// AfxMessageBox (hello);
// z ,y (smaller the lower) ,x (smaller the lefter)
// 0.0 5.0 -50.0
m_pEye->SetPosition(m_pPar
D3DVALUE(5.0), D3DVALUE(-50.0));
// m_pRack->SetPosition(m_pPa
// D3DVALUE(10.0), D3DVALUE(-50.0));
result = m_pD3D->CreateViewport(m_p
0, 0, m_pDevice->GetWidth(),
m_pDevice->GetHeight(), &m_pViewport);
//result = m_pD3D->CreateViewport(m_p
// 0, 0, m_pDevice->GetWidth(),
// m_pDevice->GetHeight(), &m_pViewport);
// result = m_pD3D->CreateViewport(m_p
// 0, 0, 640,
// 480, &m_pViewport);
if (result != D3DRM_OK)
return FALSE;
return TRUE;
}
//////////////////////////
BOOL CInputView::CreateLight()
{
LPDIRECT3DRMLIGHT pLight;
HRESULT result = m_pD3D->
CreateLightRGB(D3DRMLIGHT_
D3DVALUE(1.0), D3DVALUE(1.0), D3DVALUE(1.0),
&pLight);
if (result != D3DRM_OK)
return FALSE;
LPDIRECT3DRMFRAME pLightFrame;
result = m_pD3D->CreateFrame(m_pPar
&pLightFrame );
if (result != D3DRM_OK)
return FALSE;
pLightFrame->AddLight(pLig
pLightFrame->SetOrientatio
D3DVALUE(0.0), D3DVALUE(-1.0), D3DVALUE(1.0),
D3DVALUE(0.0), D3DVALUE(1.0), D3DVALUE(0.0));
pLight->Release();
pLight = NULL;
pLightFrame->Release();
pLightFrame = NULL;
return TRUE;
}
//////////////////////////
void CInputView::OnPaint()
{
BOOL repaint = GetUpdateRect(NULL);
if (!repaint)
return;
// PAINTSTRUCT pstruct, sstruct;
PAINTSTRUCT pstruct;
BeginPaint(&pstruct);
// BeginPaint(&sstruct);
if (m_D3DOK)
{
LPDIRECT3DRMWINDEVICE pWndDevice;
m_pDevice->QueryInterface(
IID_IDirect3DRMWinDevice,
(void**)&pWndDevice);
pWndDevice->HandlePaint(ps
pWndDevice->Release();
// LPDIRECT3DRMWINDEVICE pWndDeviceRack;
// m_pDeviceRack->QueryInterf
// IID_IDirect3DRMWinDevice,
// (void**)&pWndDeviceRack);
// pWndDeviceRack->HandlePain
// pWndDeviceRack->Release();
}
else
::TextOut(pstruct.hdc, 20, 20,
"Direct3D failed to start.", 25);
EndPaint(&pstruct);
// EndPaint(&sstruct);
}
//////////////////////////
// CInputView diagnostics
void CInputView::OnTimer(UINT nIDEvent)
{
static float aPoint = 0;
static float aPoint2 = 0;
static float aPoint3 = 0;
// char abc[10];
static int count = 2;
// static int Direc = GO_LEFT;
if (m_D3DOK)
m_pD3D->Tick(D3DVALUE(1));
//x,y,z
if (count <= 10 && count >= 2) {
m_pMeshBuilder->Translate(
aPoint -= 0.2;
} else {
m_pMeshBuilder->Translate(
aPoint += 0.2;
}
count++;
if (count >= 100)
count = 2;
// sprintf (abc, "%f", aPoint);
// AfxMessageBox (abc);
/*if (aPoint <= -1.0) {
aPoint += 0.2;
return;
} else if (aPoint >= 1.0) {
aPoint -= 0.2;
return;
} else {
aPoint -= 0.2;
return;
}*/
/* if (aPoint <= 0 && aPoint >= -12.0 && Direc == GO_LEFT) {
if (aPoint == -10.0)
Direc = GO_RIGHT;
aPoint -= 0.2;
} else if (aPoint >= -12.2 && aPoint <= 0 && Direc == GO_RIGHT) {
aPoint += 0.2;
} */
}
void CInputView::OnDestroy()
{
if (m_pMeshBuilder)
m_pMeshBuilder->Release();
if (m_pMeshRack)
m_pMeshRack->Release();
if (m_pEye)
m_pEye->Release();
if (m_pRack)
m_pRack->Release();
if (m_pViewport)
m_pViewport->Release();
if (m_pParentFrame)
m_pParentFrame->Release();
if (m_pDevice)
m_pDevice->Release();
if (m_pD3D)
m_pD3D->Release();
if (m_pClipper)
m_pClipper->Release();
}
#ifdef _DEBUG
void CInputView::AssertValid() const
{
CFormView::AssertValid();
}
void CInputView::Dump(CDumpCont
{
CFormView::Dump(dc);
}
#endif //_DEBUG
//////////////////////////
// CInputView message handlers
ASKER
what is the counterpart of setRenderstate of the IDDirect3Ddevice8 under retained mode?
ASKER
Sorry, Do I look like a trouble? Really sorry, because I read all the related materials... they all did not make sense to me! hope you can understand me...
ASKER
Sorry, Do I look like a trouble? Really sorry, because I read all the related materials... they all did not make sense to me! hope you can understand me...
Please don't post all the code again just your changes.
ASKER
Could you pls help? I'm in danger now...
I hope you have solved it allready. But here is some simple code.
/Joachim
#ifndef APP_H
#define APP_H
#include <tchar.h>
#include <stdio.h>
#include <windows.h>
#include <d3dx8.h>
#include "d3dapp.h"
#include "d3dfont.h"
#include "d3dfile.h"
#include "D3DUtil.h"
#include "animator.h"
class App : public CD3DApplication
{
private:
CD3DMesh ourMeshes[10];
CD3DFont* ourFont;
char timeText[10];
Xanimator* ourAnimator;
public:
static float time;
HRESULT OneTimeSceneInit();
HRESULT InitDeviceObjects();
HRESULT RestoreDeviceObjects();
HRESULT InvalidateDeviceObjects();
HRESULT DeleteDeviceObjects();
HRESULT Render();
HRESULT FrameMove();
HRESULT FinalCleanup();
LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
App();
};
#endif
//Very minimal setup for rendering several objects
#include <stdafx.h>
#include "App.h"
#include <windows.h>
#include "dxutil.h"
#include <d3dx8.h>
float App::time;
//Set flexible vertex format to use one texture and diffuse color
const int FVF = (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
App d3dApp;
if( FAILED( d3dApp.Create( hInst ) ) )
return 0;
return d3dApp.Run();
}
//Constructor
App::App()
{
//Windowed and in 640 * 480
m_bWindowed = true;
m_dwCreationHeight = 480;
m_dwCreationWidth = 640;
//A title
m_strWindowTitle = _T("Engine window");
//Enable Z-buffer
m_bUseDepthBuffer = true;
}
//Overidden function not doing anything
HRESULT App::OneTimeSceneInit()
{
return S_OK;
}
//Init the device objects not doing anything right now
HRESULT App::InitDeviceObjects()
{
ourFont = new CD3DFont(_T("arial"), 16);
ourFont->InitDeviceObjects (m_pd3dDev ice);
return S_OK;
}
//If the device is lost we need to restore everything
HRESULT App::RestoreDeviceObjects( )
{
ourFont->RestoreDeviceObje cts();
// Setup render state
m_pd3dDevice->SetRenderSta te( D3DRS_LIGHTING, TRUE );
m_pd3dDevice->SetRenderSta te( D3DRS_DITHERENABLE, TRUE );
m_pd3dDevice->SetRenderSta te( D3DRS_ZENABLE, TRUE );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureSt ageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
// Setup the light
D3DLIGHT8 light;
light.Type = D3DLIGHT_DIRECTIONAL;
light.Diffuse.r = light.Diffuse.g = light.Diffuse.b = 1.0f;
light.Specular.r = light.Specular.g = light.Specular.b = 0.0f;
light.Ambient.r = light.Ambient.g = light.Ambient.b = 0.3f;
light.Position = D3DXVECTOR3( 0.0f, 5.0f, -10.0f );
D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direc tion, &D3DXVECTOR3( 0.0f, 0.0f, 0.0f ) );
light.Attenuation0 = light.Attenuation1 = light.Attenuation2 = 0.0f;
light.Range = sqrtf(FLT_MAX);
m_pd3dDevice->SetLight(0, &light );
m_pd3dDevice->LightEnable( 0, TRUE );
FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.H eight;
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH ( &matProj, D3DX_PI/4, fAspect, 1.0f, 200.0f );
//D3DXMatrixPerspectiveFov LH( &matProj, D3DX_PI/4, fAspect, m_fObjectRadius/64.0f,
// m_fObjectRadius*200.0f);
m_pd3dDevice->SetTransform ( D3DTS_PROJECTION, &matProj );
DXUtil_Timer(TIMER_START);
//Do as many as you want here
ourMeshes[0].Create(m_pd3d Device, "models/olsen3.x", "models/textures/");
ourMeshes[0].SetFVF(m_pd3d Device, FVF);
ourMeshes[0].m_pLocalMesh = ourMeshes[0].m_pSysMemMesh ;
ourMeshes[0].m_pLocalMesh- >AddRef();
ourAnimator = new Xanimator(m_pd3dDevice);
ourAnimator->LoadXAnimatio n("female_ customer_w alk");
return S_OK;
}
HRESULT App::InvalidateDeviceObjec ts()
{
return S_OK;
}
HRESULT App::DeleteDeviceObjects()
{
//delete[] ourMeshes;
return S_OK;
}
HRESULT App::Render()
{
D3DXMATRIX worldMatrix;
//Clear the rendering device with blue color
m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_Z BUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
//Start rendering
m_pd3dDevice->BeginScene() ;
//Set first object to (0,0,0)
D3DXMatrixIdentity(&worldM atrix);
m_pd3dDevice->SetTransform (D3DTS_WOR LD, &worldMatrix);
ourMeshes[0].Render(m_pd3d Device); //Render object 0
//Move the world matrix 2.0f in x
worldMatrix._41 = 2.0f;
m_pd3dDevice->SetTransform (D3DTS_WOR LD, &worldMatrix);
ourMeshes[1].Render(m_pd3d Device); //Render object 1
D3DXMatrixRotationYawPitch Roll(&worl dMatrix, 0.0f, 1.6f, -0.0f);
worldMatrix._41 = -2.0f;
m_pd3dDevice->SetTransform (D3DTS_WOR LD, &worldMatrix);
ourAnimator->Render();
//Write something on the screen
ourFont->DrawText(16,16, 0xffffff00, "Using directional light");
ourFont->DrawText(16,32, 0xffffff00, timeText);
m_pd3dDevice->EndScene();
return S_OK;
}
HRESULT App::FrameMove()
{
D3DXMATRIX viewMatrix;
D3DXVECTOR3 cameraPlace, lookAt, vUp;
//Set the camera up and a little back we asume that we are rendering in z+
cameraPlace = D3DXVECTOR3 (0.0f, 5.0f, -10.0f);
lookAt = D3DXVECTOR3 (0.0f, 0.0f, 0.0f); //look at the center of the world
vUp = D3DXVECTOR3 (0.0f, 1.0f, 0.0f); //up is y
//Set the view matrix according to the above vectors
D3DXMatrixLookAtLH(&viewMa trix, &cameraPlace, &lookAt, &vUp);
m_pd3dDevice->SetTransform (D3DTS_VIE W, &viewMatrix);
//Check the FPS
time = DXUtil_Timer( TIMER_GETELAPSEDTIME);
time = time /1000.0f;
int dec, sign;
strcpy(timeText, "FPS: ");
strcat(timeText, fcvt(time, 10, &dec, &sign));
return S_OK;
}
HRESULT App::FinalCleanup()
{
//Delete font
ourFont->DeleteDeviceObjec ts();
delete ourFont;
delete ourAnimator;
return S_OK;
}
LRESULT App::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch (uMsg)
{
case WM_KEYDOWN:
if (wParam == VK_ESCAPE) //Check for escape key for exit
PostQuitMessage(0);
break;
}
//Return the stuff I don't process to parent class
return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}
/Joachim
#ifndef APP_H
#define APP_H
#include <tchar.h>
#include <stdio.h>
#include <windows.h>
#include <d3dx8.h>
#include "d3dapp.h"
#include "d3dfont.h"
#include "d3dfile.h"
#include "D3DUtil.h"
#include "animator.h"
class App : public CD3DApplication
{
private:
CD3DMesh ourMeshes[10];
CD3DFont* ourFont;
char timeText[10];
Xanimator* ourAnimator;
public:
static float time;
HRESULT OneTimeSceneInit();
HRESULT InitDeviceObjects();
HRESULT RestoreDeviceObjects();
HRESULT InvalidateDeviceObjects();
HRESULT DeleteDeviceObjects();
HRESULT Render();
HRESULT FrameMove();
HRESULT FinalCleanup();
LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
App();
};
#endif
//Very minimal setup for rendering several objects
#include <stdafx.h>
#include "App.h"
#include <windows.h>
#include "dxutil.h"
#include <d3dx8.h>
float App::time;
//Set flexible vertex format to use one texture and diffuse color
const int FVF = (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
App d3dApp;
if( FAILED( d3dApp.Create( hInst ) ) )
return 0;
return d3dApp.Run();
}
//Constructor
App::App()
{
//Windowed and in 640 * 480
m_bWindowed = true;
m_dwCreationHeight = 480;
m_dwCreationWidth = 640;
//A title
m_strWindowTitle = _T("Engine window");
//Enable Z-buffer
m_bUseDepthBuffer = true;
}
//Overidden function not doing anything
HRESULT App::OneTimeSceneInit()
{
return S_OK;
}
//Init the device objects not doing anything right now
HRESULT App::InitDeviceObjects()
{
ourFont = new CD3DFont(_T("arial"), 16);
ourFont->InitDeviceObjects
return S_OK;
}
//If the device is lost we need to restore everything
HRESULT App::RestoreDeviceObjects(
{
ourFont->RestoreDeviceObje
// Setup render state
m_pd3dDevice->SetRenderSta
m_pd3dDevice->SetRenderSta
m_pd3dDevice->SetRenderSta
m_pd3dDevice->SetTextureSt
m_pd3dDevice->SetTextureSt
// Setup the light
D3DLIGHT8 light;
light.Type = D3DLIGHT_DIRECTIONAL;
light.Diffuse.r = light.Diffuse.g = light.Diffuse.b = 1.0f;
light.Specular.r = light.Specular.g = light.Specular.b = 0.0f;
light.Ambient.r = light.Ambient.g = light.Ambient.b = 0.3f;
light.Position = D3DXVECTOR3( 0.0f, 5.0f, -10.0f );
D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direc
light.Attenuation0 = light.Attenuation1 = light.Attenuation2 = 0.0f;
light.Range = sqrtf(FLT_MAX);
m_pd3dDevice->SetLight(0, &light );
m_pd3dDevice->LightEnable(
FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.H
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH
//D3DXMatrixPerspectiveFov
// m_fObjectRadius*200.0f);
m_pd3dDevice->SetTransform
DXUtil_Timer(TIMER_START);
//Do as many as you want here
ourMeshes[0].Create(m_pd3d
ourMeshes[0].SetFVF(m_pd3d
ourMeshes[0].m_pLocalMesh = ourMeshes[0].m_pSysMemMesh
ourMeshes[0].m_pLocalMesh-
ourAnimator = new Xanimator(m_pd3dDevice);
ourAnimator->LoadXAnimatio
return S_OK;
}
HRESULT App::InvalidateDeviceObjec
{
return S_OK;
}
HRESULT App::DeleteDeviceObjects()
{
//delete[] ourMeshes;
return S_OK;
}
HRESULT App::Render()
{
D3DXMATRIX worldMatrix;
//Clear the rendering device with blue color
m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_Z
//Start rendering
m_pd3dDevice->BeginScene()
//Set first object to (0,0,0)
D3DXMatrixIdentity(&worldM
m_pd3dDevice->SetTransform
ourMeshes[0].Render(m_pd3d
//Move the world matrix 2.0f in x
worldMatrix._41 = 2.0f;
m_pd3dDevice->SetTransform
ourMeshes[1].Render(m_pd3d
D3DXMatrixRotationYawPitch
worldMatrix._41 = -2.0f;
m_pd3dDevice->SetTransform
ourAnimator->Render();
//Write something on the screen
ourFont->DrawText(16,16, 0xffffff00, "Using directional light");
ourFont->DrawText(16,32, 0xffffff00, timeText);
m_pd3dDevice->EndScene();
return S_OK;
}
HRESULT App::FrameMove()
{
D3DXMATRIX viewMatrix;
D3DXVECTOR3 cameraPlace, lookAt, vUp;
//Set the camera up and a little back we asume that we are rendering in z+
cameraPlace = D3DXVECTOR3 (0.0f, 5.0f, -10.0f);
lookAt = D3DXVECTOR3 (0.0f, 0.0f, 0.0f); //look at the center of the world
vUp = D3DXVECTOR3 (0.0f, 1.0f, 0.0f); //up is y
//Set the view matrix according to the above vectors
D3DXMatrixLookAtLH(&viewMa
m_pd3dDevice->SetTransform
//Check the FPS
time = DXUtil_Timer( TIMER_GETELAPSEDTIME);
time = time /1000.0f;
int dec, sign;
strcpy(timeText, "FPS: ");
strcat(timeText, fcvt(time, 10, &dec, &sign));
return S_OK;
}
HRESULT App::FinalCleanup()
{
//Delete font
ourFont->DeleteDeviceObjec
delete ourFont;
delete ourAnimator;
return S_OK;
}
LRESULT App::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch (uMsg)
{
case WM_KEYDOWN:
if (wParam == VK_ESCAPE) //Check for escape key for exit
PostQuitMessage(0);
break;
}
//Return the stuff I don't process to parent class
return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. I have switched to VB.... :)
Bye
Bye
It's not a great idea to change to VB if this is the only reason :)
ASKER