下面这个函数从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. 在CMD中操作mysql数据库出现中文乱码解决方案

    百度了一下..有说将cmd字符编码用chcp命令改为65001(utf8字符编码),可这样之后根本无法输入中文,查询出的中问结果依旧乱码 其实,只要保证cmd客户端和MySQL两者编码一致即可. 但现 ...

  2. [LC] 129. Sum Root to Leaf Numbers

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  3. 关于后端下载后端返回的blob类型文件的下载

    关于后端返回blob类型的文件下载记录,在请求的时候前端设置响应类型 responseType: 'blob', const blob = new Blob([r], {type: r.type}); ...

  4. python3下scrapy爬虫(第四卷:初步抓取网页内容之抓取网页里的指定数据延展方法)

    上卷中我运用创建HtmlXPathSelector 对象进行抓取数据: 现在咱们再试一下其他的方法,先试一下我得最爱XPATH 看下结果: 直接打印出结果了 我现在就正常拼下路径 只求打印结果: 现在 ...

  5. python开发时小问题之端口占用

    昨天开发时遇到个小问题: 在使用pycharm编写tornado代码时: 直接用这种方式开启了服务,当我想修改代码时发现端口已经被占用代码提交不上去 所以现在该关闭进程: 步骤一: 打开CMD 步骤二 ...

  6. HDU-1425-sort(计数排序以及快速排序和堆排序的变种)

    计数排序 Accepted 1425 483MS 5276K 997 B G++ #include "bits/stdc++.h" using namespace std; typ ...

  7. OpenWrt编译后生成的bin文件:jffs2与squashfs、factory与sysupgrade

    OpenWrt编译后会生成多个bin文件,比如 openwrt-ar71xx-generic-tl-wr841nd-jffs2-factory.bin 8126464 openwrt-ar71xx-g ...

  8. ibator使用

    一.ibator是一个ibatis的代码生成工具,它能根据数据表自动生成javabean.sqlmap. ibator的官方地址是 http://ibatis.apache.org/ibator.ht ...

  9. jQuery 源码学习 - 02 - jQuery.fn.extend 与 jQuery.extend

    参考资料:[深入浅出jQuery]源码浅析--整体架构,备用地址:chokcoco/jQuery-. extend 方法在 jQuery 中是一个很重要的方法.jQuery 内部用它来拓展静态方法或者 ...

  10. python ATM项目

    1.需求: 指定最大透支额度 可取款 定期还款(每月指定日期还款,如15号) 可存款 定期出账单 支持多用户登陆,用户间转帐 支持多用户 管理员可添加账户.指定用户额度.冻结用户等 购物车: 商品信息 ...