DisplayRegion in C++

Hello, I’m trying to make a viewport in panda3D C++ by using:

get_graphics_window()->make_display_region(l,r,b,t)

but nothing seem to work so far. I’m wondering if anyone could point me in the right direction in making a display region and setting the camera for it?

The function make_display_region creates a DisplayRegion object. See this API page for the DisplayRegion listing.
To get the default displayRegion, do this:

get_graphics_window()->get_active_display_region(0);

Or you can create a new one using the method you already referred to, where the four parameters are screen coordinates between 0.0 and 1.0 (or -1.0 and 1.0, I don’t know excactly which)

You can then set the cameras and other things on the DisplayRegion, with the set_active(true), and set_camera(mycam) methods. See the API page I referred to earlier this post for more functions.

Thanks for your reply =D But i still can’t get it to render out. This is my code so far:

NodePath myCam = window->get_camera_group();
myCam.set_pos(0,0,10);

DisplayRegion *dr = window->get_graphics_window()->make_display_region(0,0.2,0,0.2);
dr->set_camera(myCam);
dr->set_active(true);

somehow when i run the program it crashes with a fatal error, when i debug it, it always points to the dr->set_camera(myCam).

Try this:

dr->set_camera(myCam.node());

since I think you need a camera node and not a nodepath to the camera. Not sure, though. Maybe one of the c++ devs can shine more light on this.

EDIT: forget it. it does take a nodepath. the problem is somewhere else.

DisplayRegion is another class that inherits from ReferenceCount. You can see this by looking at displayRegion.h.

This means that the line:

DisplayRegion *dr = window->get_graphics_window()->make_display_region(0,0.2,0,0.2);

is going to result in the construction, and immediate destruction, of your new DisplayRegion, since you are not storing it in a PT(DisplayRegion). Thus, your dr pointer is pointing to unallocated memory, and the first time you try to use it, it crashes.

Replace this with:

PT(DisplayRegion) dr = window->get_graphics_window()->make_display_region(0,0.2,0,0.2);

Incidentally, dr->set_camera() does receive a NodePath, not a node, so the code is otherwise correct.

David

thanks for the replies!

well i’ve done what you’ve said but somehow i’m still crashing at the dr->set_camera(myCam) part. On debug, i’ve gotten argv and argc expressions cannot be evaluated errors (it always points to whatever line that is after the set_camera code). Did i instantiate something wrongly?

Oh, it’s also a requirement that the NodePath you pass in contain a reference to a Camera node, not just an ordinary PandaNode. But if you call window->get_camera_group(), you’re just getting an ordinary PandaNode.

There should have been an assertion failure that would have alerted you to this before it crashed. Since you didn’t get the assertion failure, you must be compiling with OPTIMIZE 4 (or with NDEBUG defined). Is this true? If so, I strongly recommend compiling with OPTIMIZE 3 instead, and without NDEBUG defined. Compiling with OPTIMIZE 4 shouldn’t ever be done until you have fully developed and debugged your application, otherwise you will be scratching your head over little mistakes like this.

David

once again a big thank you to you. Well it may sound a little silly, how do i disable NDEDEFINE and change to OPTIMIZE 3? I’ve checked project->properties but cant find anything there, sorry, i’ve never come across this before.

It’s mainly a question of how Panda was built.

Did you build your own Panda, or did you use the prebuilt download? If you built your own, how did you build it?

David

hello, i believe the problem he is facing isnt the assertion error, if i m not wrong, hes from the same course as i, which means both of us are building the panda framework based on what our teacher tells us.
anyway, i m also trying to create a multiple viewport effect, but to no avail, i have researched the manual and classes and it gives no reference as to how to create a proper camera using C++, i tried using windowsframework->make_Camera() only to find it a protected member, all the codes in the forums are python and only show this snippet of code base.makecamera, as showbase.h contains nothing that i can understand, would you please show us how to create a camera using C++?

A Camera is just a node.

PT(Camera) cam = new Camera("cam");

You can also look at the code in windowFramework.cxx, to see what make_camera() is doing. It’s does exactly this line, plus some additional stuff to attach the camera to the camera_group NodePath, and also set up a specific aspect ratio to match the window shape.

If you can read Python, you might also want to take a look at the code in ShowBase.makeCamera(). This does a bit more work, including creating a DisplayRegion and associating it with the camera.

David

Maybe you should try to encourage your teacher to switch gears.

First, he/she is asking you to use C++ instead of python, despite the fact that the manual is in python and the sample programs are in python. Ie, you’re working without documentation. Is that really sensible?

Second, the teacher has asked you to compile your own version, and he’s created his own project file which clearly isn’t set up correctly. Wouldn’t it make more sense to use the precompiled binaries that we provide?

well, we felt the same way too, looking at how everything is documented in python and the amount of python which is discussed in the forums. We’re encouraged to look into the cxx source codes ourselves to find out how things work but i guess sometimes theres just too much to look into.

As for the compilation, i’m pretty sure we used the files provided here (the installer and such) but probably theres a mistake here or there during compilation and we’re paying the price now.

Anyway we’re putting this (viewport) on hold and trying to finish the other components of our assignment first. All of your replies are really greatly appreciated by us.

Cheers!

Well, it’s a nice sentiment to look into the source code, but we’re talking about 50 MB of source files. That’s not including the utility programs or sample programs. It’s a little hard to grasp just how big that is. I started working on Panda a few years ago, and I’m only just now really getting to feel like I can grab a source file and understand it fully without asking David a whole bunch of questions. BTW, I’m a Ph.D. in computer science.

It’s feasible for a beginner to get a high level, abstract understanding of how things work, but not by digging through source code. Also, it’s quite feasible to pick out one particular subsystem and come to understand that at the code level, with a little help from David.

Well, Assignments are assignments, they cant change it for my batch, while doing display regions, i realise that they sometimes get drawn over by the models shown by the first camera, is it because of the depth test?

If there are two overlapping display regions, then yes, they can interact like that.

Panda optionally clears every display region, and optionally clears every window. I believe that the fireflies sample program shows how to turn clears on and off. If a region doesn’t get cleared before it is rendered, then there will be residual crap in the depth buffer and that can have weird effects.