root/OpenSceneGraph/trunk/examples/osgparametric/osgparametric.cpp @ 4346

Revision 4346, 5.9 kB (checked in by robert, 9 years ago)

Tweaked the vertex program.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/Vec3>
2#include <osg/Vec4>
3#include <osg/Quat>
4#include <osg/Matrix>
5#include <osg/ShapeDrawable>
6#include <osg/Geometry>
7#include <osg/Geode>
8#include <osg/Texture2D>
9
10#include <osgDB/FileUtils>
11#include <osgDB/ReadFile>
12
13#include <osgUtil/Optimizer>
14
15#include <osgProducer/Viewer>
16
17char vertexShaderSource[] =
18    "uniform vec2  xCoeff; \n"
19    "uniform vec2  yCoeff; \n"
20    "//uniform sampler2D baseTexture; \n"
21    "\n"
22    "void main(void) \n"
23    "{ \n"
24    "\n"
25    "    gl_TexCoord[0] = gl_Vertex; \n"
26    "    gl_Vertex.z = gl_Vertex.x*xCoeff[0] + gl_Vertex.x*gl_Vertex.x* xCoeff[1] + \n"
27    "                  gl_Vertex.y*yCoeff[1] + gl_Vertex.y*gl_Vertex.y* yCoeff[1]; \n"
28    "    //gl_Vertex.z = texture2D( vertexTexture, gl_TexCoord[0].xy).r; \n"
29    "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
30    "}\n";
31   
32char fragmentShaderSource[] =
33    "uniform sampler2D baseTexture; \n"
34    "\n"
35    "void main(void) \n"
36    "{ \n"
37    "    gl_FragColor = texture2D( baseTexture, gl_TexCoord[0].xy); \n"
38    "}\n";
39
40
41class UniformVarying : public osg::Uniform::Callback
42{
43    virtual void operator () (osg::Uniform* uniform, osg::NodeVisitor* nv)
44    {
45        const osg::FrameStamp* fs = nv->getFrameStamp();
46        float value = sinf(fs->getReferenceTime());
47        uniform->set(osg::Vec2(value,-value));
48    }
49};
50
51osg::Node* createModel()
52{
53    osg::Geode* geode = new osg::Geode;
54   
55    osg::Geometry* geom = new osg::Geometry;
56    geode->addDrawable(geom);
57   
58    // dimensions for ~one million triangles :-)
59    unsigned int num_x = 708;
60    unsigned int num_y = 708;
61
62    osg::Vec3Array* vertices = new osg::Vec3Array( num_x * num_y );
63   
64    float dx = 1.0f/(float)(num_x-1);
65    float dy = 1.0f/(float)(num_y-1);
66    osg::Vec3 row(0.0f,0.0f,0.0);
67   
68    unsigned int vert_no = 0;
69    for(unsigned int iy=0; iy<num_y; ++iy)
70    {
71        osg::Vec3 column = row;
72        for(unsigned int ix=0;ix<num_x;++ix)
73        {
74            (*vertices)[vert_no++] = column;
75            column.x() += dx;
76        }       
77        row.y() += dy;
78    }
79
80    geom->setVertexArray(vertices);
81
82    for(unsigned int iy=0; iy<num_y-1; ++iy)
83    {
84        unsigned int element_no = 0;
85        osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLE_STRIP, num_x*2);
86        unsigned int index = iy * num_x;
87        for(unsigned int ix = 0; ix<num_x; ++ix)
88        {
89            (*elements)[element_no++] = index + num_x;
90            (*elements)[element_no++] = index++;
91        }
92        geom->addPrimitiveSet(elements);   
93    }
94   
95    geom->setUseVertexBufferObjects(true);
96   
97   
98    osg::StateSet* stateset = geom->getOrCreateStateSet();
99
100    osg::Program* program = new osg::Program;
101    stateset->setAttribute(program);
102
103    osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource);
104    program->addShader(vertex_shader);
105   
106
107    osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource);
108    program->addShader(fragment_shader);
109
110
111    osg::Uniform* xCoeff = new osg::Uniform("xCoeff",osg::Vec2(1.0,-1.0f));
112    xCoeff->setUpdateCallback(new UniformVarying);
113    stateset->addUniform(xCoeff);
114
115    osg::Uniform* yCoeff = new osg::Uniform("yCoeff",osg::Vec2(-1.0f,1.0f));
116    stateset->addUniform(yCoeff);
117   
118   
119    osg::Texture2D* texture = new osg::Texture2D(osgDB::readImageFile("lz.rgb"));
120    texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST);
121    texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST);
122    stateset->setTextureAttributeAndModes(0,texture);
123   
124    osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0);
125    stateset->addUniform(baseTextureSampler);
126
127
128    return geode;
129
130}
131
132int main(int argc, char *argv[])
133{
134    // use an ArgumentParser object to manage the program arguments.
135    osg::ArgumentParser arguments(&argc,argv);
136
137    // set up the usage document, in case we need to print out how to use this program.
138    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrate support for ARB_vertex_program.");
139    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
140    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
141   
142    // construct the viewer.
143    osgProducer::Viewer viewer(arguments);
144
145    // set up the value with sensible default event handlers.
146    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
147
148    // get details on keyboard and mouse bindings used by the viewer.
149    viewer.getUsage(*arguments.getApplicationUsage());
150
151    // if user request help write it out to cout.
152    if (arguments.read("-h") || arguments.read("--help"))
153    {
154        arguments.getApplicationUsage()->write(std::cout);
155        return 1;
156    }
157
158    // any option left unread are converted into errors to write out later.
159    arguments.reportRemainingOptionsAsUnrecognized();
160
161    // report any errors if they have occured when parsing the program aguments.
162    if (arguments.errors())
163    {
164        arguments.writeErrorMessages(std::cout);
165        return 1;
166    }
167
168    // load the nodes from the commandline arguments.
169    osg::Node* model = createModel();
170    if (!model)
171    {
172        return 1;
173    }
174
175    // add a viewport to the viewer and attach the scene graph.
176    viewer.setSceneData(model);
177   
178    // create the windows and run the threads.
179    viewer.realize();
180
181    while( !viewer.done() )
182    {
183        // wait for all cull and draw threads to complete.
184        viewer.sync();
185
186        // update the scene by traversing it with the the update visitor which will
187        // call all node update callbacks and animations.
188        viewer.update();
189         
190        // fire off the cull and draw traversals of the scene.
191        viewer.frame();
192       
193    }
194   
195    // wait for all cull and draw threads to complete before exit.
196    viewer.sync();
197
198    return 0;
199}
Note: See TracBrowser for help on using the browser.