#include "dxstdafx.h" #include ".\submesh.h" #include "Mesh.h" #include "PathMapEffect.h" #include "TriangleMesh.h" #include "L.h" SubMesh::SubMesh(Mesh* parent, int subsetId, D3DXMATERIAL& dMaterial) { this->parent = parent; this->subsetId = subsetId; rayTraceMesh = NULL; bumpTexture = NULL; normalTexture = NULL; material = dMaterial; if(material.pTextureFilename) { material.pTextureFilename = new char[strlen(dMaterial.pTextureFilename) + 1]; strcpy(material.pTextureFilename, dMaterial.pTextureFilename); //set submesh textures from material and shader properties (combine partition and attribute buffer) } lambertTexture = parent->pathMapEffect->loadTexture( L::l+dMaterial.pTextureFilename, &rayTraceMaterial); } void SubMesh::buildRayTraceMesh(DWORD startIndex, DWORD indexCount) { //lock the buffers and create ray tracing meshes from the data LPDIRECT3DVERTEXBUFFER9 vertexBuffer; parent->mesh->GetVertexBuffer(&vertexBuffer); FlexVertexArray vertexData(parent->mesh->GetNumBytesPerVertex()); D3DVERTEXELEMENT9 elem[64]; parent->mesh->GetDeclaration(elem); vertexData.setFormat(elem); vertexBuffer->Lock(0,parent->mesh->GetNumVertices()*parent->mesh->GetNumBytesPerVertex(),vertexData.getDataPointerReference(),0); LPDIRECT3DINDEXBUFFER9 indexBuffer; parent->mesh->GetIndexBuffer(&indexBuffer); unsigned short* indexData; indexBuffer->Lock(startIndex*sizeof(unsigned short),indexCount*sizeof(unsigned short),(void**)&indexData,0); //create a TriangleMesh for ray-tracing rayTraceMesh = new TriangleMesh(rayTraceMaterial, indexData, indexCount/3, vertexData, parent->mesh->GetNumVertices()); vertexBuffer->Unlock(); indexBuffer->Unlock(); vertexBuffer->Release(); indexBuffer->Release(); } SubMesh::~SubMesh(void) { if(normalTexture) normalTexture->Release(); if(rayTraceMesh) delete rayTraceMesh; } void SubMesh::draw() { parent->mesh->DrawSubset(subsetId); } void SubMesh::drawToAtlas() { parent->mesh->DrawSubset(subsetId); LPDIRECT3DDEVICE9 device = parent->pathMapEffect->device; if(edgeCount) { parent->pathMapEffect->effect->SetBool("bred", true); parent->pathMapEffect->effect->CommitChanges(); device->SetStreamSource(0, parent->edgeVertexBuffer, 0, parent->mesh->GetNumBytesPerVertex()); device->DrawPrimitive(D3DPT_LINELIST, edgeStartIndex * 2, edgeCount); parent->pathMapEffect->effect->SetBool("bred", false); } } void SubMesh::loadBumpMap(LPCSTR fileName, float bumpScale) { bumpTexture = NULL; normalTexture = NULL; if(fileName[0] == '\0') return; LPDIRECT3DDEVICE9 device = parent->pathMapEffect->device; bumpTexture = parent->pathMapEffect->loadTexture( L::l+fileName ); strcpy(bumpFileName, fileName); HRESULT hrwft; if(bumpTexture) { D3DSURFACE_DESC bDesc; bumpTexture->GetLevelDesc(0, &bDesc); hrwft = device->CreateTexture(bDesc.Width, bDesc.Height, 1, bDesc.Usage, D3DFMT_A16B16G16R16F, bDesc.Pool, &normalTexture, NULL); hrwft = D3DXComputeNormalMap( normalTexture, bumpTexture, NULL, D3DX_NORMALMAP_COMPUTE_OCCLUSION, D3DX_CHANNEL_RED, bumpScale); } else normalTexture = NULL; }