SHARE
TWEET

Untitled

a guest Mar 2nd, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. from direct.showbase.ShowBase import ShowBase
  3. from direct.task import Task
  4. from panda3d.core import *
  5.  
  6. class MyApp(ShowBase):
  7.     def __init__(self):
  8.         ShowBase.__init__(self)
  9.  
  10.         # Move camera for a better view
  11.         self.disableMouse() # if you leave mouse mode enabled camera position will be governed by Panda mouse control
  12.         self.camera.setY(- 10)
  13.  
  14.         # Enable fast exit
  15.         self.accept("escape", sys.exit)
  16.  
  17.         # Create cube
  18.         gNode = self.createCube()
  19.         self.cubeNodePath = self.render.attachNewNode(gNode)
  20.  
  21.         # Add a simple point light
  22.         plight = PointLight('plight')
  23.         plight.setColor(VBase4(0.5, 0.5, 0.5, 1))
  24.         #plight.setAttenuation(Point3(0, 0, 0.5))
  25.         plnp = self.render.attachNewNode(plight)
  26.         plnp.setPos(4, -4, 4)
  27.         self.render.setLight(plnp)
  28.         # Add an ambient light
  29.         alight = AmbientLight('alight')
  30.         alight.setColor(VBase4(0.2, 0.2, 0.2, 1))
  31.         alnp = self.render.attachNewNode(alight)
  32.         self.render.setLight(alnp)
  33.  
  34.         # Add the spinCubeTask procedure to the task manager.
  35.         self.taskMgr.add(self.spinCubeTask, "spinCubeTask")
  36.  
  37.     def spinCubeTask(self, task):
  38.         angleDegrees = task.time * 6.0
  39.         self.cubeNodePath.setHpr(angleDegrees, angleDegrees, angleDegrees)
  40.         return Task.cont
  41.  
  42.     def createCube(self):
  43.         format = GeomVertexFormat.getV3n3t2()
  44.         vertexData = GeomVertexData('cube', format, Geom.UHStatic)
  45.  
  46.         vertexData.setNumRows(24)
  47.  
  48.         vertices = GeomVertexWriter(vertexData, 'vertex')
  49.         normals = GeomVertexWriter(vertexData, 'normal')
  50.         texcoord = GeomVertexWriter(vertexData, 'texcoord')
  51.        
  52.         n_forward = (0, 0, 1)
  53.         n_back = (0, 0, -1)
  54.         n_down = (0, -1, 0)
  55.         n_left = (-1, 0, 0)
  56.         n_right = (1, 0, 0)
  57.         n_up = (0, 1, 0)
  58.  
  59.         p0 = (-0.5, -0.5, 0.5)
  60.         p1 = (0.5, -0.5,  0.5)
  61.         p2 = (0.5, -0.5, -0.5)
  62.         p3 = (-0.5, -0.5, -0.5)
  63.         p4 = (-0.5, 0.5, 0.5)
  64.         p5 = (0.5, 0.5, 0.5)
  65.         p6 = (0.5, 0.5, -0.5)
  66.         p7 = (-0.5, 0.5, -0.5)
  67.        
  68.         uv00 = (0, 0)
  69.         uv10 = (1, 0)
  70.         uv01 = (0, 1)
  71.         uv11 = (1, 1)
  72.        
  73.         vertices.addData3f(p4)
  74.         vertices.addData3f(p5)
  75.         vertices.addData3f(p1)
  76.         vertices.addData3f(p0)
  77.        
  78.         texcoord.addData2f(uv11)
  79.         texcoord.addData2f(uv01)
  80.         texcoord.addData2f(uv00)
  81.         texcoord.addData2f(uv10)
  82.        
  83.         normals.addData3f(n_forward)
  84.         normals.addData3f(n_forward)
  85.         normals.addData3f(n_forward)
  86.         normals.addData3f(n_forward)
  87.  
  88.         primitive = GeomTriangles(Geom.UHStatic)
  89.         primitive.addVertices(3, 1, 0)
  90.         primitive.addVertices(3, 2, 1)
  91.  
  92.         geom = Geom(vertexData)
  93.         geom.addPrimitive(primitive)
  94.  
  95.         node = GeomNode('cube gnode')
  96.         node.addGeom(geom)
  97.         return node
  98.  
  99. app = MyApp()
  100. app.run()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top