#include <osg/Node>
#include <osg/Geometry>
#include <osg/Notify>
#include <osg/MatrixTransform>
#include <osg/Texture2D>
#include <osg/DrawPixels>
#include <osg/PolygonOffset>
#include <osg/Geode> #include <osgDB/Registry>
#include <osgDB/ReadFile> #include <osgText/Text> #include <osgViewer/Viewer> #include <osg/ShapeDrawable> #ifdef _DEBUG
#pragma comment(lib,"osgd.lib")
#pragma comment(lib,"osgDBd.lib")
#pragma comment(lib,"osgTextd.lib")
#pragma comment(lib,"osgViewerd.lib")
#endif 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::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;
} static char * fragShader = {
"varying vec4 color;\n"
"uniform sampler2D baseTex;\n"
"uniform int osg_FrameNumber;\n"//当前OSG程序运行的帧数;
"uniform float osg_FrameTime;\n"//当前OSG程序的运行总时间;
"uniform float osg_DeltaFrameTime;\n"//当前OSG程序运行每帧的间隔时间;
"uniform mat4 osg_ViewMatrix;\n"//当前OSG摄像机的观察矩阵;
"uniform mat4 osg_ViewMatrixInverse;\n"// 当前OSG摄像机观察矩阵的逆矩阵。
"void main(void){\n"
"vec2 coord = gl_TexCoord[0].xy+vec2(0,osg_FrameTime*0.8);"
" gl_FragColor = texture2D(baseTex, coord);\n"
"}\n"
};
osg::Node* createCone()
{
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
osg::ref_ptr<osg::Cone> cone = new osg::Cone(osg::Vec3(, , ), 1.0f, );
osg::ref_ptr<osg::ShapeDrawable> shapeDrawable = new osg::ShapeDrawable(cone.get());
geode->addDrawable(shapeDrawable.get()); // 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/test.jpg")); osg::StateSet* stateset = shapeDrawable->getOrCreateStateSet();
stateset->setTextureAttributeAndModes(, texture, osg::StateAttribute::ON);
osg::Program * program = new osg::Program;
program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragShader));
stateset->addUniform(new osg::Uniform("baseTex", ));
stateset->setAttributeAndModes(program, osg::StateAttribute::ON); return geode.release();
}
int main(int, char **)
{
// construct the viewer.
osgViewer::Viewer viewer; // add model to viewer.
//viewer.setSceneData(createPyramidModel());
viewer.setSceneData(createCone()); return viewer.run();
}

osg使用shader动态修改纹理坐标的更多相关文章

  1. OSG绘制金字塔geode+动态纹理坐标

    osg::Node* createPyramidModel() { // create the root node which will hold the model. osg::Group* roo ...

  2. Unity 3D动态修改Shader状态,使物体透明等等

    Unity动态改Shader状态透明 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...

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

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

  4. OSG立体模式下动态修改相机远近裁剪面的实现

    1. 非立体模式下动态修改相机远近裁剪面 class GLB_DLLCLASS_EXPORT CGlbGlobeClipHandler : public osg::NodeCallback    { ...

  5. OSG与Shader的结合使用

    目录 1. 概述 2. 固定管线着色 3. 纹理着色 4. 参考 1. 概述 以往在OpenGL中学习渲染管线的时候,是依次按照申请数据.传送缓冲区.顶点着色器.片元着色器这几个步骤编程的.OSG是O ...

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

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

  7. 关于Unity中如何代码动态修改天空盒

    在Unity中动态修改天空盒有两种方法: 一.为每个Texture建立天空盒材质球,需要更换时直接将对应材质球作为天空盒,缺点是建立的材质球太多 private void ChangeSkybox(M ...

  8. Unity3D代码动态修改材质球的颜色

    代码动态修改材质球的颜色: gameObject.GetComponent<Renderer>().material.color=Color.red;//当材质球的Shader为标准时,可 ...

  9. ReactNative 根据scrollView/listview滑动距离动态修改NavBar颜色

    我们常见某些APP上滑的时候,NavBar颜色会从透明渐变为某种颜色 原理非常简单,根据scrollView的回调动态修改NavBar的透明度即可. 在RN中,尤其是ListView中这个回调不是很好 ...

随机推荐

  1. 安卓程序代写 网上程序代写[原]Android开发技巧--Application

    1. Application用途 创建Application时机 : Application在启动的时候会调用Application无参的构造方法创建实例; Application构造方法 : App ...

  2. ZARM in Linux & MIUI

    zram是Linux内核的一个模块,之前被称为“compcache”.zram通过在RAM内的压缩快设备上分页,直到必须使用硬盘上的交换空间,以避免在磁盘上进行分页,从而提高性能.由于zram可以用内 ...

  3. 输入一个int型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数。

    题目描述 输入一个int型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数. 输入描述: 输入一个int型整数 输出描述: 按照从右向左的阅读顺序,返回一个不含重复数字的新的整数 输入例子 ...

  4. Android pid uid

    PID:为Process Identifier, PID就是各进程的身份标识,程序一运行系统就会自动分配给进程一个独一无二的PID.进程中止后PID被系统回收,可能会被继续分配给新运行的程序,但是在a ...

  5. Maven Web应用

    创建Web应用程序 要创建一个简单的java web应用程序,我们将使用Maven的原型 - web应用插件.因此,让我们打开命令控制台,进入到C: MVN目录并执行以下命令mvn命令. C:MVN& ...

  6. Linux使用 tar命令-g参数进行增量+差异备份、还原文件

    原文链接:http://www.cnblogs.com/gnuhpc/ 完整备份: 建立测试路径与档案 mkdir test touch test/{a,b,c} 在test下生成三个文件 执行完整备 ...

  7. Windows下安装Oracle12C(二)

    一.安装Oracle 参考 Windows下安装Oracle12C(一) 二.新建用户 1. 安装完成后,启动SQLPlus,然后输入内置的用户名和密码 用户名:sys   密码:as sysdba ...

  8. 了解ASP.NET Core 依赖注入,看这篇就够了 于2017年11月6日由jesseliu发布

    DI在.NET Core里面被提到了一个非常重要的位置, 这篇文章主要再给大家普及一下关于依赖注入的概念,身边有工作六七年的同事还个东西搞不清楚.另外再介绍一下.NET  Core的DI实现以及对实例 ...

  9. 阿里云centos7安装桌面环境

    centos7. 1.安装X11.yum groupinstall "X Window System". 2.安装gnome. 全安装:yum groupinstall -y &q ...

  10. Redhat 5.6(RHEL 5.6)下安装PostgreSQL9.3

    1,下载Respository的更新包 http://yum.postgresql.org/9.3/redhat/rhel-5-x86_64/pgdg-redhat93-9.3-1.noarch.rp ...