[2197] | 1 | #include "dxstdafx.h"
|
---|
| 2 | #include ".\entity.h"
|
---|
| 3 | #include "SubEntity.h"
|
---|
| 4 | #include "Mesh.h"
|
---|
| 5 | #include "SubMesh.h"
|
---|
| 6 | #include <fstream>
|
---|
| 7 |
|
---|
| 8 | Entity::Entity(Mesh* baseMesh, const char* entityName, const char* prmFileName, const D3DXMATRIX& trafo, int nNearClusters)
|
---|
| 9 | {
|
---|
| 10 | surfaceArea = -1.0;
|
---|
| 11 |
|
---|
| 12 | modelWorldTransform = trafo;
|
---|
| 13 | D3DXMatrixInverse(&inverseTransposedModelWorldTransform, NULL, &modelWorldTransform);
|
---|
| 14 |
|
---|
| 15 | strcpy(this->name, entityName);
|
---|
| 16 | strcpy(this->prmFileName, prmFileName);
|
---|
| 17 |
|
---|
| 18 | this->mesh = baseMesh;
|
---|
| 19 | this->nNearClusters = nNearClusters;
|
---|
| 20 |
|
---|
| 21 | std::vector<SubMesh*>::iterator iSubMesh = baseMesh->subMeshes.begin();
|
---|
| 22 | while(iSubMesh != baseMesh->subMeshes.end())
|
---|
| 23 | {
|
---|
| 24 | SubEntity* nse = new SubEntity(this, *iSubMesh, nNearClusters);
|
---|
| 25 | subEntities.push_back(nse);
|
---|
| 26 | iSubMesh++;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | Entity::~Entity(void)
|
---|
| 32 | {
|
---|
| 33 | std::vector<SubEntity*>::iterator subEntityIterator = subEntities.begin();
|
---|
| 34 | while(subEntityIterator != subEntities.end())
|
---|
| 35 | {
|
---|
| 36 | delete (*subEntityIterator);
|
---|
| 37 | subEntityIterator++;
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | void Entity::drawAllSubEntities(const RenderStrategy& renderStrategy)
|
---|
| 42 | {
|
---|
| 43 | renderStrategy.applyTransforms(this);
|
---|
| 44 |
|
---|
| 45 | std::vector<SubEntity*>::iterator subEntityIterator = subEntities.begin();
|
---|
| 46 | while(subEntityIterator != subEntities.end())
|
---|
| 47 | {
|
---|
| 48 | (*subEntityIterator)->draw(renderStrategy);
|
---|
| 49 | subEntityIterator++;
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | float Entity::getSurfaceArea()
|
---|
| 54 | {
|
---|
| 55 | if(surfaceArea > 0.0)
|
---|
| 56 | return surfaceArea;
|
---|
| 57 | float area = 0;
|
---|
| 58 | std::vector<SubEntity*>::iterator subEntityIterator = subEntities.begin();
|
---|
| 59 | while(subEntityIterator != subEntities.end())
|
---|
| 60 | {
|
---|
| 61 | area += (*subEntityIterator)->getSurfaceArea();
|
---|
| 62 | subEntityIterator++;
|
---|
| 63 | }
|
---|
| 64 | this->surfaceArea = area;
|
---|
| 65 | return area;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | void Entity::findNearClusters(const std::vector<Radion>& starters, unsigned int nClusters)
|
---|
| 69 | {
|
---|
| 70 | std::vector<SubEntity*>::iterator subEntityIterator = subEntities.begin();
|
---|
| 71 | while(subEntityIterator != subEntities.end())
|
---|
| 72 | {
|
---|
| 73 | (*subEntityIterator)->findNearClusters(starters, nClusters);
|
---|
| 74 | subEntityIterator++;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | void Entity::renderPRM( std::vector<Radion>& clusterRadions, unsigned int clusterId)
|
---|
| 80 | {
|
---|
| 81 | std::vector<SubEntity*>::iterator subEntityIterator = subEntities.begin();
|
---|
| 82 | while(subEntityIterator != subEntities.end())
|
---|
| 83 | {
|
---|
| 84 | (*subEntityIterator)->renderPRM(clusterRadions, clusterId);
|
---|
| 85 | subEntityIterator++;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | void Entity::normalizePRM()
|
---|
| 91 | {
|
---|
| 92 | std::vector<SubEntity*>::iterator subEntityIterator = subEntities.begin();
|
---|
| 93 | while(subEntityIterator != subEntities.end())
|
---|
| 94 | {
|
---|
| 95 | (*subEntityIterator)->normalizePRM();
|
---|
| 96 | subEntityIterator++;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | void Entity::clearPRM()
|
---|
| 101 | {
|
---|
| 102 | std::vector<SubEntity*>::iterator subEntityIterator = subEntities.begin();
|
---|
| 103 | while(subEntityIterator != subEntities.end())
|
---|
| 104 | {
|
---|
| 105 | (*subEntityIterator)->clearPRM();
|
---|
| 106 | subEntityIterator++;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | void Entity::savePRM()
|
---|
| 111 | {
|
---|
| 112 | std::vector<SubEntity*>::iterator subEntityIterator = subEntities.begin();
|
---|
| 113 | while(subEntityIterator != subEntities.end())
|
---|
| 114 | {
|
---|
| 115 | (*subEntityIterator)->savePRM();
|
---|
| 116 | subEntityIterator++;
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | void Entity::loadPRM()
|
---|
| 121 | {
|
---|
| 122 | std::vector<SubEntity*>::iterator subEntityIterator = subEntities.begin();
|
---|
| 123 | while(subEntityIterator != subEntities.end())
|
---|
| 124 | {
|
---|
| 125 | (*subEntityIterator)->loadPRM();
|
---|
| 126 | subEntityIterator++;
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | void Entity::gatherRayTraceEntities(std::vector<Intersectable*>& rayTraceEntities)
|
---|
| 131 | {
|
---|
| 132 | std::vector<SubEntity*>::iterator subEntityIterator = subEntities.begin();
|
---|
| 133 | while(subEntityIterator != subEntities.end())
|
---|
| 134 | {
|
---|
| 135 | rayTraceEntities.push_back((*subEntityIterator)->getRayTraceEntity());
|
---|
| 136 | subEntityIterator++;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | const Material* Entity::sampleSurface(Radion& sample)
|
---|
| 141 | {
|
---|
| 142 | float sarea = getSurfaceArea();
|
---|
| 143 | float randa = ((double)rand() / RAND_MAX) * sarea;
|
---|
| 144 |
|
---|
| 145 | float uptonowSurfaceArea = 0.0;
|
---|
| 146 | SubEntity* e = NULL;
|
---|
| 147 | std::vector<SubEntity*>::iterator sei = subEntities.begin();
|
---|
| 148 | while(sei != subEntities.end())
|
---|
| 149 | {
|
---|
| 150 | uptonowSurfaceArea += (*sei)->getSurfaceArea();
|
---|
| 151 | if(uptonowSurfaceArea >= randa)
|
---|
| 152 | {
|
---|
| 153 | e = *sei;
|
---|
| 154 | break;
|
---|
| 155 | }
|
---|
| 156 | sei++;
|
---|
| 157 | }
|
---|
| 158 | const Material* ret = e->sampleSurface(sample);
|
---|
| 159 | sample.radiance.z = sarea;
|
---|
| 160 | return ret;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | void Entity::saveSceneInfo(std::ofstream& psf)
|
---|
| 164 | {
|
---|
| 165 | psf << "entity " << name << '\n';
|
---|
| 166 | psf << "{\n";
|
---|
| 167 | psf << "\tmesh " << mesh->getName() << '\n';
|
---|
| 168 | psf << "\ttransformation ";
|
---|
| 169 | for(int mi=0; mi<16; mi++)
|
---|
| 170 | psf << modelWorldTransform.m[mi%4][mi/4] << " ";
|
---|
| 171 | psf << "\n";
|
---|
| 172 | psf << "\tpathmapfile " << prmFileName << '\n';
|
---|
| 173 | psf << "\tpathmapclusters " << nNearClusters << '\n';
|
---|
| 174 | std::vector<SubEntity*>::iterator sei = subEntities.begin();
|
---|
| 175 | while(sei != subEntities.end())
|
---|
| 176 | {
|
---|
| 177 | (*sei)->saveSceneInfo(psf);
|
---|
| 178 | sei++;
|
---|
| 179 | }
|
---|
| 180 | psf << "}\n";
|
---|
| 181 | } |
---|