DirectX in Visual C++: Help moving a mesh starting from BasicHLSL Tutorial

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 90
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 4
Clawsy Clawsy is offline Offline
Junior Poster in Training

DirectX in Visual C++: Help moving a mesh starting from BasicHLSL Tutorial

 
0
  #1
Jun 2nd, 2009
Hello,

I just started reading and learning DirectX SDK's tutorials in order to develop skills for game programming.
I don't know COM very well (i just start learning it). I read lights, materials, mesh and BasicHLSL DirectX9 tutorials (line by line). I know C++ and math (not VERY good math but it's working ). Win32 skills in progress...
I don't now how to move that mesh or do other operations on it because I didn't find something to modify in code (I'm a started in DirectX as I said).
I searched the web but most of it it's the the same tutorials in slightly different forms.

My final point would be to develop an educational game: 2,3 or 4 rooms, a teacher, me as a student moving around, etc.
I know how to use a 3D engine in c# but I don't want this. I want for the future to develop my game programming skills
seriously. A good way would be if I would understand some practical points to start from.

So I need to move that mesh with keyboard and mouse (and probably the camera should be moved with the mesh, too, as in GTA). After that I will try to build a room and walk in it (but stop if I hit a wall). I don't know if I could read *.PAK files in C++
to build the world (some rooms, objects). Hope I was clear. Every help will be appreciated!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 90
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 4
Clawsy Clawsy is offline Offline
Junior Poster in Training

Re: DirectX in Visual C++: Help moving a mesh starting from BasicHLSL Tutorial

 
0
  #2
Jun 2nd, 2009
This tutorial also uses DXUT as you might expect to help on some code generation. I know to move the camera but, on the mesh side I saw only the code for loading it but not location coordinates.

Here it's the code for the mesh loading and texture, memory allocation, etc. It includes also other operations on text, optimisation...:
  1.  
  2. //--------------------------------------------------------------------------------------
  3. // This callback function will be called immediately after the Direct3D device has been
  4. // created, which will happen during application initialization and windowed/full screen
  5. // toggles. This is the best location to create D3DPOOL_MANAGED resources since these
  6. // resources need to be reloaded whenever the device is destroyed. Resources created
  7. // here should be released in the OnDestroyDevice callback.
  8. //--------------------------------------------------------------------------------------
  9. HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
  10. void* pUserContext )
  11. {
  12. HRESULT hr;
  13.  
  14. V_RETURN( g_DialogResourceManager.OnD3D9CreateDevice( pd3dDevice ) );
  15. V_RETURN( g_SettingsDlg.OnD3D9CreateDevice( pd3dDevice ) );
  16. // Initialize the font
  17. V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
  18. OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
  19. L"Arial", &g_pFont ) );
  20.  
  21. // Load the mesh *******************************************************************
  22. V_RETURN( LoadMesh( pd3dDevice, L"tiny\\tiny.x", &g_pMesh ) );
  23.  
  24. D3DXVECTOR3* pData;
  25. D3DXVECTOR3 vCenter;
  26. FLOAT fObjectRadius;
  27.  
  28. // memory locking *******************************************************************
  29. V( g_pMesh->LockVertexBuffer( 0, ( LPVOID* )&pData ) );
  30. V( D3DXComputeBoundingSphere( pData, g_pMesh->GetNumVertices(),
  31. D3DXGetFVFVertexSize( g_pMesh->GetFVF() ), &vCenter, &fObjectRadius ) );
  32. V( g_pMesh->UnlockVertexBuffer() );
  33. ////////
  34.  
  35. D3DXMatrixTranslation( &g_mCenterWorld, -vCenter.x, -vCenter.y, -vCenter.z );
  36. D3DXMATRIXA16 m;
  37. D3DXMatrixRotationY( &m, D3DX_PI );
  38. g_mCenterWorld *= m;
  39. D3DXMatrixRotationX( &m, D3DX_PI / 2.0f );
  40. g_mCenterWorld *= m;
  41.  
  42. V_RETURN( CDXUTDirectionWidget::StaticOnD3D9CreateDevice( pd3dDevice ) );
  43. for( int i = 0; i < MAX_LIGHTS; i++ )
  44. g_LightControl[i].SetRadius( fObjectRadius );
  45.  
  46. // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the
  47. // shader debugger. Debugging vertex shaders requires either REF or software vertex
  48. // processing, and debugging pixel shaders requires REF. The
  49. // D3DXSHADER_FORCE_*_SOFTWARE_NOOPT flag improves the debug experience in the
  50. // shader debugger. It enables source level debugging, prevents instruction
  51. // reordering, prevents dead code elimination, and forces the compiler to compile
  52. // against the next higher available software target, which ensures that the
  53. // unoptimized shaders do not exceed the shader model limitations. Setting these
  54. // flags will cause slower rendering since the shaders will be unoptimized and
  55. // forced into software. See the DirectX documentation for more information about
  56. // using the shader debugger.
  57. DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE;
  58.  
  59. #if defined( DEBUG ) || defined( _DEBUG )
  60. // Set the D3DXSHADER_DEBUG flag to embed debug information in the shaders.
  61. // Setting this flag improves the shader debugging experience, but still allows
  62. // the shaders to be optimized and to run exactly the way they will run in
  63. // the release configuration of this program.
  64. dwShaderFlags |= D3DXSHADER_DEBUG;
  65. #endif
  66.  
  67. #ifdef DEBUG_VS
  68. dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT;
  69. #endif
  70. #ifdef DEBUG_PS
  71. dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT;
  72. #endif
  73.  
  74. // Preshaders are parts of the shader that the effect system pulls out of the
  75. // shader and runs on the host CPU. They should be used if you are GPU limited.
  76. // The D3DXSHADER_NO_PRESHADER flag disables preshaders.
  77. if( !g_bEnablePreshader )
  78. dwShaderFlags |= D3DXSHADER_NO_PRESHADER;
  79.  
  80. // Read the D3DX effect file
  81. WCHAR str[MAX_PATH];
  82. V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"BasicHLSL.fx" ) );
  83.  
  84. // If this fails, there should be debug output as to
  85. // why the .fx file failed to compile
  86. V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, dwShaderFlags, NULL, &g_pEffect, NULL ) );
  87.  
  88. // Create the mesh texture from a file *******************************************************************
  89. V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"tiny\\tiny_skin.dds" ) );
  90.  
  91. V_RETURN( D3DXCreateTextureFromFileEx( pd3dDevice, str, D3DX_DEFAULT, D3DX_DEFAULT,
  92. D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED,
  93. D3DX_DEFAULT, D3DX_DEFAULT, 0,
  94. NULL, NULL, &g_pMeshTexture ) );
  95.  
  96. // Set effect variables as needed
  97. D3DXCOLOR colorMtrlDiffuse( 1.0f, 1.0f, 1.0f, 1.0f );
  98. D3DXCOLOR colorMtrlAmbient( 0.35f, 0.35f, 0.35f, 0 );
  99.  
  100. V_RETURN( g_pEffect->SetValue( "g_MaterialAmbientColor", &colorMtrlAmbient, sizeof( D3DXCOLOR ) ) );
  101. V_RETURN( g_pEffect->SetValue( "g_MaterialDiffuseColor", &colorMtrlDiffuse, sizeof( D3DXCOLOR ) ) );
  102. V_RETURN( g_pEffect->SetTexture( "g_MeshTexture", g_pMeshTexture ) );
  103.  
  104. // Setup the camera's view parameters
  105. D3DXVECTOR3 vecEye( 0.0f, 0.0f, -15.0f );
  106. D3DXVECTOR3 vecAt ( 0.0f, -0.0f, -0.0f );
  107. g_Camera.SetViewParams( &vecEye, &vecAt );
  108. g_Camera.SetRadius( fObjectRadius * 3.0f, fObjectRadius * 0.5f, fObjectRadius * 10.0f );
  109.  
  110. return S_OK;
  111. }
  112.  
  113. //--------------------------------------------------------------------------------------
  114. // This function loads the mesh and ensures the mesh has normals; it also optimizes the
  115. // mesh for the graphics card's vertex cache, which improves performance by organizing
  116. // the internal triangle list for less cache misses.
  117. //--------------------------------------------------------------------------------------
  118. HRESULT LoadMesh( IDirect3DDevice9* pd3dDevice, WCHAR* strFileName, ID3DXMesh** ppMesh )
  119. {
  120. ID3DXMesh* pMesh = NULL;
  121. WCHAR str[MAX_PATH];
  122. HRESULT hr;
  123.  
  124. // Load the mesh with D3DX and get back a ID3DXMesh*. For this
  125. // sample we'll ignore the X file's embedded materials since we know
  126. // exactly the model we're loading. See the mesh samples such as
  127. // "OptimizedMesh" for a more generic mesh loading example.
  128. V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, strFileName ) );
  129. V_RETURN( D3DXLoadMeshFromX( str, D3DXMESH_MANAGED, pd3dDevice, NULL, NULL, NULL, NULL, &pMesh ) );
  130.  
  131. DWORD* rgdwAdjacency = NULL;
  132.  
  133. // Make sure there are normals which are required for lighting
  134. if( !( pMesh->GetFVF() & D3DFVF_NORMAL ) )
  135. {
  136. ID3DXMesh* pTempMesh;
  137. V( pMesh->CloneMeshFVF( pMesh->GetOptions(),
  138. pMesh->GetFVF() | D3DFVF_NORMAL,
  139. pd3dDevice, &pTempMesh ) );
  140. V( D3DXComputeNormals( pTempMesh, NULL ) );
  141.  
  142. SAFE_RELEASE( pMesh );
  143. pMesh = pTempMesh;
  144. }
  145.  
  146. // Optimize the mesh for this graphics card's vertex cache
  147. // so when rendering the mesh's triangle list the vertices will
  148. // cache hit more often so it won't have to re-execute the vertex shader
  149. // on those vertices so it will improve perf.
  150. rgdwAdjacency = new DWORD[pMesh->GetNumFaces() * 3];
  151. if( rgdwAdjacency == NULL )
  152. return E_OUTOFMEMORY;
  153. V( pMesh->GenerateAdjacency( 1e-6f, rgdwAdjacency ) );
  154. V( pMesh->OptimizeInplace( D3DXMESHOPT_VERTEXCACHE, rgdwAdjacency, NULL, NULL, NULL ) );
  155. delete []rgdwAdjacency;
  156.  
  157. *ppMesh = pMesh;
  158.  
  159. return S_OK;
  160. }

I'm not experienced in DirectX so I need some help to start running. Any help appreciated.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 109
Reputation: Stinomus is on a distinguished road 
Solved Threads: 16
Stinomus Stinomus is offline Offline
Junior Poster

Re: DirectX in Visual C++: Help moving a mesh starting from BasicHLSL Tutorial

 
0
  #3
Jun 2nd, 2009
You'll notice in the code there are matrix translation functions. These control where in the 3D space things are drawn. Have a read up on 'matrix stacks' for describing the different 'spaces' in a 3D world and the methods of positioning 3D environments should become more clear.

Have a look here:
http://msdn.microsoft.com/en-us/libr...60(VS.85).aspx

Additionally HLSL - High Level Shading Language - is a more advanced topic, best to start with displaying and loading meshes then move on to user interaction. Predominantly you will use HLSL for special effects.
Last edited by Stinomus; Jun 2nd, 2009 at 9:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 90
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 4
Clawsy Clawsy is offline Offline
Junior Poster in Training

Re: DirectX in Visual C++: Help moving a mesh starting from BasicHLSL Tutorial

 
0
  #4
Jun 3rd, 2009
thanks,
i'll read... however i'm not triyng to move around the world (and the mesh) but to position the mesh in the world. Thanks againn i'll try
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 109
Reputation: Stinomus is on a distinguished road 
Solved Threads: 16
Stinomus Stinomus is offline Offline
Junior Poster

Re: DirectX in Visual C++: Help moving a mesh starting from BasicHLSL Tutorial

 
0
  #5
Jun 4th, 2009
Originally Posted by Clawsy View Post
thanks,
i'll read... however i'm not triyng to move around the world (and the mesh) but to position the mesh in the world. Thanks againn i'll try
Yes understanding matrices is necessary for any kind of positioning in 3D space, be that the position of the eye or the position of the object in world space.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC