//================================================================================================= void Terrain::Rebuild(bool smooth) { assert(state == 2); VTerrain* v; V(mesh->LockVertexBuffer(0, (void**)&v)); #define TRI(xx,zz) v[n++].pos.y = h[x+xx+(z+zz)*hszer] uint n = 0; for(uint z = 0; z < n_tiles; ++z) { for(uint x = 0; x < n_tiles; ++x) { TRI(0, 0); TRI(0, 1); TRI(1, 0); CalculateNormal(v[n - 3], v[n - 2], v[n - 1]); TRI(0, 1); TRI(1, 1); TRI(1, 0); CalculateNormal(v[n - 3], v[n - 2], v[n - 1]); } } #undef TRI if(smooth) SmoothNormals(v); V(mesh->UnlockVertexBuffer()); }
//================================================================================================= void Terrain::SmoothNormals() { assert(state > 0); VTerrain* v; V(mesh->LockVertexBuffer(0, (void**)&v)); SmoothNormals(v); V(mesh->UnlockVertexBuffer()); }
//================================================================================================= void Terrain::Build(bool smooth) { assert(state == 1); HRESULT hr = D3DXCreateMeshFVF(n_tris, n_verts, D3DXMESH_MANAGED | D3DXMESH_32BIT, VTerrain::fvf, device, &mesh); if(FAILED(hr)) throw Format("Failed to create new terrain mesh (%d)!", hr); VTerrain* v; //word* idx; uint* idx; uint n = 0; //uint index = 0; V(mesh->LockVertexBuffer(0, (void**)&v)); V(mesh->LockIndexBuffer(0, (void**)&idx)); #define TRI(xx,zz,uu,vv) v[n++] = VTerrain((x+xx)*tile_size, h[x+xx+(z+zz)*hszer], (z+zz)*tile_size, float(uu)/uv_mod, float(vv)/uv_mod,\ ((float)(x+xx)) / n_tiles, ((float)(z+zz)) / n_tiles) for(uint z = 0; z < n_tiles; ++z) { for(uint x = 0; x < n_tiles; ++x) { int u1 = (x%uv_mod); int u2 = ((x + 1) % uv_mod); if(u2 == 0) u2 = uv_mod; int v1 = (z%uv_mod); int v2 = ((z + 1) % uv_mod); if(v2 == 0) v2 = uv_mod; TRI(0, 0, u1, v1); TRI(0, 1, u1, v2); TRI(1, 0, u2, v1); CalculateNormal(v[n - 3], v[n - 2], v[n - 1]); TRI(0, 1, u1, v2); TRI(1, 1, u2, v2); TRI(1, 0, u2, v1); CalculateNormal(v[n - 3], v[n - 2], v[n - 1]); } } #undef TRI for(uint z = 0; z < n_parts; ++z) { for(uint x = 0; x < n_parts; ++x) { const uint z_start = z*tiles_per_part, z_end = z_start + tiles_per_part, x_start = x*tiles_per_part, x_end = x_start + tiles_per_part; for(uint zz = z_start; zz < z_end; ++zz) { for(uint xx = x_start; xx < x_end; ++xx) { for(uint j = 0; j < 6; ++j) { *idx = (xx + zz*n_tiles) * 6 + j; ++idx; } } } } } V(mesh->UnlockIndexBuffer()); state = 2; if(smooth) SmoothNormals(v); V(mesh->UnlockVertexBuffer()); }
// Here we process mesh geometry for given node. // Vertices for the geometry from the all meshes of the scene are allocated in // one big vertex pool. One vertex can occur several times in this pool, because // we add 3 new vertices for every face. // After all meshes in the scene are processed, this pool is collapsed to remove // duplicate vertices. void RBExport::ProcessMesh( INode* node ) { if (!node->Renderable() || node->IsNodeHidden()) { return; } ObjectState os = node->EvalWorldState( m_CurTime ); Object* pObject = os.obj; if (!pObject) { Warn( "Could not evaluate object state in node <%s>. Mesh was skipped.", node->GetName() ); return; } Matrix3 nodeTM = node->GetNodeTM( m_CurTime ); Matrix3 nodeTMAfterWSM = node->GetObjTMAfterWSM( m_CurTime ); Matrix3 mOffs = nodeTMAfterWSM*Inverse( nodeTM ); // triangulate TriObject* pTriObj = 0; if (pObject->CanConvertToType( Class_ID( TRIOBJ_CLASS_ID, 0 ) )) { pTriObj = (TriObject*)pObject->ConvertToType( m_CurTime, Class_ID( TRIOBJ_CLASS_ID, 0 ) ); } bool bReleaseTriObj = (pTriObj != pObject); if (!pTriObj) { Warn( "Could not triangulate mesh in node <%s>. Node was skipped", node->GetName() ); return; } if (!MeshModStackIsValid( node )) { Warn( "Modifier stack for node %s should be collapsed or contain only Skin modifier.", node->GetName() ); } // ensure, that vertex winding direction in polygon is CCW Matrix3 objTM = node->GetObjTMAfterWSM( m_CurTime ); bool bNegScale = (DotProd( CrossProd( objTM.GetRow(0), objTM.GetRow(1) ), objTM.GetRow(2) ) >= 0.0); int vx[3]; if (bNegScale) { vx[0] = 0; vx[1] = 1; vx[2] = 2; } else { vx[0] = 2; vx[1] = 1; vx[2] = 0; } Mesh& mesh = pTriObj->GetMesh(); bool bHasTexCoord = (mesh.numTVerts != 0); bool bHasVertexColor = (mesh.numCVerts != 0) && m_pConfig->m_bExportVertexColors; bool bHasNormal = m_pConfig->m_bExportNormals; bool bHasSkin = ProcessSkin( node ); // some cosmetics BOOL res = mesh.RemoveDegenerateFaces(); if (res) { Spam( "Degenerate faces were fixed." ); } res = mesh.RemoveIllegalFaces(); if (res) { Spam( "Degenerate indices were fixed." ); } ExpNode* pExpNode = GetExportedNode( node ); if (!pExpNode) { return; } int numPri = mesh.getNumFaces(); int numVert = mesh.numVerts; // initialize helper array for building vertices' 0-circles VertexPtrArray vertexEntry; vertexEntry.resize( numVert ); memset( &vertexEntry[0], 0, sizeof( ExpVertex* )*numVert ); Spam( "Original mesh has %d vertices and %d faces.", numVert, numPri ); bool bHasVoidFaces = false; // loop on mesh faces for (int i = 0; i < numPri; i++) { Face& face = mesh.faces[i]; // calculate face normal const Point3& v0 = mesh.verts[face.v[vx[0]]]; const Point3& v1 = mesh.verts[face.v[vx[1]]]; const Point3& v2 = mesh.verts[face.v[vx[2]]]; Point3 normal = -Normalize( (v1 - v0)^(v2 - v1) ); normal = mOffs.VectorTransform( normal ); normal = c_FlipTM.VectorTransform( normal ); // loop on face vertices ExpVertex* pFaceVertex[3]; for (int j = 0; j < 3; j++) { ExpVertex v; v.mtlID = pExpNode->GetMaterialIdx( face.getMatID() ); v.nodeID = pExpNode->m_Index; if (v.mtlID < 0) { bHasVoidFaces = true; } // extract vertex position and apply world-space modifier to it int vIdx = face.v[vx[j]]; Point3 pt = mesh.verts[vIdx]; pt = mOffs.PointTransform( pt ); pt = c_FlipTM.PointTransform( pt ); v.index = vIdx; v.pos = Vec3( pt.x, pt.y, pt.z ); //v.pos *= m_WorldScale; // extract skinning info if (bHasSkin) { GetSkinInfo( v ); } // assign normal if (bHasNormal) { v.normal = Vec3( normal.x, normal.y, normal.z ); v.smGroup = face.smGroup; } // extract vertex colors if (bHasVertexColor) { const VertColor& vcol = mesh.vertCol[mesh.vcFace[i].t[vx[j]]]; v.color = ColorToDWORD( Color( vcol.x, vcol.y, vcol.z ) ); } // extract texture coordinates if (bHasTexCoord) { const Point3& texCoord = mesh.tVerts[mesh.tvFace[i].t[vx[j]]]; v.uv.x = texCoord.x; v.uv.y = 1.0f - texCoord.y; // second texture coordinate channel if (mesh.getNumMaps() > 1 && mesh.mapSupport( 2 )) { UVVert* pUVVert = mesh.mapVerts( 2 ); TVFace* pTVFace = mesh.mapFaces( 2 ); if (pUVVert && pTVFace) { const Point3& tc2 = pUVVert[pTVFace[i].t[vx[j]]]; v.uv2.x = tc2.x; v.uv2.y = 1.0f - tc2.y; } } } // allocate new vertex pFaceVertex[j] = AddVertex( v ); // we want vertices in the 0-ring neighborhood to be linked into closed linked list if (vertexEntry[vIdx] == NULL) { vertexEntry[vIdx] = pFaceVertex[j]; pFaceVertex[j]->pNext = pFaceVertex[j]; } else { vertexEntry[vIdx]->AddToZeroRing( pFaceVertex[j] ); } } AddPolygon( pFaceVertex[0], pFaceVertex[1], pFaceVertex[2] ); if (IsCanceled()) { return; } } // correct normals at vertices corresponding to smoothing groups SmoothNormals( vertexEntry ); // put vertices into set (this removes duplicate vertices) for (int i = 0; i < numVert; i++) { ExpVertex::ZeroRingIterator it( vertexEntry[i] ); while (it) { VertexSet::iterator sIt = m_VertexSet.find( it ); if (sIt == m_VertexSet.end()) { m_VertexSet.insert( it ); } else { it->pBase = (*sIt); } ++it; } } if (bReleaseTriObj) { delete pTriObj; } if (bHasVoidFaces) { Warn( "Mesh %s has faces with no material assigned.", node->GetName() ); } } // RBExport::ProcessMesh