panda3d加载模型复习和python面向对象编程属性学习

运行一个python示例;然后去除一些代码,只剩下加载模型相关,如下;

python 复制代码
from panda3d.core import loadPrcFileData
# Configure the parallax mapping settings (these are just the defaults)
loadPrcFileData("", "parallax-mapping-samples 3")
loadPrcFileData("", "parallax-mapping-scale 0.1")

import direct.directbase.DirectStart
from panda3d.core import WindowProperties
from panda3d.core import Filename,Shader
from panda3d.core import AmbientLight,PointLight
from panda3d.core import TextNode
from panda3d.core import Point3,Vec3,Vec4
from direct.task.Task import Task
from direct.actor.Actor import Actor
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.DirectObject import DirectObject
from direct.filter.CommonFilters import *
import sys,os

# Function to put instructions on the screen.
def addInstructions(pos, msg):
    return OnscreenText(text=msg, style=1, fg=(1,1,1,1),
                        pos=(-1.3, pos), align=TextNode.ALeft, scale = .05)

# Function to put title on the screen.
def addTitle(text):
    return OnscreenText(text=text, style=1, fg=(1,1,1,1),
                        pos=(1.3,-0.95), align=TextNode.ARight, scale = .07)

class BumpMapDemo(DirectObject):

    def __init__(self):

        # Check video card capabilities.

        if (base.win.getGsg().getSupportsBasicShaders() == 0):
            addTitle("Bump Mapping: Video driver reports that shaders are not supported.")
            return

        # Post the instructions
        self.title = addTitle("Panda3D: Tutorial - Bump Mapping")
        self.inst1 = addInstructions(0.95, "Press ESC to exit")
        self.inst2 = addInstructions(0.90, "Move mouse to rotate camera")
        self.inst3 = addInstructions(0.85, "Left mouse button: Move forwards")
        self.inst4 = addInstructions(0.80, "Right mouse button: Move backwards")
        self.inst5 = addInstructions(0.75, "Enter: Turn bump maps Off")

        self.room = loader.loadModel("models/abstractroom")
        self.room.reparentTo(render)

        # Make the mouse invisible, turn off normal mouse controls
        base.disableMouse()
        props = WindowProperties()
        props.setCursorHidden(True)
        base.win.requestProperties(props)

        # Set the current viewing target
        self.focus = Vec3(55,-55,20)
        self.heading = 180
        self.pitch = 0
        self.mousex = 0
        self.mousey = 0
        self.last = 0
        self.mousebtn = [0,0,0]

        self.room.setShaderAuto()

        self.shaderenable = 1        

t = BumpMapDemo()

run()

运行如下;

先看一下在python面向对象编程中,init()相当于构造函数,self代表类的实例;

还不太了解,看上去它的属性都是不须声明直接可以用,像下面这些,

self.title = xxx

self.inst1 = xxx

self.inst2 = xxx

......

self.inst5 = xxx

self.room = xxx

看一下把self.title改为self.biaoti是不是一样;是一样,程序跑起来都一样;

它的模型加载了之后是赋值给 self.room,看一下把全部的room都改为house,运行一下,也是一样;

模型是调用 loader.loadModel()方法加载,加载之后赋值给 self.room;

之后还有一句 self.room.reparentTo(render),模型才会显示;

对于 reparentTo(render) 方法,手册说明如下;

Placing the Model in the Scene Graph

The most important manipulation is to change the parent of a node. A model is by default loaded without a parent, but it needs to be placed into an active scene graph so that Panda3D will be able to find the model's geometry and render it to the screen.

The default 3D scene graph is called render, and this is how to reparent the model to this scene graph:

myModel.reparentTo(render)

It is possible to reparent the model to any node (even to another model, or to a sub-part of a different model), not just to render! What's important is that it is parented to a node that is itself parented to a scene graph, so that Panda3D can find it. Otherwise, the model will remain invisible.

The converse is to remove a model from the scene graph, which can be done as follows:

myModel.detachNode()

大致是说,模型载入后默认没有父对象,需要放入一个激活的场景图,这样panda3d可以发现模型的几何然后把它渲染到屏幕;reparentTo(render) 方法放置模型进场景图;

Scene Graph,先翻译为场景图;

如果把模型从场景图移除,调用detachNode();

相关推荐
数据小爬虫@21 分钟前
利用Python爬虫获取淘宝店铺详情
开发语言·爬虫·python
编程修仙1 小时前
Collections工具类
linux·windows·python
芝麻团坚果2 小时前
对subprocess启动的子进程使用VSCode python debugger
linux·ide·python·subprocess·vscode debugger
EterNity_TiMe_2 小时前
【论文复现】神经网络的公式推导与代码实现
人工智能·python·深度学习·神经网络·数据分析·特征分析
Stara05112 小时前
Git推送+拉去+uwsgi+Nginx服务器部署项目
git·python·mysql·nginx·gitee·github·uwsgi
hence..3 小时前
Vscode写markdown快速插入python代码
ide·vscode·python
DanielYQ3 小时前
LCR 001 两数相除
开发语言·python·算法
vener_4 小时前
LuckySheet协同编辑后端示例(Django+Channel,Websocket通信)
javascript·后端·python·websocket·django·luckysheet
封步宇AIGC4 小时前
量化交易系统开发-实时行情自动化交易-4.2.3.指数移动平均线实现
人工智能·python·机器学习·数据挖掘
互联网杂货铺4 小时前
自动化测试基础知识总结
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例