procedural geometry, lights and shadows


i’m playing around with volumes/voxels and surface extraction. i just got my marching cubes algorithm working and now i wanted to render the geometry with panda. there is just one little problem. the objekt is all flat, there is no light or shadow. i added ambient light to render and to the nodepath of the object, but nothing happened.

heres a picture:

any ideas? i did not know with which keywords to search for this in the forums, so please don’t be mad if this is something what has been answered a thousand times…

thanks, loschi

AmbientLight adds no shading. Use any other kind of light, for instance, a DirectionalLight is a good choice.


i added a directional light to the object but nothing happened…

i tried to place the lights an the object with the method. when moving and rotating the procedurally generated object its color changed, so i think the lights do have an influence on it. but there was no shading.

here is the code i’m using for the light and for generating the geometry:

# Create Ambient Light
		ambientLight = AmbientLight('ambientLight')
		ambientLight.setColor(Vec4(0.1, 0.1, 0.1, 1))
		ambientLightNP = render.attachNewNode(ambientLight)
		# Directional light 01
		directionalLight = DirectionalLight('directionalLight')
		directionalLight.setColor(Vec4(0.8, 0.2, 0.2, 1))
		directionalLightNP = render.attachNewNode(directionalLight)
		# This light is facing backwards, towards the camera.
		directionalLightNP.setHpr(180, -20, 0)
		# Directional light 02
		directionalLight = DirectionalLight('directionalLight')
		directionalLight.setColor(Vec4(0.2, 0.2, 0.8, 1))
		directionalLightNP = render.attachNewNode(directionalLight)
		# This light is facing forwards, away from the camera.
		directionalLightNP.setHpr(0, -20, 0)
		# Create and register GeomVertexFormat
		format = GeomVertexFormat.getV3()
		format = GeomVertexFormat.registerFormat(format)
		# Create GeomVertexData and Writers for vertices, normals and colors
		vdata = GeomVertexData('Cube', format, Geom.UHDynamic)
		vertex = GeomVertexWriter(vdata, 'vertex')
		tris = self.mesh.triangles
		volume = self.volume
		extractor = self.extractor
		mesh = self.mesh
		extractor.extractSurface(volume, mesh)
		# Add some Data to the Tables ( iterate through given surface)
		for i in range(len(tris)):
			vertex.addData3f(tris[i][0][0], tris[i][0][1], tris[i][0][2])
			vertex.addData3f(tris[i][1][0], tris[i][1][1], tris[i][1][2])
			vertex.addData3f(tris[i][2][0], tris[i][2][1], tris[i][2][2])
		# Create a primitive which is using the vertices
		prim = GeomTriangles(Geom.UHDynamic)
		for j in range(1, len(tris)):
		# Create Geom and add primitives
		geom = Geom(vdata)
		# Create Node and add Geom
		node = GeomNode('gnode')
		nodePath = render.attachNewNode(node)

thanks in advance, loschi

You will also need to compute a normal for each vertex of your geometry, and store that in your GeomVertexData. The lighting calculation is made based on the vertex position and the normal at that vertex; without normals, you will get only a random, flat shade.


yay, that looks much better :wink: thanks!

Hi Loschi, remember me from thermite3d? Looks like me and you have exactly the same things in mind :wink:
It’s a shame there’s no other package like Polyvox with a good python interface, or at least a package that is easier to wrap for a python interface.

Can I ask what your project is all about?

Sure :slight_smile: Initially it was for my Bachelor Thesis. I was writing about realtime destruction in virtual gaming environments. (My university is NOT into gaming OR computer graphics, so i picked this topic on my own risk :wink: )

And now, after i’m done writing, i just want to play around with panda and volumes/voxels to create a stone/wood carving game. In the fashion of “colors by numbers” i want the game to be “carving by colors”. There is a white boulder and inside it there are colored layers which are getting more and more detailed. The last layer will be the finished figure.

I think i will try to wrap PolyVox again. PolyVox-David proposed to create a simpler c++ class which wraps the needed functionality of PolyVox and then wrap this simpler class for python. I hope my rusty c++ will suffice :wink: