Solved

Multiple Meshes/Sprites

Posted on 2002-05-26
44
435 Views
Last Modified: 2013-12-26
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::CMyD3DApplication()
{
      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_bShowCursorWhenFullscreen = TRUE;

      m_dwNumSegs = 1;
      _tcscpy (m_strInitialDir, DXUtil_GetDXSDKMediaPath() );
      _tcscpy (m_strMeshFilename, _T("car.x"));


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

}

HRESULT CMyD3DApplication::InitDeviceObjects()
{
      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->GetVertexBuffer (&pVB);
      pVB->Lock (0,0,&pVertices,0);

      D3DXComputeBoundingSphere (pVertices, m_pMeshSysMem->GetNumVertices(),
                                             m_pMeshSysMem->GetFVF(), &m_vObjectCenter,
                                             &m_fObjectRadius);

      pVB->Unlock();
      SAFE_RELEASE(pVB);

      m_pMaterials = (D3DXMATERIAL*) m_pbufMaterials->GetBufferPointer();
      m_ppTextures = new LPDIRECT3DTEXTURE8[m_dwNumMaterials];


      for (UINT i=0; i < m_dwNumMaterials; i++)
      {
            TCHAR strTexturePath[512] = _T("");
            DXUtil_FindMediaFile (strTexturePath, m_pMaterials[i].pTextureFilename);
            D3DXCreateTextureFromFile(m_pd3dDevice, strTexturePath, &m_ppTextures[i]);
      }

      if (! (m_pMeshSysMem->GetFVF() & D3DFVF_NORMAL ))
      {
            m_pMeshSysMem->CloneMeshFVF (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::OneTimeSceneInit()
{
      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_pd3dDevice);
      m_pRack->Render(m_pd3dDevice);
      m_pPallet->Render(m_pd3dDevice);

      ////////////////// added
      for (UINT i = 0; i < m_dwNumMaterials; i++) {
            m_pd3dDevice->SetMaterial(&m_pMaterials[i].MatD3D);
            m_pd3dDevice->SetTexture (0, m_ppTextures[i]);
            m_pd3dDevice->SetRenderState (D3DRS_ZFUNC, D3DCMP_ALWAYS);
            m_pMeshEnhanced->DrawSubset (i);
            m_pd3dDevice->SetRenderState (D3DRS_ZFUNC, D3DCMP_LESSEQUAL);

      }

      
      m_pd3dDevice->EndScene();

      return S_OK;
}

HRESULT CMyD3DApplication::FrameMove()
{
      D3DXMATRIX matWorld;

      // view for XMesh
      D3DXMatrixTranslation (&matWorld, -m_vObjectCenter.x,
                                                        -m_vObjectCenter.y,
                                                        -m_vObjectCenter.z);
      D3DXMatrixMultiply (&matWorld, &matWorld, m_ArcBall.GetRotationMatrix());
      D3DXMatrixMultiply (&matWorld, &matWorld, m_ArcBall.GetTranslationMatrix());
                                    


      m_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
      D3DXMATRIX matView;
      D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3 (0,0,-2*m_fObjectRadius),
                                              &D3DXVECTOR3 (0, 0, 0),
                                                &D3DXVECTOR3 (0, 1, 0) );
      m_pd3dDevice->SetTransform(D3DTS_VIEW, &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::GenerateEnhancedMesh(UINT dwNewNumSegs)
{
 LPD3DXMESH pMeshTemp;
 LPD3DXMESH pMeshEnhancedSysMem = NULL;
 HRESULT hr;

////////////////// temp cancelled
// m_pMeshSysMem->CloneMeshFVF (D3DXMESH_WRITEONLY | D3DXMESH_NPATCHES |
//          (m_pMeshSysMem->GetOptions() & D3DXMESH_32BIT),
//             m_pMeshSysMem->GetFVF(), m_pd3dDevice, &pMeshTemp );

  hr = D3DXTessellateNPatches( m_pMeshSysMem, (DWORD*)m_pbufAdjacency->GetBufferPointer(),
                                     (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->CloneMeshFVF( D3DXMESH_SYSTEMMEM | D3DXMESH_32BIT,
                m_pMeshSysMem->GetFVF(), m_pd3dDevice, &pMeshTemp );
            if (FAILED(hr))
                return hr;

            hr = D3DXTessellateNPatches( pMeshTemp, (DWORD*)m_pbufAdjacency->GetBufferPointer(),
                                         (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->GetOptions() & D3DXMESH_32BIT;
        if( (dwMeshEnhancedFlags & D3DXMESH_32BIT) == 0)
            dwMeshEnhancedFlags |= D3DXMESH_WRITEONLY;
        hr = pMeshEnhancedSysMem->CloneMeshFVF( 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::RestoreDeviceObjects()
{

      m_pForkLift->RestoreDeviceObjects( m_pd3dDevice );
      m_pPallet->RestoreDeviceObjects( m_pd3dDevice );
      m_pRack->RestoreDeviceObjects (m_pd3dDevice );
      m_pFont->RestoreDeviceObjects();

      HRESULT hr;

    // m_bUseHWNPatches = (m_d3dCaps.DevCaps & D3DDEVCAPS_NPATCHES);

    hr = GenerateEnhancedMesh( m_dwNumSegs );
    if( FAILED(hr) )
        return hr;

    // Setup render state
    m_pd3dDevice->SetRenderState( D3DRS_LIGHTING,     TRUE );
    m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );
    m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      TRUE );
    m_pd3dDevice->SetRenderState( D3DRS_AMBIENT,      0x33333333 );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
    m_pd3dDevice->SetTextureStageState( 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.Width / (FLOAT)m_d3dsdBackBuffer.Height;
    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->RestoreDeviceObjects();

      // 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->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU,  D3DTADDRESS_CLAMP );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV,  D3DTADDRESS_CLAMP );

    m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );
    m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      TRUE );
    m_pd3dDevice->SetRenderState( D3DRS_LIGHTING,     TRUE ); */

      return S_OK;
}

HRESULT CMyD3DApplication::DeleteDeviceObjects()
{
      m_pFont->DeleteDeviceObjects();

      m_pForkLift->Destroy();
      m_pRack->Destroy();
      m_pPallet->Destroy();

      return S_OK;
}

HRESULT CMyD3DApplication::FinalCleanup()
{
      SAFE_DELETE (m_pFont);
      SAFE_DELETE (m_pForkLift);
      SAFE_DELETE (m_pPallet);
      SAFE_DELETE (m_pRack);
      return S_OK;

}

HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8 *pCaps, DWORD dwBehavior, D3DFORMAT Format)
{
      if (dwBehavior & D3DCREATE_PUREDEVICE)
            return E_FAIL;

      if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHAPALETTE)
            return S_OK;
      if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHA)
            return S_OK;

      return E_FAIL;


}


Thanks in advance
Jacky

0
Comment
Question by:luckie
  • 29
  • 15
44 Comments
 

Author Comment

by:luckie
ID: 7035096
This version is a bit out-dated, but the story is quite the same. Bye
0
 
LVL 4

Expert Comment

by:joachimc
ID: 7036513
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
0
 

Author Comment

by:luckie
ID: 7036537
in simpler words, is to animate/display multiple objects
0
 
LVL 4

Expert Comment

by:joachimc
ID: 7036558
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("someXfile2.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];

};

0
 

Author Comment

by:luckie
ID: 7036561
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
0
 
LVL 4

Expert Comment

by:joachimc
ID: 7036567
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
0
 

Author Comment

by:luckie
ID: 7036570
how come myFirstMesh and mySecondMesh weren't declared in the class. How can Render() accesses them. :) Thanks

Bye
Jacky
0
 

Author Comment

by:luckie
ID: 7036573
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
0
 

Author Comment

by:luckie
ID: 7036586
what is 'ourlocation'?
0
 
LVL 4

Expert Comment

by:joachimc
ID: 7036595
a matrix with the location of the object.
0
 

Author Comment

by:luckie
ID: 7036605
How do I set it up with myFirstMesh?
A deeper explanation is preferred :)
pts increased to 200
Regards,
Jacky
0
 

Author Comment

by:luckie
ID: 7036613
How do I set it up with myFirstMesh?
A deeper explanation is preferred :)
pts increased to 200
Regards,
Jacky
0
 
LVL 4

Expert Comment

by:joachimc
ID: 7036616
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(LPDIRECT3DDEVICE8 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(&ourLocation);
     Util::WriteLogFile("Initialized 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*)pD3DXMtrlBuffer->GetBufferPointer();
    g_pMeshMaterials = new D3DMATERIAL8[g_dwNumMaterials];
    g_pMeshTextures  = new LPDIRECT3DTEXTURE8[g_dwNumMaterials];

    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].Ambient = g_pMeshMaterials[i].Diffuse;
          g_pMeshMaterials[i].Power = 35;

          strcpy(texturePath, "d:\\models\\textures\\");
          strcat(texturePath, d3dxMaterials[i].pTextureFilename);    


          // 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("Created 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;
}
0
 

Author Comment

by:luckie
ID: 7036634
The example I shown you can only display one object! do you know why?


0
 
LVL 4

Expert Comment

by:joachimc
ID: 7036675
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::GenerateEnhancedMesh(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

0
 

Author Comment

by:luckie
ID: 7036679
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
0
 

Author Comment

by:luckie
ID: 7036686
If you don't mind, please send it over about 5 hrs later.Thanks
0
 

Author Comment

by:luckie
ID: 7036703
just holding out for a savior... my boss nearly complains about me ha ha Bye
0
 

Author Comment

by:luckie
ID: 7036705
doesn't matter... sending immediately is okay!
0
 

Author Comment

by:luckie
ID: 7036706
but it is best to send later .. going back and forth...
0
 
LVL 4

Expert Comment

by:joachimc
ID: 7036707
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(&objectWorldMatrix)
m_pForkLift->Render(m_pd3dDevice);

objectWorldMatrix._41 = 2.0f; //Move it to the right
m_pRack->Render(m_pd3dDevice);

objectWorldMatrix._42 = 2.0f; //Move it up a little
m_pPallet->Render(m_pd3dDevice);

/Joachim
0
 

Author Comment

by:luckie
ID: 7036712
but it is best to send later .. going back and forth...
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:luckie
ID: 7036714
I still insist a complete source code as a starting point. Could you please help me out?
0
 

Author Comment

by:luckie
ID: 7036716
so much detail to handle!!! please
0
 
LVL 4

Expert Comment

by:joachimc
ID: 7036729
now you must be able to put it together.
0
 

Author Comment

by:luckie
ID: 7036742
Alright, Let me digest it thoroughtly before I get back to you... Thanks a lot!
Jack
0
 

Author Comment

by:luckie
ID: 7036798
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::CMyD3DApplication()
{
      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_bShowCursorWhenFullscreen = TRUE;

//      m_dwNumSegs = 1;
      _tcscpy (m_strInitialDir, DXUtil_GetDXSDKMediaPath() );
      _tcscpy (m_strMeshFilename, _T("car.x"));
      _tcscpy (m_strMeshFilename2, _T("pallet.x"));


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

}

HRESULT CMyD3DApplication::InitDeviceObjects()
{
      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]->GetVertexBuffer (&pVB);
      pVB->Lock (0,0,&pVertices,0);

      D3DXComputeBoundingSphere (pVertices, m_pMeshSysMem[0]->GetNumVertices(),
                                             m_pMeshSysMem[0]->GetFVF(), &m_vObjectCenter,
                                             &m_fObjectRadius);

      pVB->Unlock();
      SAFE_RELEASE(pVB);

      m_pMaterials = (D3DXMATERIAL*) m_pbufMaterials->GetBufferPointer();
      m_ppTextures = new LPDIRECT3DTEXTURE8[m_dwNumMaterials];


      for (UINT i=0; i < m_dwNumMaterials; i++)
      {
            TCHAR strTexturePath[512] = _T("");
            DXUtil_FindMediaFile (strTexturePath, m_pMaterials[i].pTextureFilename);
            D3DXCreateTextureFromFile(m_pd3dDevice, strTexturePath, &m_ppTextures[i]);
      }

      if (! (m_pMeshSysMem[0]->GetFVF() & D3DFVF_NORMAL ))
      {
            m_pMeshSysMem[0]->CloneMeshFVF (m_pMeshSysMem[0]->GetOptions(),
                                                       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]->GetVertexBuffer (&pVB2);
      pVB2->Lock (0,0,&pVertices2,0);

//      D3DXComputeBoundingSphere (pVertices2, m_pMeshSysMem2->GetNumVertices(),
//                                             m_pMeshSysMem2->GetFVF(), &m_vObjectCenter,
//                                             &m_fObjectRadius);

      pVB2->Unlock();
      SAFE_RELEASE(pVB2);

      m_pMaterials2 = (D3DXMATERIAL*) m_pbufMaterials2->GetBufferPointer();
      m_ppTextures2 = new LPDIRECT3DTEXTURE8[m_dwNumMaterials2];


      for (i=0; i < m_dwNumMaterials2; i++)
      {
            TCHAR strTexturePath[512] = _T("");
            DXUtil_FindMediaFile (strTexturePath, m_pMaterials2[i].pTextureFilename);
            D3DXCreateTextureFromFile(m_pd3dDevice, strTexturePath, &m_ppTextures2[i]);
      }

      if (! (m_pMeshSysMem[1]->GetFVF() & D3DFVF_NORMAL ))
      {
            m_pMeshSysMem[1]->CloneMeshFVF (m_pMeshSysMem[1]->GetOptions(),
                                                       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::OneTimeSceneInit()
{
      return S_OK;
}

HRESULT CMyD3DApplication::Render()
{
      D3DXMATRIX objectWorldMatrix;
      m_pd3dDevice->Clear (0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x000000ff, 1.0f, 0L);
      D3DXMatrixIdentity(&objectWorldMatrix);

      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_pd3dDevice);
      objectWorldMatrix._41 = 2.0f;
      m_pRack->Render(m_pd3dDevice);
      m_pd3dDevice->SetRenderState (D3DRS_ZFUNC, D3DCMP_ALWAYS);
      objectWorldMatrix._41 = 2.0f;
      m_pPallet->Render(m_pd3dDevice);
      m_pd3dDevice->SetRenderState (D3DRS_ZFUNC, D3DCMP_LESSEQUAL);

      ////////////////// added
      for (UINT k = 0 ; k < 2; k++) {
      for (UINT i = 0; i < m_dwNumMaterials; i++) {
            m_pd3dDevice->SetMaterial(&m_pMaterials[i].MatD3D);
            m_pd3dDevice->SetTexture (0, m_ppTextures[i]);
            m_pd3dDevice->SetRenderState (D3DRS_ZFUNC, D3DCMP_ALWAYS);
            m_pMeshEnhanced[k]->DrawSubset (i);
            m_pd3dDevice->SetRenderState (D3DRS_ZFUNC, D3DCMP_LESSEQUAL);

      }      
      }
      m_pd3dDevice->EndScene();

      return S_OK;
}

HRESULT CMyD3DApplication::FrameMove()
{
      D3DXMATRIX matWorld;

      // view for XMesh
      D3DXMatrixTranslation (&matWorld, -m_vObjectCenter.x,
                                                        -m_vObjectCenter.y,
                                                        -m_vObjectCenter.z);
      D3DXMatrixMultiply (&matWorld, &matWorld, m_ArcBall.GetRotationMatrix());
      D3DXMatrixMultiply (&matWorld, &matWorld, m_ArcBall.GetTranslationMatrix());
                                    


      m_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
      D3DXMATRIX matView;
      D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3 (0,0,-2*m_fObjectRadius),
                                              &D3DXVECTOR3 (0, 0, 0),
                                                &D3DXVECTOR3 (0, 1, 0) );
      m_pd3dDevice->SetTransform(D3DTS_VIEW, &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::GenerateEnhancedMesh(UINT dwNewNumSegs, int Num)
{
 LPD3DXMESH pMeshTemp;
 LPD3DXMESH pMeshEnhancedSysMem[3] =  {NULL, NULL, NULL};
 HRESULT hr;

////////////////// temp cancelled
// m_pMeshSysMem->CloneMeshFVF (D3DXMESH_WRITEONLY | D3DXMESH_NPATCHES |
//          (m_pMeshSysMem->GetOptions() & D3DXMESH_32BIT),
//             m_pMeshSysMem->GetFVF(), m_pd3dDevice, &pMeshTemp );

  hr = D3DXTessellateNPatches( m_pMeshSysMem[Num], (DWORD*)m_pbufAdjacency->GetBufferPointer(),
                                     (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]->CloneMeshFVF( D3DXMESH_SYSTEMMEM | D3DXMESH_32BIT,
                m_pMeshSysMem[Num]->GetFVF(), m_pd3dDevice, &pMeshTemp );
            if (FAILED(hr))
                return hr;

            hr = D3DXTessellateNPatches( pMeshTemp, (DWORD*)m_pbufAdjacency->GetBufferPointer(),
                                         (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]->CloneMeshFVF( 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::RestoreDeviceObjects()
{

      m_pForkLift->RestoreDeviceObjects( m_pd3dDevice );
      m_pPallet->RestoreDeviceObjects( m_pd3dDevice );
      m_pRack->RestoreDeviceObjects (m_pd3dDevice );
      m_pFont->RestoreDeviceObjects();

      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->SetRenderState( D3DRS_LIGHTING,     TRUE );
    m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );
    m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      TRUE );
    m_pd3dDevice->SetRenderState( D3DRS_AMBIENT,      0x33333333 );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
    m_pd3dDevice->SetTextureStageState( 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.Width / (FLOAT)m_d3dsdBackBuffer.Height;
    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->RestoreDeviceObjects();

      // 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->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU,  D3DTADDRESS_CLAMP );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV,  D3DTADDRESS_CLAMP );

    m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );
    m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      TRUE );
    m_pd3dDevice->SetRenderState( D3DRS_LIGHTING,     TRUE ); */

      return S_OK;
}

HRESULT CMyD3DApplication::DeleteDeviceObjects()
{
      m_pFont->DeleteDeviceObjects();

      m_pForkLift->Destroy();
      m_pRack->Destroy();
      m_pPallet->Destroy();

      return S_OK;
}

HRESULT CMyD3DApplication::FinalCleanup()
{
      SAFE_DELETE (m_pFont);
      SAFE_DELETE (m_pForkLift);
      SAFE_DELETE (m_pPallet);
      SAFE_DELETE (m_pRack);
      return S_OK;

}

HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8 *pCaps, DWORD dwBehavior, D3DFORMAT Format)
{
      if (dwBehavior & D3DCREATE_PUREDEVICE)
            return E_FAIL;

      if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHAPALETTE)
            return S_OK;
      if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHA)
            return S_OK;

      return E_FAIL;


}
0
 
LVL 4

Expert Comment

by:joachimc
ID: 7036858
Found an error in your code.

//This needs to be called for every mesh.
hr = GenerateEnhancedMesh( m_dwNumSegs );
0
 

Author Comment

by:luckie
ID: 7037121
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
0
 
LVL 4

Expert Comment

by:joachimc
ID: 7037197
Your first job? Are you working on a games company ?
0
 
LVL 4

Expert Comment

by:joachimc
ID: 7037253
do you really need the enhanced mesh or can you use a normal mesh ?
0
 

Author Comment

by:luckie
ID: 7038006
Normal Mesh willl do
0
 

Author Comment

by:luckie
ID: 7038152
I want to make it simple by adopting retained mode....
0
 

Author Comment

by:luckie
ID: 7038215
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(CInputView, CFormView)

BEGIN_MESSAGE_MAP(CInputView, CFormView)
      ON_WM_CREATE()
      ON_WM_PAINT()
      ON_WM_TIMER()
      ON_WM_DESTROY()
END_MESSAGE_MAP()

 

int CInputView::OnCreate(LPCREATESTRUCT)
{
      // 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->CreateDeviceFromClipper(m_pClipper,
//            NULL, rect.right, rect.bottom, &m_pDevice);
      result = m_pD3D->CreateDeviceFromClipper(m_pClipper,
            NULL, rect.right, rect.bottom, &m_pDevice);
      if (result != D3DRM_OK)
            return 0;
      m_pDevice->SetQuality(D3DRMRENDER_GOURAUD);


      //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(CInputView)
            // NOTE: the ClassWizard will add member initialization here
      //}}AFX_DATA_INIT
}

CInputView::~CInputView()
{
}

void CInputView::DoDataExchange(CDataExchange* pDX)
{
      CFormView::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CInputView)
            // 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_pMeshBuilder);

      result = m_pD3D->CreateMeshBuilder(&m_pMeshRack);
      if (result != D3DRM_OK)
            return FALSE;

      result = m_pMeshBuilder->Load("model.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_pParentFrame, &pFrame);
      if (result != D3DRM_OK)
            return FALSE;
      pFrame->AddVisual(m_pMeshBuilder);
      pFrame->AddVisual(m_pMeshRack);
//      pFrame->SetRotation(m_pParentFrame, 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_pParentFrame, &m_pEye);
//      result = m_pD3D->CreateFrame(m_pParentFrame,&m_pRack);
      
      if (result != D3DRM_OK)
            AfxMessageBox ("Hi!");
//      abc->x = 0.0;
//      abc->y = 0.0;
//      abc->z = 0.0;
      
//      m_pRack->GetPosition(m_pParentFrame, 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_pParentFrame, D3DVALUE(0.0),
            D3DVALUE(5.0), D3DVALUE(-50.0));
//      m_pRack->SetPosition(m_pParentFrame, D3DVALUE(0.0),
//            D3DVALUE(10.0), D3DVALUE(-50.0));

      result = m_pD3D->CreateViewport(m_pDevice, m_pEye,
            0, 0, m_pDevice->GetWidth(),
            m_pDevice->GetHeight(), &m_pViewport);

    //result = m_pD3D->CreateViewport(m_pDevice, m_pRack,
      //      0, 0, m_pDevice->GetWidth(),


//      m_pDevice->GetHeight(), &m_pViewport);

//      result = m_pD3D->CreateViewport(m_pDevice, 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_DIRECTIONAL,
            D3DVALUE(1.0), D3DVALUE(1.0), D3DVALUE(1.0),
            &pLight);
      if (result != D3DRM_OK)
            return FALSE;

      LPDIRECT3DRMFRAME pLightFrame;
      result = m_pD3D->CreateFrame(m_pParentFrame,
            &pLightFrame );
      if (result != D3DRM_OK)
            return FALSE;

      pLightFrame->AddLight(pLight);
      pLightFrame->SetOrientation(m_pParentFrame,
            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(pstruct.hdc);
            pWndDevice->Release();
//        LPDIRECT3DRMWINDEVICE pWndDeviceRack;
//            m_pDeviceRack->QueryInterface(
//                  IID_IDirect3DRMWinDevice,
//                  (void**)&pWndDeviceRack);
  //      pWndDeviceRack->HandlePaint(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,aPoint,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(CDumpContext& dc) const
{
      CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CInputView message handlers
0
 

Author Comment

by:luckie
ID: 7038255
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(CInputView, CFormView)

BEGIN_MESSAGE_MAP(CInputView, CFormView)
      ON_WM_CREATE()
      ON_WM_PAINT()
      ON_WM_TIMER()
      ON_WM_DESTROY()
END_MESSAGE_MAP()

 

int CInputView::OnCreate(LPCREATESTRUCT)
{
      // 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->CreateDeviceFromClipper(m_pClipper,
//            NULL, rect.right, rect.bottom, &m_pDevice);
      result = m_pD3D->CreateDeviceFromClipper(m_pClipper,
            NULL, rect.right, rect.bottom, &m_pDevice);
      if (result != D3DRM_OK)
            return 0;
      m_pDevice->SetQuality(D3DRMRENDER_GOURAUD);


      //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(CInputView)
            // NOTE: the ClassWizard will add member initialization here
      //}}AFX_DATA_INIT
}

CInputView::~CInputView()
{
}

void CInputView::DoDataExchange(CDataExchange* pDX)
{
      CFormView::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CInputView)
            // 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_pMeshBuilder);

      result = m_pD3D->CreateMeshBuilder(&m_pMeshRack);
      if (result != D3DRM_OK)
            return FALSE;

      result = m_pMeshBuilder->Load("model.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_pParentFrame, &pFrame);
      if (result != D3DRM_OK)
            return FALSE;
      pFrame->AddVisual(m_pMeshBuilder);
      pFrame->AddVisual(m_pMeshRack);
//      pFrame->SetRotation(m_pParentFrame, 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_pParentFrame, &m_pEye);
//      result = m_pD3D->CreateFrame(m_pParentFrame,&m_pRack);
      
      if (result != D3DRM_OK)
            AfxMessageBox ("Hi!");
//      abc->x = 0.0;
//      abc->y = 0.0;
//      abc->z = 0.0;
      
//      m_pRack->GetPosition(m_pParentFrame, 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_pParentFrame, D3DVALUE(0.0),
            D3DVALUE(5.0), D3DVALUE(-50.0));
//      m_pRack->SetPosition(m_pParentFrame, D3DVALUE(0.0),
//            D3DVALUE(10.0), D3DVALUE(-50.0));

      result = m_pD3D->CreateViewport(m_pDevice, m_pEye,
            0, 0, m_pDevice->GetWidth(),
            m_pDevice->GetHeight(), &m_pViewport);

    //result = m_pD3D->CreateViewport(m_pDevice, m_pRack,
      //      0, 0, m_pDevice->GetWidth(),


//      m_pDevice->GetHeight(), &m_pViewport);

//      result = m_pD3D->CreateViewport(m_pDevice, 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_DIRECTIONAL,
            D3DVALUE(1.0), D3DVALUE(1.0), D3DVALUE(1.0),
            &pLight);
      if (result != D3DRM_OK)
            return FALSE;

      LPDIRECT3DRMFRAME pLightFrame;
      result = m_pD3D->CreateFrame(m_pParentFrame,
            &pLightFrame );
      if (result != D3DRM_OK)
            return FALSE;

      pLightFrame->AddLight(pLight);
      pLightFrame->SetOrientation(m_pParentFrame,
            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(pstruct.hdc);
            pWndDevice->Release();
//        LPDIRECT3DRMWINDEVICE pWndDeviceRack;
//            m_pDeviceRack->QueryInterface(
//                  IID_IDirect3DRMWinDevice,
//                  (void**)&pWndDeviceRack);
  //      pWndDeviceRack->HandlePaint(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,aPoint,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(CDumpContext& dc) const
{
      CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CInputView message handlers
0
 

Author Comment

by:luckie
ID: 7038342
what is the counterpart of setRenderstate of the  IDDirect3Ddevice8 under retained mode?
0
 

Author Comment

by:luckie
ID: 7038345
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...
0
 

Author Comment

by:luckie
ID: 7038400
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...
0
 
LVL 4

Expert Comment

by:joachimc
ID: 7038449
Please don't post all the code again just your changes.
0
 

Author Comment

by:luckie
ID: 7040940
Could you pls help? I'm in danger now...
0
 
LVL 4

Expert Comment

by:joachimc
ID: 7046445
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_pd3dDevice);
      return S_OK;
}


//If the device is lost we need to restore everything
HRESULT App::RestoreDeviceObjects()
{
      ourFont->RestoreDeviceObjects();
      
    // Setup render state
    m_pd3dDevice->SetRenderState( D3DRS_LIGHTING,     TRUE );
    m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );
    m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      TRUE );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
    m_pd3dDevice->SetTextureStageState( 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.Direction, &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.Height;

    D3DXMATRIX matProj;
      D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 200.0f );
    //D3DXMatrixPerspectiveFovLH( &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_pd3dDevice, "models/olsen3.x", "models/textures/");
      ourMeshes[0].SetFVF(m_pd3dDevice, FVF);
      ourMeshes[0].m_pLocalMesh = ourMeshes[0].m_pSysMemMesh;
      ourMeshes[0].m_pLocalMesh->AddRef();

      ourAnimator = new Xanimator(m_pd3dDevice);
      ourAnimator->LoadXAnimation("female_customer_walk");

    return S_OK;
}

HRESULT App::InvalidateDeviceObjects()
{
      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_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
      //Start rendering
      m_pd3dDevice->BeginScene();
      
      //Set first object to (0,0,0)
      D3DXMatrixIdentity(&worldMatrix);
      m_pd3dDevice->SetTransform(D3DTS_WORLD, &worldMatrix);
      ourMeshes[0].Render(m_pd3dDevice); //Render object 0

      //Move the world matrix 2.0f in x
      worldMatrix._41 = 2.0f;
      m_pd3dDevice->SetTransform(D3DTS_WORLD, &worldMatrix);
      ourMeshes[1].Render(m_pd3dDevice); //Render object 1
      
      D3DXMatrixRotationYawPitchRoll(&worldMatrix, 0.0f, 1.6f, -0.0f);
      worldMatrix._41 = -2.0f;
      m_pd3dDevice->SetTransform(D3DTS_WORLD, &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(&viewMatrix, &cameraPlace, &lookAt, &vUp);
      m_pd3dDevice->SetTransform(D3DTS_VIEW, &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->DeleteDeviceObjects();
      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 );
}
0
 
LVL 4

Accepted Solution

by:
joachimc earned 200 total points
ID: 7046447
I hope you have solved it allready. But here is some simple code.

By the way you have to change a few small things to get it to compile since you haven't got the animation class.

/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_pd3dDevice);
      return S_OK;
}


//If the device is lost we need to restore everything
HRESULT App::RestoreDeviceObjects()
{
      ourFont->RestoreDeviceObjects();
      
    // Setup render state
    m_pd3dDevice->SetRenderState( D3DRS_LIGHTING,     TRUE );
    m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );
    m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      TRUE );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
    m_pd3dDevice->SetTextureStageState( 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.Direction, &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.Height;

    D3DXMATRIX matProj;
      D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 200.0f );
    //D3DXMatrixPerspectiveFovLH( &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_pd3dDevice, "models/olsen3.x", "models/textures/");
      ourMeshes[0].SetFVF(m_pd3dDevice, FVF);
      ourMeshes[0].m_pLocalMesh = ourMeshes[0].m_pSysMemMesh;
      ourMeshes[0].m_pLocalMesh->AddRef();

      ourAnimator = new Xanimator(m_pd3dDevice);
      ourAnimator->LoadXAnimation("female_customer_walk");

    return S_OK;
}

HRESULT App::InvalidateDeviceObjects()
{
      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_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
      //Start rendering
      m_pd3dDevice->BeginScene();
      
      //Set first object to (0,0,0)
      D3DXMatrixIdentity(&worldMatrix);
      m_pd3dDevice->SetTransform(D3DTS_WORLD, &worldMatrix);
      ourMeshes[0].Render(m_pd3dDevice); //Render object 0

      //Move the world matrix 2.0f in x
      worldMatrix._41 = 2.0f;
      m_pd3dDevice->SetTransform(D3DTS_WORLD, &worldMatrix);
      ourMeshes[1].Render(m_pd3dDevice); //Render object 1
      
      D3DXMatrixRotationYawPitchRoll(&worldMatrix, 0.0f, 1.6f, -0.0f);
      worldMatrix._41 = -2.0f;
      m_pd3dDevice->SetTransform(D3DTS_WORLD, &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(&viewMatrix, &cameraPlace, &lookAt, &vUp);
      m_pd3dDevice->SetTransform(D3DTS_VIEW, &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->DeleteDeviceObjects();
      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 );
}
0
 

Author Comment

by:luckie
ID: 7046582
Thanks. I have switched to VB.... :)
Bye
0
 
LVL 4

Expert Comment

by:joachimc
ID: 7046598
It's not a great idea to change to VB if this is the only reason :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now