Set lens of default camera

I want to test if faces of a large, procedurally generated mesh are inside the view frustum. I have a point on the triangle, and do the following

PT(Camera) camNode = (Camera*)cam.node();
LPoint3f p(nv->GetX(), nv->GetY(), nv->GetZ() );
LPoint3f rp = cam.get_relative_point(roamer->np, p);
if( !camNode->is_in_view(rp) )

That’s fine, except that I get an assertion:

Assertion failed: lens != (Lens *)NULL at line 148 of panda/src/pgraph/lensNode.cxx

I assumed this somehow meant that the default camera didn’t have a lens associated with it. So, I tried to add a lens the camera, but then get a memory access error, as produced by the following minimal example, at the set_lens method:


#include "pandaFramework.h"
#include "pandaSystem.h"
#include "genericAsyncTask.h"
#include "perspectiveLens.h"

PandaFramework framework;
WindowFramework *window;






int main(int argc, char *argv[]) 
{
	//open a new window framework
	framework.open_framework(argc, argv);
	
    //set the window title to My Panda3D Window
	framework.set_window_title("My Panda3D Window");
    
	//open the window
	window = framework.open_window();
	
	NodePath cam = window->get_camera_group();
	PT(Camera) camNode = (Camera*)cam.node();
	PerspectiveLens *pl = new PerspectiveLens();
	pl->set_fov(67);
        // memory access error when calling this:
	camNode->set_lens(pl);
		
	//do the main loop, equal to run() in python
 	framework.main_loop();
	
    
	//close the window framework
	framework.close_framework();
	return (0);
}

So, question is, what am I missing/doing wrong?

Hi, try this one

#include "camera.h"
#include "lens.h"
#include "perspectiveLens.h"

...
PT(Camera)camera_main    = window->get_camera(0);
PT(PerspectiveLens) lens = DCAST(PerspectiveLens,camera_main->get_lens());
lens->set_fov(67);
...

Thank you,
though not quite answering what I asked, it does help.

It seems that the camera I get from

PT(Camera)camera_main    = window->get_camera(0);

does permit me to use

camera_main->is_in_view(rp)

which the camera I otherwise got would not.Not sure why this should be the case, but I shall try using this to solve my actual problem.

It still leaves open the question of why the provided sample code gives a memory access error, so if someone does have an answer to that, I’d still be interested in a response.

Never used it but shouldn’t it be :

lens->is_in_view(rp) 

The problem is that this node:

NodePath cam = window->get_camera_group(); 

is a “camera group”, it isn’t a camera. So this call:

PT(Camera) camNode = (Camera*)cam.node(); 

is incorrectly casting an ordinary PandaNode into an incorrect type (it’s not a Camera), and this causes all sorts of badness.

This is, for the record, why we have the DCAST macro, which does typesafe casting. If you had written:

PT(Camera) camNode = DCAST(Camera, cam.node());

then you would have gotten an assertion failure at that point, which would hopefully have alerted you to your mistake.

David