香港新手遊戲開發者論壇

 找回密碼
 立即註冊
查看: 3753|回復: 2

Panda3d教學(五)熊貓3D Hello World教學

[複製鏈接]

16

主題

29

帖子

220 小時

在線時間

版主

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

積分
378
發表於 2019-12-5 19:36:04 | 顯示全部樓層 |閱讀模式

馬上註冊,結交更多好友,享用更多功能,讓你輕鬆玩轉社區。

您需要 登錄 才可以下載或查看,沒有帳號?立即註冊

x
本帖最後由 小丑遊戲 於 2019-12-5 07:41 PM 編輯

官方教學鍊結:https://www.panda3d.org/manual/?title=A_Panda3D_Hello_World_Tutorial
漢化教學帖子前言(第二層):http://power-plant.dlinkddns.com/newplayer-hk/forum.php?mod=viewthread&tid=966&extra=page%3D1
前言

This tutorial is called "A Panda Hello World". It is a typical example of a simple Panda3D program. Walking through this tutorial will enable you to obtain some limited familiarity with the Panda3D API without having to learn the entire thing.
The program that we are going to create will load up a small scene containing some grass and a panda. The panda will be animated to walk back and forth over the grass.

下一層則開始熊貓3D Hello World教學

16

主題

29

帖子

220 小時

在線時間

版主

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

積分
378
 樓主| 發表於 2019-12-5 19:48:43 | 顯示全部樓層
官方教學鍊結:https://www.panda3d.org/manual/?title=Starting_Panda3D

1.運行Panda3D

創造新的Panda3D運行程式

ShowBase

To start Panda3D, create a text file and save it with the .py extension. PYPE, SPE and IDLE are Python-specific text-editors, but any text editor will work. Enter the following text into your Python file:

  1. <div>from direct.showbase.ShowBase import ShowBase</div><div> </div><div>class MyApp(ShowBase):</div><div> </div><div>    def __init__(self):</div><div>        ShowBase.__init__(self)</div><div> </div><div>app = MyApp()</div><div>app.run()</div>
複製代碼


Here we made our main class inherit from ShowBase. The ShowBase class loads most of the other Panda3D modules, and causes the 3D window to appear. The run() procedure in ShowBase 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. In this particular example, there will be nothing to render, so you should expect a window containing an empty grey area.

程式運作

To run your program on Windows, enter the following in a terminal (command prompt):

ppython filename.py

To run it on GNU/Linux or macOS, enter the following in a terminal:
python filename.py

If Panda3D has been installed properly, a grey window titled Panda appears. There is nothing we can do with this window, but that will change shortly.

16

主題

29

帖子

220 小時

在線時間

版主

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

積分
378
 樓主| 發表於 2019-12-5 20:00:13 | 顯示全部樓層
官方教學鍊結:https://www.panda3d.org/manual/?title=Loading_the_Grassy_Scenery
2.加載環境背景

THE SCENE GRAPH

Panda3D contains a data structure called the Scene Graph. The Scene Graph is a tree containing all objects that need to be rendered. At the root of the tree is an object named render. Nothing is rendered until it is first inserted into the Scene Graph.

To install the grassy scenery model into the Scene Graph, we use the method reparentTo(). This sets the parent of the model, thereby giving it a place in the Scene Graph. Doing so makes the model visible in the scene.

Finally, we adjust the position and scale of the model. In this particular case, the environment model is a little too large and somewhat offset for our purposes. The setScale() and setPos() procedures rescale and center the model.

Panda3D uses the "geographical" coordinate system where position (-8, 42, 0) means map coordinates (8, 42) and height 0. If you are used to OpenGL/Direct3D coordinates, then hold up your right hand in the classical position with thumb as X, fingers as Y, and palm as Z facing toward you; then tilt backward until your hand is level with the fingers pointing away and palm facing up. Moving "forward" in Panda3D is a positive change in Y coordinate.

程式

更新代碼

With Panda3D running properly, it is now possible to load some grassy scenery. Update your code as follows:

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



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. Note that Panda Filename Syntax uses the forward-slash, even under Windows.

程式運作

Go ahead and run the program. You should see this:



The rock and tree appear to be hovering. The camera is slightly below ground, and back-face culling is making the ground invisible to us. If we reposition the camera, the terrain will look better.




您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

手機版|Archiver|植物油氣

GMT+8, 2024-9-20 03:17 AM , Processed in 0.054653 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回復 返回頂部 返回列表