1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
#include "StaticModel.h"
#include "StaticModelSurface.h"
#include "ivolumetest.h"
#include "iselectiontest.h"
#include "texturelib.h"
#include "ishaders.h"
#include "modelskin.h"
#include "ifilter.h"
#include "imodelsurface.h"
#include "VolumeIntersectionValue.h"
#include "math/Ray.h"
#include "BasicUndoMemento.h"
namespace model
{
StaticModel::StaticModel(const std::vector<StaticModelSurfacePtr>& surfaces) :
_scaleTransformed(1, 1, 1),
_scale(1, 1, 1),
_undoStateSaver(nullptr)
{
for (const auto& surface : surfaces)
{
auto& inserted = _surfaces.emplace_back(surface);
// Extend the model AABB to include each surface's AABB
_localAABB.includeAABB(inserted.surface->getAABB());
}
}
StaticModel::StaticModel(const StaticModel& other) :
_surfaces(other._surfaces.size()),
_scaleTransformed(other._scaleTransformed),
_scale(other._scale), // use scale of other model
_localAABB(other._localAABB),
_filename(other._filename),
_modelPath(other._modelPath),
_undoStateSaver(nullptr)
{
// Copy the other model's surfaces, but not its shaders, revert to default
for (std::size_t i = 0; i < other._surfaces.size(); ++i)
{
// Copy-construct the other surface, inheriting any applied scale
_surfaces[i].surface = std::make_shared<StaticModelSurface>(*(other._surfaces[i].surface));
_surfaces[i].originalSurface = other._surfaces[i].originalSurface;
_surfaces[i].surface->setActiveMaterial(_surfaces[i].surface->getDefaultMaterial());
}
}
void StaticModel::connectUndoSystem(IUndoSystem& undoSystem)
{
assert(_undoStateSaver == nullptr);
_undoStateSaver = undoSystem.getStateSaver(*this);
}
void StaticModel::disconnectUndoSystem(IUndoSystem& undoSystem)
{
assert(_undoStateSaver != nullptr);
_undoStateSaver = nullptr;
undoSystem.releaseStateSaver(*this);
}
void StaticModel::foreachVisibleSurface(const std::function<void(const Surface& s)>& func) const
{
for (const Surface& surface : _surfaces)
{
assert(surface.shader);
// Check if the surface's shader is filtered, if not then submit it for rendering
const MaterialPtr& surfaceShader = surface.shader->getMaterial();
if (surfaceShader && surfaceShader->isVisible())
{
func(surface);
}
}
}
void StaticModel::setRenderSystem(const RenderSystemPtr& renderSystem)
{
_renderSystem = renderSystem;
captureShaders();
}
std::string StaticModel::getFilename() const
{
return _filename;
}
void StaticModel::setFilename(const std::string& name)
{
_filename = name;
}
// Return vertex count of this model
int StaticModel::getVertexCount() const
{
int sum = 0;
for (const Surface& s : _surfaces)
{
sum += s.surface->getNumVertices();
}
return sum;
}
// Return poly count of this model
int StaticModel::getPolyCount() const
{
int sum = 0;
for (const Surface& s : _surfaces)
{
sum += s.surface->getNumTriangles();
}
return sum;
}
const IModelSurface& StaticModel::getSurface(unsigned surfaceNum) const
{
assert(surfaceNum >= 0 && surfaceNum < _surfaces.size());
return *(_surfaces[surfaceNum].surface);
}
// Apply the given skin to this model
void StaticModel::applySkin(const decl::ISkin::Ptr& skin)
{
// Apply the skin to each surface, then try to capture shaders
for (auto& s : _surfaces)
{
const std::string& defaultMaterial = s.surface->getDefaultMaterial();
const std::string& activeMaterial = s.surface->getActiveMaterial();
// Look up the remap for this surface's material name. If there is a remap
// change the Shader* to point to the new shader.
auto remap = skin ? skin->getRemap(defaultMaterial) : std::string();
if (!remap.empty() && remap != activeMaterial)
{
// Save the remapped shader name
s.surface->setActiveMaterial(remap);
}
else if (remap.empty() && activeMaterial != defaultMaterial)
{
// No remap, so reset our shader to the original unskinned shader
s.surface->setActiveMaterial(defaultMaterial);
}
}
captureShaders();
// greebo: Update the active material list after applying this skin
updateMaterialList();
}
void StaticModel::captureShaders()
{
auto renderSystem = _renderSystem.lock();
// Capture or release our shaders
for (auto& s : _surfaces)
{
if (renderSystem)
{
s.shader = renderSystem->capture(s.surface->getActiveMaterial());
}
else
{
s.shader.reset();
}
}
_sigShadersChanged.emit();
}
sigc::signal<void>& StaticModel::signal_ShadersChanged()
{
return _sigShadersChanged;
}
sigc::signal<void>& StaticModel::signal_SurfaceScaleApplied()
{
return _sigSurfaceScaleApplied;
}
// Update the list of active materials
void StaticModel::updateMaterialList() const
{
_materialList.clear();
for (const auto& s : _surfaces)
{
_materialList.push_back(s.surface->getActiveMaterial());
}
}
// Return the list of active skins for this model
const StringList& StaticModel::getActiveMaterials() const
{
// If the material list is empty, populate it
if (_materialList.empty())
{
updateMaterialList();
}
// Return the list
return _materialList;
}
void StaticModel::testSelect(Selector& selector, SelectionTest& test, const Matrix4& localToWorld)
{
// Perform a volume intersection (AABB) check on each surface. For those
// that intersect, call the surface's own testSelection method to perform
// a proper selection test.
for (const auto& surface : _surfaces)
{
// Check volume intersection
if (test.getVolume().TestAABB(surface.surface->getAABB(), localToWorld) != VOLUME_OUTSIDE)
{
bool twoSided = surface.shader && surface.shader->getMaterial()->getCullType() == Material::CULL_NONE;
// Volume intersection passed, delegate the selection test
surface.surface->testSelect(selector, test, localToWorld, twoSided);
}
}
}
bool StaticModel::getIntersection(const Ray& ray, Vector3& intersection, const Matrix4& localToWorld)
{
Vector3 bestIntersection = ray.origin;
// Test each surface and take the nearest point to the ray origin
for (SurfaceList::iterator i = _surfaces.begin(); i != _surfaces.end(); ++i)
{
Vector3 surfaceIntersection;
if (i->surface->getIntersection(ray, surfaceIntersection, localToWorld))
{
// Test if this surface intersection is better than what we currently have
auto oldDistSquared = (bestIntersection - ray.origin).getLengthSquared();
auto newDistSquared = (surfaceIntersection - ray.origin).getLengthSquared();
if ((oldDistSquared == 0 && newDistSquared > 0) || newDistSquared < oldDistSquared)
{
bestIntersection = surfaceIntersection;
}
}
}
if ((bestIntersection - ray.origin).getLengthSquared() > 0)
{
intersection = bestIntersection;
return true;
}
else
{
return false;
}
}
const StaticModel::SurfaceList& StaticModel::getSurfaces() const
{
return _surfaces;
}
std::string StaticModel::getModelPath() const
{
return _modelPath;
}
void StaticModel::setModelPath(const std::string& modelPath)
{
_modelPath = modelPath;
}
bool StaticModel::revertScale()
{
if (_scaleTransformed == _scale) return false;
_scaleTransformed = _scale;
return true;
}
void StaticModel::evaluateScale(const Vector3& scale)
{
_scaleTransformed *= scale;
applyScaleToSurfaces();
}
void StaticModel::applyScaleToSurfaces()
{
_localAABB = AABB();
// Apply the scale to each surface
for (Surface& surf : _surfaces)
{
// Are we still using the original surface? If yes,
// it's now time to create a working copy
if (surf.surface == surf.originalSurface)
{
// Copy-construct the surface
surf.surface = std::make_shared<StaticModelSurface>(*surf.originalSurface);
}
// Apply the scale, on top of the original surface, this should save us from
// reverting the transformation each time the scale changes
surf.surface->applyScale(_scaleTransformed, *(surf.originalSurface));
// Extend the model AABB to include the surface's AABB
_localAABB.includeAABB(surf.surface->getAABB());
}
// Notify the model node to queue a renderable update
_sigSurfaceScaleApplied.emit();
}
// Freeze transform, move the applied scale to the original model
void StaticModel::freezeScale()
{
undoSave();
// Apply the scale to each surface
_scale = _scaleTransformed;
}
void StaticModel::undoSave()
{
if (_undoStateSaver != nullptr)
{
_undoStateSaver->saveState();
}
}
IUndoMementoPtr StaticModel::exportState() const
{
return IUndoMementoPtr(new undo::BasicUndoMemento<Vector3>(_scale));
}
void StaticModel::importState(const IUndoMementoPtr& state)
{
undoSave();
_scale = std::static_pointer_cast< undo::BasicUndoMemento<Vector3> >(state)->data();
_scaleTransformed = _scale;
applyScaleToSurfaces();
}
const Vector3& StaticModel::getScale() const
{
return _scale;
}
void StaticModel::foreachSurface(const std::function<void(const StaticModelSurface&)>& func) const
{
for (const Surface& surf : _surfaces)
{
func(*surf.surface);
}
}
} // namespace
|