Minimal triangle rendering example not working

Following code produces an empty image and I cannot figure out what’s wrong:

#! /usr/bin/env python3
from panda3d.core import *
loadPrcFileData('', 'audio-library-name null')
loadPrcFileData('', 'load-display pandagl')
loadPrcFileData('', 'win-size 640 480')
loadPrcFileData('', 'window-type none')
from direct.showbase.ShowBase import ShowBase

gvd = GeomVertexData('', GeomVertexFormat.getV3(), Geom.UHStatic)
gvw = GeomVertexWriter(gvd, 'vertex')
gvw.addData3(0, 10, 0)
gvw.addData3(5, 10, 5)
gvw.addData3(5, 10, 0)
gt = GeomTriangles(Geom.UHStatic)
gt.addVertices(0, 1, 2)
gt.closePrimitive()
g = Geom(gvd)
g.addPrimitive(gt)
gn = GeomNode('')
gn.addGeom(g)
np = NodePath(gn)

class MyApp(ShowBase):
	def __init__(self):
		super().__init__(self, windowType = 'offscreen')
		self.camera.setPos(0, 0, 0)
		self.camera.lookAt(0, 10, 0)
		np.reparentTo(self.render)
		self.graphicsEngine.renderFrame()
		self.graphicsEngine.flipFrame()
		self.win.saveScreenshot('offscreen.png')
app = MyApp()

Using np = self.loader.loadModel(...) instead within __init__ before reparendTo() line works fine.

Based on some experimentation, it looks to me like the problem is twofold:

First, the order in which your triangle vertices are given results in the triangle facing away from the camera, and thus backface-culling is removing it. Changing the order of the vertices in the call to “addVertices” (e.g. to (0, 2, 1)) seems to fix this.

And second, it looks like a second set of “render-” and “flip-” frames is required to get the triangle to be rendered.

That is, instead of this:

		self.graphicsEngine.renderFrame()
		self.graphicsEngine.flipFrame()

Something like this:

		self.graphicsEngine.renderFrame()
		self.graphicsEngine.flipFrame()
		self.graphicsEngine.renderFrame()
		self.graphicsEngine.flipFrame()

(Or, alternatively, setting the y-position of “np”. I’m not sure of why this also works, offhand…)

Yeah adding vertices in different order and using np.setPos(0, 10, 0) fixed it. Thanks!

1 Like