Using DisplayRegion in cpp

How can we use displayregion in cpp, its the case that the make_display_region is not a function of the windowframework but of the graphicsoutput if i set the display region for the graphicsoutput of the window then it does not change anything in the output.

My Code:

// MovingPanda.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "pandaFramework.h"
#include "pandaSystem.h"
#include "displayregion.h" 
#include "genericAsyncTask.h"
#include "asyncTaskManager.h"
 
// Global stuff
PandaFramework framework;
PT(AsyncTaskManager) taskMgr = AsyncTaskManager::get_global_ptr(); 
PT(ClockObject) globalClock = ClockObject::get_global_clock();
NodePath camera;
NodePath camera2;
NodePath pandaActor;
DisplayRegion *dRegion1=NULL;
// Task to move the camera
AsyncTask::DoneStatus SpinCameraTask(GenericAsyncTask* task, void* data) {
    double time = globalClock->get_real_time();
    double angledegrees = time * 6.0;
    double angleradians = angledegrees * (3.14 / 180.0);

	//camera.set_pos(20*sin(angleradians),-20.0*cos(angleradians),100);
    //camera.set_hpr(angledegrees, -90, 0);
    
   
    pandaActor.set_pos(10*cos(angleradians),10*sin(angleradians),0);
	pandaActor.set_hpr(180+angledegrees,0,0);
    return AsyncTask::DS_cont;
}
 
int main(int argc, char *argv[]) {

	framework.open_framework(argc, argv);
    framework.set_window_title("My Panda3D Window");
 
    // Open the window
    WindowFramework *window = framework.open_window();
	dRegion1 = window ->get_graphics_output()->make_display_region(0.5,1,0,1);
    
	camera = window->get_camera(0); // Get the camera and store it
    camera2 = window->get_camera(1);
    dRegion1->set_camera(camera2);
	// Load the environment model
    NodePath environ = window->load_model(framework.get_models(), "models/environment");
    environ.reparent_to(window->get_render());
    environ.set_scale(0.25 , 0.25, 0.25);
    environ.set_pos(-8, 42, 0);

	camera.set_pos(0,0,100);
    camera.set_hpr(0, -90, 0);

	camera2.set_pos(0,0,100);
    camera2.set_hpr(0, -90, 0);


    // Load our panda
    pandaActor = window->load_model(framework.get_models(), "panda-model");
    pandaActor.set_scale(0.005);
    pandaActor.reparent_to(window->get_render());
    
    // Load the walk animation
    window->load_model(pandaActor, "panda-walk4");
    window->loop_animations(0);
 
    // Add our task do the main loop, then rest in peace.
    taskMgr->add(new GenericAsyncTask("Spins the camera", &SpinCameraTask, (void*) NULL));
    framework.main_loop();
    framework.close_framework();
    return (0);
}

can someone tell me what to do.

(1) You should store the DisplayRegion in a PT(DisplayRegion), not a simple DisplayRegion*.

(2) You need to create a camera for the DisplayRegion. You can re-use an existing camera if you want, but I don’t think you’ve done that successfully in your code. You say camera2 = window->get_camera(1), but if there is no camera(1) already, this will display an error message and return the empty NodePath, which will do nothing for the DisplayRegion.

(3) Since your new DisplayRegion overlaps the existing one, you will need to set a sort value to specify the order in which it will be drawn. If you leave the sort value at its default value of 0 (which is the same sort value as the existing one), its order is unspecified, and it may draw behind the existing DisplayRegion, making it invisible. Give it a larger sort value. Or, find the original DisplayRegion and disable it or change its region.

David

Thanks for the prompt reply. I did get the display region working.
But i was doing some event handling i, and i get a runtime error:

Unhandled exception at 0x776915ee in MovingPanda.exe: 0xC0000005: Access violation reading location 0x0000018c.
in the lines(while executing the first line):
ButtonThrower *bt = DCAST(ButtonThrower, btnp.node());
bt->set_button_down_event(“button_down”);

my code is :

// MovingPanda.cpp : Defines the entry point for the console application. 
 // 

 #include "stdafx.h" 
 #include "pandaFramework.h" 
 #include "pandaSystem.h" 
 #include "displayregion.h" 
 #include "genericAsyncTask.h" 
 #include "asyncTaskManager.h" 
 #include "eventHandler.h"   
 #include "buttonThrower.h" 
 // Global stuff 
 PandaFramework framework; 
 PT(AsyncTaskManager) taskMgr = AsyncTaskManager::get_global_ptr(); 
 PT(ClockObject) globalClock = ClockObject::get_global_clock(); 
 PT(DisplayRegion) dRegion1; 
 NodePath camera; 
 NodePath camera2; 
 NodePath pandaActor; 
 void event_button_down (const Event* evenmt, void* data) ; 
 // Task to move the camera 
 AsyncTask::DoneStatus SpinCameraTask(GenericAsyncTask* task, void* data) { 
     double time = globalClock->get_real_time(); 
     double angledegrees = time * 6.0; 
     double angleradians = angledegrees * (3.14 / 180.0); 

    //camera.set_pos(20*sin(angleradians),-20.0*cos(angleradians),100); 
     //camera.set_hpr(angledegrees, -90, 0); 
     
     
     pandaActor.set_pos(10*cos(angleradians),10*sin(angleradians),0); 
    pandaActor.set_hpr(180+angledegrees,0,0); 
     return AsyncTask::DS_cont; 
 } 
   
 int main(int argc, char *argv[]) { 

    framework.open_framework(argc, argv); 
     framework.set_window_title("My Panda3D Window"); 
   
     // Open the window 
     WindowFramework *window = framework.open_window(); 
    dRegion1 = window ->get_graphics_output()->make_display_region(.75,1,.75,1); 
    dRegion1->set_sort(1); 
    camera = window->get_camera(0); // Get the camera and store it 
	camera2 = window->make_camera();
     //camera2 = window->get_camera(1); 
     dRegion1->set_camera(camera2); 
    // Load the environment model 
     NodePath environ = window->load_model(framework.get_models(), "models/environment"); 
     environ.reparent_to(window->get_render()); 
     environ.set_scale(0.25 , 0.25, 0.25); 
     environ.set_pos(-8, 42, 0); 

  
     camera.set_pos(0,0,100); 
     camera.set_hpr(0, -90, 0); 

     camera2.set_pos(0,-20,3); 
     camera2.set_hpr(0, 0, 0); 


     // Load our panda 
     pandaActor = window->load_model(framework.get_models(), "panda-model"); 
     pandaActor.set_scale(0.005); 
     pandaActor.reparent_to(window->get_render()); 
     
     // Load the walk animation 
     window->load_model(pandaActor, "panda-walk4"); 
     window->loop_animations(0); 
   
     // Add our task do the main loop, then rest in peace. 
     taskMgr->add(new GenericAsyncTask("Spins the camera", &SpinCameraTask, (void*) NULL)); 
     
	 NodePath btnp = window->get_button_thrower(); 
	 printf("%d",btnp.node());
	 ButtonThrower *bt = DCAST(ButtonThrower, btnp.node()); 
 	 bt->set_button_down_event("button_down"); 
	 framework.get_event_handler().add_hook("button_down", event_button_down,NULL); 
     
	 
	 framework.main_loop(); 

	  

	 framework.close_framework(); 
     return (0); 
 }

 void event_button_down (const Event* evenmt, void* data) { 
 std::cout << "Button Down\n"; 
}

commenting the last 5 lines in main makes the display region work properly,

 NodePath btnp = window->get_button_thrower(); 
	 printf("%d",btnp.node());
	 ButtonThrower *bt = DCAST(ButtonThrower, btnp.node()); 
 	 bt->set_button_down_event("button_down"); 
	 framework.get_event_handler().add_hook("button_down", event_button_down,NULL); 
     

some error in the code.
Thanks again david.

If you didn’t call window->enable_keyboard(), then window->button_thrower() will be an empty NodePath, and window->button_thrower().node() will be NULL.

For developing code in C++, I strongly recommend setting “assert-abort 1” in your Config.prc file. This will make the assertion errors that you are currently not noticing cause a C++ exception that you will see in the debugger.

David

Thanks david once again,
Finally got the code working, Added some functions to move around the camera, not exactly intuitive but it works.

// MovingPanda.cpp : Defines the entry point for the console application. 
 // 

 #include "stdafx.h" 
 #include "pandaFramework.h" 
 #include "pandaSystem.h" 
 #include "displayregion.h" 
 #include "genericAsyncTask.h" 
 #include "asyncTaskManager.h" 
 #include "eventHandler.h"   
 #include "buttonThrower.h" 
 // Global stuff 
 PandaFramework framework; 
 PT(AsyncTaskManager) taskMgr = AsyncTaskManager::get_global_ptr(); 
 PT(ClockObject) globalClock = ClockObject::get_global_clock(); 
 PT(DisplayRegion) dRegion1; 
 NodePath camera; 
 NodePath camera2; 
 NodePath pandaActor; 
 void event_button_down (const Event* evenmt, void* data) ; 
 // Task to move the camera 
 AsyncTask::DoneStatus SpinCameraTask(GenericAsyncTask* task, void* data) { 
     double time = globalClock->get_real_time(); 
     double angledegrees = time * 6.0; 
     double angleradians = angledegrees * (3.14 / 180.0); 

    //camera.set_pos(20*sin(angleradians),-20.0*cos(angleradians),100); 
     //camera.set_hpr(angledegrees, -90, 0); 
     
     
     pandaActor.set_pos(10*cos(angleradians),10*sin(angleradians),0); 
    pandaActor.set_hpr(180+angledegrees,0,0); 
     return AsyncTask::DS_cont; 
 } 
   
 int main(int argc, char *argv[]) { 

    framework.open_framework(argc, argv); 
     framework.set_window_title("My Panda3D Window"); 
   
     // Open the window 
     WindowFramework *window = framework.open_window(); 
    dRegion1 = window ->get_graphics_output()->make_display_region(.75,1,.75,1); 
    dRegion1->set_sort(1); 
    camera = window->get_camera(0); // Get the camera and store it 
	camera2 = window->make_camera();
     //camera2 = window->get_camera(1); 
     dRegion1->set_camera(camera2); 
    // Load the environment model 
     NodePath environ = window->load_model(framework.get_models(), "models/environment"); 
     environ.reparent_to(window->get_render()); 
     environ.set_scale(0.25 , 0.25, 0.25); 
     environ.set_pos(-8, 42, 0); 

  
     camera.set_pos(0,0,100); 
     camera.set_hpr(0, -90, 0); 

     camera2.set_pos(0,-20,3); 
     camera2.set_hpr(0, 0, 0); 


     // Load our panda 
     pandaActor = window->load_model(framework.get_models(), "panda-model"); 
     pandaActor.set_scale(0.005); 
     pandaActor.reparent_to(window->get_render()); 
     
     // Load the walk animation 
     window->load_model(pandaActor, "panda-walk4"); 
     window->loop_animations(0); 
     window->enable_keyboard();
     // Add our task do the main loop, then rest in peace. 
     taskMgr->add(new GenericAsyncTask("Spins the camera", &SpinCameraTask, (void*) NULL)); 
     
	 NodePath btnp = window->get_button_thrower(); 
	 printf("%d",btnp.node());
	 ButtonThrower *bt = DCAST(ButtonThrower, btnp.node()); 
// 	 bt->set_button_down_event("button_down"); 
//	 framework.get_event_handler().add_hook("button_down", event_button_down,NULL); 
     bt->set_button_repeat_event("button_press");
	 framework.get_event_handler().add_hook("button_press", event_button_down,NULL); 
	 
	 framework.main_loop(); 

	  

	 framework.close_framework(); 
     return (0); 
 }

 void event_button_down (const Event* evenmt, void* data) { 
 std::cout << evenmt->get_parameter(0).get_string_value(); 
 if(strcmp(evenmt->get_parameter(0).get_string_value().data(),"e")==0)
 {
	//cout << "Go Deeper";
	camera.set_z(camera.get_z()-0.5);
 }
 if(strcmp(evenmt->get_parameter(0).get_string_value().data(),"q")==0)
 {
	//cout << "Go Higher";
	camera.set_z(camera.get_z()+0.5);
 }

 if(strcmp(evenmt->get_parameter(0).get_string_value().data(),"a")==0)
 {
	//cout << "Go Left";
	camera.set_x(camera.get_x()-.5);
 }
 if(strcmp(evenmt->get_parameter(0).get_string_value().data(),"d")==0)
 {
	//cout << "Go Right";
	camera.set_x(camera.get_x()+.5);
 }

 if(strcmp(evenmt->get_parameter(0).get_string_value().data(),"w")==0)
 {
	//cout << "Go Forward";
	camera.set_y(camera.get_y()+.5);
 }
 if(strcmp(evenmt->get_parameter(0).get_string_value().data(),"s")==0)
 {
	//cout << "Go Back";
	camera.set_y(camera.get_y()-.5);
 }

  if(strcmp(evenmt->get_parameter(0).get_string_value().data(),"d")==0)
 {
	//cout << "Go Right";
	camera.set_x(camera.get_x()+.5);
 }

 if(strcmp(evenmt->get_parameter(0).get_string_value().data(),"i")==0)
 {
	//cout << "Go Forward";
	camera.set_h(camera.get_h()+.5);
 }
 if(strcmp(evenmt->get_parameter(0).get_string_value().data(),"p")==0)
 {
	//cout << "Go Back";
	camera.set_h(camera.get_h()-.5);
 }


 if(strcmp(evenmt->get_parameter(0).get_string_value().data(),"o")==0)
 {
	//cout << "Go Forward";
	camera.set_p(camera.get_p()+.5);
 }
 if(strcmp(evenmt->get_parameter(0).get_string_value().data(),"l")==0)
 {
	//cout << "Go Back";
	camera.set_p(camera.get_p()-.5);
 }

 if(strcmp(evenmt->get_parameter(0).get_string_value().data(),"k")==0)
 {
	//cout << "Go Forward";
	camera.set_r(camera.get_r()+.5);
 }
 if(strcmp(evenmt->get_parameter(0).get_string_value().data(),";")==0)
 {
	//cout << "Go Back";
	camera.set_r(camera.get_r()-.5);
 }
}