2
0

step3_load_model.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 3: In this step, we create a function called loadPlanets, which will
  9. # eventually be used to load all of the planets in our simulation. For now
  10. # we will load just the sun and and the sky-sphere we use to create the
  11. # star-field.
  12. from direct.showbase.ShowBase import ShowBase
  13. base = ShowBase()
  14. from panda3d.core import NodePath, TextNode
  15. from direct.gui.DirectGui import *
  16. import sys
  17. class World(object):
  18. def __init__(self):
  19. # This is the initialization we had before
  20. self.title = OnscreenText( # Create the title
  21. text="Panda3D: Tutorial 1 - Solar System",
  22. parent=base.a2dBottomRight, align=TextNode.A_right,
  23. style=1, fg=(1, 1, 1, 1), pos=(-0.1, 0.1), scale=.07)
  24. base.setBackgroundColor(0, 0, 0) # Set the background to black
  25. base.disableMouse() # disable mouse control of the camera
  26. camera.setPos(0, 0, 45) # Set the camera position (X, Y, Z)
  27. camera.setHpr(0, -90, 0) # Set the camera orientation
  28. #(heading, pitch, roll) in degrees
  29. # We will now define a variable to help keep a consistent scale in
  30. # our model. As we progress, we will continue to add variables here as we
  31. # need them
  32. # The value of this variable scales the size of the planets. True scale size
  33. # would be 1
  34. self.sizescale = 0.6
  35. # Now that we have finished basic initialization, we call loadPlanets which
  36. # will handle actually getting our objects in the world
  37. self.loadPlanets()
  38. def loadPlanets(self):
  39. # Here, inside our class, is where we are creating the loadPlanets function
  40. # For now we are just loading the star-field and sun. In the next step we
  41. # will load all of the planets
  42. # Loading objects in Panda is done via the command loader.loadModel, which
  43. # takes one argument, the path to the model file. Models in Panda come in
  44. # two types, .egg (which is readable in a text editor), and .bam (which is
  45. # not readable but makes smaller files). When you load a file you leave the
  46. # extension off so that it can choose the right version
  47. # Load model returns a NodePath, which you can think of as an object
  48. # containing your model
  49. # Here we load the sky model. For all the planets we will use the same
  50. # sphere model and simply change textures. However, even though the sky is
  51. # a sphere, it is different from the planet model because its polygons
  52. #(which are always one-sided in Panda) face inside the sphere instead of
  53. # outside (this is known as a model with reversed normals). Because of
  54. # that it has to be a separate model.
  55. self.sky = loader.loadModel("models/solar_sky_sphere")
  56. # After the object is loaded, it must be placed in the scene. We do this by
  57. # changing the parent of self.sky to render, which is a special NodePath.
  58. # Each frame, Panda starts with render and renders everything attached to
  59. # it.
  60. self.sky.reparentTo(render)
  61. # You can set the position, orientation, and scale on a NodePath the same
  62. # way that you set those properties on the camera. In fact, the camera is
  63. # just another special NodePath
  64. self.sky.setScale(40)
  65. # Very often, the egg file will know what textures are needed and load them
  66. # automatically. But sometimes we want to set our textures manually, (for
  67. # instance we want to put different textures on the same planet model)
  68. # Loading textures works the same way as loading models, but instead of
  69. # calling loader.loadModel, we call loader.loadTexture
  70. self.sky_tex = loader.loadTexture("models/stars_1k_tex.jpg")
  71. # Finally, the following line sets our new sky texture on our sky model.
  72. # The second argument must be one or the command will be ignored.
  73. self.sky.setTexture(self.sky_tex, 1)
  74. # Now we load the sun.
  75. self.sun = loader.loadModel("models/planet_sphere")
  76. # Now we repeat our other steps
  77. self.sun.reparentTo(render)
  78. self.sun_tex = loader.loadTexture("models/sun_1k_tex.jpg")
  79. self.sun.setTexture(self.sun_tex, 1)
  80. # The sun is really much bigger than
  81. self.sun.setScale(2 * self.sizescale)
  82. # this, but to be able to see the
  83. # planets we're making it smaller
  84. # end loadPlanets()
  85. # end class world
  86. # instantiate the class
  87. w = World()
  88. base.run()