Lighting and C++

I’m using Panda along with 2 other Engines in a Uni project looking at different engines, along with this I need to do simple demos across all three to compare the implementation and the performance. Panda3D is my last to do, and I’m finding the documentation rather frustrating as I know nothing of Python, and especially with lighting I see these examples done in Python that baffle me as to how to recreate in C++!

dlight = DirectionalLight(‘dlight’)
dlnp = render.attachNewNode(dlight.upcastToPandaNode())

What IS dlight in this context? What is ‘dlight’? The Constructor for that takes nothing of the sort in C++. upcastToPandaNode() isn’t in the Light object either, is that as_node()?

Also annoying having the documentation contain different naming conventions than the C++ implementation :frowning:

The following would be very close, but not exactly right:

DirectionalLight *dlight = new DirectionalLight(“dlight”);
NodePath dlnp = render.attach_new_node((PandaNode *)dlight);

This is wrong for three reasons.

First, you see that I’m casting the DirectionalLight to a PandaNode. The cast is totally unnecessary — C++ automatically converts an object of a subclass to an object of a superclass without your having to ask. The explicit upcast in python is there because our python wrapper-generator used to be incapable of this. In fact, we’ve improved our wrapper generator, and the explicit upcast is no longer necessary in python, either.

Second, the global variable “render” (the root of the scene graph) does not exist. When using python, there is some initialization code that creates a scene graph for you, and stores it in a global variable called “render.” But since this is python code that does this, it’s not there when you use C++. Instead, C++ programmers have to create their own scene graphs. The C++ tutorial program shows how to do this.

Third, panda uses reference counting to garbage collect nodes that are no longer needed. But the code above is not maintaining the reference counts. To do so, you must use smart pointers, not ordinary pointers. The corrected version looks like this:

PT(DirectionalLight) dlight = new DirectionalLight(“dlight”);
NodePath dlnp = render.attach_new_node(DCAST(PandaNode, dlight));

I’ve gotten as far as setting up a scene and have everything in the demo working bar lighting.

As for your correct version, I actually got as far before I got lost because of the same error your example gives:

error C2514: ‘PointLight’ : class has no constructors

(PointLight, DirectionalLight etc)

Which made me think I was interpreting the Python horribly :stuck_out_tongue:

I assume you were attempting to create a PointLight when you got that error message. Are you sure you #included pointLight.h?


Aha that solved it, though boggles to mind how I’d figure it out based on the documentation for C++ users :stuck_out_tongue: