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

Revision 2197, 3.0 KB checked in by szirmay, 18 years ago (diff)
RevLine 
[2197]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);      FlexVertex::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        if(edgeCount)
73        {
74                device->SetStreamSource(0, parent->edgeVertexBuffer, 0, parent->mesh->GetNumBytesPerVertex());
75                device->DrawPrimitive(D3DPT_LINELIST, edgeStartIndex * 2, edgeCount);
76        }
77}
78
79void SubMesh::loadBumpMap(LPCSTR fileName, float bumpScale)
80{
81        bumpTexture = NULL;
82        normalTexture = NULL;
83
84        if(fileName[0] == '\0')
85                return;
86
87        LPDIRECT3DDEVICE9 device = parent->pathMapEffect->device;
88
89        bumpTexture = parent->pathMapEffect->loadTexture( L::l+fileName );
90        strcpy(bumpFileName, fileName);
91
92        HRESULT hrwft;
93        if(bumpTexture)
94        {
95                D3DSURFACE_DESC bDesc;
96                bumpTexture->GetLevelDesc(0, &bDesc);
97                hrwft = device->CreateTexture(bDesc.Width, bDesc.Height, 1, bDesc.Usage, D3DFMT_A16B16G16R16F, bDesc.Pool,
98                        &normalTexture, NULL);
99                hrwft = D3DXComputeNormalMap(
100                        normalTexture,
101                        bumpTexture,
102                        NULL,
103                        D3DX_NORMALMAP_COMPUTE_OCCLUSION,
104                        D3DX_CHANNEL_RED,
105                        bumpScale);
106        }
107        else
108                normalTexture = NULL;
109
110}
Note: See TracBrowser for help on using the repository browser.