//----------------------------------------------------------------------------- // File: Meshes.cpp // // Desc: For advanced geometry, most apps will prefer to load pre-authored // Meshes from a file. Fortunately, when using Meshes, D3DX does most of // the work for this, parsing a geometry file and creating vertx buffers // (and index buffers) for us. This tutorial shows how to use a D3DXMESH // object, including loading it from a file and rendering it. One thing // D3DX does not handle for us is the materials and textures for a mesh, // so note that we have to handle those manually. // // Note: one advanced (but nice) feature that we don't show here is that // when cloning a mesh we can specify the FVF. So, regardless of how the // mesh was authored, we can add/remove normals, add more texture // coordinate sets (for multi-texturing), etc. // // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #include #include #include #pragma warning( disable : 4996 ) // disable deprecated warning #include #pragma warning( default : 4996 ) #include "../Common/Common.h" #include #include #include using namespace std; //----------------------------------------------------------------------------- // Global variables //----------------------------------------------------------------------------- LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device LPD3DXMESH g_pMesh = NULL; // Our mesh object in sysmem D3DMATERIAL9* g_pMeshMaterials = NULL; // Materials for our mesh LPDIRECT3DTEXTURE9* g_pMeshTextures = NULL; // Textures for our mesh DWORD g_dwNumMaterials = 0L; // Number of mesh materials struct CUSTOMVERTEX { float x, y, z, a, b, c, p, q; //FLOAT tu, tv; // The texture coordinates }; struct vertex { double x,y,z; }; struct texturecoords { double p, q; }; const char* FbxSurfaceMaterial::sDiffuse = "DiffuseColor"; const char* FbxSurfaceMaterial::sAmbient = "AmbientColor"; const char* FbxSurfaceMaterial::sSpecular = "SpecularColor"; //----------------------------------------------------------------------------- // Name: InitD3D() // Desc: Initializes Direct3D //----------------------------------------------------------------------------- HRESULT WINAPI D3DXLoadMeshFromFBX( LPCWSTR pFilename, DWORD Options, LPDIRECT3DDEVICE9 pD3DDevice, LPD3DXBUFFER *ppAdjacency, LPD3DXBUFFER *ppMaterials, LPD3DXBUFFER *ppEffectInstances, DWORD *pNumMaterials, LPD3DXMESH* ppMesh) { FbxManager *manager = FbxManager::Create(); FbxIOSettings *ioSettings = FbxIOSettings::Create(manager, IOSROOT); manager->SetIOSettings(ioSettings); FbxImporter *importer=FbxImporter::Create(manager,""); importer->Initialize("cube1.FBX",-1,manager->GetIOSettings()); FbxScene *scene = FbxScene::Create(manager,"tempName"); importer->Import(scene); importer->Destroy(); FbxNode* rootNode = scene->GetRootNode(); int numKids = rootNode->GetChildCount(); FbxNode *childNode = 0; childNode = rootNode->GetChild(0); FbxMesh *mesh = childNode->GetMesh(); /*int yourVertexCount = mesh->GetControlPointsCount(); // no of vertices of object (eg. : CUBE) int yourPolygonCount = mesh->GetPolygonVertexCount(); // no of vertices of all triangles of oject int polygonCount = mesh->GetPolygonCount(); // no of polygons or no of triangles*/ D3DVERTEXELEMENT9 vertElem[] = { {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0}, {0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, D3DDECL_END() }; HRESULT hr; hr = D3DXCreateMesh (12, 36, D3DXMESH_MANAGED, vertElem, pD3DDevice, &g_pMesh); if FAILED(hr) MessageBox( NULL, L"Error creating mesh container", L"Meshes.exe", MB_OK ); vertex vertices[8]; texturecoords txt[36]; float *normals; int numNormals; int *indices; int numIndices=0; int numVertices=0; if ( mesh != NULL) { //================= Get Vertices ==================================== int numVerts = mesh->GetControlPointsCount(); for ( int j=0; jGetControlPointAt(j); vertices[numVertices].x=vert[0]; vertices[numVertices].y=vert[1]; vertices[numVertices++].z=vert[2]; } //================= Get Indices ==================================== numIndices=mesh->GetPolygonVertexCount(); indices = new int[numIndices]; indices = mesh->GetPolygonVertices(); //================= Get Normals ==================================== FbxGeometryElementNormal* normalEl = mesh->GetElementNormal(); if( normalEl) { numNormals = mesh->GetPolygonCount()*3; normals = new float[numNormals*3]; int vertexCounter=0; for(int polyCounter = 0 ; polyCounterGetPolygonCount(); polyCounter++) { for(int i=0;i<3;i++) { FbxVector4 normal = normalEl->GetDirectArray().GetAt(vertexCounter); normals[vertexCounter*3+0] = normal[0]; normals[vertexCounter*3+1] = normal[1]; normals[vertexCounter*3+2] = normal[2]; vertexCounter++; } } } //================= Get UV maps ==================================== int polygonCount = mesh->GetPolygonCount(); FbxLayerElementArrayTemplate* uv= 0; mesh->GetTextureUV(&uv, FbxLayerElement::eTextureDiffuse); for (int i = 0,k = 0; i < polygonCount; ++i) { for (int j = 0; j < mesh->GetPolygonSize(i); ++j) { FbxVector2 uv1 = uv->GetAt(mesh->GetTextureUVIndex(i,j)); txt[k].p = uv1[0]; txt[k].q = uv1[1]; ++k; } } // Fill in vertices of a box CUSTOMVERTEX v1[]={ /*{vertices[0].x,vertices[0].y,vertices[0].z,normals[0],normals[1],normals[2],txt[0].p,txt[0].q}, {vertices[1].x,vertices[1].y,vertices[1].z,normals[3],normals[4],normals[5],txt[1].p,txt[1].q}, {vertices[2].x,vertices[2].y,vertices[2].z,normals[6],normals[7],normals[8],txt[2].p,txt[2].q}, {vertices[3].x,vertices[3].y,vertices[3].z,normals[9],normals[10],normals[11],txt[3].p,txt[3].q}, {vertices[4].x,vertices[4].y,vertices[4].z,normals[12],normals[13],normals[14],txt[4].p,txt[4].q}, {vertices[5].x,vertices[5].y,vertices[5].z,normals[15],normals[16],normals[17],txt[5].p,txt[5].q}, {vertices[6].x,vertices[6].y,vertices[6].z,normals[18],normals[19],normals[20],txt[6].p,txt[6].q}, {vertices[7].x,vertices[7].y,vertices[7].z,normals[21],normals[22],normals[23],txt[7].p,txt[7].q}, */ /*{vertices[0].x,vertices[0].y,vertices[0].z,normals[0],normals[1],normals[2],txt[0].p,txt[0].q}, {vertices[1].x,vertices[1].y,vertices[1].z,normals[3],normals[4],normals[5],txt[1].p,txt[1].q}, {vertices[2].x,vertices[2].y,vertices[2].z,normals[6],normals[7],normals[8],txt[2].p,txt[2].q}, {vertices[0].x,vertices[0].y,vertices[0].z,normals[9],normals[10],normals[11],txt[3].p,txt[3].q}, {vertices[2].x,vertices[2].y,vertices[2].z,normals[12],normals[13],normals[14],txt[4].p,txt[4].q}, {vertices[3].x,vertices[3].y,vertices[3].z,normals[15],normals[16],normals[17],txt[5].p,txt[5].q}, {vertices[1].x,vertices[1].y,vertices[1].z,normals[18],normals[19],normals[20],txt[6].p,txt[6].q}, {vertices[5].x,vertices[5].y,vertices[5].z,normals[21],normals[22],normals[23],txt[7].p,txt[7].q}, {vertices[6].x,vertices[6].y,vertices[6].z,normals[24],normals[25],normals[26],txt[8].p,txt[8].q}, {vertices[1].x,vertices[1].y,vertices[1].z,normals[27],normals[28],normals[29],txt[9].p,txt[9].q}, {vertices[6].x,vertices[6].y,vertices[6].z,normals[30],normals[31],normals[32],txt[10].p,txt[10].q}, {vertices[2].x,vertices[2].y,vertices[2].z,normals[33],normals[34],normals[35],txt[11].p,txt[11].q}, {vertices[5].x,vertices[5].y,vertices[5].z,normals[36],normals[37],normals[38],txt[12].p,txt[12].q}, {vertices[4].x,vertices[4].y,vertices[4].z,normals[39],normals[40],normals[41],txt[13].p,txt[13].q}, {vertices[7].x,vertices[7].y,vertices[7].z,normals[42],normals[43],normals[44],txt[14].p,txt[14].q}, {vertices[5].x,vertices[5].y,vertices[5].z,normals[45],normals[46],normals[47],txt[15].p,txt[15].q}, {vertices[7].x,vertices[7].y,vertices[7].z,normals[48],normals[49],normals[50],txt[16].p,txt[16].q}, {vertices[6].x,vertices[6].y,vertices[6].z,normals[51],normals[52],normals[53],txt[17].p,txt[17].q}, {vertices[4].x,vertices[4].y,vertices[4].z,normals[54],normals[55],normals[56],txt[18].p,txt[18].q}, {vertices[0].x,vertices[0].y,vertices[0].z,normals[57],normals[58],normals[59],txt[19].p,txt[19].q}, {vertices[3].x,vertices[3].y,vertices[3].z,normals[60],normals[61],normals[62],txt[20].p,txt[20].q}, {vertices[4].x,vertices[4].y,vertices[4].z,normals[63],normals[64],normals[65],txt[21].p,txt[21].q}, {vertices[3].x,vertices[3].y,vertices[3].z,normals[66],normals[67],normals[68],txt[22].p,txt[22].q}, {vertices[7].x,vertices[7].y,vertices[7].z,normals[69],normals[70],normals[71],txt[23].p,txt[23].q}, {vertices[4].x,vertices[4].y,vertices[4].z,normals[72],normals[73],normals[74],txt[24].p,txt[24].q}, {vertices[5].x,vertices[5].y,vertices[5].z,normals[75],normals[76],normals[77],txt[25].p,txt[25].q}, {vertices[1].x,vertices[1].y,vertices[1].z,normals[78],normals[79],normals[80],txt[26].p,txt[26].q}, {vertices[4].x,vertices[4].y,vertices[4].z,normals[81],normals[82],normals[83],txt[27].p,txt[27].q}, {vertices[1].x,vertices[1].y,vertices[1].z,normals[84],normals[85],normals[86],txt[28].p,txt[28].q}, {vertices[0].x,vertices[0].y,vertices[0].z,normals[87],normals[88],normals[89],txt[29].p,txt[29].q}, {vertices[7].x,vertices[7].y,vertices[7].z,normals[90],normals[91],normals[92],txt[30].p,txt[30].q}, {vertices[6].x,vertices[6].y,vertices[6].z,normals[93],normals[94],normals[95],txt[31].p,txt[31].q}, {vertices[2].x,vertices[2].y,vertices[2].z,normals[96],normals[97],normals[98],txt[32].p,txt[32].q}, {vertices[7].x,vertices[7].y,vertices[7].z,normals[99],normals[100],normals[101],txt[33].p,txt[33].q}, {vertices[2].x,vertices[2].y,vertices[2].z,normals[102],normals[103],normals[104],txt[34].p,txt[34].q}, {vertices[3].x,vertices[3].y,vertices[3].z,normals[105],normals[106],normals[107],txt[35].p,txt[35].q}, */ {vertices[0].x,vertices[0].y,vertices[0].z,normals[0],normals[0],normals[0],txt[0].p,txt[0].q}, {vertices[1].x,vertices[1].y,vertices[1].z,normals[1],normals[1],normals[1],txt[1].p,txt[1].q}, {vertices[2].x,vertices[2].y,vertices[2].z,normals[2],normals[2],normals[2],txt[2].p,txt[2].q}, {vertices[0].x,vertices[0].y,vertices[0].z,normals[3],normals[3],normals[3],txt[3].p,txt[3].q}, {vertices[2].x,vertices[2].y,vertices[2].z,normals[4],normals[4],normals[4],txt[4].p,txt[4].q}, {vertices[3].x,vertices[3].y,vertices[3].z,normals[5],normals[5],normals[5],txt[5].p,txt[5].q}, {vertices[1].x,vertices[1].y,vertices[1].z,normals[6],normals[6],normals[6],txt[6].p,txt[6].q}, {vertices[5].x,vertices[5].y,vertices[5].z,normals[7],normals[7],normals[7],txt[7].p,txt[7].q}, {vertices[6].x,vertices[6].y,vertices[6].z,normals[8],normals[8],normals[8],txt[8].p,txt[8].q}, {vertices[1].x,vertices[1].y,vertices[1].z,normals[9],normals[9],normals[9],txt[9].p,txt[9].q}, {vertices[6].x,vertices[6].y,vertices[6].z,normals[10],normals[10],normals[10],txt[10].p,txt[10].q}, {vertices[2].x,vertices[2].y,vertices[2].z,normals[11],normals[11],normals[11],txt[11].p,txt[11].q}, {vertices[5].x,vertices[5].y,vertices[5].z,normals[12],normals[12],normals[12],txt[12].p,txt[12].q}, {vertices[4].x,vertices[4].y,vertices[4].z,normals[13],normals[13],normals[13],txt[13].p,txt[13].q}, {vertices[7].x,vertices[7].y,vertices[7].z,normals[14],normals[14],normals[14],txt[14].p,txt[14].q}, {vertices[5].x,vertices[5].y,vertices[5].z,normals[15],normals[15],normals[15],txt[15].p,txt[15].q}, {vertices[7].x,vertices[7].y,vertices[7].z,normals[16],normals[16],normals[16],txt[16].p,txt[16].q}, {vertices[6].x,vertices[6].y,vertices[6].z,normals[17],normals[17],normals[17],txt[17].p,txt[17].q}, {vertices[4].x,vertices[4].y,vertices[4].z,normals[18],normals[18],normals[18],txt[18].p,txt[18].q}, {vertices[0].x,vertices[0].y,vertices[0].z,normals[19],normals[19],normals[19],txt[19].p,txt[19].q}, {vertices[3].x,vertices[3].y,vertices[3].z,normals[20],normals[20],normals[20],txt[20].p,txt[20].q}, {vertices[4].x,vertices[4].y,vertices[4].z,normals[21],normals[21],normals[21],txt[21].p,txt[21].q}, {vertices[3].x,vertices[3].y,vertices[3].z,normals[22],normals[22],normals[22],txt[22].p,txt[22].q}, {vertices[7].x,vertices[7].y,vertices[7].z,normals[23],normals[23],normals[23],txt[23].p,txt[23].q}, {vertices[4].x,vertices[4].y,vertices[4].z,normals[24],normals[24],normals[24],txt[24].p,txt[24].q}, {vertices[5].x,vertices[5].y,vertices[5].z,normals[25],normals[25],normals[25],txt[25].p,txt[25].q}, {vertices[1].x,vertices[1].y,vertices[1].z,normals[26],normals[26],normals[26],txt[26].p,txt[26].q}, {vertices[4].x,vertices[4].y,vertices[4].z,normals[27],normals[27],normals[27],txt[27].p,txt[27].q}, {vertices[1].x,vertices[1].y,vertices[1].z,normals[28],normals[28],normals[28],txt[28].p,txt[28].q}, {vertices[0].x,vertices[0].y,vertices[0].z,normals[29],normals[29],normals[29],txt[29].p,txt[29].q}, {vertices[7].x,vertices[7].y,vertices[7].z,normals[30],normals[30],normals[30],txt[30].p,txt[30].q}, {vertices[6].x,vertices[6].y,vertices[6].z,normals[31],normals[31],normals[31],txt[31].p,txt[31].q}, {vertices[2].x,vertices[2].y,vertices[2].z,normals[32],normals[32],normals[32],txt[32].p,txt[32].q}, {vertices[7].x,vertices[7].y,vertices[7].z,normals[33],normals[33],normals[33],txt[33].p,txt[33].q}, {vertices[2].x,vertices[2].y,vertices[2].z,normals[34],normals[34],normals[34],txt[34].p,txt[34].q}, {vertices[3].x,vertices[3].y,vertices[3].z,normals[35],normals[35],normals[35],txt[35].p,txt[35].q}, }; CUSTOMVERTEX* v; //=new CUSTOMVERTEX[8]; g_pMesh->LockVertexBuffer(0, (void**)&v); for(int i=0;i<36;i++) v[i]=v1[i]; g_pMesh->UnlockVertexBuffer(); WORD* j = 0; g_pMesh->LockIndexBuffer(0, (void**)&j); for(int i=0;i<36;i++) j[i]=indices[i]; g_pMesh->UnlockIndexBuffer(); DWORD* attributeBuffer = 0; g_pMesh->LockAttributeBuffer(0, &attributeBuffer); for(int a = 0; a < 12; a++) // triangles 1-12 attributeBuffer[a] = 0; // subset 0 g_pMesh->UnlockAttributeBuffer(); *pNumMaterials = childNode->GetMaterialCount(); FbxSurfaceMaterial * lMaterial = childNode->GetMaterial(0); FbxProperty lProperty = lMaterial->FindProperty(FbxSurfaceMaterial::sDiffuse); FbxDouble3 lResult(0, 0, 0); lResult = lProperty.Get(); D3DCOLORVALUE diffuse ={lResult.mData[0],lResult.mData[1],lResult.mData[2]}; lProperty = lMaterial->FindProperty(FbxSurfaceMaterial::sAmbient); lResult = lProperty.Get(); D3DCOLORVALUE ambient={lResult.mData[0],lResult.mData[1],lResult.mData[2]}; lProperty = lMaterial->FindProperty(FbxSurfaceMaterial::sSpecular); lResult = lProperty.Get(); D3DCOLORVALUE specular={lResult.mData[0],lResult.mData[1],lResult.mData[2]}; D3DMATERIAL9* mtrl = new D3DMATERIAL9[1]; mtrl->Ambient = ambient; mtrl->Diffuse = diffuse; mtrl->Specular = specular; D3DXMATERIAL* myMaterial = new D3DXMATERIAL[1]; myMaterial->MatD3D = *mtrl; //FbxFileTexture* lTexture = FbxCast (lProperty.GetSrcObject()); //myMaterial->pTextureFilename = (char *)lTexture->GetFileName(); myMaterial->pTextureFilename="cube.png"; //FbxTexture* pTexture = FbxCast (lProperty.GetSrcObject(0)); LPD3DXBUFFER buffer = 0; hr = D3DXCreateBuffer( sizeof(D3DXMATERIAL), &buffer ); LPVOID ptr = buffer->GetBufferPointer(); memcpy(ptr,myMaterial,sizeof(D3DXMATERIAL)); D3DXMATERIAL *ptr2 = (D3DXMATERIAL*) ptr; *ppMaterials = buffer; return 0; } } HRESULT InitD3D( HWND hWnd ) { // Create the D3D object. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL; // Set up the structure used to create the D3DDevice. Since we are now // using more complex geometry, we will create a device with a zbuffer. D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof( d3dpp ) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16; // Create the D3DDevice if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; } // Turn on the zbuffer g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE ); // Turn on ambient lighting g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0xffffffff ); return S_OK; } //----------------------------------------------------------------------------- // Name: InitGeometry() // Desc: Load the mesh and build the material and texture arrays //----------------------------------------------------------------------------- HRESULT InitGeometry() { LPD3DXBUFFER pD3DXMtrlBuffer; // Load the mesh from the specified file //if( FAILED( D3DXLoadMeshFromX( L"Tiger.x", D3DXMESH_SYSTEMMEM, // g_pd3dDevice, NULL, // &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials, // &g_pMesh ) ) ) //{ // // If model is not in current folder, try parent folder // if( FAILED( D3DXLoadMeshFromX( L"..\\Tiger.x", D3DXMESH_SYSTEMMEM, // g_pd3dDevice, NULL, // &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials, // &g_pMesh ) ) ) // { // MessageBox( NULL, L"Could not find tiger.x", L"Meshes.exe", MB_OK ); // return E_FAIL; // } //} D3DXLoadMeshFromFBX( L"cube1.FBX", D3DXMESH_SYSTEMMEM, g_pd3dDevice, NULL, &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials, &g_pMesh ); // We need to extract the material properties and texture names from the pD3DXMtrlBuffer D3DXMATERIAL* d3dxMaterials = ( D3DXMATERIAL* )pD3DXMtrlBuffer->GetBufferPointer(); g_pMeshMaterials = new D3DMATERIAL9[g_dwNumMaterials]; if( g_pMeshMaterials == NULL ) return E_OUTOFMEMORY; g_pMeshTextures = new LPDIRECT3DTEXTURE9[g_dwNumMaterials]; if( g_pMeshTextures == NULL ) return E_OUTOFMEMORY; 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_pMeshTextures[i] = NULL; if( d3dxMaterials[i].pTextureFilename != NULL && lstrlenA( d3dxMaterials[i].pTextureFilename ) > 0 ) { // Create the texture if( FAILED( D3DXCreateTextureFromFileA( g_pd3dDevice, d3dxMaterials[i].pTextureFilename, &g_pMeshTextures[i] ) ) ) { // If texture is not in current folder, try parent folder const CHAR* strPrefix = "..\\"; CHAR strTexture[MAX_PATH]; strcpy_s( strTexture, MAX_PATH, strPrefix ); strcat_s( strTexture, MAX_PATH, d3dxMaterials[i].pTextureFilename ); // If texture is not in current folder, try parent folder if( FAILED( D3DXCreateTextureFromFileA( g_pd3dDevice, strTexture, &g_pMeshTextures[i] ) ) ) { MessageBox( NULL, L"Could not find texture map", L"Meshes.exe", MB_OK ); } } } } // Done with the material buffer pD3DXMtrlBuffer->Release(); return S_OK; } //----------------------------------------------------------------------------- // Name: Cleanup() // Desc: Releases all previously initialized objects //----------------------------------------------------------------------------- VOID Cleanup() { if( g_pMeshMaterials != NULL ) delete[] g_pMeshMaterials; if( g_pMeshTextures ) { for( DWORD i = 0; i < g_dwNumMaterials; i++ ) { if( g_pMeshTextures[i] ) g_pMeshTextures[i]->Release(); } delete[] g_pMeshTextures; } if( g_pMesh != NULL ) g_pMesh->Release(); if( g_pd3dDevice != NULL ) g_pd3dDevice->Release(); if( g_pD3D != NULL ) g_pD3D->Release(); } //----------------------------------------------------------------------------- // Name: SetupMatrices() // Desc: Sets up the world, view, and projection transform matrices. //----------------------------------------------------------------------------- VOID SetupMatrices() { // Set up world matrix D3DXMATRIXA16 matWorld; D3DXMatrixRotationY( &matWorld, timeGetTime() / 1000.0f ); g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld ); // Set up our view matrix. A view matrix can be defined given an eye point, // a point to lookat, and a direction for which way is up. Here, we set the // eye five units back along the z-axis and up three units, look at the // origin, and define "up" to be in the y-direction. D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f ); D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f ); D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f ); D3DXMATRIXA16 matView; D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec ); g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView ); // For the projection matrix, we set up a perspective transform (which // transforms geometry from 3D view space to 2D viewport space, with // a perspective divide making objects smaller in the distance). To build // a perpsective transform, we need the field of view (1/4 pi is common), // the aspect ratio, and the near and far clipping planes (which define at // what distances geometry should be no longer be rendered). D3DXMATRIXA16 matProj; D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f ); g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj ); } //----------------------------------------------------------------------------- // Name: Render() // Desc: Draws the scene //----------------------------------------------------------------------------- VOID Render() { // Clear the backbuffer and the zbuffer g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 ); // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { // Setup the world, view, and projection matrices SetupMatrices(); // Meshes are divided into subsets, one for each material. Render them in // a loop //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 ); //} /*g_pd3dDevice->SetFVF(D3DFVF_NORMAL); g_pd3dDevice->SetStreamSource(0, g_pMeshTextures, 0, 0);*/ g_pd3dDevice->SetMaterial( &g_pMeshMaterials[0] ); g_pd3dDevice->SetTexture( 0, g_pMeshTextures[0] ); g_pMesh->DrawSubset( 0 ); // End the scene g_pd3dDevice->EndScene(); } // Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); } //----------------------------------------------------------------------------- // Name: MsgProc() // Desc: The window's message handler //----------------------------------------------------------------------------- LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { switch( msg ) { case WM_DESTROY: Cleanup(); PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, msg, wParam, lParam ); } //----------------------------------------------------------------------------- // Name: WinMain() // Desc: The application's entry point //----------------------------------------------------------------------------- INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT ) { UNREFERENCED_PARAMETER( hInst ); // Register the window class WNDCLASSEX wc = { sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle( NULL ), NULL, NULL, NULL, NULL, L"D3D Tutorial", NULL }; RegisterClassEx( &wc ); // Create the application's window HWND hWnd = CreateWindow( L"D3D Tutorial", L"D3D Tutorial 06: Meshes", WS_OVERLAPPEDWINDOW, 100, 100, 600, 600, NULL, NULL, wc.hInstance, NULL ); // Initialize Direct3D if( SUCCEEDED( InitD3D( hWnd ) ) ) { // Create the scene geometry if( SUCCEEDED( InitGeometry() ) ) { // Show the window ShowWindow( hWnd, SW_SHOWDEFAULT ); UpdateWindow( hWnd ); // Enter the message loop MSG msg; ZeroMemory( &msg, sizeof( msg ) ); while( msg.message != WM_QUIT ) { if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else Render(); } } } UnregisterClass( L"D3D Tutorial", wc.hInstance ); return 0; }