下面这个函数从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

最新文章

  1. 用C#从数据库动态生成AdminLTE菜单的一种方法
  2. [c++]默认参数
  3. MATLAB 文件对话框之图片格式转换
  4. IBATIS动态SQL(转)
  5. 微软发布Windows Phone 8.1 Update 和中文版Cortana“小娜”
  6. 跟我学 NHibernate (二)
  7. 第二个参数(那个 properties)确定你将如何使用这个特性值
  8. Android Monkey具体解释
  9. BZOJ 3439: Kpm的MC密码( trie + DFS序 + 主席树 )
  10. TheFourthJavaText
  11. 三、Spring的面向切面
  12. C#、Java之比较
  13. 想想我们能拿HoloLens 做点什么
  14. 记一次yii2 上传文件
  15. Eciplce ALT+/失效的解决方法
  16. 三维计算机视觉 — 中层次视觉 — Point Pair Feature
  17. POJ1611(KB2-B)
  18. mxnet与tensorflow的卷积实现细节比较
  19. web前端----JavaScript的DOM(三)
  20. EOSS V3.0 企业运营支撑系统(基于RBAC原理的权限管理)

热门文章

  1. [Linux] Ubuntu 配置nfs
  2. spring-boot jpa mysql emoji utfmb4 异常处理
  3. Point Estimate|unbiased estimator|Confidence-Interval Estimate
  4. B-Tree索引
  5. Android开发之《USB Camera》
  6. win7/win8下vmware/VirtualBox虚拟网卡显示未识别网络的解决
  7. win7图片只显示图标不显示预览图解决方案
  8. python登陆接口编写
  9. 《软件自动化测试开发-Java和Python测试开发指南》第6次印刷
  10. Random Forest And Extra Trees