Skip to main content
deleted 186 characters in body
Source Link

In my project I have simple scene graph to render whole scene and Bullet physics SDK to provide physics simulation.

Each rendered object is represented as scene node. Camera always has target and located behind this target. Target can be any scene node.

First, I want to describe my rendering pipeline.

1)When time to render whole scene, we calculate view matrix from target's world matrix. We take offset vector in order to locate camera behind targeted scene node and transform it to scene node world's coordinate. Then add position of target with transformed offset vector. Finally, get inverse matrix of scene node. This method always is called before rendering whole scene.

HRESULT CameraNode::SetViewTransform(Scene *pScene)
{
    //If there is a target, make sure the camera is
    //rigidly attached right behind the target
    if(m_pTarget)
    {
        Mat4x4 mat = m_pTarget->VGet()->ToWorld();
        Vec4 at = m_CamOffsetVector;
        Vec4 atWorld = mat.Xform(at);
        Vec3 pos = mat.GetPosition() + Vec3(at);
        mat.SetPosition(pos); 
        VSetTransform(&mat); // Set normal matrix and calculate inverse matrix
    }
    m_View = VGet()->FromWorld(); // Get inversed matrix
    pScene->GetRenderer()->VSetViewTransform(&m_View);
    return S_OK;
}

2)Then, when time to render particular scene node, we calculate projection matrix and send it to the vertex shader.

Mat4x4 CameraNode::GetWorldViewProjection(Scene *pScene)
{ 
     Mat4x4 world = pScene->GetTopMatrix();
     Mat4x4 view = VGet()->FromWorld();
     Mat4x4 worldView = world * view;
     return m_Projection * worldView;
}

I have next problem, when I calculate view matrix from target's world matrix and locate target on coordinate ( x = 0; y = 10; z = 1) it started to fall due to physics and jerk/twitch. I have captured this moment. with camera[1]

When I set view/camera matrix only offset position. Scene node falls without jerking/twitching. You can watch it here. without camera[2]

How I can fix this jerking/twitching when camera is following the scene node ?

I suppose that it is problem of matrices multiplication, when Bullet SDK sets new coordinates to scene node. But I have no idea how it can be solved.

In my project I have simple scene graph to render whole scene and Bullet physics SDK to provide physics simulation.

Each rendered object is represented as scene node. Camera always has target and located behind this target. Target can be any scene node.

First, I want to describe my rendering pipeline.

1)When time to render whole scene, we calculate view matrix from target's world matrix. We take offset vector in order to locate camera behind targeted scene node and transform it to scene node world's coordinate. Then add position of target with transformed offset vector. Finally, get inverse matrix of scene node. This method always is called before rendering whole scene.

HRESULT CameraNode::SetViewTransform(Scene *pScene)
{
    //If there is a target, make sure the camera is
    //rigidly attached right behind the target
    if(m_pTarget)
    {
        Mat4x4 mat = m_pTarget->VGet()->ToWorld();
        Vec4 at = m_CamOffsetVector;
        Vec4 atWorld = mat.Xform(at);
        Vec3 pos = mat.GetPosition() + Vec3(at);
        mat.SetPosition(pos); 
        VSetTransform(&mat); // Set normal matrix and calculate inverse matrix
    }
    m_View = VGet()->FromWorld(); // Get inversed matrix
    pScene->GetRenderer()->VSetViewTransform(&m_View);
    return S_OK;
}

2)Then, when time to render particular scene node, we calculate projection matrix and send it to the vertex shader.

Mat4x4 CameraNode::GetWorldViewProjection(Scene *pScene)
{ 
     Mat4x4 world = pScene->GetTopMatrix();
     Mat4x4 view = VGet()->FromWorld();
     Mat4x4 worldView = world * view;
     return m_Projection * worldView;
}

I have next problem, when I calculate view matrix from target's world matrix and locate target on coordinate ( x = 0; y = 10; z = 1) it started to fall due to physics and jerk/twitch. I have captured this moment. with camera

When I set view/camera matrix only offset position. Scene node falls without jerking/twitching. You can watch it here. without camera

How I can fix this jerking/twitching when camera is following the scene node ?

I suppose that it is problem of matrices multiplication, when Bullet SDK sets new coordinates to scene node. But I have no idea how it can be solved.

In my project I have simple scene graph to render whole scene and Bullet physics SDK to provide physics simulation.

Each rendered object is represented as scene node. Camera always has target and located behind this target. Target can be any scene node.

First, I want to describe my rendering pipeline.

1)When time to render whole scene, we calculate view matrix from target's world matrix. We take offset vector in order to locate camera behind targeted scene node and transform it to scene node world's coordinate. Then add position of target with transformed offset vector. Finally, get inverse matrix of scene node. This method always is called before rendering whole scene.

HRESULT CameraNode::SetViewTransform(Scene *pScene)
{
    //If there is a target, make sure the camera is
    //rigidly attached right behind the target
    if(m_pTarget)
    {
        Mat4x4 mat = m_pTarget->VGet()->ToWorld();
        Vec4 at = m_CamOffsetVector;
        Vec4 atWorld = mat.Xform(at);
        Vec3 pos = mat.GetPosition() + Vec3(at);
        mat.SetPosition(pos); 
        VSetTransform(&mat); // Set normal matrix and calculate inverse matrix
    }
    m_View = VGet()->FromWorld(); // Get inversed matrix
    pScene->GetRenderer()->VSetViewTransform(&m_View);
    return S_OK;
}

2)Then, when time to render particular scene node, we calculate projection matrix and send it to the vertex shader.

Mat4x4 CameraNode::GetWorldViewProjection(Scene *pScene)
{ 
     Mat4x4 world = pScene->GetTopMatrix();
     Mat4x4 view = VGet()->FromWorld();
     Mat4x4 worldView = world * view;
     return m_Projection * worldView;
}

I have next problem, when I calculate view matrix from target's world matrix and locate target on coordinate ( x = 0; y = 10; z = 1) it started to fall due to physics and jerk/twitch. [1]

When I set view/camera matrix only offset position. Scene node falls without jerking/twitching. [2]

How I can fix this jerking/twitching when camera is following the scene node ?

I suppose that it is problem of matrices multiplication, when Bullet SDK sets new coordinates to scene node. But I have no idea how it can be solved.

deleted 1022 characters in body
Source Link

2)Before push child matrix in the Matrix Stack, it has been multiplied with other matrices in the stack.

void PushAndSetMatrix(const Mat4x4 &toWorld)
{
    if(!m_pMatrixStack->empty())
    {
        Mat4x4 temp = toWorld * m_pMatrixStack->top();
        m_pMatrixStack->push(temp);
        m_Renderer->VSetWorldTransform(&temp);
    }
}

3)Then, when time to render particular scene node, we calculate projection matrix and send it to the vertex shader.

4)FInally, pop up top matrix from the Matrix Stack after each scene node rendering.

void PopMatrix() 
{
     Mat4x4 mat = GetTopMatrix();
     if(!m_pMatrixStack->empty())
     {
          m_pMatrixStack->pop();
          m_Renderer->VSetWorldTransform(&mat);
     }
}

I have next problem, when I calculate view matrix from target's world matrix and locate target on coordinate ( x = 0; y = 10; z = 1) it started to fall due to physics and jerk/twitch. I have captured this moment. with camera

UPDATED:

This method is called, when I start to render scene. SetViewTransform() is called only from this place.

HRESULT Scene::OnRender(float& interpolation)
{
    if (m_Root && m_Camera)
    {
        m_Camera->SetViewTransform(this);
        if (m_Root->VPreRender(this)==S_OK)
        {
            m_Root->VRender(this, interpolation); // Do nothing
            m_Root->VRenderChildren(this, interpolation); // Render scene nodes
            m_Root->VPostRender(this); // Pop root matrix. (in our case identity matrix)
        }
    }
    return S_OK;
}   

2)Before push child matrix in the Matrix Stack, it has been multiplied with other matrices in the stack.

void PushAndSetMatrix(const Mat4x4 &toWorld)
{
    if(!m_pMatrixStack->empty())
    {
        Mat4x4 temp = toWorld * m_pMatrixStack->top();
        m_pMatrixStack->push(temp);
        m_Renderer->VSetWorldTransform(&temp);
    }
}

3)Then, when time to render particular scene node, we calculate projection matrix and send it to the vertex shader.

4)FInally, pop up top matrix from the Matrix Stack after each scene node rendering.

void PopMatrix() 
{
     Mat4x4 mat = GetTopMatrix();
     if(!m_pMatrixStack->empty())
     {
          m_pMatrixStack->pop();
          m_Renderer->VSetWorldTransform(&mat);
     }
}

I have next problem, when I calculate view matrix from target's world matrix and locate target on coordinate ( x = 0; y = 10; z = 1) it started to fall due to physics and jerk/twitch. I have captured this moment. with camera

UPDATED:

This method is called, when I start to render scene. SetViewTransform() is called only from this place.

HRESULT Scene::OnRender(float& interpolation)
{
    if (m_Root && m_Camera)
    {
        m_Camera->SetViewTransform(this);
        if (m_Root->VPreRender(this)==S_OK)
        {
            m_Root->VRender(this, interpolation); // Do nothing
            m_Root->VRenderChildren(this, interpolation); // Render scene nodes
            m_Root->VPostRender(this); // Pop root matrix. (in our case identity matrix)
        }
    }
    return S_OK;
}   

2)Then, when time to render particular scene node, we calculate projection matrix and send it to the vertex shader.

I have next problem, when I calculate view matrix from target's world matrix and locate target on coordinate ( x = 0; y = 10; z = 1) it started to fall due to physics and jerk/twitch. I have captured this moment. with camera

deleted 504 characters in body
Source Link

This is vertex shader program.

#version 330

layout (location = 0) in vec3 inPosition;
layout (location = 1) in vec4 inColor;  
layout (location = 2) in vec2 inTexCoord;

uniform mat4 gWorld;

out vec4 outColor;
out vec2 outTexCoord;

void main()
{
    gl_Position = gWorld * vec4(inPosition, 1.0);
    outColor = inColor;
    outTexCoord = vec2(inTexCoord.x, 1.0f - inTexCoord.y); // *HACK* just to flip texture.

}

This is vertex shader program.

#version 330

layout (location = 0) in vec3 inPosition;
layout (location = 1) in vec4 inColor;  
layout (location = 2) in vec2 inTexCoord;

uniform mat4 gWorld;

out vec4 outColor;
out vec2 outTexCoord;

void main()
{
    gl_Position = gWorld * vec4(inPosition, 1.0);
    outColor = inColor;
    outTexCoord = vec2(inTexCoord.x, 1.0f - inTexCoord.y); // *HACK* just to flip texture.

}
added 572 characters in body
Source Link
Loading
added 572 characters in body
Source Link
Loading
added 74 characters in body
Source Link
Loading
Source Link
Loading