Hi rdb,
I run this example in python and it works great. I’m trying to achieve a similar result in C++, so I started from a very simple scene with a ground plane and the teapot model. I setup a directional light and enabled ‘set_shader_auto()’ on the ‘get_render()’ but I’m not seeing the shadow.
This is the result I get, what am I missing?
Here is my code:
key a/z iincrease/decrease the light
key s calls the callback2 function which “should” enable/disable shadows
Thanks for any help
#include <pandaFramework.h>
#include <pandaSystem.h>
#include <cardMaker.h>
#include "directionalLight.h"
#include "load_prc_file.h" // To handle Panda Config file
NodePath model_np{"model"};
LColor direct_color = LColor(0.5, 0.5, 0.5, 1.0f);
DirectionalLight *dlight;
PandaFramework framework;
WindowFramework* windowFramework;
void callback(const Event* eventPtr, void* data)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
float *diff = (float*)data;
direct_color += LColor(*diff, *diff, *diff, 0.0);
dlight->set_color(direct_color);
}
bool toggle{true};
void callback2(const Event* eventPtr, void* data)
{
if(toggle)
{
std::cout << "enable shader " << std::endl;
dlight->set_shadow_caster(true, 512, 512);
dlight->mark_used_by_auto_shader();
windowFramework->get_render().set_shader_auto();
}
else
{
std::cout << "disable shader " << std::endl;
windowFramework->get_render().clear_shader();
}
toggle = !toggle;
}
int main()
{
// open a new window framework
framework.open_framework();
load_prc_file_data("", "show-frame-rate-meter 1"); // enable frame rate meter
load_prc_file_data("", "framebuffer-multisample 0"); // enable antialising
load_prc_file_data("", "multisamples 0"); // enable antialising
load_prc_file_data("", "sync-video 1");
// set the window title to My Panda3D Window
framework.set_window_title("Shadow test C++");
// open the window
windowFramework = framework.open_window();
windowFramework->setup_trackball();
windowFramework->enable_keyboard();
//
// setup scene
//
NodePath scene = windowFramework->get_render().attach_new_node("scene");
// Flat ground
CardMaker flat_ground("floor");
flat_ground.set_frame({-10.0, -10.0, 0.0}, {10.0, -10.0, 0.0}, {10.0, 10.0, 0.0}, {-10.0, 10.0, 0.0});
PT(PandaNode) flat_ground_node = flat_ground.generate();
NodePath flat_ground_np = scene.attach_new_node(flat_ground_node);
flat_ground_np.set_color(0.8, 0.8, 0.8, 1.0);
flat_ground_np.set_two_sided(true);
// Load model
NodePath model_loader = windowFramework->get_panda_framework()->get_models();
Filename pandafile = Filename::from_os_specific("teapot.egg");
model_np = windowFramework->load_model(model_loader, pandafile);
if(model_np.is_empty())
{
std::cerr << "Cannot load model " << std::endl;
return -1;
}
model_np.reparent_to(scene);
model_np.set_pos(0.0, 0.0, 0.0);
model_np.set_two_sided(true);
NodePath camera_group = windowFramework->get_camera_group();
camera_group.set_pos(0, 0, 10);
camera_group.set_hpr(0, -30, 0);
// lights
NodePath light_group = camera_group.attach_new_node("lights");
dlight = new DirectionalLight("directional");
dlight->set_color(direct_color);
NodePath directional_light = light_group.attach_new_node(dlight);
directional_light.set_hpr(-10, -20, 0);
dlight->get_lens()->set_near_far(1, 30);
dlight->get_lens()->set_film_size(20, 40);
dlight->show_frustum();
dlight->set_shadow_caster(true, 4096, 4096);
// apply light to model
model_np.set_light(directional_light);
// setup automatic shadow handler
callback2(nullptr, nullptr);
// keyboard shortcut to increase/descrease lighting
float inc = 0.05;
float dic = -0.05;
framework.define_key("a", "more light", callback, &inc);
framework.define_key("z", "less light", callback, &dic);
framework.define_key("s", "shadows", callback2, nullptr);
// do the main loop, equal to run() in python
framework.main_loop();
// close the window framework
framework.close_framework();
return 0;
}