当前位置:网站首页>Panda3D绘制立方体
Panda3D绘制立方体
2022-07-19 09:29:00 【bcbobo21cn】
看一下Panda3D的Tut-Procedural-Cube示例;
一个旋转的立方体;
可以在六个面上添加纹理;
单独看一下绘制立方体这部分代码;
from direct.showbase.ShowBase import ShowBase
from panda3d.core import lookAt
from panda3d.core import GeomVertexFormat, GeomVertexData
from panda3d.core import Geom, GeomTriangles, GeomVertexWriter
from panda3d.core import Texture, GeomNode
from panda3d.core import PerspectiveLens
from panda3d.core import Vec3, Vec4, Point3
import sys, os
def myNormalize(myVec):
myVec.normalize()
return myVec
def makeSquare(x1,y1,z1, x2,y2,z2):
format=GeomVertexFormat.getV3n3cpt2()
vdata=GeomVertexData('square', format, Geom.UHDynamic)
vertex=GeomVertexWriter(vdata, 'vertex')
normal=GeomVertexWriter(vdata, 'normal')
color=GeomVertexWriter(vdata, 'color')
texcoord=GeomVertexWriter(vdata, 'texcoord')
#make sure we draw the sqaure in the right plane
if x1!=x2:
vertex.addData3f(x1, y1, z1)
vertex.addData3f(x2, y1, z1)
vertex.addData3f(x2, y2, z2)
vertex.addData3f(x1, y2, z2)
normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z1-1)))
normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y1-1, 2*z1-1)))
normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z2-1)))
normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y2-1, 2*z2-1)))
else:
vertex.addData3f(x1, y1, z1)
vertex.addData3f(x2, y2, z1)
vertex.addData3f(x2, y2, z2)
vertex.addData3f(x1, y1, z2)
normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z1-1)))
normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z1-1)))
normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z2-1)))
normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z2-1)))
#adding different colors to the vertex for visibility
color.addData4f(1.0,0.0,0.0,1.0)
color.addData4f(0.0,1.0,0.0,1.0)
color.addData4f(0.0,0.0,1.0,1.0)
color.addData4f(1.0,0.0,1.0,1.0)
texcoord.addData2f(0.0, 1.0)
texcoord.addData2f(0.0, 0.0)
texcoord.addData2f(1.0, 0.0)
texcoord.addData2f(1.0, 1.0)
#quads arent directly supported by the Geom interface
#you might be interested in the CardMaker class if you are
#interested in rectangle though
tri1=GeomTriangles(Geom.UHDynamic)
tri2=GeomTriangles(Geom.UHDynamic)
tri1.addVertex(0)
tri1.addVertex(1)
tri1.addVertex(3)
tri2.addConsecutiveVertices(1,3)
tri1.closePrimitive()
tri2.closePrimitive()
square=Geom(vdata)
square.addPrimitive(tri1)
square.addPrimitive(tri2)
return square
class MyApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
self.camera.setPos(0, -10, 0)
square0=makeSquare(-1,-1,-1, 1,-1, 1)
square1=makeSquare(-1, 1,-1, 1, 1, 1)
square2=makeSquare(-1, 1, 1, 1,-1, 1)
square3=makeSquare(-1, 1,-1, 1,-1,-1)
square4=makeSquare(-1,-1,-1,-1, 1, 1)
square5=makeSquare( 1,-1,-1, 1, 1, 1)
snode=GeomNode('square')
snode.addGeom(square0)
snode.addGeom(square1)
snode.addGeom(square2)
snode.addGeom(square3)
snode.addGeom(square4)
snode.addGeom(square5)
cube=render.attachNewNode(snode)
app = MyApp()
app.run()
运行一下;如下;应该是只看到一个面;
square0=makeSquare(-1,-1,-1, 1,-1, 1)
square1=makeSquare(-1, 1,-1, 1, 1, 1)
square2=makeSquare(-1, 1, 1, 1,-1, 1)
square3=makeSquare(-1, 1,-1, 1,-1,-1)
square4=makeSquare(-1,-1,-1,-1, 1, 1)
square5=makeSquare( 1,-1,-1, 1, 1, 1)
这部分把1改为3或者0.5,都是一样;
先大体理解一下上面代码;
顶点信息由GeomVertexData保存;
顶点一般有四列:位置,法线,颜色,纹理坐标;
默认,顶点索引从0开始,连续递增;
每个GeomVertexData和一个GeomVertexFormat关联;GeomVertexFormat描述顶点格式;
GeomTriangles,三角形条带类,三角形条带就是多个三角形组成一条带状;
Geom,几何形体类;单个Geom对象构成最小的场景;
GeomNode,几何形体节点类;一个或多个几何形体组合构成几何形体节点;
GeomVertexWriter,顶点数据读写器,每列都有;
Geom.UHDynamic、Geom.UHStatic,顶点数据在帧之间可调整或不需要修改;
大体就是生成顶点数据,构成三角形条带,构成Geom,再形成几何形体节点,把几何形体节点加入场景;下回再看为什么占了整个绘制窗口;
此处是用三角形条带构成立方体,绘制立方体不一定使用三角形条带;
边栏推荐
- Discussion on segment tree · number of ordinary segments | number of multiplicative segments | chairman tree
- A go interview question about defer understanding
- tar.gz文件修复
- 在 IDEA 里下个五子棋不过分吧?
- QGIS开发插件的文件路径
- Hackers crack gambling website vulnerabilities and "collect wool" 100000 per month
- php 零点定时任务,PHP Laravel定时任务Schedule【干货】
- Feign自定义配置详解
- scratch判断闰年 电子学会图形化编程scratch等级考试四级真题和答案解析2022年6月
- QT plays audio and video files with errors directshowplayerservice:: dorender: unresolved error code 0x80040266
猜你喜欢
基于CoreData PencilKit如何使用 SwiftUI 创建绘图应用程序
零编码制作报表真地可能吗?
Docker installs common software MySQL
Rancher installation, deployment and basic use
Use of Evo assessment tools
QIODevice
Discussion on segment tree · number of ordinary segments | number of multiplicative segments | chairman tree
Nacos配置管理——统一配置管理
从工程师到技术leader思维升级
Unity实战问题-WebGL问题集锦-下篇
随机推荐
Basic use of argparse Library
Nacos configuration management - configuration hot update
Web3创业具备创新爆炸式增长的所有要素
C# 基于MessageBox类的消息对话框讲解
使用OneDNS完美解决办公网络优化问题
WIN11安装visual studio2019出现“无法下载安装文件,请检查Internet连接,然后重试”
tar. GZ file repair
供应链攻击日益严重,微软开源 SBOM 生成工具 Salus
Docker安装常用软件-Redis
Nacos configuration management - configuration sharing
线段树杂谈·普通线段数|乘法线段数|主席树
DolphinScheduler
浏览器调试
【数据库】概念设计、逻辑设计、关系数据库设计理论
用C#为国产智能手表写“Hello, China. ”
一汽红旗“王炸”上市,安全、舒适一个不落
A go interview question about defer understanding
Scratch judge leap year electronic society graphical programming scratch grade examination level 4 true question and answer analysis June 2022
QGIS导出单个矢量
Develop those things: how to solve the memory leak in the conversion between go and C type?