More lighting in C++

I read with interest the other thread on lighting … but I’m having trouble with using the LightAttrib class.

The Python code I’m trying to convert is:

      lAttrib = LightAttrib.makeAllOff()
      ambientLight = AmbientLight( "ambientLight" )
      ambientLight.setColor( Vec4( .8, .8, .8, 1 ) )
      lAttrib = lAttrib.addLight( ambientLight )
      directionalLight = DirectionalLight( "directionalLight" )
      directionalLight.setDirection( Vec3( 0, 45, -45 ) )
      directionalLight.setColor( Vec4( 0.2, 0.2, 0.2, 1 ) )
      lAttrib = lAttrib.addLight( directionalLight )
      render.attachNewNode( directionalLight.upcastToPandaNode() )
      render.attachNewNode( ambientLight.upcastToPandaNode() )
      render.node().setAttrib( lAttrib )

Now, I can convert most of this into C++, but no matter what I try, I can’t create or use a LightAttrib type …

This is what I came up with …

{
  NodePath render = window->get_render();

   PT(LigthAttrib) lightAttrib = new LightAttrib( "lightAttrib" );
   
   PT(AmbientLight) ambientLight = new AmbientLight( "ambientLight" );
   ambientLight->set_color( LVecBase4f( .8, .8, .8, 1 ) );
   lightAttrib->add_on_light( DCAST(PandaNode,ambientLight) );

   PT(DirectionalLight) directionalLight = new DirectionalLight( "directionalLight" );
   directionalLight->set_direction( LVecBase3f( 0, 45, -45 ) );
   directionalLight->set_color( LVecBase4f( 0.2, 0.2, 0.2, 1 ) );
   lightAttrib->add_on_light( DCAST(PandaNode,directionalLight) );
   render.attach_new_node(DCAST(PandaNode, directionalLight));
   render.attach_new_node(DCAST(PandaNode, ambientLight));
   render.node().setAttrib(lightAttrib);
}

This code does not compile, but if I remove the references to lattrib it compiles, but does not work … I get a variety of errors no matter what I try for use of LightAttrib. There are comments in the lightattirb.h file about not using the old interface … but basically none of the public functions seem to be useable …

Any help would be greatly appreciated!

Any more C++ documenation or more importantly sample code would be MUCH appreciated. I have been coding C and C++ for over 20 years, but getting my head around these classes without sample code is very very difficult.

Thanks again,

Joseph

LightAttrib is deprecated. You shouldn’t attempt to use it.
Use the new NodePath::set_light interface.

What he said. Though there is a new, public interface to LightAttrib that replaces the old interface; but you probably don’t need to use it because of the NodePath::set_light() method.

That Python code you dug up is ancient. It looks like it dates back to Panda3D version 1.0 or earlier, with the references to upcastToPandaNode() (made irrelevant in 1.1).

Still, translating Python code to the equivalent C++ code really is a very cut-and-dried process. You almost got it right, but you went wrong on the first line. You should have written:

CPT(LightAttrib) lightAttrib = LightAttrib::make_all_off()

The CPT is necessary instead of PT, because LightAttrib (like all RenderAttrib and RenderState objects) are always const, a concept that doesn’t exist in Python. The Python call LightAttrib.makeAllOff(), a call to the class method of LightAttrib (as opposed to an instance method) translates directly to the C++ call LightAttrib::make_all_off().

Note that this would all be much easier with NodePath::set_light().

David

Thanks for the info.

The python code is from the current chess example tutorial! So if there is something more recent I should be using as a reference, please let me know!!

I tried your example as well, but it does not work either!!

can not convert from ‘ConstPointerTo’ to ‘ConstPointerTo’
1> with [ T=RenderAttrib ] and [ T=LightAttrib ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous

Anyway, I’ll try the NodePath::set_light() method … hopefully I’ll have better luck there … are there any other C++ examples using this method?

Thanks again,

J

Yeah, the chess sample ought to be updated.

To cast from CPT(RenderAttrib) from CPT(LightAttrib), do:

CPT(LightAttrib) lattr = DCAST(LightAttrib, your_render_attrib);

Not aware of any C++ samples, but I recommend looking at the Disco Lights example, which uses the correct way, I believe. Translating that to C++ is a pretty trivial task.