博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在osg场景中使用GLSL语言——一个例子
阅读量:4110 次
发布时间:2019-05-25

本文共 2329 字,大约阅读时间需要 7 分钟。

1、由于环境变量神马的都已经设置好了,基本上要使用osg,只需要在添加连接器中的附加依赖项中添加:

kernel32.lib

user32.lib
gdi32.lib
winspool.lib
shell32.lib
ole32.lib
oleaut32.lib
uuid.lib
comdlg32.lib
advapi32.lib
OpenThreadsd.lib
osgd.lib
osgDBd.lib
osgUtild.lib
osgGAd.lib
osgViewerd.lib
osgTextd.lib
glu32.lib
opengl32.lib

2、直接添加代码如下:

// 1.cpp : 定义控制台应用程序的入口点。

//
#include "stdafx.h"
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
static const char* vertSource=
{
"varying vec3 normal;\n"
"void main()\n"
"{\n"
"     normal=normalize(gl_NormalMatrix*gl_Normal);\n"
"     gl_Position=ftransform();\n"
"}\n"
};
static const char* fragSource={
"uniform vec4 mainColor;\n"
"varying vec3 normal;\n"
"void main()\n"
"{\n"
"    float intensity=dot(vec3(gl_LightSource[0].position),normal);\n"
"    if(intensity>0.95) gl_FragColor=mainColor;\n"
"    else if(intensity>0.5)  gl_FragColor=vec4(0.6,0.3,0.3,1.0);\n" 
"    else if(intensity>0.25)  gl_FragColor=vec4(0.4,0.2,0.2,1.0);\n" 
"    else gl_FragColor=vec4(0.2,0.1,0.1,1.0);\n" 
"}\n"
};
class ColorCallback:public osg::Uniform::Callback
{
public:
ColorCallback():_incRed(false){}
virtual void operator()(osg::Uniform * uniform,osg::NodeVisitor* nv)
{
if (!uniform) return;
osg::Vec4 color;
uniform->get(color);
if (_incRed==true)
{
if (color.x()<1.0) color.x()+=0.01;
else _incRed=false;
}
else
{
if (color.x()>0.0) color.x()-=0.01;
else _incRed=true;
}
uniform->set(color);
}
protected:
bool _incRed;
};
void createShaders(osg::StateSet & ss)
{
osg::ref_ptr<osg::Shader> vertShader=new osg::Shader(osg::Shader::VERTEX,vertSource);
osg::ref_ptr<osg::Shader> fragShader=new osg::Shader(osg::Shader::FRAGMENT,fragSource);
osg::ref_ptr<osg::Program> program=new osg::Program;
program->addShader(vertShader.get());
program->addShader(fragShader.get());
osg::ref_ptr<osg::Uniform> mainColor=new osg::Uniform("mainColor",osg::Vec4(1.0,0.5,0.5,1.0));
mainColor->setUpdateCallback(new ColorCallback);
ss.addUniform(mainColor.get());
ss.setAttributeAndModes(program.get());
}
int _tmain(int argc, _TCHAR* argv[])
{
osg::ArgumentParser arguments(&argc,argv);
osg::Node *model=osgDB::readNodeFiles(arguments);
if (!model) model=osgDB::readNodeFile("cow.osg");
createShaders(*(model->getOrCreateStateSet()));
osgViewer::Viewer a;
a.setSceneData(model);
return a.run();
}
效果截图:

具体的参考文档是

http://download.csdn.net/detail/smbluesky/7504667

可参考该文档的6.4章节,里面有具体的介绍。

你可能感兴趣的文章
Linux基础系列-Kernel 初始化宏
查看>>
Linux子系统系列-I2C
查看>>
<iOS>关于自定义description的一点用法
查看>>
Unix 命令,常用到的
查看>>
Linux操作系统文件系统基础知识详解
查看>>
部分常用到的SQLite语句
查看>>
堆和栈的区别
查看>>
当异常出现时
查看>>
<iOS>iPhone 应用里实现截屏功能的代码
查看>>
iOS6 中新的控件UIRefreshControl下拉刷新
查看>>
bitbucket和git 进行代码管理
查看>>
在CGD中快速实现多线程的并发控制
查看>>
IOS开发网络篇之──ASIHTTPRequest详解
查看>>
IOS开发网络篇之──ASIHTTPRequest下载示例(支持断点续传)
查看>>
<iOS>通过运行时来实例化一个,只知道名字的类, 名字为变量
查看>>
第三次遇到同样DNS无法解析的问题,不得不把解决方法分享了
查看>>
DLL中建立进程共享数据段需要注意的语法问题
查看>>
重温WIN32 API ------ 最简单的Windows窗口封装类
查看>>
重温WIN32 API ------ Window消息跟踪
查看>>
一个通过捕获ARP发现IP的小工具
查看>>