|
楼主 |
发表于 2014-8-25 16:15:49
|
显示全部楼层
下面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));
这三个变量名在着色器在哪使用的?
- // August20_t.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <math.h>
- #include <stdlib.h>
- #include <osg/Notify>
- #include <osgGA/GUIEventAdapter>
- #include <osgGA/GUIActionAdapter>
- #include <osgDB/ReadFile>
- #include <osgUtil/Optimizer>
- #include <osgViewer/Viewer>
- #include <osg/ShapeDrawable>
- #include <osg/PositionAttitudeTransform>
- #include <osg/Geode>
- #include <osg/Node>
- #include <osg/Material>
- #include <osg/Notify>
- #include <osg/Vec3>
- #include <osg/Texture3D>
- #include <osgDB/FileUtils>
- #include <osg/Program>
- #include <osg/Shader>
- #include <osg/Uniform>
- #include <iostream>
- #define MAXB 0x100
- #define N 0x1000
- #define NP 12 /* 2^N */
- #define NM 0xfff
- int start = 1;
- int B = 5;
- int BM = 5;
- #define s_curve(t) ( t * t * (3. - 2. * t) )
- #define lerp(t, a, b) ( a + t * (b - a) )
- #define setup(i,b0,b1,r0,r1)\
- t = vec[i] + N;\
- b0 = ((int)t) & BM;\
- b1 = (b0+1) & BM;\
- r0 = t - (int)t;\
- r1 = r0 - 1.;
- #define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
- static int p[MAXB + MAXB + 2];
- static double g3[MAXB + MAXB + 2][3];
- void normalize3(double v[3])
- {
- double s;
- s = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
- v[0] = v[0] / s;
- v[1] = v[1] / s;
- v[2] = v[2] / s;
- }
- void initNoise(void)
- {
- int i, j, k;
- srand(30757);
- for (i = 0 ; i < B ; i++)
- {
- p[i] = i;
- for (j = 0 ; j < 3 ; j++)
- g3[i][j] = (double)((rand() % (B + B)) - B) / B;
- normalize3(g3[i]);
- }
- while (--i) {
- k = p[i];
- p[i] = p[j = rand() % B];
- p[j] = k;
- }
- for (i = 0 ; i < B + 2 ; i++) {
- p[B + i] = p[i];
- for (j = 0 ; j < 3 ; j++)
- g3[B + i][j] = g3[i][j];
- }
- for (i = 0 ; i < MAXB ; i++) {
- printf("%d ",p[i]);
- if((i+1)%10 == 0)
- printf("\n");
- }
- }
- double noise3(double vec[3])
- {
- int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
- double rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v;
- int i, j;
- if (start) {
- start = 0;
- initNoise();
- }
- setup(0, bx0,bx1, rx0,rx1);
- setup(1, by0,by1, ry0,ry1);
- setup(2, bz0,bz1, rz0,rz1);
- i = p[ bx0 ];
- j = p[ bx1 ];
- b00 = p[ i + by0 ];
- b10 = p[ j + by0 ];
- b01 = p[ i + by1 ];
- b11 = p[ j + by1 ];
- t = s_curve(rx0);
- sy = s_curve(ry0);
- sz = s_curve(rz0);
- q = g3[ b00 + bz0 ] ; u = at3(rx0,ry0,rz0);
- q = g3[ b10 + bz0 ] ; v = at3(rx1,ry0,rz0);
- a = lerp(t, u, v);
- q = g3[ b01 + bz0 ] ; u = at3(rx0,ry1,rz0);
- q = g3[ b11 + bz0 ] ; v = at3(rx1,ry1,rz0);
- b = lerp(t, u, v);
- c = lerp(sy, a, b);
- q = g3[ b00 + bz1 ] ; u = at3(rx0,ry0,rz1);
- q = g3[ b10 + bz1 ] ; v = at3(rx1,ry0,rz1);
- a = lerp(t, u, v);
- q = g3[ b01 + bz1 ] ; u = at3(rx0,ry1,rz1);
- q = g3[ b11 + bz1 ] ; v = at3(rx1,ry1,rz1);
- b = lerp(t, u, v);
- d = lerp(sy, a, b);
- return lerp(sz, c, d);
- }
- static osg::Image*
- make3DNoiseImage(int texSize)
- {
- osg::Image* image = new osg::Image;
- image->setImage(texSize, texSize, texSize,
- 4, GL_RGBA, GL_UNSIGNED_BYTE,
- new unsigned char[4 * texSize * texSize * texSize],
- osg::Image::USE_NEW_DELETE);
- const int startFrequency = 4;
- const int numOctaves = 4;
- int f, i, j, k, inc;
- double ni[3];
- double inci, incj, inck;
- int frequency = startFrequency;
- GLubyte *ptr;
- double amp = 0.5;
- osg::notify(osg::INFO) << "creating 3D noise texture... ";
- for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
- {
- //SetNoiseFrequency(frequency);
- ptr = image->data();
- ni[0] = ni[1] = ni[2] = 0;
- inci = 1.0 / (texSize / frequency);
- for (i = 0; i < texSize; ++i, ni[0] += inci)
- {
- incj = 1.0 / (texSize / frequency);
- for (j = 0; j < texSize; ++j, ni[1] += incj)
- {
- inck = 1.0 / (texSize / frequency);
- for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
- {
- *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) ) * 128.0);
- }
- }
- }
- }
- osg::notify(osg::INFO) << "DONE" << std::endl;
- return image;
- }
- static osg::Texture3D*
- make3DNoiseTexture(int texSize )
- {
- osg::Texture3D* noiseTexture = new osg::Texture3D;
- noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
- noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
- noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
- noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
- noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
- noiseTexture->setImage( make3DNoiseImage(texSize) );
- return noiseTexture;
- }
- static const char *microshaderVertSource = {
- "// microshader - colors a fragment based on its position\n"
- "varying vec4 color;\n"
- "void main(void)\n"
- "{\n"
- " color = gl_Vertex;\n"
- " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
- "}\n"
- };
- static const char *microshaderFragSource = {
- "varying vec4 color;\n"
- "void main(void)\n"
- "{\n"
- " gl_FragColor = clamp( color, 0.0, 1.0 );\n"
- "}\n"
- };
- static osg::ref_ptr<osg::Group> rootNode;
- static osg::Geode*
- CreateModel()
- {
- osg::Geode* geode = new osg::Geode();
- geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),1.0f)));
- return geode;
- }
- static osg::StateSet*
- ModelInstance()
- {
- static float zvalue = 0.0f;
- static osg::Node* masterModel = CreateModel();
- osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform();
- xform->setPosition(osg::Vec3( 0.0f, -1.0f, zvalue ));
- zvalue = zvalue + 2.2f;
- xform->addChild(masterModel);
- rootNode->addChild(xform);
- return xform->getOrCreateStateSet();
- }
- // load source from a file.
- static void
- LoadShaderSource( osg::Shader* shader, const std::string& fileName )
- {
- std::string fqFileName = osgDB::findDataFile(fileName);
- if( fqFileName.length() != 0 )
- {
- shader->loadShaderSourceFromFile( fqFileName.c_str() );
- }
- else
- {
- osg::notify(osg::WARN) << "File "" << fileName << "" not found." << std::endl;
- }
- }
- static osg::Program* ErodedProgram;
- static osg::Shader* ErodedVertObj;
- static osg::Shader* ErodedFragObj;
- #define TEXUNIT_SINE 1
- #define TEXUNIT_NOISE 2
- class GL2Scene : public osg::Referenced
- {
- public:
- GL2Scene()
- {
- _rootNode = buildScene();
- }
- osg::ref_ptr<osg::Group> getRootNode() { return _rootNode; }
- void reloadShaderSource()
- {
- LoadShaderSource( ErodedVertObj, "shaders/eroded.vert" );
- LoadShaderSource( ErodedFragObj, "shaders/eroded.frag" );
- }
- protected:
- ~GL2Scene(){};
- private: /*methods*/
- osg::ref_ptr<osg::Group> buildScene()
- {
- osg::Texture3D* noiseTexture = make3DNoiseTexture( 32 /*128*/ );
- rootNode = new osg::Group;
- osg::StateSet* ss = ModelInstance();
- ss->setTextureAttribute(TEXUNIT_NOISE, noiseTexture);
- ErodedProgram = new osg::Program;
- ErodedProgram->setName( "eroded" );
- _programList.push_back( ErodedProgram );
- ErodedVertObj = new osg::Shader( osg::Shader::VERTEX );
- ErodedFragObj = new osg::Shader( osg::Shader::FRAGMENT );
- ErodedProgram->addShader( ErodedFragObj );
- ErodedProgram->addShader( ErodedVertObj );
- ss->setAttributeAndModes(ErodedProgram, osg::StateAttribute::ON);
- 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));
- reloadShaderSource();
- return rootNode;
- }
- private: /*data*/
- osg::ref_ptr<osg::Group> _rootNode;
- std::vector< osg::ref_ptr<osg::Program> > _programList;
- };
- typedef osg::ref_ptr<GL2Scene> GL2ScenePtr;
- int _tmain(int argc, _TCHAR* argv[])
- {
- osgViewer::Viewer viewer;
- GL2ScenePtr gl2Scene = new GL2Scene;
- osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
- traits->x = 40;
- traits->y = 40;
- traits->width = 600;
- traits->height = 480;
- traits->windowDecoration = true;
- traits->doubleBuffer = true;
- traits->sharedContext = 0;
- osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
- osg::ref_ptr<osg::Camera> camera = new osg::Camera;
- camera->setGraphicsContext(gc.get());
- camera->setViewport(new osg::Viewport(0,0, traits->width, traits->height));
- GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
- camera->setDrawBuffer(buffer);
- camera->setReadBuffer(buffer);
- // add this slave camera to the viewer, with a shift left of the projection matrix
- viewer.addSlave(camera.get());
-
- viewer.setSceneData(gl2Scene->getRootNode().get());
- //viewer->
- return viewer.run();
- return 0;
- }
复制代码 |
|