OSG绘制金字塔geode+动态纹理坐标
osg::Node* createPyramidModel()
{
// create the root node which will hold the model.
osg::Group* root = new osg::Group(); // turn off lighting
root->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); osg::Geode* pyramidGeode = new osg::Geode();
osg::Geometry* pyramidGeometry = new osg::Geometry();
pyramidGeode->setUpdateCallback(new TextureCoordUpdateCallback(0.01));
pyramidGeode->setDataVariance(osg::Object::DYNAMIC);
pyramidGeode->addDrawable(pyramidGeometry);
root->addChild(pyramidGeode); osg::Vec3Array* pyramidVertices = new osg::Vec3Array;
pyramidVertices->push_back(osg::Vec3(, , )); // 左前
pyramidVertices->push_back(osg::Vec3(, , )); // 右前
pyramidVertices->push_back(osg::Vec3(, , )); // 右后
pyramidVertices->push_back(osg::Vec3(, , )); // 左后
pyramidVertices->push_back(osg::Vec3(, , )); // 塔尖
pyramidGeometry->setVertexArray(pyramidVertices);
osg::DrawElementsUInt* pyramidBase = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, );
pyramidBase->push_back();
pyramidBase->push_back();
pyramidBase->push_back();
pyramidBase->push_back();
pyramidGeometry->addPrimitiveSet(pyramidBase);
osg::DrawElementsUInt* pyramidFaceOne = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, );
pyramidFaceOne->push_back();
pyramidFaceOne->push_back();
pyramidFaceOne->push_back();
pyramidGeometry->addPrimitiveSet(pyramidFaceOne);
osg::DrawElementsUInt* pyramidFaceTwo = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, );
pyramidFaceTwo->push_back();
pyramidFaceTwo->push_back();
pyramidFaceTwo->push_back();
pyramidGeometry->addPrimitiveSet(pyramidFaceTwo);
osg::DrawElementsUInt* pyramidFaceThree = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, );
pyramidFaceThree->push_back();
pyramidFaceThree->push_back();
pyramidFaceThree->push_back();
pyramidGeometry->addPrimitiveSet(pyramidFaceThree);
osg::DrawElementsUInt* pyramidFaceFour = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, );
pyramidFaceFour->push_back();
pyramidFaceFour->push_back();
pyramidFaceFour->push_back();
pyramidGeometry->addPrimitiveSet(pyramidFaceFour); osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f)); //红色
colors->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f)); //绿色
colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f)); //蓝色
colors->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f)); //白色
pyramidGeometry->setColorArray(colors);
pyramidGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); osg::Vec3Array* normals = new osg::Vec3Array();
(*normals)[].set(0.0f, -1.0f, 0.0f);
pyramidGeometry->setNormalArray(normals, osg::Array::BIND_OVERALL); osg::Vec2Array* texcoords = new osg::Vec2Array();
(*texcoords)[].set(0.00f, 0.0f);
(*texcoords)[].set(0.25f, 0.0f);
(*texcoords)[].set(0.50f, 0.0f);
(*texcoords)[].set(0.75f, 0.0f);
(*texcoords)[].set(0.50f, 1.0f);
pyramidGeometry->setTexCoordArray(, texcoords); // set up the texture state.
osg::Texture2D* texture = new osg::Texture2D;
texture->setDataVariance(osg::Object::DYNAMIC); // protect from being optimized away as static state.
texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);
texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);
texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);
texture->setImage(osgDB::readImageFile("Images/road.png"));
osg::StateSet* stateset = pyramidGeometry->getOrCreateStateSet();
stateset->setTextureAttributeAndModes(, texture, osg::StateAttribute::ON); return root;
}
class TextureCoordUpdateCallback : public osg::NodeCallback
{
public: TextureCoordUpdateCallback(double delay = 1.0) :
_delay(delay),
_prevTime(0.0)
{
} virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
if (nv->getFrameStamp())
{
double currTime = nv->getFrameStamp()->getSimulationTime();
if (currTime - _prevTime > _delay)
{
osg::Geode* geode = node->asGeode();
osg::Geometry* geom = geode->getDrawable()->asGeometry();
// 获取纹理坐标数组
osg::Array* tmp = geom->getTexCoordArray();
osg::Vec2Array* coorArray = (osg::Vec2Array*) tmp;
auto it = coorArray->begin();
for (; it < coorArray->end(); it++)
{
// 动起来
it->x() += 0.001;
}
// 更新纹理坐标数组
geom->setTexCoordArray(, coorArray); // record time
_prevTime = currTime;
}
}
} protected:
double _delay;
double _prevTime; };
OSG绘制金字塔geode+动态纹理坐标的更多相关文章
- osg使用shader动态修改纹理坐标
#include <osg/Node> #include <osg/Geometry> #include <osg/Notify> #include <osg ...
- OSG学习:计算纹理坐标
在很多时候,直接指定纹理坐标是非常不方便的,如曲面纹理坐标,只有少数的曲面(如圆锥.圆柱等)可以在不产生扭曲的情况下映射到平面上,其他的曲面在映射到表面时都会产生一定程度的扭曲.一般而言,曲面表面的曲 ...
- Stage3D学习笔记(七):动态纹理
本章用来作为Starling的滤镜实现原理的一个补充,但是为了了解原理,我们会使用原生API进行编码. 我们知道,当我们调用drawTriangles方法时,我们的图像是绘制到后台缓冲区的,只有调用p ...
- OpenGL超级宝典总结(二)2D/3D笛卡尔坐标、坐标裁剪、纹理坐标、MVP转换等概念
如果你想把图形渲染在正确的位置上,那么坐标的设置就很重要了.在OpenGL中,与坐标相关的主要有笛卡尔坐标.坐标裁剪.纹理坐标.MVP(Model View Projection)转换. 1.笛卡尔坐 ...
- OpenMesh 读写网格控制(读取写入纹理坐标,法向等)
OpenMesh读取网格默认是不自动读取obj网格中的法向,纹理坐标等信息的,写入网格同样也是.所以要读取(或写入)这些信息需要修改默认的选项. 先看一下其读写网格的函数 template<cl ...
- uv纹理坐标设定与贴图规则
1.什么是UV? 对于三维模型,有两个最重要的坐标系统,一是顶点的位置(X,Y,Z)坐标,另一个就是UV坐标.什么是UV?简单的说,就是贴图影射到模型表面的依据. 完整的说,其实应该是UVW(因为 ...
- OpenGL的glTexCoord2f纹理坐标配置
纹理坐标配置函数,先看定义: void glTexCoord2f (GLfloat s, GLfloat t); 1.glTexCoord2f()函数 有两个参数:GLfloat s, GLfloat ...
- WPF 3D: MeshGeometry3D纹理坐标的正确定义
原文 WPF 3D: MeshGeometry3D纹理坐标的正确定义 为了使基于2D的纹理显示在3D对象中,我们必须定义3D Mesh对象的纹理贴图坐标.在WPF中,此项功能则通过MeshGeomet ...
- [osg]osg绘制动态改变顶点的几何体
最简单的顶点数据更新方法是预先获取setVertexArray()所用的数组数据,并对其进行更新.但是对于开启显示列表支持的几何体(这是默认的情况)来说,有一个问题需要特别需要引起注意,即显示列表中的 ...
随机推荐
- LeetCode: Generate Parentheses 解题报告
Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...
- AIX逻辑卷扩展空间
aix磁盘系统 a.基本概念: PV 物理卷:普通的直接访问的存储设备,有固定的和可移动的之分,代表性的就是硬盘. vg 卷组:AIX中最大的存储单位,一个卷组由一组物理硬盘组成,也就是由一个或多个物 ...
- ld,连接器
连接器的功能,是将一个可执行程序所需的目标文件和库文件最终整合为一体.一个程序通常包含传统的三个段,.test, .data, .bss段.连接器的功能就是将各个目标文件个库文件中的三个段进行合并. ...
- Win10系统设置专用网络和公用网络
打开设置界面或者按Win+I组合键打开设置界面,打开后点击“网络和Internet”; 在网络和Internet界面,点击左侧“拔号”(有些是以太网),在右侧窗口点击“宽带连接”(大家基本都是以太网连 ...
- kafka操作清单
1. 查看topic个数 ./kafka-topics.,dwtest-data3:,dwtest-data4: 2. 查看topic的生产者与消费者消息 ./kafka-topics.,dwtest ...
- MySQL 源代码scr.rpm安装的一点注意事项
rpm安装包通常为二进制包(Binary)以及源代码包(Source)两种形式. 在使用源代码方式安装MySQL的时候,官方站点上下载的源代码包通常为scr.rpm格式,而不是直接的tar包.对此,须 ...
- 使用rcp命令实现从远程机器拷贝文件
rcp:远程文件复制 使用rcp命令可以在远程主机之间复制文件,如果同时指定2个以上的文件或目录,且最后的目的地是一个已经存在的目录,则他会把前面指定的所有文件或目录复制到该目录中. rcp命令语法: ...
- (转)RGB接口和i80接口的区别
在嵌入式的主流 LCD屏中主要支持两大类的硬件接口,一种是常见的RGB接口,另外一种是MCU接口.后面因为最早是针对单片机的领域在使用,因此得名.后在中低端手机大量使用,其主要特点是价格便宜的. M ...
- 关于quartus ii软件中注释乱码问题的解决方法
乱码现象: 解决办法: 打开文件所在工程找到该verilog文件(后缀名是.v),使用记事本打开,这时你会看到注释好好的没乱码,很高兴是不,不用着急.接下来点击文件再另存为,选择编码:UTF-8,点保 ...
- C语言简单选择排序
#include <stdio.h> int main(int argc, char const *argv[]) { // 将数组按照从小到大排序 , , , , , }; int i, ...