Skip to content

Commit

Permalink
UPBGE: Simplify RAS_Rasterizer::SetViewMatrix.
Browse files Browse the repository at this point in the history
Previously SetViewMatrix requested view matrix, position and scale. Position
is useless at it could be found in the inverse view matrix which is computed
anyway. Scale is not needed for users of this function not using any scale
for example shadow and texture renderers.

In consideration the user could call :
SetViewMatrix(viewmat)
SetViewMatrix(viewmat, scale)

The second function test if the scale is negative and negate the rows of the
view matrix.
  • Loading branch information
panzergame committed Jun 22, 2018
1 parent ed78298 commit 11be475
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion source/gameengine/Ketsji/KX_KetsjiEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ void KX_KetsjiEngine::RenderCamera(KX_Scene *scene, const CameraRenderData& came
m_rasterizer->SetEye(cameraFrameData.m_eye);

m_rasterizer->SetProjectionMatrix(rendercam->GetProjectionMatrix());
m_rasterizer->SetViewMatrix(rendercam->GetModelviewMatrix(), rendercam->NodeGetWorldPosition(), rendercam->NodeGetLocalScaling());
m_rasterizer->SetViewMatrix(rendercam->GetModelviewMatrix(), rendercam->NodeGetWorldScaling());

if (isFirstScene) {
KX_WorldInfo *worldInfo = scene->GetWorldInfo();
Expand Down
2 changes: 1 addition & 1 deletion source/gameengine/Ketsji/KX_TextureRendererManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ bool KX_TextureRendererManager::RenderRenderer(RAS_Rasterizer *rasty, KX_Texture
const mt::mat3x4 camtrans(m_camera->GetWorldToCamera());
const mt::mat4 viewmat = mt::mat4::FromAffineTransform(camtrans);

rasty->SetViewMatrix(viewmat, m_camera->NodeGetWorldPosition(), mt::one3);
rasty->SetViewMatrix(viewmat);
m_camera->SetModelviewMatrix(viewmat);

const std::vector<KX_GameObject *> objects = m_scene->CalculateVisibleMeshes(m_camera, ~renderer->GetIgnoreLayers());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ void RAS_OpenGLLight::BindShadowBuffer(RAS_ICanvas *canvas, KX_Camera *cam, mt::

/* setup rasterizer transformations */
m_rasterizer->SetProjectionMatrix(projectionmat);
m_rasterizer->SetViewMatrix(modelviewmat, cam->NodeGetWorldPosition(), cam->NodeGetLocalScaling());
m_rasterizer->SetViewMatrix(modelviewmat);
}

void RAS_OpenGLLight::UnbindShadowBuffer()
Expand Down
39 changes: 23 additions & 16 deletions source/gameengine/Rasterizer/RAS_Rasterizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,30 +798,37 @@ mt::mat4 RAS_Rasterizer::GetViewMatrix(StereoMode stereoMode, StereoEye eye, con
return mt::mat4::FromAffineTransform(camtrans);
}

void RAS_Rasterizer::SetViewMatrix(const mt::mat4& viewmat, const mt::vec3& pos, const mt::vec3& scale)
void RAS_Rasterizer::SetViewMatrix(const mt::mat4& viewmat, bool negscale)
{
m_viewmatrix = viewmat;

// Don't making variable negX/negY/negZ allow drastic time saving.
if (scale[0] < 0.0f || scale[1] < 0.0f || scale[2] < 0.0f) {
const bool negX = (scale[0] < 0.0f);
const bool negY = (scale[1] < 0.0f);
const bool negZ = (scale[2] < 0.0f);
// m_viewmatrix.tscale((negX) ? -1.0f : 1.0f, (negY) ? -1.0f : 1.0f, (negZ) ? -1.0f : 1.0f, 1.0f);
// TODO
m_camnegscale = negX ^ negY ^ negZ;
}
else {
m_camnegscale = false;
}

m_viewinvmatrix = m_viewmatrix.Inverse();
m_campos = pos;
m_campos = m_viewinvmatrix.TranslationVector3D();
m_camnegscale = negscale;

SetMatrixMode(RAS_MODELVIEW);
LoadMatrix((float *)m_viewmatrix.Data());
}

void RAS_Rasterizer::SetViewMatrix(const mt::mat4& viewmat)
{
SetViewMatrix(viewmat, false);
}

void RAS_Rasterizer::SetViewMatrix(mt::mat4 viewmat, const mt::vec3& scale)
{
for (unsigned short i = 0; i < 3; ++i) {
// Negate row scaling if the scale is negative.
if (scale[i] < 0.0f) {
for (unsigned short j = 0; j < 4; ++j) {
viewmat(i, j) *= -1.0f;
}
}
}

const bool negscale = (scale.x * scale.y * scale.z) < 0.0f;
SetViewMatrix(viewmat, negscale);
}

void RAS_Rasterizer::SetViewport(int x, int y, int width, int height)
{
m_impl->SetViewport(x, y, width, height);
Expand Down
4 changes: 3 additions & 1 deletion source/gameengine/Rasterizer/RAS_Rasterizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,9 @@ class RAS_Rasterizer : public mt::SimdClassAllocator
/**
* Sets the modelview matrix.
*/
void SetViewMatrix(const mt::mat4 &mat, const mt::vec3 &pos, const mt::vec3 &scale);
void SetViewMatrix(const mt::mat4 &viewmat, bool negscale);
void SetViewMatrix(const mt::mat4 &viewmat);
void SetViewMatrix(mt::mat4 viewmat, const mt::vec3& scale);

/**
* Get/Set viewport area
Expand Down
2 changes: 1 addition & 1 deletion source/gameengine/VideoTexture/ImageRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ bool ImageRender::Render()
mt::mat3x4 camtrans(m_camera->GetWorldToCamera());
mt::mat4 viewmat = mt::mat4::FromAffineTransform(camtrans);

m_rasterizer->SetViewMatrix(viewmat, m_camera->NodeGetWorldPosition(), m_camera->NodeGetLocalScaling());
m_rasterizer->SetViewMatrix(viewmat, m_camera->NodeGetWorldScaling());
m_camera->SetModelviewMatrix(viewmat);

// Render Background
Expand Down

0 comments on commit 11be475

Please sign in to comment.