From 1adf2cc8ba7b20c9c0e9ab9e30f93622a49a9167 Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 8 Dec 2015 16:45:12 -0800 Subject: [PATCH] Repesenting the collision meshes of a Model correctly --- libraries/gpu/src/gpu/Stream.h | 1 + .../render-utils/src/MeshPartPayload.cpp | 72 ++++++++++--------- libraries/render-utils/src/MeshPartPayload.h | 4 +- libraries/render-utils/src/Model.cpp | 5 +- 4 files changed, 45 insertions(+), 37 deletions(-) diff --git a/libraries/gpu/src/gpu/Stream.h b/libraries/gpu/src/gpu/Stream.h index 4fff3b651d..d2fb8a37e9 100644 --- a/libraries/gpu/src/gpu/Stream.h +++ b/libraries/gpu/src/gpu/Stream.h @@ -106,6 +106,7 @@ public: bool setAttribute(Slot slot, Frequency frequency = PER_VERTEX); bool setAttribute(Slot slot, Slot channel, Frequency frequency = PER_VERTEX); + bool hasAttribute(Slot slot) const { return (_attributes.find(slot) != _attributes.end()); } protected: AttributeMap _attributes; diff --git a/libraries/render-utils/src/MeshPartPayload.cpp b/libraries/render-utils/src/MeshPartPayload.cpp index 456f6eac51..3aee6cb62c 100644 --- a/libraries/render-utils/src/MeshPartPayload.cpp +++ b/libraries/render-utils/src/MeshPartPayload.cpp @@ -39,30 +39,31 @@ namespace render { using namespace render; -MeshPartPayload::MeshPartPayload(Model* model, int meshIndex, int partIndex, int shapeIndex, - glm::vec3 position, glm::quat orientation) : +MeshPartPayload::MeshPartPayload(Model* model, model::MeshPointer drawMesh, int meshIndex, int partIndex, int shapeIndex, + glm::vec3 position, glm::quat orientation, bool applyMeshJoints) : model(model), + _drawMesh(drawMesh), meshIndex(meshIndex), partIndex(partIndex), _shapeID(shapeIndex), _modelPosition(position), - _modelOrientation(orientation) { + _modelOrientation(orientation), + _applyMeshJoints(applyMeshJoints) { initCache(); } void MeshPartPayload::initCache() { - const std::vector>& networkMeshes = model->_geometry->getMeshes(); - const NetworkMesh& networkMesh = *(networkMeshes.at(meshIndex).get()); - _drawMesh = networkMesh._mesh; + if (_drawMesh) { + auto vertexFormat = _drawMesh->getVertexFormat(); + _hasColorAttrib = vertexFormat->hasAttribute(gpu::Stream::COLOR); + _isSkinned = vertexFormat->hasAttribute(gpu::Stream::SKIN_CLUSTER_WEIGHT) && vertexFormat->hasAttribute(gpu::Stream::SKIN_CLUSTER_INDEX); - const FBXGeometry& geometry = model->_geometry->getFBXGeometry(); - const FBXMesh& mesh = geometry.meshes.at(meshIndex); - _hasColorAttrib = !mesh.colors.isEmpty(); - _isBlendShaped = !mesh.blendshapes.isEmpty(); - _isSkinned = !mesh.clusterIndices.isEmpty(); + const FBXGeometry& geometry = model->_geometry->getFBXGeometry(); + const FBXMesh& mesh = geometry.meshes.at(meshIndex); + _isBlendShaped = !mesh.blendshapes.isEmpty(); - - _drawPart = _drawMesh->getPartBuffer().get(partIndex); + _drawPart = _drawMesh->getPartBuffer().get(partIndex); + } auto networkMaterial = model->_geometry->getShapeMaterial(_shapeID); if (networkMaterial) { @@ -219,25 +220,34 @@ void MeshPartPayload::bindMaterial(gpu::Batch& batch, const ModelRender::Locatio } void MeshPartPayload::bindTransform(gpu::Batch& batch, const ModelRender::Locations* locations) const { - // Still relying on the raw data from the model - const Model::MeshState& state = model->_meshStates.at(meshIndex); + if (_applyMeshJoints) { + // Still relying on the raw data from the model + const Model::MeshState& state = model->_meshStates.at(meshIndex); - Transform transform; - if (state.clusterBuffer) { - if (model->_cauterizeBones) { - batch.setUniformBuffer(ModelRender::SKINNING_GPU_SLOT, state.cauterizedClusterBuffer); + Transform transform; + if (state.clusterBuffer) { + if (model->_cauterizeBones) { + batch.setUniformBuffer(ModelRender::SKINNING_GPU_SLOT, state.cauterizedClusterBuffer); + } else { + batch.setUniformBuffer(ModelRender::SKINNING_GPU_SLOT, state.clusterBuffer); + } } else { - batch.setUniformBuffer(ModelRender::SKINNING_GPU_SLOT, state.clusterBuffer); + if (model->_cauterizeBones) { + transform = Transform(state.cauterizedClusterMatrices[0]); + } else { + transform = Transform(state.clusterMatrices[0]); + } } + transform.preTranslate(_modelPosition); + batch.setModelTransform(transform); } else { - if (model->_cauterizeBones) { - transform = Transform(state.cauterizedClusterMatrices[0]); - } else { - transform = Transform(state.clusterMatrices[0]); - } + Transform transform; + transform.setTranslation(_modelPosition); + transform.setRotation(_modelOrientation); + transform.postScale(model->getScale()); + transform.postTranslate(model->getOffset()); + batch.setModelTransform(transform); } - transform.preTranslate(_modelPosition); - batch.setModelTransform(transform); } @@ -280,13 +290,7 @@ void MeshPartPayload::render(RenderArgs* args) const { // sanity check return; // FIXME! } - - - // guard against partially loaded meshes - if (partIndex >= mesh.parts.size()) { - return; - } - + model::MaterialKey drawMaterialKey; if (_drawMaterial) { drawMaterialKey = _drawMaterial->getKey(); diff --git a/libraries/render-utils/src/MeshPartPayload.h b/libraries/render-utils/src/MeshPartPayload.h index 5d30eddd20..867dd2d264 100644 --- a/libraries/render-utils/src/MeshPartPayload.h +++ b/libraries/render-utils/src/MeshPartPayload.h @@ -24,7 +24,7 @@ class Model; class MeshPartPayload { public: - MeshPartPayload(Model* model, int meshIndex, int partIndex, int shapeIndex, glm::vec3 position, glm::quat orientation); + MeshPartPayload(Model* model, model::MeshPointer drawMesh, int meshIndex, int partIndex, int shapeIndex, glm::vec3 position, glm::quat orientation, bool applyMeshJoints = true); typedef render::Payload Payload; typedef Payload::DataPointer Pointer; @@ -36,6 +36,7 @@ public: glm::vec3 _modelPosition; glm::quat _modelOrientation; + // can replace the material used to draw that item void updateDrawMaterial(model::MaterialPointer material); @@ -62,6 +63,7 @@ public: bool _hasColorAttrib = false; bool _isSkinned = false; bool _isBlendShaped = false; + bool _applyMeshJoints = true; }; namespace render { diff --git a/libraries/render-utils/src/Model.cpp b/libraries/render-utils/src/Model.cpp index 96fd52edcc..56c1f642b8 100644 --- a/libraries/render-utils/src/Model.cpp +++ b/libraries/render-utils/src/Model.cpp @@ -90,7 +90,7 @@ void Model::setScale(const glm::vec3& scale) { _scaledToFit = false; } -const float METERS_PER_MILLIMETER = 0.01f; +const float METERS_PER_MILLIMETER = 0.01f; void Model::setScaleInternal(const glm::vec3& scale) { if (glm::distance(_scale, scale) > METERS_PER_MILLIMETER) { @@ -1147,11 +1147,12 @@ void Model::segregateMeshGroups() { int shapeID = 0; for (int i = 0; i < (int)networkMeshes.size(); i++) { const FBXMesh& mesh = geometry.meshes.at(i); + const NetworkMesh& networkMesh = *(networkMeshes.at(i).get()); // Create the render payloads int totalParts = mesh.parts.size(); for (int partIndex = 0; partIndex < totalParts; partIndex++) { - auto renderItem = std::make_shared(this, i, partIndex, shapeID, _translation, _rotation); + auto renderItem = std::make_shared(this, networkMesh._mesh, i, partIndex, shapeID, _translation, _rotation, !showingCollisionHull); if (showingCollisionHull) { renderItem->updateDrawMaterial(ModelRender::getCollisionHullMaterial()); }