Computer Science

Panda3D 3D graphics using python | installation & basic tutorials | 작성 중

토마토. 2021. 9. 19. 10:52

index

 

what is Panda3D?

showbase

environment

control camera

load/animate model

move forward


 

so what is Panda3D?

Panda3D is a 3D engine:

 

a library of subroutines for 3D rendering and game development.

 

The library is C++ with a set of Python bindings.

 

Game development with Panda3D usually consists of writing

a Python or C++ program that controls the Panda3D library.

 

Panda3D was created for commercial game development

and is still used for developing commercial games.

 

Because of this, the engine needs to emphasize four areas:

power, speed, completeness, and error tolerance.

Everyone knows what power and speed are.

But completeness and error tolerance deserve some extra commentary.

 

Panda3D was developed by Disney

for their massively multiplayer online game, Toontown Online

It was released as free software in 2002.

 

Carnegie Mellon University’s Entertainment Technology Center,

which currently hosts the website and other Panda3D services,

was actively involved in the development of Panda3D into an open source project.

 

내가 주목하려는 부분

 

point 1. 클래스 / api 프로그래밍

 

point 2. 파일 여러 개 다루는 것

(from [filename] import *)하면 끝~

 

point 3. 일단 만들고 싶은 것에 주목

 

Q1. 3D 모델링을 panda3D에서 할 수 있는가?

-> 튜토리얼에서 해결 안된 부분 : 그래서 모델을 어디서 만드는가?

 

.

1. showbase

실행화면

from direct.showbase.ShowBase import ShowBase


class MyApp(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)


app = MyApp()
app.run()

showbase class loads most of the other Panda3D modules

and causes the 3D window to appear

 

run() method(app.run()) contains the panda3D main loop

 

It renders a frame, handles the background tasks, and then repeats.

It does not normally return, so it needs to be called only once and must be the last line in your script.

 

실행을 위해서는 터미널에 다음 입력

(리눅스 기준)

$ python3 panda.py
Known pipe types:
  glxGraphicsPipe
(1 aux display modules not yet loaded.)

 

2. loadmodel (load background)

실행 화면

from direct.showbase.ShowBase import ShowBase
 
 
class MyApp(ShowBase):
 
    def __init__(self):
        ShowBase.__init__(self)
 
         # Load the environment model.
        self.scene = self.loader.loadModel("models/environment")
        # Reparent the model to render.
        self.scene.reparentTo(self.render)
        # Apply scale and position transforms on the model.
        self.scene.setScale(0.25, 0.25, 0.25)
        self.scene.setPos(-8, 42, 0)

app = MyApp()
app.run()

다시 실행하려면?

$ python3 grassy_scenery.py

The ShowBase procedure loader.loadModel() loads the specified file,

in this case the environment.egg file in the models folder.

 

The return value is an object of the NodePath class,

effectively a pointer to the model.

<nodepath>

Note that Panda Filename Syntax uses the forward- slash(/),

even under Windows.

3. controlling the camera

(카메라를 이용한다기보다는, 시점을 정한다는 것)

추가된 코드 = 회전

 

Left Button Pan left and right.
Right Button Move forwards and backwards.
Middle Button Rotate around the origin of the application.
Right and Middle Buttons Roll the point of view around the view axis.

Mouse ButtonAction

        # Add the spinCameraTask procedure to the task manager.
        self.taskMgr.add(self.spinCameraTask, "SpinCameraTask")

    # Define a procedure to move the camera.
    def spinCameraTask(self, task):
        angleDegrees = task.time * 6.0
        angleRadians = angleDegrees * (pi / 180.0)
        self.camera.setPos(20 * sin(angleRadians), -20 * cos(angleRadians), 3)
        self.camera.setHpr(angleDegrees, 0, 0)
        return Task.cont

 

The procedure taskMgr.add()

tells

Panda3D’s task manager

to call the procedure spinCameraTask() every frame.

 

This is a procedure that we have written to control the camera.

 

As long as the procedure spinCameraTask() returns the constant AsyncTask.DS_cont,

the task manager will continue to call it every frame.

 

The setPos() call actually sets the position of the camera.

(Remember that Y is horizontal and Z is vertical,

so the position is changed by animating X and Y while Z is left fixed at 3 units above ground level.)

 

The setHpr() call actually sets the orientation.

 

 

4. loading and animating the panda model

loadModel -> static model

actor -> animated model

 

The Actor class is for animated models.

 

Note that we use loadModel() for static models

and Actor only when they are animated.

 

The two constructor arguments for the Actor class are

the name of the file containing the model

and a Python dictionary containing the names of the files containing the animations.

 

The command loop("walk") causes the walk animation to begin looping.

 

 

추가된 코드

from direct.actor.Actor import Actor


20        # Add the spinCameraTask procedure to the task manager.
21        self.taskMgr.add(self.spinCameraTask, "SpinCameraTask")
22
23        # Load and transform the panda actor.
24        self.pandaActor = Actor("models/panda-model",
25                                {"walk": "models/panda-walk4"})
26        self.pandaActor.setScale(0.005, 0.005, 0.005)
27        self.pandaActor.reparentTo(self.render)
28        # Loop its animation.
29        self.pandaActor.loop("walk")

 

5. using intervals to move the panda

 

Intervals

Intervals are tasks

that change a property

from one value to another over a specified period of time.

 

Starting an interval effectively starts a background process

that modifies the property over the specified period of time.

 

Sequences

Sequences, sometimes called MetaIntervals,

are a type of interval that contains other intervals.

 

Playing a sequence will cause each contained interval

to execute in sequence, one after the other.

 

The next step is to cause the panda to actually move back and forth.

 

When the pandaPosInterval1 interval is started,

it will gradually adjust the position of the panda

from (0, 10, 0) to (0, -10, 0) over a period of 13 seconds.

 

Similarly,

when the pandaHprInterval1 interval is started,

the heading of the panda will rotate 180 degrees over a period of 3 seconds.

 

The pandaPace sequence above causes the panda

to move in a straight line, turn, move in the opposite straight line, and finally turn again.

 

The code pandaPace.loop() causes the Sequence to be started in looping mode.

 

추가된 코드

 

36        # Create the four lerp intervals needed for the panda to
37        # walk back and forth.
38        posInterval1 = self.pandaActor.posInterval(13,
39                                                   Point3(0, -10, 0),
40                                                   startPos=Point3(0, 10, 0))
41        posInterval2 = self.pandaActor.posInterval(13,
42                                                   Point3(0, 10, 0),
43                                                   startPos=Point3(0, -10, 0))
44        hprInterval1 = self.pandaActor.hprInterval(3,
45                                                   Point3(180, 0, 0),
46                                                   startHpr=Point3(0, 0, 0))
47        hprInterval2 = self.pandaActor.hprInterval(3,
48                                                   Point3(0, 0, 0),
49                                                   startHpr=Point3(180, 0, 0))
50
51        # Create and play the sequence that coordinates the intervals.
52        self.pandaPace = Sequence(posInterval1, hprInterval1,
53                                  posInterval2, hprInterval2,
54                                  name="pandaPace")
55        self.pandaPace.loop()
56

 

6. 끝!

 

This concludes the “A Panda Hello World” tutorial. 

For more information on any part of the Panda3D engine, 

consult the rest of the Manual.

 

 If you still have questions, 

don’t hesitate to ask on the Panda3D Forums or on the Panda3D IRC channel. 

 

Have fun developing 3D applications with Panda3D!

If you are looking for a more comprehensive tutorial for Panda3D, 

try out the Beginner’s Tutorial for Panda3D, 

contributed by community member Thaumaturge.

from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from direct.actor.Actor import Actor
from direct.interval.IntervalGlobal import Sequence
from panda3d.core import Point3

from math import pi, sin, cos
 
class MyApp(ShowBase):
 
    def __init__(self):
        ShowBase.__init__(self)
 
         # Load the environment model.
        self.scene = self.loader.loadModel("models/environment")
        # Reparent the model to render.
        self.scene.reparentTo(self.render)
        # Apply scale and position transforms on the model.
        self.scene.setScale(0.25, 0.25, 0.25)
        self.scene.setPos(-8, 42, 0)

        # Add the spinCameraTask procedure to the task manager.
        self.taskMgr.add(self.spinCameraTask, "SpinCameraTask")

        # Add the spinCameraTask procedure to the task manager.
        self.taskMgr.add(self.spinCameraTask, "SpinCameraTask")

        # Load and transform the panda actor.
        self.pandaActor = Actor("models/panda-model",
                                {"walk": "models/panda-walk4"})
        self.pandaActor.setScale(0.005, 0.005, 0.005)
        self.pandaActor.reparentTo(self.render)
        # Loop its animation.
        self.pandaActor.loop("walk")
        # Create the four lerp intervals needed for the panda to
        # walk back and forth.
        posInterval1 = self.pandaActor.posInterval(13,
                                                   Point3(0, -20, 0),
                                                   startPos=Point3(0, 10, 0))
        posInterval2 = self.pandaActor.posInterval(13,
                                                   Point3(0, 20, 0),
                                                   startPos=Point3(0, -10, 0))
        hprInterval1 = self.pandaActor.hprInterval(3,
                                                   Point3(180, 0, 0),
                                                   startHpr=Point3(0, 0, 0))
        hprInterval2 = self.pandaActor.hprInterval(3,
                                                   Point3(0, 0, 0),
                                                   startHpr=Point3(180, 0, 0))

        # Create and play the sequence that coordinates the intervals.
        self.pandaPace = Sequence(posInterval1, hprInterval1,
                                  posInterval2, hprInterval2,
                                  name="pandaPace")
        self.pandaPace.loop()



    # Define a procedure to move the camera.
    def spinCameraTask(self, task):
        angleDegrees = task.time * 6.0
        angleRadians = angleDegrees * (pi / 180.0)
        self.camera.setPos(20 * sin(angleRadians), -20 * cos(angleRadians), 3)
        self.camera.setHpr(angleDegrees, 0, 0)
        return Task.cont

app = MyApp()
app.run()

 

https://docs.panda3d.org/1.10/python/introduction/tutorial/index

 

A Panda3D Hello World Tutorial — Panda3D Manual

© Copyright 2019 Carnegie Mellon University.

docs.panda3d.org

https://docs.panda3d.org/1.10/python/more-resources/samples/index#samples

 

Sample Programs in the Distribution — Panda3D Manual

Sample Programs in the Distribution The Panda3D Distribution includes quite a few sample programs. The following is a list of what’s included, and which features of the engine each sample demonstrates. If you are just learning Panda3D, take a look at the

docs.panda3d.org