下面这个函数从osg 3.4.0 example  目录中的osggeometry.cpp  改写而来, 它演示了几种常见几何体的创建及使用方法,其中的各个类的用法与C++版本别无二致。

值得指出的是,代码移植到PYTHON下面后,个别API接口发生了变化:

1.  各种以numpy array 为输入参数的API, 不需要指明输入数据的长度,因为numpy.ndarray的纬度,我们在python环境下都是可以得到的。

2. 用numpy array作为顶点或法向量或颜色数组时候,记得调用reshape函数重新将数据组织成合适的维度样式

3. c++中的宏改成python环境下的各种常量, 如   osg.BIND_OVERALL, 省掉了中间的类名或空间名。

 def createScene():
geode = osg.Geode()
pointsGeom = osg.Geometry()
vertices = osg.Vec3Array()
vertices.push_back((-1.02168, -2.15188e-09, 0.885735))
vertices.push_back((-0.976368, -2.15188e-09, 0.832179))
vertices.push_back((-0.873376, 9.18133e-09, 0.832179))
vertices.push_back((-0.836299, -2.15188e-09, 0.885735))
vertices.push_back((-0.790982, 9.18133e-09, 0.959889))
pointsGeom.setVertexArray(vertices)
colors = osg.Vec4Array()
colors.push_back((1.0,1.0,0.0,1.0))
geode = osg.Geode()
pointsGeom.setColorArray(colors, osg.BIND_OVERALL)
normals = osg.Vec3Array()
normals.push_back((0.0,-1.0,0.0))
pointsGeom.setNormalArray(normals, osg.BIND_OVERALL)
pointsGeom.addPrimitiveSet(osg.DrawArrays(osg.POINTS,0,vertices.size()))
geode.addDrawable(pointsGeom)
#create LINES
linesGeom = osg.Geometry()
arr = np.array([-1.13704, -2.15188e-09, 0.40373,
-0.856897, -2.15188e-09, 0.531441,
-0.889855, -2.15188e-09, 0.444927,
-0.568518, -2.15188e-09, 0.40373,
-1.00933, -2.15188e-09, 0.370773,
-0.716827, -2.15188e-09, 0.292498,
-1.07936, 9.18133e-09, 0.317217,
-0.700348, 9.18133e-09, 0.362533],np.float32)
arr = np.reshape(arr,(-1,3))
vertices = osg.Vec3Array(arr)
linesGeom.setVertexArray(vertices)
colors = osg.Vec4Array()
colors.push_back((1.0,1.0,0.0,1.0))
linesGeom.setColorArray(colors, osg.BIND_OVERALL)
normals = osg.Vec3Array()
normals.push_back((0.0,-1.0,0.0))
linesGeom.setNormalArray(normals, osg.BIND_OVERALL)
linesGeom.addPrimitiveSet(osg.DrawArrays(osg.LINES,0,8))
geode.addDrawable(linesGeom)
linesGeom = osg.Geometry();
arr = np.array([-0.0741545, -2.15188e-09, 0.416089,
0.234823, -2.15188e-09, 0.259541,
0.164788, -2.15188e-09, 0.366653,
-0.0288379, -2.15188e-09, 0.333695,
-0.0453167, -2.15188e-09, 0.280139], np.float32)
arr = np.reshape(arr, [-1, 3])
vertices = osg.Vec3Array(arr)
linesGeom.setVertexArray(vertices)
colors = osg.Vec4Array()
colors.push_back((1.0,1.0,0.0,1.0))
linesGeom.setColorArray(colors, osg.BIND_OVERALL)
normals = osg.Vec3Array()
normals.push_back((0.0,-1.0,0.0))
linesGeom.setNormalArray(normals, osg.BIND_OVERALL)
linesGeom.addPrimitiveSet(osg.DrawArrays(osg.LINE_STRIP,0,5))
geode.addDrawable(linesGeom)
#create LINE_LOOP
linesGeom = osg.Geometry()
myCoords = np.array([0.741546, -2.15188e-09, 0.453167,
0.840418, -2.15188e-09, 0.304858,
1.12468, -2.15188e-09, 0.300738,
1.03816, 9.18133e-09, 0.453167,
0.968129, -2.15188e-09, 0.337815,
0.869256, -2.15188e-09, 0.531441],np.float32)
myCoords = np.reshape(myCoords, [-1, 3])
vertices = osg.Vec3Array(myCoords)
linesGeom.setVertexArray(vertices)
colors = osg.Vec4Array()
colors.push_back((1.0,1.0,0.0,1.0))
linesGeom.setColorArray(colors, osg.BIND_OVERALL)
normals = osg.Vec3Array()
normals.push_back((0.0,-1.0,0.0))
linesGeom.setNormalArray(normals, osg.BIND_OVERALL)
numCoords = myCoords.shape[0]
linesGeom.addPrimitiveSet(osg.DrawArrays(osg.LINE_LOOP,0,numCoords))
geode.addDrawable(linesGeom)
shared_colors = osg.Vec4Array()
shared_colors.push_back((1.0,1.0,0.0,1.0))
shared_normals = osg.Vec3Array()
shared_normals.push_back((0.0,-1.0,0.0))
# create POLYGON
polyGeom = osg.Geometry()
myCoords = np.array([-1.0464, 0.0, -0.193626,
-1.0258, 0.0, -0.26778,
-0.807461, 0.0, -0.181267,
-0.766264, 0.0, -0.0576758,
-0.980488, 0.0, -0.094753],np.float32)
myCoords = np.reshape(myCoords, [-1, 3])
numCoords = myCoords.shape[0]
vertices = osg.Vec3Array(myCoords)
polyGeom.setVertexArray(vertices)
polyGeom.setColorArray(shared_colors, osg.BIND_OVERALL)
polyGeom.setNormalArray(shared_normals, osg.BIND_OVERALL)
polyGeom.addPrimitiveSet(osg.DrawArrays(osg.POLYGON,0,numCoords))
geode.addDrawable(polyGeom)
#create QUADS
polyGeom = osg.Geometry()
myCoords = np.array([0.0247182, 0.0, -0.156548,
0.0247182, 0.0, -0.00823939,
-0.160668, 0.0, -0.0453167,
-0.222464, 0.0, -0.13183,
0.238942, 0.0, -0.251302,
0.333696, 0.0, 0.0329576,
0.164788, 0.0, -0.0453167,
0.13595, 0.0, -0.255421],np.float32)
myCoords = np.reshape(myCoords, [-1, 3])
numCoords = myCoords.shape[0]
vertices = osg.Vec3Array(myCoords)
polyGeom.setVertexArray(vertices)
polyGeom.setColorArray(shared_colors, osg.BIND_OVERALL)
polyGeom.setNormalArray(shared_normals, osg.BIND_OVERALL)
polyGeom.addPrimitiveSet(osg.DrawArrays(osg.QUADS,0,numCoords))
geode.addDrawable(polyGeom)
##create QUAD_STRIP
polyGeom = osg.Geometry()
myCoords = np.array([0.733306, -2.15188e-09, -0.0741545,
0.758024, -2.15188e-09, -0.205985,
0.885735, -2.15188e-09, -0.0576757,
0.885735, -2.15188e-09, -0.214224,
0.964009, 9.18133e-09, -0.0370773,
1.0464, 9.18133e-09, -0.173027,
1.11232, -2.15188e-09, 0.0123591,
1.12468, 9.18133e-09, -0.164788],np.float32)
myCoords = np.reshape(myCoords, [-1, 3])
numCoords = myCoords.shape[0]
vertices = osg.Vec3Array(myCoords) #numpy array as input
polyGeom.setVertexArray(vertices)
polyGeom.setColorArray(shared_colors, osg.BIND_OVERALL)
polyGeom.setNormalArray(shared_normals, osg.BIND_OVERALL)
polyGeom.addPrimitiveSet(osg.DrawArrays(osg.QUAD_STRIP,0,numCoords))
geode.addDrawable(polyGeom)
## create TRIANGLES, TRIANGLE_STRIP and TRIANGLE_FAN all in one Geometry/
#create Geometry object to store all the vertices and lines primitive.
polyGeom = osg.Geometry()
myCoords = np.array([-1.12056, -2.15188e-09, -0.840418,
-0.95165, -2.15188e-09, -0.840418,
-1.11644, 9.18133e-09, -0.716827,
-0.840418, 9.18133e-09, -0.778623,
-0.622074, 9.18133e-09, -0.613835,
-1.067, 9.18133e-09, -0.609715,
-0.160668, -2.15188e-09, -0.531441,
-0.160668, -2.15188e-09, -0.749785,
0.0617955, 9.18133e-09, -0.531441,
0.168908, -2.15188e-09, -0.753905,
0.238942, -2.15188e-09, -0.531441,
0.280139, -2.15188e-09, -0.823939,
0.844538, 9.18133e-09, -0.712708,
1.0258, 9.18133e-09, -0.799221,
1.03816, -2.15188e-09, -0.692109,
0.988727, 9.18133e-09, -0.568518,
0.840418, -2.15188e-09, -0.506723], np.float32) myCoords = np.reshape(myCoords, [-1, 3])
numCoords = myCoords.shape[0]
vertices = osg.Vec3Array(myCoords)
polyGeom.setVertexArray(vertices)
polyGeom.setColorArray(shared_colors, osg.BIND_OVERALL)
polyGeom.setNormalArray(shared_normals, osg.BIND_OVERALL)
polyGeom.addPrimitiveSet(osg.DrawArrays(osg.TRIANGLES,0,6))
polyGeom.addPrimitiveSet(osg.DrawArrays(osg.TRIANGLE_STRIP,6,6))
polyGeom.addPrimitiveSet(osg.DrawArrays(osg.TRIANGLE_FAN,12,5))
# polygon stipple
stateSet = osg.StateSet()
polyGeom.setStateSet(stateSet)
geode.addDrawable(polyGeom)
return geode

下面还是以osggeometry中的例子,演示纹理的用法

 def createBackground():
image = osgDB.readImageFile("./Images/primitives.gif")
if (not image):
return None
# create Geometry object to store all the vertices and lines primitive.
polyGeom = osg.Geometry()
#note, anticlockwise ordering.
myCoords = np.array([1.22908,0.0,1.0,
-1.22908,0.0,-1.0,
1.22908,0.0,-1.0,
1.22908,0.0,1.0], np.float32)
myCoords = np.reshape(myCoords, [-1,3])
numCoords = myCoords.shape[0]
# pass the created vertex array to the points geometry object.
va = osg.Vec3Array(myCoords)
polyGeom.setVertexArray(va)
colors = osg.Vec4Array()
colors.push_back((1.0,1.0,1.0,1.0))
polyGeom.setColorArray(colors, osg.BIND_OVERALL)
##set the normal in the same way color.
normals = osg.Vec3Array()
normals.push_back((0.0,-1.0,0.0))
polyGeom.setNormalArray(normals, osg.BIND_OVERALL)
myTexCoords = np.array([0,1,0,0,1,0,1,1], np.float32)
myTexCoords = np.reshape(myTexCoords,[-1,2])
numTexCoords = myTexCoords.shape[0]
# pass the created tex coord array to the points geometry object,
# and use it to set texture unit 0.
# crash!!!
# polyGeom.setTexCoordArray(0,osg.Vec2Array(myTexCoords))
texArr = osg.Vec2Array(myTexCoords)
polyGeom.setTexCoordArray(0, texArr)
# well use indices and DrawElements to define the primitive this time.
myIndices = np.array([0, 1, 3, 2], np.int)
numIndices = myIndices.shape[0]
#There are three variants of the DrawElements osg::Primitive, UByteDrawElements which
#contains unsigned char indices, UShortDrawElements which contains unsigned short indices,
#and UIntDrawElements which contains ... unsigned int indices.
#The first parameter to DrawElements is
deus = osg.DrawElementsUShort(osg.TRIANGLE_STRIP,myIndices)
polyGeom.addPrimitiveSet(deus)
# new we need to add the texture to the Drawable, we do so by creating a
# StateSet to contain the Texture2D StateAttribute.
stateset = osg.StateSet()
# set up the texture.
texture = osg.Texture2D()
texture.setImage(image)
stateset.setTextureAttributeAndModes(0, texture,osg.ON)
polyGeom.setStateSet(stateset)
geode = osg.Geode()
# add the points geometry to the geode.
geode.addDrawable(polyGeom)
transform = osg.MatrixTransform()
nodeCb = MyTransformCallback(1.0)
transform.setUpdateCallback(nodeCb)
transform.addChild(geode)
return transform

主要通过image 对象及节点的StateSet属性来实现贴图。其中用到了transform 及 nodecallback 来实现简单动画,这部分内容放在下一随笔。

https://github.com/enigma19971/pyosg

基于osg的python三维程序开发(三)------几何形体及纹理的更多相关文章

  1. 基于osg的python三维程序开发(一)

    背景: osg是一款开源的三维引擎,在过去多年的发展中积累了大量的用户,该引擎基于场景树的管理,使用方法简单.但是对长期使用python作为开发工具的朋友来说, 有一定门槛. 下面的小程序,演示了如何 ...

  2. 基于osg的python三维程序开发(二)------向量

    上一篇文章展示了如何简单创建一个osg python 程序, 本篇展示了了一些基础数据结构的使用: from pyosg import * vec = osg.Vec3Array() #push ba ...

  3. 基于Spring MVC的Web应用开发(三) - Resources

    基于Spring MVC的Web应用开发(3) - Resources 上一篇介绍了在基于Spring MVC的Web项目中加入日志,本文介绍Spring MVC如何处理资源文件. 注意到本项目的we ...

  4. python 整型--《Python 3程序开发指南》笔记

    参考:<Python 3程序开发指南> 整数转换函数: bin(i) 返回整数i的二进制表示(字符串) hex(i) 返回i的十六进制表示(字符串) int(x) 将x转换为整数,失败产生 ...

  5. Creating Dialogbased Windows Application (3) / 创建基于对话框的Windows应用程序(三)Checkbox的应用、窗体置顶、设置图标 / VC++, Windows

    创建基于对话框的Windows应用程序(三) —— Checkbox的应用.窗体置顶.设置图标 上一节创建的窗体应用程序中,我们用到了Button和StaticText这两个控件.这一节中我们将学习使 ...

  6. 基于Oracle ADF的应用程序开发

    ADF简介 ADF(Application Development Framework)是Oracle公司为简化J2EE程序开发的复杂性专门开发的一种解决方案,ADF通过减少实现设计模式和应用程序框架 ...

  7. 基于bmob后端云小程序开发——口袋吉他

    人的一生90%的时间都在做着无聊的事情,社会的发展使得我们的闲暇时间越来越多,我们把除了工作的其他时间放在各种娱乐活动上. 程序员有点特殊,他们把敲代码看成娱乐活动的一部分,以此打发时间的不占少数.这 ...

  8. 基于Python的Webservice开发(三)-Django安装配置

    一.安装Django pip install django 二.创建项目 进入指定的目录后 django-admin startproject WebApi 目录说明: WebApi 项目的容器. m ...

  9. Python 全栈开发三 python基础 条件与循环

    一. 条件语句 python条件语句是根据一条或多条语句的执行结果的真假(True Or False)来决定代码块的执行. 而执行内容可以多行,以缩进来区分表示同一范围. 1.Python判断条件真假 ...

随机推荐

  1. KUKA机器人常见十大问题及解决方法

    1 开机坐标系无效 世界坐标系是以枪头为基点,在这种坐标系中,机器人所有的动作都是按照以枪头为顶点来完成移动,XYZ方向切割枪方向不改变,如果机器人在世界坐标系中移动,枪头也随着改变方向,那就是我们在 ...

  2. POJ3264 Balanced Lineup [RMQ模板]

    题意:有n头牛,输入他们的身高,求某区间身高的极值的差(max-min), 用RMQ模板,同时构造求极大值和极小值的两个数组. //poj3264 #include <iostream> ...

  3. python之循删list

    先来看下循环遍历删除list元素的一段代码: L=[1,3,1,4,3,6,5] # 0 1 2 3 4 5 6(下标) for i in L: if i%2!=0:#%表示除商取余数,除以2余数为0 ...

  4. android优化中国风应用、完整NBA客户端、动态积分效果、文件传输、小说阅读器等源码

    Android精选源码 android拖拽下拉关闭效果源码 一款优雅的中国风Android App源码 EasySignSeekBar一个漂亮而强大的自定义view15 android仿蘑菇街,蜜芽宝 ...

  5. 线程、volatile与synchronized、Lock

    目录 线程 1.概念: 2.线程生命周期: 3.线程调度 4.线程实现 4.1.实现方式 4.2.之间的区别: 5.线程安全 5.1.volatile与synchronized 5.1.synchro ...

  6. when|nobody|hazard|lane|circuit|

    How can I help them  they won't listen to me? 题目解析 考查从句.此句意为:如果他们要是不听我的话,我怎么帮助他们?此处,when引导的状语从句表示假设事 ...

  7. Ubuntu gnome安装Monaco字体,FontForge module is probably not installed

    首先下载原始Monaco字体,注意我只找到了这一款在ubuntu的gnome下可见,其他的各种monaco即使安装了也看不到. https://gist.github.com/epegzz/16342 ...

  8. VBA 读取加密的Excel文件(VBA 加密Excel)

    实验成功的: ExcelApp.Workbooks.Open(文件路径,,,'密码') 这里很坑,搜了别人的博客,下面这个方法试了N次,都没用... ExcelApp.Workbooks.Open(文 ...

  9. SWUST OJ NBA Finals(0649)

    NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128   Descri ...

  10. IDEA 详细使用教程

    第一步:安装 略 安装首先需要Java语言的运行环境,这里不做概述. 第二步:破解 这里使用的是2016版的,所以直接在网上搜索秘钥即可 https://blog.csdn.net/ksksjipen ...