source: GTP/trunk/App/Demos/Illum/pathmap/SubMesh.cpp @ 2498

Revision 2498, 3.2 KB checked in by szirmay, 17 years ago (diff)
Line 
1#include "dxstdafx.h"
2#include ".\submesh.h"
3#include "Mesh.h"
4#include "PathMapEffect.h"
5#include "TriangleMesh.h"
6#include "L.h"
7
8SubMesh::SubMesh(Mesh* parent, int subsetId, D3DXMATERIAL& dMaterial)
9{
10        this->parent = parent;
11        this->subsetId = subsetId;
12
13        rayTraceMesh = NULL;
14
15        bumpTexture = NULL;
16        normalTexture = NULL;
17
18        material = dMaterial;
19        if(material.pTextureFilename)
20        {
21                material.pTextureFilename = new char[strlen(dMaterial.pTextureFilename) + 1];
22                strcpy(material.pTextureFilename, dMaterial.pTextureFilename);
23                //set submesh textures from material and shader properties (combine partition and attribute buffer)
24        }
25
26        lambertTexture = parent->pathMapEffect->loadTexture( L::l+dMaterial.pTextureFilename, &rayTraceMaterial);
27
28}
29
30void SubMesh::buildRayTraceMesh(DWORD startIndex, DWORD indexCount)
31{
32        //lock the buffers and create ray tracing meshes from the data
33        LPDIRECT3DVERTEXBUFFER9 vertexBuffer;
34        parent->mesh->GetVertexBuffer(&vertexBuffer);
35        FlexVertexArray vertexData(parent->mesh->GetNumBytesPerVertex());
36        D3DVERTEXELEMENT9 elem[64];     parent->mesh->GetDeclaration(elem);      vertexData.setFormat(elem);
37        vertexBuffer->Lock(0,parent->mesh->GetNumVertices()*parent->mesh->GetNumBytesPerVertex(),vertexData.getDataPointerReference(),0);
38        LPDIRECT3DINDEXBUFFER9 indexBuffer;
39        parent->mesh->GetIndexBuffer(&indexBuffer);
40        unsigned short* indexData;
41        indexBuffer->Lock(startIndex*sizeof(unsigned short),indexCount*sizeof(unsigned short),(void**)&indexData,0);
42
43        //create a TriangleMesh for ray-tracing
44        rayTraceMesh = new TriangleMesh(rayTraceMaterial, indexData, indexCount/3,
45                vertexData, parent->mesh->GetNumVertices());
46
47        vertexBuffer->Unlock();
48        indexBuffer->Unlock();
49        vertexBuffer->Release();
50        indexBuffer->Release();
51}
52
53SubMesh::~SubMesh(void)
54{
55        if(normalTexture)
56                normalTexture->Release();
57        if(rayTraceMesh)
58                delete rayTraceMesh;
59}
60
61void SubMesh::draw()
62{
63        parent->mesh->DrawSubset(subsetId);
64}
65
66void SubMesh::drawToAtlas()
67{
68        parent->mesh->DrawSubset(subsetId);
69
70        LPDIRECT3DDEVICE9 device = parent->pathMapEffect->device;
71
72       
73        if(edgeCount)
74        {
75                parent->pathMapEffect->effect->SetBool("bred", true);
76                parent->pathMapEffect->effect->CommitChanges();
77                device->SetStreamSource(0, parent->edgeVertexBuffer, 0, parent->mesh->GetNumBytesPerVertex());
78                device->DrawPrimitive(D3DPT_LINELIST, edgeStartIndex * 2, edgeCount);
79                parent->pathMapEffect->effect->SetBool("bred", false);
80        }
81}
82
83void SubMesh::loadBumpMap(LPCSTR fileName, float bumpScale)
84{
85        bumpTexture = NULL;
86        normalTexture = NULL;
87
88        if(fileName[0] == '\0')
89                return;
90
91        LPDIRECT3DDEVICE9 device = parent->pathMapEffect->device;
92
93        bumpTexture = parent->pathMapEffect->loadTexture( L::l+fileName );
94        strcpy(bumpFileName, fileName);
95
96        HRESULT hrwft;
97        if(bumpTexture)
98        {
99                D3DSURFACE_DESC bDesc;
100                bumpTexture->GetLevelDesc(0, &bDesc);
101                hrwft = device->CreateTexture(bDesc.Width, bDesc.Height, 1, bDesc.Usage, D3DFMT_A16B16G16R16F, bDesc.Pool,
102                        &normalTexture, NULL);
103                hrwft = D3DXComputeNormalMap(
104                        normalTexture,
105                        bumpTexture,
106                        NULL,
107                        D3DX_NORMALMAP_COMPUTE_OCCLUSION,
108                        D3DX_CHANNEL_RED,
109                        bumpScale);
110        }
111        else
112                normalTexture = NULL;
113
114}
Note: See TracBrowser for help on using the repository browser.