step4_load_system.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env python
  2. # Author: Shao Zhang and Phil Saltzman
  3. # Last Updated: 2015-03-13
  4. #
  5. # This tutorial is intended as a initial panda scripting lesson going over
  6. # display initialization, loading models, placing objects, and the scene graph.
  7. #
  8. # Step 4: In this step, we will load the rest of the planets up to Mars.
  9. # In addition to loading them, we will organize how the planets are grouped
  10. # hierarchically in the scene. This will help us rotate them in the next step
  11. # to give a rough simulation of the solar system. You can see them move by
  12. # running step_5_complete_solar_system.py.
  13. from direct.showbase.ShowBase import ShowBase
  14. base = ShowBase()
  15. from panda3d.core import NodePath, TextNode
  16. from direct.gui.DirectGui import *
  17. import sys
  18. class World(object):
  19. def __init__(self):
  20. # This is the initialization we had before
  21. self.title = OnscreenText( # Create the title
  22. text="Panda3D: Tutorial 1 - Solar System",
  23. parent=base.a2dBottomRight, align=TextNode.A_right,
  24. style=1, fg=(1, 1, 1, 1), pos=(-0.1, 0.1), scale=.07)
  25. base.setBackgroundColor(0, 0, 0) # Set the background to black
  26. base.disableMouse() # disable mouse control of the camera
  27. camera.setPos(0, 0, 45) # Set the camera position (X, Y, Z)
  28. camera.setHpr(0, -90, 0) # Set the camera orientation
  29. #(heading, pitch, roll) in degrees
  30. # This section has our variables. This time we are adding a variable to
  31. # control the relative size of the orbits.
  32. self.sizescale = 0.6 # relative size of planets
  33. self.orbitscale = 10 # relative size of orbits
  34. self.loadPlanets() # Load our models and make them render
  35. def loadPlanets(self):
  36. # Here is where we load all of the planets, and place them.
  37. # The first thing we do is create a dummy node for each planet. A dummy
  38. # node is simply a node path that does not have any geometry attached to it.
  39. # This is done by <NodePath>.attachNewNode('name_of_new_node')
  40. # We do this because positioning the planets around a circular orbit could
  41. # be done with a lot of messy sine and cosine operations. Instead, we define
  42. # our planets to be a given distance from a dummy node, and when we turn the
  43. # dummy, the planets will move along with it, kind of like turning the
  44. # center of a disc and having an object at its edge move. Most attributes,
  45. # like position, orientation, scale, texture, color, etc., are inherited
  46. # this way. Panda deals with the fact that the objects are not attached
  47. # directly to render (they are attached through other NodePaths to render),
  48. # and makes sure the attributes inherit.
  49. # This system of attaching NodePaths to each other is called the Scene
  50. # Graph
  51. self.orbit_root_mercury = render.attachNewNode('orbit_root_mercury')
  52. self.orbit_root_venus = render.attachNewNode('orbit_root_venus')
  53. self.orbit_root_mars = render.attachNewNode('orbit_root_mars')
  54. self.orbit_root_earth = render.attachNewNode('orbit_root_earth')
  55. # orbit_root_moon is like all the other orbit_root dummy nodes except that
  56. # it will be parented to orbit_root_earth so that the moon will orbit the
  57. # earth instead of the sun. So, the moon will first inherit
  58. # orbit_root_moon's position and then orbit_root_earth's. There is no hard
  59. # limit on how many objects can inherit from each other.
  60. self.orbit_root_moon = (
  61. self.orbit_root_earth.attachNewNode('orbit_root_moon'))
  62. ###############################################################
  63. # These are the same steps used to load the sky model that we used in the
  64. # last step
  65. # Load the model for the sky
  66. self.sky = loader.loadModel("models/solar_sky_sphere")
  67. # Load the texture for the sky.
  68. self.sky_tex = loader.loadTexture("models/stars_1k_tex.jpg")
  69. # Set the sky texture to the sky model
  70. self.sky.setTexture(self.sky_tex, 1)
  71. # Parent the sky model to the render node so that the sky is rendered
  72. self.sky.reparentTo(render)
  73. # Scale the size of the sky.
  74. self.sky.setScale(40)
  75. # These are the same steps we used to load the sun in the last step.
  76. # Again, we use loader.loadModel since we're using planet_sphere more
  77. # than once.
  78. self.sun = loader.loadModel("models/planet_sphere")
  79. self.sun_tex = loader.loadTexture("models/sun_1k_tex.jpg")
  80. self.sun.setTexture(self.sun_tex, 1)
  81. self.sun.reparentTo(render)
  82. self.sun.setScale(2 * self.sizescale)
  83. # Now we load the planets, which we load using the same steps we used to
  84. # load the sun. The only difference is that the models are not parented
  85. # directly to render for the reasons described above.
  86. # The values used for scale are the ratio of the planet's radius to Earth's
  87. # radius, multiplied by our global scale variable. In the same way, the
  88. # values used for orbit are the ratio of the planet's orbit to Earth's
  89. # orbit, multiplied by our global orbit scale variable
  90. # Load mercury
  91. self.mercury = loader.loadModel("models/planet_sphere")
  92. self.mercury_tex = loader.loadTexture("models/mercury_1k_tex.jpg")
  93. self.mercury.setTexture(self.mercury_tex, 1)
  94. self.mercury.reparentTo(self.orbit_root_mercury)
  95. # Set the position of mercury. By default, all nodes are pre assigned the
  96. # position (0, 0, 0) when they are first loaded. We didn't reposition the
  97. # sun and sky because they are centered in the solar system. Mercury,
  98. # however, needs to be offset so we use .setPos to offset the
  99. # position of mercury in the X direction with respect to its orbit radius.
  100. # We will do this for the rest of the planets.
  101. self.mercury.setPos(0.38 * self.orbitscale, 0, 0)
  102. self.mercury.setScale(0.385 * self.sizescale)
  103. # Load Venus
  104. self.venus = loader.loadModel("models/planet_sphere")
  105. self.venus_tex = loader.loadTexture("models/venus_1k_tex.jpg")
  106. self.venus.setTexture(self.venus_tex, 1)
  107. self.venus.reparentTo(self.orbit_root_venus)
  108. self.venus.setPos(0.72 * self.orbitscale, 0, 0)
  109. self.venus.setScale(0.923 * self.sizescale)
  110. # Load Mars
  111. self.mars = loader.loadModel("models/planet_sphere")
  112. self.mars_tex = loader.loadTexture("models/mars_1k_tex.jpg")
  113. self.mars.setTexture(self.mars_tex, 1)
  114. self.mars.reparentTo(self.orbit_root_mars)
  115. self.mars.setPos(1.52 * self.orbitscale, 0, 0)
  116. self.mars.setScale(0.515 * self.sizescale)
  117. # Load Earth
  118. self.earth = loader.loadModel("models/planet_sphere")
  119. self.earth_tex = loader.loadTexture("models/earth_1k_tex.jpg")
  120. self.earth.setTexture(self.earth_tex, 1)
  121. self.earth.reparentTo(self.orbit_root_earth)
  122. self.earth.setScale(self.sizescale)
  123. self.earth.setPos(self.orbitscale, 0, 0)
  124. # The center of the moon's orbit is exactly the same distance away from
  125. # The sun as the Earth's distance from the sun
  126. self.orbit_root_moon.setPos(self.orbitscale, 0, 0)
  127. # Load the moon
  128. self.moon = loader.loadModel("models/planet_sphere")
  129. self.moon_tex = loader.loadTexture("models/moon_1k_tex.jpg")
  130. self.moon.setTexture(self.moon_tex, 1)
  131. self.moon.reparentTo(self.orbit_root_moon)
  132. self.moon.setScale(0.1 * self.sizescale)
  133. self.moon.setPos(0.1 * self.orbitscale, 0, 0)
  134. # end loadPlanets()
  135. # end class world
  136. w = World()
  137. base.run()