diff --git a/kraken/KRModelView.cpp b/kraken/KRModelView.cpp index 15b8f20..b0febc2 100644 --- a/kraken/KRModelView.cpp +++ b/kraken/KRModelView.cpp @@ -68,6 +68,11 @@ bool KRModelView::getShaderValue(ShaderValue value, Vector3* output) const *output = lightDirObject; return true; } + case ShaderValue::light_direction_view_space: + { + *output = m_directionalLight->getViewSpaceLightDirection(m_viewport->getViewMatrix()); + return true; + } default: break; } diff --git a/kraken/KROctreeNode.cpp b/kraken/KROctreeNode.cpp index afc2fd6..46469b9 100755 --- a/kraken/KROctreeNode.cpp +++ b/kraken/KROctreeNode.cpp @@ -170,7 +170,7 @@ KROctreeNode* KROctreeNode::stripChild() } -KROctreeNode* KROctreeNode::getParent() +KROctreeNode* KROctreeNode::getParent() const { return m_parent; } diff --git a/kraken/KROctreeNode.h b/kraken/KROctreeNode.h index cb151f2..ef32364 100755 --- a/kraken/KROctreeNode.h +++ b/kraken/KROctreeNode.h @@ -52,7 +52,7 @@ public: hydra::AABB getBounds(); - KROctreeNode* getParent(); + KROctreeNode* getParent() const; void setChildNode(int iChild, KROctreeNode* pChild); int getChildIndex(KRNode* pNode); hydra::AABB getChildBounds(int iChild); diff --git a/kraken/nodes/KRDirectionalLight.cpp b/kraken/nodes/KRDirectionalLight.cpp index 75f0556..b0d29b0 100755 --- a/kraken/nodes/KRDirectionalLight.cpp +++ b/kraken/nodes/KRDirectionalLight.cpp @@ -61,12 +61,12 @@ std::string KRDirectionalLight::getElementName() return "directional_light"; } -Vector3 KRDirectionalLight::getWorldLightDirection() +Vector3 KRDirectionalLight::getWorldLightDirection() const { return Matrix4::Dot(getWorldRotation().rotationMatrix(), getLocalLightDirection()); } -Vector3 KRDirectionalLight::getLocalLightDirection() +Vector3 KRDirectionalLight::getLocalLightDirection() const { return Vector3::Up(); //&KRF HACK changed from Vector3::Forward(); - to compensate for the way Maya handles post rotation. } @@ -125,6 +125,19 @@ int KRDirectionalLight::configureShadowBufferViewports(const KRViewport& viewpor return 1; } +Vector3 KRDirectionalLight::getViewSpaceLightDirection(const Matrix4& viewMatrix) const +{ + Matrix4 matModelViewInverseTranspose = viewMatrix * getModelMatrix(); + matModelViewInverseTranspose.transpose(); + matModelViewInverseTranspose.invert(); + + Vector3 light_direction_view_space = getWorldLightDirection(); + light_direction_view_space = Matrix4::Dot(matModelViewInverseTranspose, light_direction_view_space); + light_direction_view_space.normalize(); + + return light_direction_view_space; +} + void KRDirectionalLight::render(RenderInfo& ri) { ri.reflectedObjects.push_back(this); @@ -137,14 +150,6 @@ void KRDirectionalLight::render(RenderInfo& ri) std::vector this_light; this_light.push_back(this); - Matrix4 matModelViewInverseTranspose = ri.viewport->getViewMatrix() * getModelMatrix(); - matModelViewInverseTranspose.transpose(); - matModelViewInverseTranspose.invert(); - - Vector3 light_direction_view_space = getWorldLightDirection(); - light_direction_view_space = Matrix4::Dot(matModelViewInverseTranspose, light_direction_view_space); - light_direction_view_space.normalize(); - KRMeshManager::KRVBOData& vertices = getContext().getMeshManager()->KRENGINE_VBO_DATA_2D_SQUARE_VERTICES; PipelineInfo info{}; @@ -159,13 +164,10 @@ void KRDirectionalLight::render(RenderInfo& ri) info.modelFormat = ModelFormat::KRENGINE_MODEL_FORMAT_STRIP; KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); - if (pShader) { - pShader->setPushConstant(ShaderValue::light_direction_view_space, light_direction_view_space); - if (pShader->bind(ri, getModelMatrix())) { // TODO: Need to pass in the light index to the shader - // Render a full screen quad - m_pContext->getMeshManager()->bindVBO(ri.commandBuffer, &vertices, 1.0f); - vkCmdDraw(ri.commandBuffer, 4, 1, 0, 0); - } + if (pShader && pShader->bind(ri, getModelMatrix())) { // TODO: Need to pass in the light index to the shader + // Render a full screen quad + m_pContext->getMeshManager()->bindVBO(ri.commandBuffer, &vertices, 1.0f); + vkCmdDraw(ri.commandBuffer, 4, 1, 0, 0); } } diff --git a/kraken/nodes/KRDirectionalLight.h b/kraken/nodes/KRDirectionalLight.h index 5fb3d5d..6277383 100755 --- a/kraken/nodes/KRDirectionalLight.h +++ b/kraken/nodes/KRDirectionalLight.h @@ -43,8 +43,9 @@ public: virtual ~KRDirectionalLight(); virtual std::string getElementName() override; - hydra::Vector3 getLocalLightDirection(); - hydra::Vector3 getWorldLightDirection(); + hydra::Vector3 getLocalLightDirection() const; + hydra::Vector3 getWorldLightDirection() const; + hydra::Vector3 getViewSpaceLightDirection(const hydra::Matrix4 & viewMatrix) const; virtual void render(RenderInfo& ri) override; virtual hydra::AABB getBounds() override; diff --git a/kraken/nodes/KRNode.cpp b/kraken/nodes/KRNode.cpp index 2d8d6de..a551132 100755 --- a/kraken/nodes/KRNode.cpp +++ b/kraken/nodes/KRNode.cpp @@ -189,7 +189,7 @@ void KRNode::setScaleCompensation(bool scale_compensation) invalidateBindPoseMatrix(); } } -bool KRNode::getScaleCompensation() +bool KRNode::getScaleCompensation() const { return m_scale_compensation; } @@ -510,87 +510,87 @@ void KRNode::setPostRotation(const Vector3& v, bool set_original) invalidateModelMatrix(); } -const Vector3& KRNode::getRotationOffset() +const Vector3& KRNode::getRotationOffset() const { return m_rotationOffset; } -const Vector3& KRNode::getScalingOffset() +const Vector3& KRNode::getScalingOffset() const { return m_scalingOffset; } -const Vector3& KRNode::getRotationPivot() +const Vector3& KRNode::getRotationPivot() const { return m_rotationPivot; } -const Vector3& KRNode::getScalingPivot() +const Vector3& KRNode::getScalingPivot() const { return m_scalingPivot; } -const Vector3& KRNode::getPreRotation() +const Vector3& KRNode::getPreRotation() const { return m_preRotation; } -const Vector3& KRNode::getPostRotation() +const Vector3& KRNode::getPostRotation() const { return m_postRotation; } -const Vector3& KRNode::getInitialRotationOffset() +const Vector3& KRNode::getInitialRotationOffset() const { return m_initialRotationOffset; } -const Vector3& KRNode::getInitialScalingOffset() +const Vector3& KRNode::getInitialScalingOffset() const { return m_initialScalingOffset; } -const Vector3& KRNode::getInitialRotationPivot() +const Vector3& KRNode::getInitialRotationPivot() const { return m_initialRotationPivot; } -const Vector3& KRNode::getInitialScalingPivot() +const Vector3& KRNode::getInitialScalingPivot() const { return m_initialScalingPivot; } -const Vector3& KRNode::getInitialPreRotation() +const Vector3& KRNode::getInitialPreRotation() const { return m_initialPreRotation; } -const Vector3& KRNode::getInitialPostRotation() +const Vector3& KRNode::getInitialPostRotation() const { return m_initialPostRotation; } -const Vector3& KRNode::getLocalTranslation() +const Vector3& KRNode::getLocalTranslation() const { return m_localTranslation; } -const Vector3& KRNode::getLocalScale() +const Vector3& KRNode::getLocalScale() const { return m_localScale; } -const Vector3& KRNode::getLocalRotation() +const Vector3& KRNode::getLocalRotation() const { return m_localRotation; } -const Vector3& KRNode::getInitialLocalTranslation() +const Vector3& KRNode::getInitialLocalTranslation() const { return m_initialLocalTranslation; } -const Vector3& KRNode::getInitialLocalScale() +const Vector3& KRNode::getInitialLocalScale() const { return m_initialLocalScale; } -const Vector3& KRNode::getInitialLocalRotation() +const Vector3& KRNode::getInitialLocalRotation() const { return m_initialLocalRotation; } -const Vector3 KRNode::getWorldTranslation() +const Vector3 KRNode::getWorldTranslation() const { return localToWorld(Vector3::Zero()); } -const Vector3 KRNode::getWorldScale() +const Vector3 KRNode::getWorldScale() const { return Matrix4::DotNoTranslate(getModelMatrix(), m_localScale); } @@ -683,7 +683,7 @@ void KRNode::render(RenderInfo& ri) m_lastRenderFrame = getContext().getCurrentFrame(); } -KRNode* KRNode::getParent() +KRNode* KRNode::getParent() const { return m_parentNode; } @@ -741,7 +741,7 @@ void KRNode::invalidateBindPoseMatrix() } } -const Matrix4& KRNode::getModelMatrix() +const Matrix4& KRNode::getModelMatrix() const { if (!m_modelMatrixValid) { @@ -802,7 +802,7 @@ const Matrix4& KRNode::getModelMatrix() return m_modelMatrix; } -const Matrix4& KRNode::getBindPoseMatrix() +const Matrix4& KRNode::getBindPoseMatrix() const { if (!m_bindPoseMatrixValid) { m_bindPoseMatrix = Matrix4(); @@ -861,7 +861,7 @@ const Matrix4& KRNode::getBindPoseMatrix() return m_bindPoseMatrix; } -const Matrix4& KRNode::getActivePoseMatrix() +const Matrix4& KRNode::getActivePoseMatrix() const { if (!m_activePoseMatrixValid) { @@ -919,7 +919,7 @@ const Matrix4& KRNode::getActivePoseMatrix() } -const Quaternion KRNode::getWorldRotation() +const Quaternion KRNode::getWorldRotation() const { Quaternion world_rotation = Quaternion::Create(m_postRotation) * Quaternion::Create(m_localRotation) * Quaternion::Create(m_preRotation); if (m_parentNode) { @@ -928,7 +928,7 @@ const Quaternion KRNode::getWorldRotation() return world_rotation; } -const Quaternion KRNode::getBindPoseWorldRotation() +const Quaternion KRNode::getBindPoseWorldRotation() const { Quaternion world_rotation = Quaternion::Create(m_initialPostRotation) * Quaternion::Create(m_initialLocalRotation) * Quaternion::Create(m_initialPreRotation); if (dynamic_cast(m_parentNode)) { @@ -937,7 +937,7 @@ const Quaternion KRNode::getBindPoseWorldRotation() return world_rotation; } -const Quaternion KRNode::getActivePoseWorldRotation() +const Quaternion KRNode::getActivePoseWorldRotation() const { Quaternion world_rotation = Quaternion::Create(m_postRotation) * Quaternion::Create(m_localRotation) * Quaternion::Create(m_preRotation); if (dynamic_cast(m_parentNode)) { @@ -946,7 +946,7 @@ const Quaternion KRNode::getActivePoseWorldRotation() return world_rotation; } -const Matrix4& KRNode::getInverseModelMatrix() +const Matrix4& KRNode::getInverseModelMatrix() const { if (!m_inverseModelMatrixValid) { m_inverseModelMatrix = Matrix4::Invert(getModelMatrix()); @@ -954,7 +954,7 @@ const Matrix4& KRNode::getInverseModelMatrix() return m_inverseModelMatrix; } -const Matrix4& KRNode::getInverseBindPoseMatrix() +const Matrix4& KRNode::getInverseBindPoseMatrix() const { if (!m_inverseBindPoseMatrixValid) { m_inverseBindPoseMatrix = Matrix4::Invert(getBindPoseMatrix()); @@ -1142,12 +1142,12 @@ KRNode::LodVisibility KRNode::getLODVisibility() return m_lod_visible; } -const Vector3 KRNode::localToWorld(const Vector3& local_point) +const Vector3 KRNode::localToWorld(const Vector3& local_point) const { return Matrix4::Dot(getModelMatrix(), local_point); } -const Vector3 KRNode::worldToLocal(const Vector3& world_point) +const Vector3 KRNode::worldToLocal(const Vector3& world_point) const { return Matrix4::Dot(getInverseModelMatrix(), world_point); } @@ -1175,7 +1175,7 @@ kraken_stream_level KRNode::getStreamLevel(const KRViewport& viewport) return stream_level; } -void KRNode::invalidateBounds() const +void KRNode::invalidateBounds() { m_boundsValid = false; if (m_parentNode) { diff --git a/kraken/nodes/KRNode.h b/kraken/nodes/KRNode.h index fbde65c..c34e663 100755 --- a/kraken/nodes/KRNode.h +++ b/kraken/nodes/KRNode.h @@ -142,7 +142,7 @@ public: void prependChild(KRNode* child); void insertBefore(KRNode* child); void insertAfter(KRNode* child); - KRNode* getParent(); + KRNode* getParent() const; void setLocalTranslation(const hydra::Vector3& v, bool set_original = false); void setLocalScale(const hydra::Vector3& v, bool set_original = false); @@ -156,50 +156,50 @@ public: void setPreRotation(const hydra::Vector3& v, bool set_original = false); void setPostRotation(const hydra::Vector3& v, bool set_original = false); - const hydra::Vector3& getRotationOffset(); - const hydra::Vector3& getScalingOffset(); - const hydra::Vector3& getRotationPivot(); - const hydra::Vector3& getScalingPivot(); - const hydra::Vector3& getPreRotation(); - const hydra::Vector3& getPostRotation(); + const hydra::Vector3& getRotationOffset() const; + const hydra::Vector3& getScalingOffset() const; + const hydra::Vector3& getRotationPivot() const; + const hydra::Vector3& getScalingPivot() const; + const hydra::Vector3& getPreRotation() const; + const hydra::Vector3& getPostRotation() const; - const hydra::Vector3& getInitialRotationOffset(); - const hydra::Vector3& getInitialScalingOffset(); - const hydra::Vector3& getInitialRotationPivot(); - const hydra::Vector3& getInitialScalingPivot(); - const hydra::Vector3& getInitialPreRotation(); - const hydra::Vector3& getInitialPostRotation(); + const hydra::Vector3& getInitialRotationOffset() const; + const hydra::Vector3& getInitialScalingOffset() const; + const hydra::Vector3& getInitialRotationPivot() const; + const hydra::Vector3& getInitialScalingPivot() const; + const hydra::Vector3& getInitialPreRotation() const; + const hydra::Vector3& getInitialPostRotation() const; - const hydra::Vector3& getLocalTranslation(); - const hydra::Vector3& getLocalScale(); - const hydra::Vector3& getLocalRotation(); + const hydra::Vector3& getLocalTranslation() const; + const hydra::Vector3& getLocalScale() const; + const hydra::Vector3& getLocalRotation() const; - const hydra::Vector3& getInitialLocalTranslation(); - const hydra::Vector3& getInitialLocalScale(); - const hydra::Vector3& getInitialLocalRotation(); + const hydra::Vector3& getInitialLocalTranslation() const; + const hydra::Vector3& getInitialLocalScale() const; + const hydra::Vector3& getInitialLocalRotation() const; - const hydra::Vector3 getWorldTranslation(); - const hydra::Vector3 getWorldScale(); - const hydra::Quaternion getWorldRotation(); + const hydra::Vector3 getWorldTranslation() const; + const hydra::Vector3 getWorldScale() const; + const hydra::Quaternion getWorldRotation() const; - const hydra::Quaternion getBindPoseWorldRotation(); - const hydra::Quaternion getActivePoseWorldRotation(); + const hydra::Quaternion getBindPoseWorldRotation() const; + const hydra::Quaternion getActivePoseWorldRotation() const; - const hydra::Vector3 localToWorld(const hydra::Vector3& local_point); - const hydra::Vector3 worldToLocal(const hydra::Vector3& world_point); + const hydra::Vector3 localToWorld(const hydra::Vector3& local_point) const; + const hydra::Vector3 worldToLocal(const hydra::Vector3& world_point) const; void setWorldTranslation(const hydra::Vector3& v); void setWorldScale(const hydra::Vector3& v); void setWorldRotation(const hydra::Vector3& v); virtual hydra::AABB getBounds(); - void invalidateBounds() const; - const hydra::Matrix4& getModelMatrix(); - const hydra::Matrix4& getInverseModelMatrix(); - const hydra::Matrix4& getBindPoseMatrix(); - const hydra::Matrix4& getActivePoseMatrix(); - const hydra::Matrix4& getInverseBindPoseMatrix(); + void invalidateBounds(); + const hydra::Matrix4& getModelMatrix() const; + const hydra::Matrix4& getInverseModelMatrix() const; + const hydra::Matrix4& getBindPoseMatrix() const; + const hydra::Matrix4& getActivePoseMatrix() const; + const hydra::Matrix4& getInverseBindPoseMatrix() const; enum node_attribute_type { @@ -250,7 +250,7 @@ public: LodVisibility getLODVisibility(); void setScaleCompensation(bool scale_compensation); - bool getScaleCompensation(); + bool getScaleCompensation() const; void setAnimationEnabled(node_attribute_type attrib, bool enable); bool getAnimationEnabled(node_attribute_type attrib) const; @@ -299,24 +299,23 @@ private: long m_lastRenderFrame; void invalidateModelMatrix(); void invalidateBindPoseMatrix(); - hydra::Matrix4 m_modelMatrix; - hydra::Matrix4 m_inverseModelMatrix; - hydra::Matrix4 m_bindPoseMatrix; - hydra::Matrix4 m_activePoseMatrix; - hydra::Matrix4 m_inverseBindPoseMatrix; - bool m_modelMatrixValid; - bool m_inverseModelMatrixValid; - bool m_bindPoseMatrixValid; - bool m_activePoseMatrixValid; - bool m_inverseBindPoseMatrixValid; + // Members are mutable to enable lazy calculation from const accessors + mutable hydra::Matrix4 m_modelMatrix; + mutable hydra::Matrix4 m_inverseModelMatrix; + mutable hydra::Matrix4 m_bindPoseMatrix; + mutable hydra::Matrix4 m_activePoseMatrix; + mutable hydra::Matrix4 m_inverseBindPoseMatrix; + mutable bool m_modelMatrixValid; + mutable bool m_inverseModelMatrixValid; + mutable bool m_bindPoseMatrixValid; + mutable bool m_activePoseMatrixValid; + mutable bool m_inverseBindPoseMatrixValid; - mutable hydra::AABB m_bounds; - mutable bool m_boundsValid; + hydra::AABB m_bounds; + bool m_boundsValid; std::string m_name; - - KRScene* m_pScene; std::set m_octree_nodes; diff --git a/kraken/resources/mesh/KRMesh.cpp b/kraken/resources/mesh/KRMesh.cpp index 768f93c..ed7220d 100755 --- a/kraken/resources/mesh/KRMesh.cpp +++ b/kraken/resources/mesh/KRMesh.cpp @@ -236,10 +236,7 @@ void KRMesh::render(KRNode::RenderInfo& ri, const std::string& object_name, cons { //fprintf(stderr, "Rendering model: %s\n", m_name.c_str()); if (ri.renderPass->getType() != RenderPassType::RENDER_PASS_ADDITIVE_PARTICLES && ri.renderPass->getType() != RenderPassType::RENDER_PASS_PARTICLE_OCCLUSION && ri.renderPass->getType() != RenderPassType::RENDER_PASS_VOLUMETRIC_EFFECTS_ADDITIVE) { - if (getStreamLevel() == kraken_stream_level::STREAM_LEVEL_OUT) { - - } else { - + if (getStreamLevel() > kraken_stream_level::STREAM_LEVEL_OUT) { getSubmeshes(); getMaterials(); @@ -260,7 +257,7 @@ void KRMesh::render(KRNode::RenderInfo& ri, const std::string& object_name, cons if ((!pMaterial->isTransparent() && ri.renderPass->getType() != RenderPassType::RENDER_PASS_FORWARD_TRANSPARENT) || (pMaterial->isTransparent() && ri.renderPass->getType() == RenderPassType::RENDER_PASS_FORWARD_TRANSPARENT)) { std::vector bone_bind_poses; for (int i = 0; i < (int)bones.size(); i++) { - bone_bind_poses.push_back(getBoneBindPose(i)); + bone_bind_poses.push_back(getBoneBindPose(i)); } switch (pMaterial->getAlphaMode()) {