uniform int osg_FrameNumber:当前OSG程序运行的帧数;
uniform float osg_FrameTime:当前OSG程序的运行总时间;
uniform float osg_DeltaFrameTime:当前OSG程序运行每帧的间隔时间;
uniform mat4 osg_ViewMatrix:当前OSG摄像机的观察矩阵;
uniform mat4 osg_ViewMatrixInverse:当前OSG摄像机观察矩阵的逆矩阵。
uniform mat4 osg_ModelViewMatrix:内置gl_ModelViewMatrix
uniform mat4 osg_ModelViewProjectionMatrix:内置gl_ModelViewProjectionMatrix
uniform mat4 osg_ProjectionMatrix:内置gl_ProjectionMatrix
uniform mat3 osg_NormalMatrix:内置gl_NormalMatrix
  • attribute:应用程序与顶点着色器的接口,使用顶点属性定义函数进行定义;
  • uniform:应用程序与所有着色器的接口,定义不随顶点变化的“一致变量”;
  • varying:着色器之间的“易变变量”接口,用于传递插值得到的顶点数据;
  • const:用于声明常量数据;
  • in:作为函数形参进行传递,函数返回时不保留改变,只保留传入值;
  • out:作为函数形参进行传递,本身未定义,函数返回时保留改变值;
  • inout:作为函数形参进行传递,可以定义传入值,也会保留返回时的改变值。
  • uniform mat4 gl_NormalMatrix:法线变换矩阵;
  • uniform mat4 gl_ModelViewMatrix:模型视点变换矩阵;
  • attribute vec4 gl_Vertex:顶点坐标属性;
  • attribute vec4 gl_MultiTexCoord0:纹理单元0的纹理坐标属性;
  • varying vec4 gl_TexCoord[0]:纹理单元0的实际纹理坐标。
  • 实现片元着色器的一段示例代码如下:
    uniform sampler2D texture;
    varying vec3 tangent;
    void main( void )
    {
    gl_FragColor = texture2D( texture, gl_TexCoord[] );
    }

    着色器一致变量的接口类。对于OpenGL着色语言而言,一致变量(uniform)是用户应用程序与着色器的主要交互接口。Uniform类支持绑定多种类型的一致变量,并使用set()和setArray()更新变量或变量数组的值。而为了实现一致变量的每帧变化,进而达到顶点和片元的各种动画特效,Uniform类还提供了相应的回调工具:使用setUpdateCallback设置自定义的回调类,并在其中更新这个一致变量的值,以实现所需的效果。

#include <osgViewer/Viewer>

#include <osg/ShapeDrawable>
#include <osg/Geode>
#include <osg/Vec3> #include <osg/Program>
#include <osg/Shader>
#include <osg/Uniform> using namespace osg; ///////////////////////////////////////////////////////////////////////////
// in-line GLSL source code static const char *blockyVertSource = {
"// blocky.vert - an GLSL vertex shader with animation\n"
"// the App updates uniforms \"slowly\" (eg once per frame) for animation.\n"
"uniform float Sine;\n"
"const vec3 LightPosition = vec3(0.0, 0.0, 4.0);\n"
"const float BlockScale = 0.30;\n"
"// varyings are written by vert shader, interpolated, and read by frag shader.\n"
"varying float LightIntensity;\n"
"varying vec2 BlockPosition;\n"
"void main(void)\n"
"{\n"
" // per-vertex diffuse lighting\n"
" vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;\n"
" vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal);\n"
" vec3 lightVec = normalize(LightPosition - vec3 (ecPosition));\n"
" LightIntensity = max(dot(lightVec, tnorm), 0.0);\n"
" // blocks will be determined by fragment's position on the XZ plane.\n"
" BlockPosition = gl_Vertex.xz / BlockScale;\n"
" // scale the geometry based on an animation variable.\n"
" vec4 vertex = gl_Vertex;\n"
" vertex.w = 1.0 + 0.4 * (Sine + 1.0);\n"
" gl_Position = gl_ModelViewProjectionMatrix * vertex;\n"
"}\n"
}; static const char *blockyFragSource = {
"// blocky.frag - an GLSL fragment shader with animation\n"
"// the App updates uniforms \"slowly\" (eg once per frame) for animation.\n"
"uniform float Sine;\n"
"const vec3 Color1 = vec3(1.0, 1.0, 1.0);\n"
"const vec3 Color2 = vec3(0.0, 0.0, 0.0);\n"
"// varyings are written by vert shader, interpolated, and read by frag shader.\n"
"varying vec2 BlockPosition;\n"
"varying float LightIntensity;\n"
"void main(void)\n"
"{\n"
" vec3 color;\n"
" float ss, tt, w, h;\n"
" ss = BlockPosition.x;\n"
" tt = BlockPosition.y;\n"
" if (fract(tt * 0.5) > 0.5)\n"
" ss += 0.5;\n"
" ss = fract(ss);\n"
" tt = fract(tt);\n"
" // animate the proportion of block to mortar\n"
" float blockFract = (Sine + 1.1) * 0.4;\n"
" w = step(ss, blockFract);\n"
" h = step(tt, blockFract);\n"
" color = mix(Color2, Color1, w * h) * LightIntensity;\n"
" gl_FragColor = vec4 (color, 1.0);\n"
"}\n"
}; ///////////////////////////////////////////////////////////////////////////
// callback for animating various Uniforms (currently only the SIN uniform) class AnimateCallback : public osg::UniformCallback
{
public:
enum Operation { SIN };
AnimateCallback(Operation op) : _operation(op) {}
virtual void operator() (osg::Uniform* uniform, osg::NodeVisitor* nv)
{
float angle = 2.0 * nv->getFrameStamp()->getSimulationTime();
float sine = sinf(angle); // -1 -> 1
switch (_operation) {
case SIN: uniform->set(sine); break;
}
}
private:
Operation _operation;
}; int main(int, char **)
{
// construct the viewer.
osgViewer::Viewer viewer; // use a geode with a Box ShapeDrawable
osg::Geode* basicModel = new osg::Geode();
basicModel->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f, 0.0f, 0.0f), 1.0f))); // create the "blocky" shader, a simple animation test
osg::StateSet *ss = basicModel->getOrCreateStateSet();
osg::Program* program = new osg::Program;
program->setName("blocky");
//program->addShader(new osg::Shader(osg::Shader::VERTEX, blockyVertSource));
//program->addShader(new osg::Shader(osg::Shader::FRAGMENT, blockyFragSource));
program->addShader(osg::Shader::readShaderFile(osg::Shader::VERTEX, "blocky.vert"));
program->addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, "blocky.frag"));
ss->setAttributeAndModes(program, osg::StateAttribute::ON); // attach some animated Uniform variable to the state set
osg::Uniform* SineUniform = new osg::Uniform("Sine", 0.0f);
ss->addUniform(SineUniform);
SineUniform->setUpdateCallback(new AnimateCallback(AnimateCallback::SIN)); // run the osg::Viewer using our model
viewer.setSceneData(basicModel);
return viewer.run();
} /*EOF*/

osg内置shader变量的更多相关文章

  1. Unity 内置Shader变量、辅助函数等

    一:标准库里的常用.cginc文件 HLSLSupport.cginc - (automatically included) Helper macros and definitions for cro ...

  2. CESIUM内置shader变量和函数[转]

    cesium中内置了一些常量.变量和函数,在vs和fs中可直接使用. 内置uniform 内置uniform主要置于AutomaticUniforms类里面,该类私有未开放文档. czm_backgr ...

  3. GLSL语言内置的变量详解

    GLSL语言内置的变量,包括内置的顶点属性(attribute).一致变量(uniform).易变变量(varying)以及常量(const),一方面加深印象,另一方面今天的文章可以为以后的编程做查询 ...

  4. Flask内置URL变量转换器

    Flask内置URL变量转换器: 转换器通过特定的规则执行,”<转换器: 变量名>”.<int: year>把year的值转换为证书,因此我们可以在视图函数中直接对year变量 ...

  5. unity, 查看内置shader源码

    1,建一个球体. 2,建一个材质,将材质拖到球体上. 3,在材质的shader下拉列表中选择想查看的内置shader,点材质栏右上设置按钮->Select Shader 进入shader面板. ...

  6. shell里的IFS内置环境变量

    IFS 的全称是 Interal Field Separator ,即“内部区域分隔符”,它也是一个内置环境变量,存储着默认的文本分隔符,默认下这分隔符是空格符(space character),制表 ...

  7. unity 内置shader

    几个有用的Unity 内置shader: (一)Standard RenderingMode:Opaque为实体渲染,更改Color的透明通道不会有影响:Cutout会把图片的透明通道显示出来,非严格 ...

  8. Unity内置shader 下载

    Unity内置shader  4.3.1 版本的  其他版本可以自己修改名称 下载地址 http://download.unity3d.com/download_unity/builtin_shade ...

  9. Jenkins内置环境变量的使用

    一.查看Jenkins有哪些环境变量 1.新建任意一个job 2.增加构建步骤:Execute shell 或 Execute Windows batch command 3.点击输入框下方的“可用环 ...

随机推荐

  1. 安卓程序代写 网上程序代写[原]Call requires API level 8 (current min is 1)错误

    导入了一个程序 , 每次运行之后都会出现该错误 . 点击clean 错误就会消失 , 但是执行该错误的时候该错误就会重新出现 . 这个错误需要在AndroidManifest.xml配置文件中修改 u ...

  2. MD5骨骼动画模型加载(一)

    前面我们分析了静态模型OBJ格式,桢动画模型MD2,这篇主要分析骨骼动画MD5的一些概念并且实现. 混合桢动画有计算简单,容易实现等优点,但是在需要比较细致的效果时,则需要更多的关键桢,每桢都添加相同 ...

  3. (笔记)boa服务器make错误

    编译一个linux下的c系统,包含词法和语法分析模块,Linux上用bison和flex.yacc是一个文法分析器的生成器,bison即是yacc的GNU版本.Lex和YACC是用于构造词法分析机和语 ...

  4. GROUP BY、HAVING、AS 的用法小例子

    需求: 查询选休了5门课程的学生的姓名 分析: 1. 先从 t_sc 表中查出 选了5门课的学生的学号: SELECT COUNT(code) AS countCourse,sid FROM t_sc ...

  5. lua------------------Unity3D研究院编辑器之打开unity不可识别的文件(十三)

    Unity3D研究院编辑器之打开unity不可识别的文件(十三) 雨松MOMO [Unity3D拓展编辑器] 围观8597次 9 条评论 编辑日期:2017-03-02 字体:大 中 小   有些特殊 ...

  6. (转) 解密H264、AAC硬件解码的关键扩展数据处理

    出自:http://blog.itpub.net/30168498/viewspace-1576794/       通过上一篇文章,我们用ffmpeg分离出一个多媒体容器中的音视频数据,但是很可能这 ...

  7. 关于sdl_ttf使用字体库加载失败的问题

    今天同事拿着前期阶段开发的视频绘图库给另外一个同事的电脑上测试,结果发现老是出现打开字体库失败,但从打印的日志信息看,路径下确实存在字体库啊,这是什么原因? 于是没办法,搬到自己本级上再测试下,从他机 ...

  8. (转)live555学习笔记7-RTP打包与发送

    七 RTP打包与发送 rtp传送开始于函数:MediaSink::startPlaying().想想也有道理,应是sink跟source要数据,所以从sink上调用startplaying(嘿嘿,相当 ...

  9. (原创)Python文件与文件系统系列(3)——os.path模块

    os.path 模块实现了一些操作路径名字符串的函数,可以通过 import os.path 使用该模块,不过即使仅仅 import os 也可以使用该模块的方法. 1. abspath(path) ...

  10. poj 1700 Crossing River C++/Java

    http://poj.org/problem?id=1700 题目大意: 有n个人要过坐船过河,每一个人划船有个时间a[i],每次最多两个人坐一条船过河.且过河时间为两个人中速度慢的,求n个人过河的最 ...