Osg-OsgShader着色器(Qt5.14.2+osgE3.6.5+win10)-No21-OsgShader

实例代码:

osgwidgetshader-vert.glsl

 1 // osgWidget/GLSL - Copyright (C) 2007 Jeremy Moles
 2 // $Id: osgwidgetshader-vert.glsl 28 2008-03-26 15:26:48Z cubicool $
 3 
 4 // TODO: If I want do picking properly I need to transform the mouse coordinates
 5 // by the matrix of the WindowManger.
 6 
 7 varying vec4 color;
 8 
 9 void main() {
10     color = gl_Color;
11 
12     gl_Position = ftransform();
13 }
View Code

osgwidgetshader-frag.glsl

 1 uniform float time; 
 2 uniform vec2 mouse; 
 3 uniform vec2 resolution; 
 4 
 5 #define M_PI 3.1415926535897932384626433832795 
 6 
 7 float rand(vec2 co) 
 8 { 
 9     return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); 
10 } 
11 
12 void main() { 
13 
14     float size = 30.0; 
15     float prob = 0.95; 
16 
17     vec2 pos = floor(1.0/size * gl_FragCoord.xy); 
18 
19     float color = 0.0; 
20     float starValue = rand(pos); 
21 
22     if (starValue > prob) 
23     { 
24      vec2 center = size * pos + vec2(size, size) * 0.5; 
25 
26      float t = 0.9 + sin(time + (starValue - prob)/(1.0 - prob) * 45.0); 
27 
28      color = 1.0 - distance(gl_FragCoord.xy, center)/(0.5 * size); 
29      color = color * t/(abs(gl_FragCoord.y - center.y)) * t/(abs(gl_FragCoord.x - center.x)); 
30     } 
31     else if (rand(gl_FragCoord.xy/resolution.xy) > 0.996) 
32     { 
33      float r = rand(gl_FragCoord.xy); 
34      color = r * (0.25 * sin(time * (r * 5.0) + 720.0 * r) + 0.75); 
35     } 
36 
37     gl_FragColor = vec4(vec3(color), 1.0); 
38 
39 
40 } 
View Code

.pro

1 https://images.cnblogs.com/cnblogs_com/FKdelphi/2045297/o_220713083806_No21-OsgShader%E7%9D%80%E8%89%B2%E5%99%A8.gif
View Code

main.cpp

 1 #include <osgViewer/Viewer>
 2 #include <osgDB/ReadFile>
 3 #include <osg/Shape>
 4 #include <osg/ShapeDrawable>
 5 #include <osg/MatrixTransform>
 6 #include <osg/Program>
 7 #include <osgDB/ReadFile>
 8 #include <osgDB/FileUtils>
 9 
10 
11 int main()
12 {
13     osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
14     osg::ref_ptr<osg::Node> geode = osgDB::readNodeFile("D:/Gitee/OsgTestQt/src/No21-OsgShader/cow.osg");//CreateNode();
15 
16     osg::Program* program2 = new osg::Program();
17     program2->addShader(osg::Shader::readShaderFile(
18         osg::Shader::VERTEX,
19         osgDB::findDataFile("D:/Gitee/OsgEarthTestQt/bin/osgWidget/osgwidgetshader-vert.glsl")
20     ));
21     program2->addShader(osg::Shader::readShaderFile(
22         osg::Shader::FRAGMENT,
23         osgDB::findDataFile("D:/Gitee/OsgEarthTestQt/bin/osgWidget/osgwidgetshader-frag.glsl")
24     ));
25     geode->getOrCreateStateSet()->setAttribute(program2);
26 
27     osg::Group * root = new osg::Group; 
28     root->addChild(geode);
29 
30     viewer->setSceneData(root);
31     viewer->setUpViewInWindow(35, 35, 500, 500);
32 
33     return viewer->run();
34 }
View Code

 

 

posted on 2022-07-13 16:41  疯狂delphi  阅读(87)  评论(0编辑  收藏  举报

导航