Skip to content

Commit

Permalink
UPBGE: Move camera projection computation in KX_KetsjiEngine::GetCame…
Browse files Browse the repository at this point in the history
…raProjection.

This function returns the current projection matrix of a camera, if the projection
is invalid then a new one is recomputed.
The function doesn't alterate the OpenGL state by calling perspective_m4 and
orthographic_m4 in RAS_OpenGLRasterizer.
  • Loading branch information
panzergame committed Jan 21, 2017
1 parent eac4633 commit b5ddf47
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 72 deletions.
108 changes: 57 additions & 51 deletions source/gameengine/Ketsji/KX_KetsjiEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,68 +848,30 @@ void KX_KetsjiEngine::RenderShadowBuffers(KX_Scene *scene)
}
}

// update graphics
void KX_KetsjiEngine::RenderFrame(KX_Scene *scene, KX_Camera *cam, unsigned short pass)
const MT_Matrix4x4& KX_KetsjiEngine::GetCameraProjection(KX_Scene *scene, KX_Camera *cam, const RAS_Rect& viewport, const RAS_Rect& area)
{
bool override_camera;
RAS_Rect viewport, area;
float nearfrust, farfrust, focallength;

if (!cam)
return;

bool isfirstscene = (scene == m_scenes->GetFront());

KX_SetActiveScene(scene);

#ifdef WITH_PYTHON
scene->RunDrawingCallbacks(KX_Scene::PRE_DRAW_SETUP, cam);
#endif

GetSceneViewport(scene, cam, area, viewport);

// set the viewport for this frame and scene
const int left = viewport.GetLeft();
const int bottom = viewport.GetBottom();
const int width = viewport.GetWidth();
const int height = viewport.GetHeight();
m_rasterizer->SetViewport(left, bottom, width + 1, height + 1);
m_rasterizer->SetScissor(left, bottom, width + 1, height + 1);

/* Clear the depth after setting the scene viewport/scissor
* if it's not the first render pass. */
if (pass > 0) {
m_rasterizer->Clear(RAS_IRasterizer::RAS_DEPTH_BUFFER_BIT);
if (cam->hasValidProjectionMatrix()) {
return cam->GetProjectionMatrix();
}

// see KX_BlenderMaterial::Activate
//m_rasterizer->SetAmbient();
m_rasterizer->DisplayFog();

override_camera = m_overrideCam && (scene->GetName() == m_overrideSceneName) &&
const bool override_camera = m_overrideCam && (scene->GetName() == m_overrideSceneName) &&
(cam->GetName() == "__default__cam__");

if (override_camera && !m_overrideCamData.m_perspective) {
m_rasterizer->SetProjectionMatrix(m_overrideCamProjMat);
if (!cam->hasValidProjectionMatrix()) {
// needed to get frustum planes for culling
MT_Matrix4x4 projmat;
projmat.setValue(m_overrideCamProjMat.getPointer());
cam->SetProjectionMatrix(projmat);
}
}
else if (cam->hasValidProjectionMatrix()) {
m_rasterizer->SetProjectionMatrix(cam->GetProjectionMatrix());
// needed to get frustum planes for culling
MT_Matrix4x4 projmat;
projmat.setValue(m_overrideCamProjMat.getPointer());
cam->SetProjectionMatrix(projmat);
}
else {
RAS_FrameFrustum frustum;
bool orthographic = !cam->GetCameraData()->m_perspective;
nearfrust = cam->GetCameraNear();
farfrust = cam->GetCameraFar();
focallength = cam->GetFocalLength();
const bool orthographic = !cam->GetCameraData()->m_perspective;
const float nearfrust = cam->GetCameraNear();
const float farfrust = cam->GetCameraFar();
const float focallength = cam->GetFocalLength();
MT_Matrix4x4 projmat;

float camzoom = override_camera ? m_overrideCamZoom : m_cameraZoom;
const float camzoom = override_camera ? m_overrideCamZoom : m_cameraZoom;
if (orthographic) {

RAS_FramingManager::ComputeOrtho(
Expand Down Expand Up @@ -965,9 +927,53 @@ void KX_KetsjiEngine::RenderFrame(KX_Scene *scene, KX_Camera *cam, unsigned shor
cam->InvalidateProjectionMatrix();
}

return cam->GetProjectionMatrix();
}

// update graphics
void KX_KetsjiEngine::RenderFrame(KX_Scene *scene, KX_Camera *cam, unsigned short pass)
{
bool override_camera;
RAS_Rect viewport, area;
float nearfrust, farfrust, focallength;

if (!cam)
return;

bool isfirstscene = (scene == m_scenes->GetFront());

KX_SetActiveScene(scene);

#ifdef WITH_PYTHON
scene->RunDrawingCallbacks(KX_Scene::PRE_DRAW_SETUP, cam);
#endif

GetSceneViewport(scene, cam, area, viewport);

// set the viewport for this frame and scene
const int left = viewport.GetLeft();
const int bottom = viewport.GetBottom();
const int width = viewport.GetWidth();
const int height = viewport.GetHeight();
m_rasterizer->SetViewport(left, bottom, width + 1, height + 1);
m_rasterizer->SetScissor(left, bottom, width + 1, height + 1);

/* Clear the depth after setting the scene viewport/scissor
* if it's not the first render pass. */
if (pass > 0) {
m_rasterizer->Clear(RAS_IRasterizer::RAS_DEPTH_BUFFER_BIT);
}

// see KX_BlenderMaterial::Activate
//m_rasterizer->SetAmbient();
m_rasterizer->DisplayFog();



MT_Transform camtrans(cam->GetWorldToCamera());
MT_Matrix4x4 viewmat(camtrans);

m_rasterizer->SetProjectionMatrix(GetCameraProjection(scene, cam, viewport, area));
m_rasterizer->SetViewMatrix(viewmat, cam->NodeGetWorldOrientation(), cam->NodeGetWorldPosition(), cam->NodeGetLocalScaling(), cam->GetCameraData()->m_perspective);
cam->SetModelviewMatrix(viewmat);

Expand Down
3 changes: 3 additions & 0 deletions source/gameengine/Ketsji/KX_KetsjiEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ class KX_KetsjiEngine
*/
void UpdateSuspendedScenes();

/// Update and return the projection matrix of a camera depending on the viewport.
const MT_Matrix4x4& GetCameraProjection(KX_Scene *scene, KX_Camera *cam, const RAS_Rect& viewport, const RAS_Rect& area);

void RenderFrame(KX_Scene *scene, KX_Camera *cam, unsigned short pass);
void PostRenderScene(KX_Scene *scene, unsigned short target);
void RenderDebugProperties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1426,9 +1426,6 @@ MT_Matrix4x4 RAS_OpenGLRasterizer::GetFrustumMatrix(
float focallength,
bool perspective)
{
MT_Matrix4x4 result;
float mat[16];

// correction for stereo
if (Stereo()) {
float near_div_focallength;
Expand Down Expand Up @@ -1466,14 +1463,10 @@ MT_Matrix4x4 RAS_OpenGLRasterizer::GetFrustumMatrix(
}
}

SetMatrixMode(RAS_PROJECTION);
LoadIdentity();
glFrustum(left, right, bottom, top, frustnear, frustfar);

glGetFloatv(GL_PROJECTION_MATRIX, mat);
result.setValue(mat);
float mat[4][4];
perspective_m4(mat, left, right, bottom, top, frustnear, frustfar);

return result;
return MT_Matrix4x4(&mat[0][0]);
}

MT_Matrix4x4 RAS_OpenGLRasterizer::GetOrthoMatrix(
Expand All @@ -1484,18 +1477,10 @@ MT_Matrix4x4 RAS_OpenGLRasterizer::GetOrthoMatrix(
float frustnear,
float frustfar)
{
MT_Matrix4x4 result;
float mat[16];

// stereo is meaningless for orthographic, disable it
SetMatrixMode(RAS_PROJECTION);
LoadIdentity();
glOrtho(left, right, bottom, top, frustnear, frustfar);

glGetFloatv(GL_PROJECTION_MATRIX, mat);
result.setValue(mat);
float mat[4][4];
orthographic_m4(mat, left, right, bottom, top, frustnear, frustfar);

return result;
return MT_Matrix4x4(&mat[0][0]);
}

// next arguments probably contain redundant info, for later...
Expand Down

0 comments on commit b5ddf47

Please sign in to comment.