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+动态纹理坐标的更多相关文章

  1. osg使用shader动态修改纹理坐标

    #include <osg/Node> #include <osg/Geometry> #include <osg/Notify> #include <osg ...

  2. OSG学习:计算纹理坐标

    在很多时候,直接指定纹理坐标是非常不方便的,如曲面纹理坐标,只有少数的曲面(如圆锥.圆柱等)可以在不产生扭曲的情况下映射到平面上,其他的曲面在映射到表面时都会产生一定程度的扭曲.一般而言,曲面表面的曲 ...

  3. Stage3D学习笔记(七):动态纹理

    本章用来作为Starling的滤镜实现原理的一个补充,但是为了了解原理,我们会使用原生API进行编码. 我们知道,当我们调用drawTriangles方法时,我们的图像是绘制到后台缓冲区的,只有调用p ...

  4. OpenGL超级宝典总结(二)2D/3D笛卡尔坐标、坐标裁剪、纹理坐标、MVP转换等概念

    如果你想把图形渲染在正确的位置上,那么坐标的设置就很重要了.在OpenGL中,与坐标相关的主要有笛卡尔坐标.坐标裁剪.纹理坐标.MVP(Model View Projection)转换. 1.笛卡尔坐 ...

  5. OpenMesh 读写网格控制(读取写入纹理坐标,法向等)

    OpenMesh读取网格默认是不自动读取obj网格中的法向,纹理坐标等信息的,写入网格同样也是.所以要读取(或写入)这些信息需要修改默认的选项. 先看一下其读写网格的函数 template<cl ...

  6. uv纹理坐标设定与贴图规则

    1.什么是UV?   对于三维模型,有两个最重要的坐标系统,一是顶点的位置(X,Y,Z)坐标,另一个就是UV坐标.什么是UV?简单的说,就是贴图影射到模型表面的依据. 完整的说,其实应该是UVW(因为 ...

  7. OpenGL的glTexCoord2f纹理坐标配置

    纹理坐标配置函数,先看定义: void glTexCoord2f (GLfloat s, GLfloat t); 1.glTexCoord2f()函数 有两个参数:GLfloat s, GLfloat ...

  8. WPF 3D: MeshGeometry3D纹理坐标的正确定义

    原文 WPF 3D: MeshGeometry3D纹理坐标的正确定义 为了使基于2D的纹理显示在3D对象中,我们必须定义3D Mesh对象的纹理贴图坐标.在WPF中,此项功能则通过MeshGeomet ...

  9. [osg]osg绘制动态改变顶点的几何体

    最简单的顶点数据更新方法是预先获取setVertexArray()所用的数组数据,并对其进行更新.但是对于开启显示列表支持的几何体(这是默认的情况)来说,有一个问题需要特别需要引起注意,即显示列表中的 ...

随机推荐

  1. mysql 存入乱码问题

    在使用mysql开发是,遇到一问题,java脚本里面的中文很正常,持久化之后数据库里面的数据则变成乱码,解决方式,在spring配置文件连接中加入指定编码格式,有些系统不需要,有些服务器系统需要,统一 ...

  2. 使用寄存器点亮LED——前言基础知识

    在点亮LED之前,我们需要具备一些基础知识: GPIO—general purpose intput output 是通用输入输出端口的简称,简单来说就是软件可控制的引脚,STM32芯片的GPIO引脚 ...

  3. 如何设置Windows server 2008 R2登陆密码?

    我第一次用该系统,登陆系统需要设置新密码,科沃怎么设置新密码都是新密码不符合条件,不允许我创建,该怎么办? 答案: 至少6个字符,要包括大小写.符号.数字这四组字符中的三组.如12abAB是有效密码, ...

  4. json demo

    package my.bigdata.movieTask.action; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.J ...

  5. Sketch Up常见问题

    墙上开门洞用铅笔在墙上画好矩形(2m*1m)->用推拉(P)工具双击左键

  6. Apache HttpComponents 多线程处理HTTP请求

    /* * ==================================================================== * * Licensed to the Apache ...

  7. datanode启动失败

    当我动态加入一个hadoop从节点的之后,出现了一个问题: [root@hadoop current]# hadoop-daemon.sh start datanode starting datano ...

  8. WCF客户端获取服务器返回数据报错

    错误信息:An error occurred while receiving the HTTP response to http://127.0.0.1/SIHIS/Infection/PubExec ...

  9. JPA联合主键

    联合主键也就是说需要多个字段才能确定数据库记录中的唯一一行.这样就需要多个字段一起,组成主键,也叫联合主键.例如飞机航线,我们需要知道飞机起飞的地点以及飞机降落的地点.所以需要飞机起飞的地点和降落的地 ...

  10. WebForm和MVC的一些知识(转)

    转自:http://www.cnblogs.com/liuhf939/p/3417203.html 比较WebForm和Mvc的请求处理方式 首先简单了解一下Asp.Net中怎么对页面进行请求处理的: ...