Skip to content

Commit

Permalink
Added support for ViewMatrix::origin to help support astronomically l…
Browse files Browse the repository at this point in the history
…arge scene graphs. To be used in combination with CoordinateFrame::origin.
  • Loading branch information
robertosfield committed Nov 14, 2024
1 parent c428914 commit 807b191
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
36 changes: 14 additions & 22 deletions include/vsg/app/ViewMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ namespace vsg
{
}

virtual dmat4 transform() const = 0;
/// origin value provides a means of translating the view matrix relative to the origin of any CoordinateFrame subgraphs
/// to maximize the precision when moving around the CoordinateFrame subgraph. This is helpful for astronmically large
/// scenes where standrd double precision is insufficient for avoiding visually significant numerical errors.
dvec3 origin;

virtual dmat4 inverse() const
virtual dmat4 transform(const vsg::dvec3& offset={}) const = 0;

virtual dmat4 inverse(const vsg::dvec3& offset={}) const
{
return vsg::inverse(transform());
return vsg::inverse(transform(offset));
}
};
VSG_type_name(vsg::ViewMatrix);
Expand Down Expand Up @@ -79,21 +84,11 @@ namespace vsg

ref_ptr<Object> clone(const CopyOp& copyop = {}) const override { return LookAt::create(*this, copyop); }

void transform(const dmat4& matrix)
{
up = normalize(matrix * (eye + up) - matrix * eye);
center = matrix * center;
eye = matrix * eye;
}
void transform(const dmat4& matrix);

void set(const dmat4& matrix)
{
up = normalize(matrix * (dvec3(0.0, 0.0, 0.0) + dvec3(0.0, 1.0, 0.0)) - matrix * dvec3(0.0, 0.0, 0.0));
center = matrix * dvec3(0.0, 0.0, -1.0);
eye = matrix * dvec3(0.0, 0.0, 0.0);
}
void set(const dmat4& matrix);

dmat4 transform() const override { return lookAt(eye, center, up); }
dmat4 transform(const vsg::dvec3& offset={}) const override;

void read(Input& input) override;
void write(Output& output) const override;
Expand All @@ -115,10 +110,7 @@ namespace vsg
}

/// returns matrix * viewMatrix->transform()
dmat4 transform() const override
{
return matrix * viewMatrix->transform();
}
dmat4 transform(const vsg::dvec3& offset={}) const override;

dmat4 matrix;
ref_ptr<ViewMatrix> viewMatrix;
Expand All @@ -141,8 +133,8 @@ namespace vsg
objectPath(path.begin(), path.end()) {}

/// returns matrix * computeTransfrom(objectPath)
dmat4 transform() const override;
dmat4 inverse() const override;
dmat4 transform(const vsg::dvec3& offset={}) const override;
dmat4 inverse(const vsg::dvec3& offset={}) const override;

dmat4 matrix;
RefObjectPath objectPath;
Expand Down
6 changes: 5 additions & 1 deletion src/vsg/app/RecordTraversal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,11 @@ void RecordTraversal::apply(const CoordinateFrame& cf)
{
GPU_INSTRUMENTATION_L2_NCO(instrumentation, *getCommandBuffer(), "CoordinateFrame", COLOR_RECORD_L2, &cf);

_state->modelviewMatrixStack.push(cf);
View* parentView = _viewDependentState ? _viewDependentState->view : nullptr;
Camera* camera = parentView ? parentView->camera : nullptr;
ViewMatrix* viewMatrix = camera ? camera->viewMatrix : nullptr;

_state->modelviewMatrixStack.push(viewMatrix->transform(cf.origin));
_state->dirty = true;

if (cf.subgraphRequiresLocalFrustum)
Expand Down
32 changes: 28 additions & 4 deletions src/vsg/app/ViewMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,36 @@ void LookAt::write(Output& output) const
output.write("up", up);
}

dmat4 TrackingViewMatrix::transform() const
void LookAt::transform(const dmat4& matrix)
{
return matrix * vsg::inverse(computeTransform(objectPath));
up = normalize(matrix * (eye + up) - matrix * eye);
center = matrix * center;
eye = matrix * eye;
}

dmat4 TrackingViewMatrix::inverse() const
void LookAt::set(const dmat4& matrix)
{
return computeTransform(objectPath) * vsg::inverse(matrix);
up = normalize(matrix * (dvec3(0.0, 0.0, 0.0) + dvec3(0.0, 1.0, 0.0)) - matrix * dvec3(0.0, 0.0, 0.0));
center = matrix * dvec3(0.0, 0.0, -1.0);
eye = matrix * dvec3(0.0, 0.0, 0.0);
}

dmat4 LookAt::transform(const vsg::dvec3& offset) const
{
return lookAt(eye, center, up) * vsg::translate(offset-origin);
}

dmat4 RelativeViewMatrix::transform(const vsg::dvec3& offset) const
{
return matrix * viewMatrix->transform(offset);
}

dmat4 TrackingViewMatrix::transform(const vsg::dvec3& offset) const
{
return matrix * vsg::translate(offset-origin) * vsg::inverse(computeTransform(objectPath));
}

dmat4 TrackingViewMatrix::inverse(const vsg::dvec3& offset) const
{
return computeTransform(objectPath) * vsg::translate(origin - offset) * vsg::inverse(matrix);
}

0 comments on commit 807b191

Please sign in to comment.