BGE: Frustrum culling not working properly #40257

Closed
opened 2014-05-19 08:58:16 +02:00 by Kevin Ednalino · 23 comments

System Information
Arch Linux 64-bit
Intel HD Graphics 4000

Blender Version
Broken: version 2.70 (sub 5), hash 003387fab5

Short description of error
In KX_Scene::CalculateVisibleMeshes, I think the CullingTest function is not working properly. In my attached blend file, it'll correctly calculate when an object enters the frustrum but not when it exits. When I use the other method with MarkVisible, it works properly.

I'm not completely sure whether the KX_GameObject attribute (m_bCulled) is not being set properly or the CullingTest is failing or both.

Exact steps for others to reproduce the error
In order to check, I've added the following code in KX_Scene:

void KX_Scene::CalculateVisibleMeshes(RAS_IRasterizer* rasty,KX_Camera* cam, int layer)
{
	bool dbvt_culling = false;
	if (m_dbvt_culling) 
	{
		// test culling through Bullet
		MT_Vector4 planes[6];
		// get the clip planes
		MT_Vector4* cplanes = cam->GetNormalizedClipPlanes();
		// and convert
		planes[0].setValue(cplanes[4].getValue());	// near
		planes[1].setValue(cplanes[5].getValue());	// far
		planes[2].setValue(cplanes[0].getValue());	// left
		planes[3].setValue(cplanes[1].getValue());	// right
		planes[4].setValue(cplanes[2].getValue());	// top
		planes[5].setValue(cplanes[3].getValue());	// bottom
		CullingInfo info(layer);

		double mvmat[16] = {0};
		cam->GetModelviewMatrix().getValue(mvmat);
		double pmat[16] = {0};
		cam->GetProjectionMatrix().getValue(pmat);

		dbvt_culling = m_physicsEnvironment->CullingTest(PhysicsCullingCallback,&info,planes,5,m_dbvt_occlusion_res,
		                                                 KX_GetActiveEngine()->GetCanvas()->GetViewPort(),
		                                                 mvmat, pmat);
	}
	if (!dbvt_culling) {
		// the physics engine couldn't help us, do it the hard way
		for (int i = 0; i < m_objectlist->GetCount(); i++)
		{
			MarkVisible(rasty, static_cast<KX_GameObject*>(m_objectlist->GetValue(i)), cam, layer);
		}
	}

	for (int i = 0; i < m_objectlist->GetCount(); i++)
	{
		KX_GameObject *gameobj = static_cast<KX_GameObject*>(m_objectlist->GetValue(i));
		bool culled = gameobj->GetCulled();
		printf("Name: %s, Culled: %s\n", gameobj->GetName().Ptr(), culled == true ? "True" : "False");
	}
}

And the blend file:
frustrum_test1.blend

**System Information** Arch Linux 64-bit Intel HD Graphics 4000 **Blender Version** Broken: version 2.70 (sub 5), hash 003387fab543711495e2ceb80a663d7f79fcf447 **Short description of error** In KX_Scene::CalculateVisibleMeshes, I think the CullingTest function is not working properly. In my attached blend file, it'll correctly calculate when an object enters the frustrum but not when it exits. When I use the other method with MarkVisible, it works properly. I'm not completely sure whether the KX_GameObject attribute (m_bCulled) is not being set properly or the CullingTest is failing or both. **Exact steps for others to reproduce the error** In order to check, I've added the following code in KX_Scene: ``` void KX_Scene::CalculateVisibleMeshes(RAS_IRasterizer* rasty,KX_Camera* cam, int layer) { bool dbvt_culling = false; if (m_dbvt_culling) { // test culling through Bullet MT_Vector4 planes[6]; // get the clip planes MT_Vector4* cplanes = cam->GetNormalizedClipPlanes(); // and convert planes[0].setValue(cplanes[4].getValue()); // near planes[1].setValue(cplanes[5].getValue()); // far planes[2].setValue(cplanes[0].getValue()); // left planes[3].setValue(cplanes[1].getValue()); // right planes[4].setValue(cplanes[2].getValue()); // top planes[5].setValue(cplanes[3].getValue()); // bottom CullingInfo info(layer); double mvmat[16] = {0}; cam->GetModelviewMatrix().getValue(mvmat); double pmat[16] = {0}; cam->GetProjectionMatrix().getValue(pmat); dbvt_culling = m_physicsEnvironment->CullingTest(PhysicsCullingCallback,&info,planes,5,m_dbvt_occlusion_res, KX_GetActiveEngine()->GetCanvas()->GetViewPort(), mvmat, pmat); } if (!dbvt_culling) { // the physics engine couldn't help us, do it the hard way for (int i = 0; i < m_objectlist->GetCount(); i++) { MarkVisible(rasty, static_cast<KX_GameObject*>(m_objectlist->GetValue(i)), cam, layer); } } for (int i = 0; i < m_objectlist->GetCount(); i++) { KX_GameObject *gameobj = static_cast<KX_GameObject*>(m_objectlist->GetValue(i)); bool culled = gameobj->GetCulled(); printf("Name: %s, Culled: %s\n", gameobj->GetName().Ptr(), culled == true ? "True" : "False"); } } ``` And the blend file: [frustrum_test1.blend](https://archive.blender.org/developer/F89401/frustrum_test1.blend)
Author

Changed status to: 'Open'

Changed status to: 'Open'
Author

Added subscriber: @mahalin

Added subscriber: @mahalin

Added subscriber: @Moguri

Added subscriber: @Moguri

The Bullet assisted culling code will never set KX_GameObject::m_bCulled to true. However, mesh slots are automatically set to true after they are rendered. This means that the meshes are indeed getting culled, but some of our other code that is relying on KX_GameObject::GetCulled() (e.g., animation culling, LoDs) are not working properly.

The Bullet assisted culling code will never set KX_GameObject::m_bCulled to true. However, mesh slots are automatically set to true after they are rendered. This means that the meshes are indeed getting culled, but some of our other code that is relying on KX_GameObject::GetCulled() (e.g., animation culling, LoDs) are not working properly.

Added subscriber: @solarlune

Added subscriber: @solarlune

Added subscriber: @JacobMerrill-1

Added subscriber: @JacobMerrill-1
Author
[Fix ](https://developer.blender.org/D575)
Author

So another idea I have is we could maintain a list of the non-culled items (i.e. visible) by adding objects to a list in the Physics callback e.g. m_nonculledobjects.push_back(KX_GameObject). We would clear it each frame before doing the culling check. Of course, it would be valid only for that frame.

This list would be useful for functions that operate only on non-culled items, like LOD. I believe it would also be more efficient as now there is no need to iterate over every object and check its culling flag.

Hmm, we could also have the material bucket be responsible for adding to this list or would that be a code smell? Ultimately, it decides if an object's mesh is culled and thus rendered.

So another idea I have is we could maintain a list of the non-culled items (i.e. visible) by adding objects to a list in the Physics callback e.g. m_nonculledobjects.push_back(KX_GameObject). We would clear it each frame before doing the culling check. Of course, it would be valid only for that frame. This list would be useful for functions that operate only on non-culled items, like LOD. I believe it would also be more efficient as now there is no need to iterate over every object and check its culling flag. Hmm, we could also have the material bucket be responsible for adding to this list or would that be a code smell? Ultimately, it decides if an object's mesh is culled and thus rendered.

This issue was referenced by 978dba4616

This issue was referenced by 978dba4616852e0b94374f2ae56934049d9b3669

Changed status from 'Open' to: 'Resolved'

Changed status from 'Open' to: 'Resolved'

Closed by commit 978dba4616.

Closed by commit 978dba4616.

This issue was referenced by a1aa96940c

This issue was referenced by a1aa96940c74f51a8661a497286c8e9b7ecc460f

Changed status from 'Resolved' to: 'Open'

Changed status from 'Resolved' to: 'Open'

The fix was reverted as it didn't actually fix the problem and, instead, broke skeletal mesh animations.

The fix was reverted as it didn't actually fix the problem and, instead, broke skeletal mesh animations.

This issue was referenced by 315609ec0c

This issue was referenced by 315609ec0c1e28eb12bde3e8bbd2a5b03672b1a9

Changed status from 'Open' to: 'Resolved'

Changed status from 'Open' to: 'Resolved'

Closed by commit 315609ec0c.

Closed by commit 315609ec0c.

This issue was referenced by 59bbe1aa61fcb590b1b046a10eea675cc1947615

This issue was referenced by 59bbe1aa61fcb590b1b046a10eea675cc1947615

This issue was referenced by a145bc7b2191fe99a48e38d102e99e940ed36acf

This issue was referenced by a145bc7b2191fe99a48e38d102e99e940ed36acf

This issue was referenced by 4fac29ca0e

This issue was referenced by 4fac29ca0e8cc09a3f7bce27f549cc75ffdbb1d4

This issue was referenced by 8ebb552a95

This issue was referenced by 8ebb552a9551abcb3d013b192a0dc9c2ea6f2bcc

This issue was referenced by a1a182c268

This issue was referenced by a1a182c2684a7f050f6a85820e183c31ff117e82

This issue was referenced by e5fbe7466e

This issue was referenced by e5fbe7466ecf733d7d3f394eded8901e07c55acd
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
5 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#40257
No description provided.