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

Revision 4851, 9.5 kB (checked in by robert, 8 years ago)

Switched off the VBO usage in osgparametric as it was tripping up ATI and 3DLabs drivers.

  • 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
17// for the grid data..
18#include "../osghangglide/terrain_coords.h"
19
20///////////////////////////////////////////////////////////////////
21// vertex shader using just Vec4 coefficients
22char vertexShaderSource_simple[] =
23    "uniform vec4 coeff; \n"
24    "\n"
25    "void main(void) \n"
26    "{ \n"
27    "\n"
28    "    gl_TexCoord[0] = gl_Vertex; \n"
29    "    vec4 vert = gl_Vertex; \n"
30    "    vert.z = gl_Vertex.x*coeff[0] + gl_Vertex.x*gl_Vertex.x* coeff[1] + \n"
31    "             gl_Vertex.y*coeff[2] + gl_Vertex.y*gl_Vertex.y* coeff[3]; \n"
32    "    gl_Position = gl_ModelViewProjectionMatrix * vert;\n"
33    "}\n";
34 
35 
36//////////////////////////////////////////////////////////////////
37// vertex shader using full Matrix4 coefficients
38char vertexShaderSource_matrix[] =
39    "uniform vec4  origin; \n"
40    "uniform mat4  coeffMatrix; \n"
41    "\n"
42    "void main(void) \n"
43    "{ \n"
44    "\n"
45    "    gl_TexCoord[0] = gl_Vertex; \n"
46    "    vec4 v = vec4(gl_Vertex.x, gl_Vertex.x*gl_Vertex.x, gl_Vertex.y, gl_Vertex.y*gl_Vertex.y ); \n"
47    "    gl_Position = gl_ModelViewProjectionMatrix * (origin + coeffMatrix * v);\n"
48    "}\n";
49
50//////////////////////////////////////////////////////////////////
51// vertex shader using texture read
52char vertexShaderSource_texture[] =
53    "uniform sampler2D vertexTexture; \n"
54    "\n"
55    "void main(void) \n"
56    "{ \n"
57    "\n"
58    "    gl_TexCoord[0] = gl_Vertex; \n"
59    "    vec4 vert = gl_Vertex; \n"
60    "    vert.z = texture2D( vertexTexture, gl_TexCoord[0].xy).x*0.0001; \n"
61    "    gl_Position = gl_ModelViewProjectionMatrix * vert;\n"
62    "}\n";
63
64
65//////////////////////////////////////////////////////////////////
66// fragment shader
67//
68char fragmentShaderSource[] =
69    "uniform sampler2D baseTexture; \n"
70    "\n"
71    "void main(void) \n"
72    "{ \n"
73    "    gl_FragColor = texture2D( baseTexture, gl_TexCoord[0].xy); \n"
74    "}\n";
75
76
77
78class UniformVarying : public osg::Uniform::Callback
79{
80    virtual void operator () (osg::Uniform* uniform, osg::NodeVisitor* nv)
81    {
82        const osg::FrameStamp* fs = nv->getFrameStamp();
83        float value = sinf(fs->getReferenceTime());
84        uniform->set(osg::Vec4(value,-value,-value,value));
85    }
86};
87
88osg::Node* createModel(const std::string& shader, const std::string& textureFileName, const std::string& terrainFileName)
89{
90    osg::Geode* geode = new osg::Geode;
91   
92    osg::Geometry* geom = new osg::Geometry;
93    geode->addDrawable(geom);
94   
95    // dimensions for ~one million triangles :-)
96    unsigned int num_x = 708;
97    unsigned int num_y = 708;
98
99    // set up state
100    {
101   
102        osg::StateSet* stateset = geom->getOrCreateStateSet();
103
104        osg::Program* program = new osg::Program;
105        stateset->setAttribute(program);
106
107        if (shader=="simple")
108        {
109            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_simple);
110            program->addShader(vertex_shader);
111
112            osg::Uniform* coeff = new osg::Uniform("coeff",osg::Vec4(1.0,-1.0f,-1.0f,1.0f));
113            coeff->setUpdateCallback(new UniformVarying);
114            stateset->addUniform(coeff);
115        }
116        else if (shader=="matrix")
117        {
118            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_matrix);
119            program->addShader(vertex_shader);
120
121            osg::Uniform* origin = new osg::Uniform("origin",osg::Vec4(0.0f,0.0f,0.0f,1.0f));
122            stateset->addUniform(origin);
123
124            osg::Uniform* coeffMatrix = new osg::Uniform("coeffMatrix",
125                osg::Matrix(1.0f,0.0f,1.0f,0.0f,
126                            0.0f,0.0f,-1.0f,0.0f,
127                            0.0f,1.0f,-1.0f,0.0f,
128                            0.0f,0.0f,1.0f,0.0f));
129
130            stateset->addUniform(coeffMatrix);
131        }
132        else if (shader=="texture")
133        {
134            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_texture);
135            program->addShader(vertex_shader);
136
137            osg::Image* image = 0;
138
139            if (terrainFileName.empty())
140            {
141                image = new osg::Image;
142                unsigned int tx = 38;
143                unsigned int ty = 39;
144                image->allocateImage(tx,ty,1,GL_LUMINANCE,GL_FLOAT,1);
145                for(unsigned int r=0;r<ty;++r)
146                {
147                    for(unsigned int c=0;c<tx;++c)
148                    {
149                        *((float*)image->data(c,r)) = vertex[r+c*39][2]*0.1;
150                    }
151                }
152
153                num_x = tx;
154                num_y = tx;
155            }
156            else
157            {
158                image = osgDB::readImageFile(terrainFileName);
159
160                num_x = image->s();
161                num_y = image->t();
162            }
163
164            osg::Texture2D* vertexTexture = new osg::Texture2D(image);
165
166
167            vertexTexture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST);
168            vertexTexture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST);
169            vertexTexture->setInternalFormat(GL_LUMINANCE_FLOAT32_ATI);
170            stateset->setTextureAttributeAndModes(1,vertexTexture);
171
172            osg::Uniform* vertexTextureSampler = new osg::Uniform("vertexTexture",1);
173            stateset->addUniform(vertexTextureSampler);
174
175        }
176
177        osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource);
178        program->addShader(fragment_shader);
179
180        osg::Texture2D* texture = new osg::Texture2D(osgDB::readImageFile(textureFileName));
181        stateset->setTextureAttributeAndModes(0,texture);
182
183        osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0);
184        stateset->addUniform(baseTextureSampler);
185
186    }
187
188
189    // set up geometry data.
190
191    osg::Vec3Array* vertices = new osg::Vec3Array( num_x * num_y );
192   
193    float dx = 1.0f/(float)(num_x-1);
194    float dy = 1.0f/(float)(num_y-1);
195    osg::Vec3 row(0.0f,0.0f,0.0);
196   
197    unsigned int vert_no = 0;
198    unsigned int iy;
199    for(iy=0; iy<num_y; ++iy)
200    {
201        osg::Vec3 column = row;
202        for(unsigned int ix=0;ix<num_x;++ix)
203        {
204            (*vertices)[vert_no++] = column;
205            column.x() += dx;
206        }       
207        row.y() += dy;
208    }
209
210    geom->setVertexArray(vertices);
211
212    for(iy=0; iy<num_y-1; ++iy)
213    {
214        unsigned int element_no = 0;
215        osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLE_STRIP, num_x*2);
216        unsigned int index = iy * num_x;
217        for(unsigned int ix = 0; ix<num_x; ++ix)
218        {
219            (*elements)[element_no++] = index + num_x;
220            (*elements)[element_no++] = index++;
221        }
222        geom->addPrimitiveSet(elements);   
223    }
224   
225    // geom->setUseVertexBufferObjects(true);
226
227    return geode;
228}
229
230int main(int argc, char *argv[])
231{
232    // use an ArgumentParser object to manage the program arguments.
233    osg::ArgumentParser arguments(&argc,argv);
234
235    // set up the usage document, in case we need to print out how to use this program.
236    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrate support for ARB_vertex_program.");
237    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
238    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
239   
240    // construct the viewer.
241    osgProducer::Viewer viewer(arguments);
242
243    // set up the value with sensible default event handlers.
244    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
245   
246    std::string shader("simple");
247    while(arguments.read("-s",shader)) {}
248   
249    std::string textureFileName("Images/lz.rgb");
250    while(arguments.read("-t",textureFileName)) {}
251
252    std::string terrainFileName("");
253    while(arguments.read("-d",terrainFileName)) {}
254
255    // get details on keyboard and mouse bindings used by the viewer.
256    viewer.getUsage(*arguments.getApplicationUsage());
257
258    // if user request help write it out to cout.
259    if (arguments.read("-h") || arguments.read("--help"))
260    {
261        arguments.getApplicationUsage()->write(std::cout);
262        return 1;
263    }
264
265    // any option left unread are converted into errors to write out later.
266    arguments.reportRemainingOptionsAsUnrecognized();
267
268    // report any errors if they have occured when parsing the program aguments.
269    if (arguments.errors())
270    {
271        arguments.writeErrorMessages(std::cout);
272        return 1;
273    }
274
275    // load the nodes from the commandline arguments.
276    osg::Node* model = createModel(shader,textureFileName,terrainFileName);
277    if (!model)
278    {
279        return 1;
280    }
281
282    // add a viewport to the viewer and attach the scene graph.
283    viewer.setSceneData(model);
284   
285    // create the windows and run the threads.
286    viewer.realize();
287
288    while( !viewer.done() )
289    {
290        // wait for all cull and draw threads to complete.
291        viewer.sync();
292
293        // update the scene by traversing it with the the update visitor which will
294        // call all node update callbacks and animations.
295        viewer.update();
296         
297        // fire off the cull and draw traversals of the scene.
298        viewer.frame();
299       
300    }
301   
302    // wait for all cull and draw threads to complete before exit.
303    viewer.sync();
304
305    return 0;
306}
Note: See TracBrowser for help on using the browser.