查看: 1385|回复: 14

osg::Uniform类在GLSL中干嘛用的?

[复制链接]

该用户从未签到

发表于 2014-8-20 12:14:34 | 显示全部楼层 |阅读模式
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

该用户从未签到

发表于 2014-8-20 17:34:14 | 显示全部楼层
传递glsl中的uniform的值

该用户从未签到

 楼主| 发表于 2014-8-21 11:02:36 | 显示全部楼层
fenma3422 发表于 2014-8-20 17:34
传递glsl中的uniform的值

osg::StateSet* ss->addUniform( new osg::Uniform("LightPosition", osg::Vec3(1.0f, 0.0f, 1.0f)) );//这参数字符串"LightPosition"怎么用呀???

该用户从未签到

发表于 2014-8-22 09:29:10 | 显示全部楼层
在shader开始处声明
uniform vec3 LightPosition;
接下来就可以用了

该用户从未签到

 楼主| 发表于 2014-8-22 16:03:34 | 显示全部楼层
fenma3422 发表于 2014-8-22 09:29
在shader开始处声明
uniform vec3 LightPosition;
接下来就可以用了

LightPosition 是规定好的变量名吗???
osgshader的例子里LightPosition就出现一次呀ss->addUniform( new osg::Uniform("LightPosition", osg::Vec3(1.0f, 0.0f, 1.0f)) );在字符串程序里没这个名字呀

该用户从未签到

发表于 2014-8-22 17:00:18 | 显示全部楼层
自己命名,确保一致。shader里可以用也可以不用,这个看shader的实现

该用户从未签到

 楼主| 发表于 2014-8-25 16:15:49 | 显示全部楼层
fenma3422 发表于 2014-8-22 17:00
自己命名,确保一致。shader里可以用也可以不用,这个看shader的实现

下面GLSL的例子
ss->addUniform( new osg::Uniform("LightPosition", osg::Vec3(1.0f, 0.0f, 1.0f)) );
ss->addUniform( new osg::Uniform("Scale", 1.0f) );
ss->addUniform( new osg::Uniform("sampler3d", TEXUNIT_NOISE));
这三个变量名在着色器在哪使用的?

  1. // August20_t.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <math.h>
  5. #include <stdlib.h>
  6. #include <osg/Notify>
  7. #include <osgGA/GUIEventAdapter>
  8. #include <osgGA/GUIActionAdapter>
  9. #include <osgDB/ReadFile>
  10. #include <osgUtil/Optimizer>
  11. #include <osgViewer/Viewer>
  12. #include <osg/ShapeDrawable>
  13. #include <osg/PositionAttitudeTransform>
  14. #include <osg/Geode>
  15. #include <osg/Node>
  16. #include <osg/Material>
  17. #include <osg/Notify>
  18. #include <osg/Vec3>
  19. #include <osg/Texture3D>
  20. #include <osgDB/FileUtils>
  21. #include <osg/Program>
  22. #include <osg/Shader>
  23. #include <osg/Uniform>
  24. #include <iostream>

  25. #define MAXB 0x100
  26. #define N 0x1000
  27. #define NP 12   /* 2^N */
  28. #define NM 0xfff
  29. int start = 1;
  30. int B = 5;
  31. int BM = 5;

  32. #define s_curve(t) ( t * t * (3. - 2. * t) )
  33. #define lerp(t, a, b) ( a + t * (b - a) )
  34. #define setup(i,b0,b1,r0,r1)\
  35.         t = vec[i] + N;\
  36.         b0 = ((int)t) & BM;\
  37.         b1 = (b0+1) & BM;\
  38.         r0 = t - (int)t;\
  39.         r1 = r0 - 1.;
  40. #define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )

  41. static int p[MAXB + MAXB + 2];
  42. static double g3[MAXB + MAXB + 2][3];

  43. void normalize3(double v[3])
  44. {
  45.         double s;
  46.         s = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
  47.         v[0] = v[0] / s;
  48.         v[1] = v[1] / s;
  49.         v[2] = v[2] / s;
  50. }

  51. void initNoise(void)
  52. {
  53.         int i, j, k;
  54.         srand(30757);
  55.         for (i = 0 ; i < B ; i++)
  56.         {
  57.                 p[i] = i;
  58.                 for (j = 0 ; j < 3 ; j++)
  59.                         g3[i][j] = (double)((rand() % (B + B)) - B) / B;
  60.                 normalize3(g3[i]);
  61.         }
  62.         while (--i) {
  63.                 k = p[i];
  64.                 p[i] = p[j = rand() % B];
  65.                 p[j] = k;
  66.         }
  67.         for (i = 0 ; i < B + 2 ; i++) {
  68.                 p[B + i] = p[i];
  69.                 for (j = 0 ; j < 3 ; j++)
  70.                         g3[B + i][j] = g3[i][j];
  71.         }
  72.         for (i = 0 ; i < MAXB ; i++) {
  73.                 printf("%d ",p[i]);
  74.                 if((i+1)%10 == 0)
  75.                         printf("\n");
  76.         }
  77. }


  78. double noise3(double vec[3])
  79. {
  80.         int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
  81.         double rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v;
  82.         int i, j;

  83.         if (start) {
  84.                 start = 0;
  85.                 initNoise();
  86.         }

  87.         setup(0, bx0,bx1, rx0,rx1);
  88.         setup(1, by0,by1, ry0,ry1);
  89.         setup(2, bz0,bz1, rz0,rz1);

  90.         i = p[ bx0 ];
  91.         j = p[ bx1 ];

  92.         b00 = p[ i + by0 ];
  93.         b10 = p[ j + by0 ];
  94.         b01 = p[ i + by1 ];
  95.         b11 = p[ j + by1 ];

  96.         t  = s_curve(rx0);
  97.         sy = s_curve(ry0);
  98.         sz = s_curve(rz0);

  99.         q = g3[ b00 + bz0 ] ; u = at3(rx0,ry0,rz0);
  100.         q = g3[ b10 + bz0 ] ; v = at3(rx1,ry0,rz0);
  101.         a = lerp(t, u, v);

  102.         q = g3[ b01 + bz0 ] ; u = at3(rx0,ry1,rz0);
  103.         q = g3[ b11 + bz0 ] ; v = at3(rx1,ry1,rz0);
  104.         b = lerp(t, u, v);

  105.         c = lerp(sy, a, b);

  106.         q = g3[ b00 + bz1 ] ; u = at3(rx0,ry0,rz1);
  107.         q = g3[ b10 + bz1 ] ; v = at3(rx1,ry0,rz1);
  108.         a = lerp(t, u, v);

  109.         q = g3[ b01 + bz1 ] ; u = at3(rx0,ry1,rz1);
  110.         q = g3[ b11 + bz1 ] ; v = at3(rx1,ry1,rz1);
  111.         b = lerp(t, u, v);

  112.         d = lerp(sy, a, b);

  113.         return lerp(sz, c, d);
  114. }

  115. static osg::Image*
  116. make3DNoiseImage(int texSize)
  117. {
  118.         osg::Image* image = new osg::Image;
  119.         image->setImage(texSize, texSize, texSize,
  120.                 4, GL_RGBA, GL_UNSIGNED_BYTE,
  121.                 new unsigned char[4 * texSize * texSize * texSize],
  122.                 osg::Image::USE_NEW_DELETE);

  123.         const int startFrequency = 4;
  124.         const int numOctaves = 4;

  125.         int f, i, j, k, inc;
  126.         double ni[3];
  127.         double inci, incj, inck;
  128.         int frequency = startFrequency;
  129.         GLubyte *ptr;
  130.         double amp = 0.5;

  131.         osg::notify(osg::INFO) << "creating 3D noise texture... ";

  132.         for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
  133.         {
  134.                 //SetNoiseFrequency(frequency);
  135.                 ptr = image->data();
  136.                 ni[0] = ni[1] = ni[2] = 0;

  137.                 inci = 1.0 / (texSize / frequency);
  138.                 for (i = 0; i < texSize; ++i, ni[0] += inci)
  139.                 {
  140.                         incj = 1.0 / (texSize / frequency);
  141.                         for (j = 0; j < texSize; ++j, ni[1] += incj)
  142.                         {
  143.                                 inck = 1.0 / (texSize / frequency);
  144.                                 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
  145.                                 {
  146.                                         *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) ) * 128.0);
  147.                                 }
  148.                         }
  149.                 }
  150.         }

  151.         osg::notify(osg::INFO) << "DONE" << std::endl;
  152.         return image;        
  153. }

  154. static osg::Texture3D*
  155. make3DNoiseTexture(int texSize )
  156. {
  157.         osg::Texture3D* noiseTexture = new osg::Texture3D;
  158.         noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
  159.         noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
  160.         noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
  161.         noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
  162.         noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
  163.         noiseTexture->setImage( make3DNoiseImage(texSize) );
  164.         return noiseTexture;
  165. }

  166. static const char *microshaderVertSource = {
  167.         "// microshader - colors a fragment based on its position\n"
  168.         "varying vec4 color;\n"
  169.         "void main(void)\n"
  170.         "{\n"
  171.         "    color = gl_Vertex;\n"
  172.         "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
  173.         "}\n"
  174. };

  175. static const char *microshaderFragSource = {
  176.         "varying vec4 color;\n"
  177.         "void main(void)\n"
  178.         "{\n"
  179.         "    gl_FragColor = clamp( color, 0.0, 1.0 );\n"
  180.         "}\n"
  181. };

  182. static osg::ref_ptr<osg::Group> rootNode;
  183. static osg::Geode*
  184. CreateModel()
  185. {
  186.         osg::Geode* geode = new osg::Geode();
  187.         geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),1.0f)));
  188.         return geode;
  189. }

  190. static osg::StateSet*
  191. ModelInstance()
  192. {
  193.         static float zvalue = 0.0f;
  194.         static osg::Node* masterModel = CreateModel();

  195.         osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform();
  196.         xform->setPosition(osg::Vec3( 0.0f, -1.0f, zvalue ));
  197.         zvalue = zvalue + 2.2f;
  198.         xform->addChild(masterModel);
  199.         rootNode->addChild(xform);
  200.         return xform->getOrCreateStateSet();
  201. }

  202. // load source from a file.
  203. static void
  204. LoadShaderSource( osg::Shader* shader, const std::string& fileName )
  205. {
  206.         std::string fqFileName = osgDB::findDataFile(fileName);
  207.         if( fqFileName.length() != 0 )
  208.         {
  209.                 shader->loadShaderSourceFromFile( fqFileName.c_str() );
  210.         }
  211.         else
  212.         {
  213.                 osg::notify(osg::WARN) << "File "" << fileName << "" not found." << std::endl;
  214.         }
  215. }


  216. static osg::Program* ErodedProgram;
  217. static osg::Shader*  ErodedVertObj;
  218. static osg::Shader*  ErodedFragObj;


  219. #define TEXUNIT_SINE        1
  220. #define TEXUNIT_NOISE       2



  221. class GL2Scene : public osg::Referenced
  222. {
  223. public:
  224.         GL2Scene()
  225.         {
  226.                 _rootNode = buildScene();
  227.         }

  228.         osg::ref_ptr<osg::Group> getRootNode() { return _rootNode; }
  229.         void reloadShaderSource()
  230.         {
  231.                 LoadShaderSource( ErodedVertObj, "shaders/eroded.vert" );
  232.                 LoadShaderSource( ErodedFragObj, "shaders/eroded.frag" );
  233.         }

  234. protected:
  235.         ~GL2Scene(){};

  236. private:        /*methods*/
  237.         osg::ref_ptr<osg::Group> buildScene()
  238.         {
  239.                 osg::Texture3D* noiseTexture = make3DNoiseTexture( 32 /*128*/ );
  240.                 rootNode = new osg::Group;
  241.                 osg::StateSet* ss = ModelInstance();
  242.                 ss->setTextureAttribute(TEXUNIT_NOISE, noiseTexture);
  243.                 ErodedProgram = new osg::Program;
  244.                 ErodedProgram->setName( "eroded" );
  245.                 _programList.push_back( ErodedProgram );
  246.                 ErodedVertObj = new osg::Shader( osg::Shader::VERTEX );
  247.                 ErodedFragObj = new osg::Shader( osg::Shader::FRAGMENT );
  248.                 ErodedProgram->addShader( ErodedFragObj );
  249.                 ErodedProgram->addShader( ErodedVertObj );
  250.                 ss->setAttributeAndModes(ErodedProgram, osg::StateAttribute::ON);
  251.                 ss->addUniform( new osg::Uniform("LightPosition", osg::Vec3(1.0f, 0.0f, 1.0f)) );
  252.                 ss->addUniform( new osg::Uniform("Scale", 1.0f) );
  253.                 ss->addUniform( new osg::Uniform("sampler3d", TEXUNIT_NOISE));
  254.                 reloadShaderSource();
  255.                 return rootNode;
  256.         }
  257. private:        /*data*/
  258.         osg::ref_ptr<osg::Group> _rootNode;
  259.         std::vector< osg::ref_ptr<osg::Program> > _programList;
  260. };

  261. typedef osg::ref_ptr<GL2Scene> GL2ScenePtr;



  262. int _tmain(int argc, _TCHAR* argv[])
  263. {       
  264.         osgViewer::Viewer viewer;
  265.         GL2ScenePtr gl2Scene = new GL2Scene;

  266.         osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
  267.         traits->x = 40;
  268.         traits->y = 40;
  269.         traits->width = 600;
  270.         traits->height = 480;
  271.         traits->windowDecoration = true;
  272.         traits->doubleBuffer = true;
  273.         traits->sharedContext = 0;

  274.         osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());

  275.         osg::ref_ptr<osg::Camera> camera = new osg::Camera;
  276.         camera->setGraphicsContext(gc.get());
  277.         camera->setViewport(new osg::Viewport(0,0, traits->width, traits->height));
  278.         GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
  279.         camera->setDrawBuffer(buffer);
  280.         camera->setReadBuffer(buffer);

  281.         // add this slave camera to the viewer, with a shift left of the projection matrix
  282.         viewer.addSlave(camera.get());
  283.        
  284.         viewer.setSceneData(gl2Scene->getRootNode().get());
  285.         //viewer->
  286.         return viewer.run();
  287.         return 0;
  288. }
复制代码

该用户从未签到

 楼主| 发表于 2014-8-28 13:19:31 | 显示全部楼层

ss->addUniform( new osg::Uniform("LightPosition", osg::Vec3(1.0f, 0.0f, 1.0f)) );
ss->addUniform( new osg::Uniform("Scale", 1.0f) );
ss->addUniform( new osg::Uniform("sampler3d", TEXUNIT_NOISE));
没用到????上面代码复制下来就能跑呀。去掉上面三句话就出不来效果啦呀

该用户从未签到

发表于 2014-8-30 13:40:01 | 显示全部楼层
确实没看到有用到的地方,除非还有别的文件?

该用户从未签到

 楼主| 发表于 2014-8-30 18:05:10 | 显示全部楼层
fenma3422 发表于 2014-8-30 13:40
确实没看到有用到的地方,除非还有别的文件?

我也纳闷呢 哥们复制下我的代码,加上库。跑下试试吧。去掉真没效果了。。。

该用户从未签到

 楼主| 发表于 2014-9-2 13:13:55 | 显示全部楼层
fenma3422 发表于 2014-8-30 13:40
确实没看到有用到的地方,除非还有别的文件?

LoadShaderSource( BlockyVertObj, "shaders/blocky.vert" );
LoadShaderSource( BlockyFragObj, "shaders/blocky.frag" );

LoadShaderSource( ErodedVertObj, "shaders/eroded.vert" );
LoadShaderSource( ErodedFragObj, "shaders/eroded.frag" );

LoadShaderSource( MarbleVertObj, "shaders/marble.vert" );
LoadShaderSource( MarbleFragObj, "shaders/marble.frag" );
我知道在哪里用的了,几个shaders文件是 加载的写好的文件呀


我以后是程序里写的那俩个呢


怪不得找不到

该用户从未签到

发表于 2014-9-2 14:14:55 | 显示全部楼层
我也只看了代码里的那两

该用户从未签到

 楼主| 发表于 2014-9-2 15:19:58 | 显示全部楼层
fenma3422 发表于 2014-9-2 14:14
我也只看了代码里的那两

新手真容易被坑。。。

该用户从未签到

 楼主| 发表于 2014-9-2 15:34:54 | 显示全部楼层
fenma3422 发表于 2014-9-2 14:14
我也只看了代码里的那两

哥们,我在地球模型上挖个坑咋整呀?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表