main.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/usr/bin/env python
  2. #
  3. # Bump mapping is a way of making polygonal surfaces look
  4. # less flat. This sample uses normal mapping for all
  5. # surfaces, and also parallax mapping for the column.
  6. #
  7. # This is a tutorial to show how to do normal mapping
  8. # in panda3d using the Shader Generator.
  9. from direct.showbase.ShowBase import ShowBase
  10. from panda3d.core import loadPrcFileData
  11. from panda3d.core import WindowProperties
  12. from panda3d.core import Filename, Shader
  13. from panda3d.core import AmbientLight, PointLight
  14. from panda3d.core import TextNode
  15. from panda3d.core import LPoint3, LVector3
  16. from direct.task.Task import Task
  17. from direct.actor.Actor import Actor
  18. from direct.gui.OnscreenText import OnscreenText
  19. from direct.showbase.DirectObject import DirectObject
  20. from direct.filter.CommonFilters import *
  21. import sys
  22. import os
  23. # Function to put instructions on the screen.
  24. def addInstructions(pos, msg):
  25. return OnscreenText(text=msg, style=1, fg=(1, 1, 1, 1), scale=.05,
  26. shadow=(0, 0, 0, 1), parent=base.a2dTopLeft,
  27. pos=(0.08, -pos - 0.04), align=TextNode.ALeft)
  28. # Function to put title on the screen.
  29. def addTitle(text):
  30. return OnscreenText(text=text, style=1, fg=(1, 1, 1, 1), scale=.08,
  31. parent=base.a2dBottomRight, align=TextNode.ARight,
  32. pos=(-0.1, 0.09), shadow=(0, 0, 0, 1))
  33. class BumpMapDemo(ShowBase):
  34. def __init__(self):
  35. # Configure the parallax mapping settings (these are just the defaults)
  36. loadPrcFileData("", "parallax-mapping-samples 3\n"
  37. "parallax-mapping-scale 0.1")
  38. # Initialize the ShowBase class from which we inherit, which will
  39. # create a window and set up everything we need for rendering into it.
  40. ShowBase.__init__(self)
  41. # Check video card capabilities.
  42. if not self.win.getGsg().getSupportsBasicShaders():
  43. addTitle("Bump Mapping: "
  44. "Video driver reports that Cg shaders are not supported.")
  45. return
  46. # Post the instructions
  47. self.title = addTitle("Panda3D: Tutorial - Bump Mapping")
  48. self.inst1 = addInstructions(0.06, "Press ESC to exit")
  49. self.inst2 = addInstructions(0.12, "Move mouse to rotate camera")
  50. self.inst3 = addInstructions(0.18, "Left mouse button: Move forwards")
  51. self.inst4 = addInstructions(0.24, "Right mouse button: Move backwards")
  52. self.inst5 = addInstructions(0.30, "Enter: Turn bump maps Off")
  53. # Load the 'abstract room' model. This is a model of an
  54. # empty room containing a pillar, a pyramid, and a bunch
  55. # of exaggeratedly bumpy textures.
  56. self.room = loader.loadModel("models/abstractroom")
  57. self.room.reparentTo(render)
  58. # Make the mouse invisible, turn off normal mouse controls
  59. self.disableMouse()
  60. props = WindowProperties()
  61. props.setCursorHidden(True)
  62. self.win.requestProperties(props)
  63. self.camLens.setFov(60)
  64. # Set the current viewing target
  65. self.focus = LVector3(55, -55, 20)
  66. self.heading = 180
  67. self.pitch = 0
  68. self.mousex = 0
  69. self.mousey = 0
  70. self.last = 0
  71. self.mousebtn = [0, 0, 0]
  72. # Start the camera control task:
  73. taskMgr.add(self.controlCamera, "camera-task")
  74. self.accept("escape", sys.exit, [0])
  75. self.accept("mouse1", self.setMouseBtn, [0, 1])
  76. self.accept("mouse1-up", self.setMouseBtn, [0, 0])
  77. self.accept("mouse2", self.setMouseBtn, [1, 1])
  78. self.accept("mouse2-up", self.setMouseBtn, [1, 0])
  79. self.accept("mouse3", self.setMouseBtn, [2, 1])
  80. self.accept("mouse3-up", self.setMouseBtn, [2, 0])
  81. self.accept("enter", self.toggleShader)
  82. self.accept("j", self.rotateLight, [-1])
  83. self.accept("k", self.rotateLight, [1])
  84. self.accept("arrow_left", self.rotateCam, [-1])
  85. self.accept("arrow_right", self.rotateCam, [1])
  86. # Add a light to the scene.
  87. self.lightpivot = render.attachNewNode("lightpivot")
  88. self.lightpivot.setPos(0, 0, 25)
  89. self.lightpivot.hprInterval(10, LPoint3(360, 0, 0)).loop()
  90. plight = PointLight('plight')
  91. plight.setColor((1, 1, 1, 1))
  92. plight.setAttenuation(LVector3(0.7, 0.05, 0))
  93. plnp = self.lightpivot.attachNewNode(plight)
  94. plnp.setPos(45, 0, 0)
  95. self.room.setLight(plnp)
  96. # Add an ambient light
  97. alight = AmbientLight('alight')
  98. alight.setColor((0.2, 0.2, 0.2, 1))
  99. alnp = render.attachNewNode(alight)
  100. self.room.setLight(alnp)
  101. # Create a sphere to denote the light
  102. sphere = loader.loadModel("models/icosphere")
  103. sphere.reparentTo(plnp)
  104. # Tell Panda that it should generate shaders performing per-pixel
  105. # lighting for the room.
  106. self.room.setShaderAuto()
  107. self.shaderenable = 1
  108. def setMouseBtn(self, btn, value):
  109. self.mousebtn[btn] = value
  110. def rotateLight(self, offset):
  111. self.lightpivot.setH(self.lightpivot.getH() + offset * 20)
  112. def rotateCam(self, offset):
  113. self.heading = self.heading - offset * 10
  114. def toggleShader(self):
  115. self.inst5.destroy()
  116. if (self.shaderenable):
  117. self.inst5 = addInstructions(0.30, "Enter: Turn bump maps On")
  118. self.shaderenable = 0
  119. self.room.setShaderOff()
  120. else:
  121. self.inst5 = addInstructions(0.30, "Enter: Turn bump maps Off")
  122. self.shaderenable = 1
  123. self.room.setShaderAuto()
  124. def controlCamera(self, task):
  125. # figure out how much the mouse has moved (in pixels)
  126. md = self.win.getPointer(0)
  127. x = md.getX()
  128. y = md.getY()
  129. if self.win.movePointer(0, 100, 100):
  130. self.heading = self.heading - (x - 100) * 0.2
  131. self.pitch = self.pitch - (y - 100) * 0.2
  132. if self.pitch < -45:
  133. self.pitch = -45
  134. if self.pitch > 45:
  135. self.pitch = 45
  136. self.camera.setHpr(self.heading, self.pitch, 0)
  137. dir = self.camera.getMat().getRow3(1)
  138. elapsed = task.time - self.last
  139. if self.last == 0:
  140. elapsed = 0
  141. if self.mousebtn[0]:
  142. self.focus = self.focus + dir * elapsed * 30
  143. if self.mousebtn[1] or self.mousebtn[2]:
  144. self.focus = self.focus - dir * elapsed * 30
  145. self.camera.setPos(self.focus - (dir * 5))
  146. if self.camera.getX() < -59.0:
  147. self.camera.setX(-59)
  148. if self.camera.getX() > 59.0:
  149. self.camera.setX(59)
  150. if self.camera.getY() < -59.0:
  151. self.camera.setY(-59)
  152. if self.camera.getY() > 59.0:
  153. self.camera.setY(59)
  154. if self.camera.getZ() < 5.0:
  155. self.camera.setZ(5)
  156. if self.camera.getZ() > 45.0:
  157. self.camera.setZ(45)
  158. self.focus = self.camera.getPos() + (dir * 5)
  159. self.last = task.time
  160. return Task.cont
  161. demo = BumpMapDemo()
  162. demo.run()