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

Revision 10897, 10.1 kB (checked in by robert, 5 years ago)

Fixed compile warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgparametric.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osg/Vec3>
20#include <osg/Vec4>
21#include <osg/Quat>
22#include <osg/Matrix>
23#include <osg/ShapeDrawable>
24#include <osg/Geometry>
25#include <osg/Geode>
26#include <osg/Texture2D>
27
28#include <osgDB/FileUtils>
29#include <osgDB/ReadFile>
30
31#include <osgUtil/Optimizer>
32
33#include <osgViewer/Viewer>
34#include <osgViewer/ViewerEventHandlers>
35
36#include <iostream>
37
38// for the grid data..
39#include "../osghangglide/terrain_coords.h"
40
41///////////////////////////////////////////////////////////////////
42// vertex shader using just Vec4 coefficients
43char vertexShaderSource_simple[] =
44    "uniform vec4 coeff; \n"
45    "\n"
46    "void main(void) \n"
47    "{ \n"
48    "\n"
49    "    gl_TexCoord[0] = gl_Vertex; \n"
50    "    vec4 vert = gl_Vertex; \n"
51    "    vert.z = gl_Vertex.x*coeff[0] + gl_Vertex.x*gl_Vertex.x* coeff[1] + \n"
52    "             gl_Vertex.y*coeff[2] + gl_Vertex.y*gl_Vertex.y* coeff[3]; \n"
53    "    gl_Position = gl_ModelViewProjectionMatrix * vert;\n"
54    "}\n";
55 
56 
57//////////////////////////////////////////////////////////////////
58// vertex shader using full Matrix4 coefficients
59char vertexShaderSource_matrix[] =
60    "uniform vec4  origin; \n"
61    "uniform mat4  coeffMatrix; \n"
62    "\n"
63    "void main(void) \n"
64    "{ \n"
65    "\n"
66    "    gl_TexCoord[0] = gl_Vertex; \n"
67    "    vec4 v = vec4(gl_Vertex.x, gl_Vertex.x*gl_Vertex.x, gl_Vertex.y, gl_Vertex.y*gl_Vertex.y ); \n"
68    "    gl_Position = gl_ModelViewProjectionMatrix * (origin + coeffMatrix * v);\n"
69    "}\n";
70
71//////////////////////////////////////////////////////////////////
72// vertex shader using texture read
73char vertexShaderSource_texture[] =
74    "uniform sampler2D vertexTexture; \n"
75    "\n"
76    "void main(void) \n"
77    "{ \n"
78    "\n"
79    "    gl_TexCoord[0] = gl_Vertex; \n"
80    "    vec4 vert = gl_Vertex; \n"
81    "    vert.z = texture2D( vertexTexture, gl_TexCoord[0].xy).x*0.0001; \n"
82    "    gl_Position = gl_ModelViewProjectionMatrix * vert;\n"
83    "}\n";
84
85
86//////////////////////////////////////////////////////////////////
87// fragment shader
88//
89char fragmentShaderSource[] =
90    "uniform sampler2D baseTexture; \n"
91    "\n"
92    "void main(void) \n"
93    "{ \n"
94    "    gl_FragColor = texture2D( baseTexture, gl_TexCoord[0].xy); \n"
95    "}\n";
96
97
98
99class UniformVarying : public osg::Uniform::Callback
100{
101    virtual void operator () (osg::Uniform* uniform, osg::NodeVisitor* nv)
102    {
103        const osg::FrameStamp* fs = nv->getFrameStamp();
104        float value = sinf(fs->getSimulationTime());
105        uniform->set(osg::Vec4(value,-value,-value,value));
106    }
107};
108
109osg::Node* createModel(const std::string& shader, const std::string& textureFileName, const std::string& terrainFileName, bool dynamic, bool useVBO)
110{
111    osg::Geode* geode = new osg::Geode;
112   
113    osg::Geometry* geom = new osg::Geometry;
114    geode->addDrawable(geom);
115   
116    // dimensions for ~one million triangles :-)
117    unsigned int num_x = 708;
118    unsigned int num_y = 708;
119
120    // set up state
121    {
122   
123        osg::StateSet* stateset = geom->getOrCreateStateSet();
124
125        osg::Program* program = new osg::Program;
126        stateset->setAttribute(program);
127
128        if (shader=="simple")
129        {
130            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_simple);
131            program->addShader(vertex_shader);
132
133            osg::Uniform* coeff = new osg::Uniform("coeff",osg::Vec4(1.0,-1.0f,-1.0f,1.0f));
134           
135            stateset->addUniform(coeff);
136
137            if (dynamic)
138            {
139                coeff->setUpdateCallback(new UniformVarying);
140                coeff->setDataVariance(osg::Object::DYNAMIC);
141                stateset->setDataVariance(osg::Object::DYNAMIC);
142            }
143           
144        }
145        else if (shader=="matrix")
146        {
147            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_matrix);
148            program->addShader(vertex_shader);
149
150            osg::Uniform* origin = new osg::Uniform("origin",osg::Vec4(0.0f,0.0f,0.0f,1.0f));
151            stateset->addUniform(origin);
152
153            osg::Uniform* coeffMatrix = new osg::Uniform("coeffMatrix",
154                osg::Matrix(1.0f,0.0f,1.0f,0.0f,
155                            0.0f,0.0f,-1.0f,0.0f,
156                            0.0f,1.0f,-1.0f,0.0f,
157                            0.0f,0.0f,1.0f,0.0f));
158
159            stateset->addUniform(coeffMatrix);
160        }
161        else if (shader=="texture")
162        {
163            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_texture);
164            program->addShader(vertex_shader);
165
166            osg::Image* image = 0;
167
168            if (terrainFileName.empty())
169            {
170                image = new osg::Image;
171                unsigned int tx = 38;
172                unsigned int ty = 39;
173                image->allocateImage(tx,ty,1,GL_LUMINANCE,GL_FLOAT,1);
174                for(unsigned int r=0;r<ty;++r)
175                {
176                    for(unsigned int c=0;c<tx;++c)
177                    {
178                        *((float*)image->data(c,r)) = vertex[r+c*39][2]*0.1;
179                    }
180                }
181
182                num_x = tx;
183                num_y = tx;
184            }
185            else
186            {
187                image = osgDB::readImageFile(terrainFileName);
188
189                num_x = image->s();
190                num_y = image->t();
191            }
192
193            osg::Texture2D* vertexTexture = new osg::Texture2D(image);
194
195
196            vertexTexture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST);
197            vertexTexture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST);
198            vertexTexture->setInternalFormat(GL_LUMINANCE_FLOAT32_ATI);
199            stateset->setTextureAttributeAndModes(1,vertexTexture);
200
201            osg::Uniform* vertexTextureSampler = new osg::Uniform("vertexTexture",1);
202            stateset->addUniform(vertexTextureSampler);
203
204        }
205
206        osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource);
207        program->addShader(fragment_shader);
208
209        osg::Texture2D* texture = new osg::Texture2D(osgDB::readImageFile(textureFileName));
210        stateset->setTextureAttributeAndModes(0,texture);
211
212        osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0);
213        stateset->addUniform(baseTextureSampler);
214
215    }
216
217
218    // set up geometry data.
219
220    osg::Vec3Array* vertices = new osg::Vec3Array( num_x * num_y );
221   
222    float dx = 1.0f/(float)(num_x-1);
223    float dy = 1.0f/(float)(num_y-1);
224    osg::Vec3 row(0.0f,0.0f,0.0);
225   
226    unsigned int vert_no = 0;
227    unsigned int iy;
228    for(iy=0; iy<num_y; ++iy)
229    {
230        osg::Vec3 column = row;
231        for(unsigned int ix=0;ix<num_x;++ix)
232        {
233            (*vertices)[vert_no++] = column;
234            column.x() += dx;
235        }       
236        row.y() += dy;
237    }
238
239    geom->setVertexArray(vertices);
240
241    osg::VertexBufferObject* vbo = useVBO ? new osg::VertexBufferObject : 0;
242    if (vbo) vertices->setVertexBufferObject(vbo);
243   
244    osg::ElementBufferObject* ebo = useVBO ? new osg::ElementBufferObject : 0;
245
246    for(iy=0; iy<num_y-1; ++iy)
247    {
248        unsigned int element_no = 0;
249        osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLE_STRIP, num_x*2);
250        unsigned int index = iy * num_x;
251        for(unsigned int ix = 0; ix<num_x; ++ix)
252        {
253            (*elements)[element_no++] = index + num_x;
254            (*elements)[element_no++] = index++;
255        }
256        geom->addPrimitiveSet(elements);
257       
258        if (ebo) elements->setElementBufferObject(ebo);
259    }
260   
261    geom->setUseVertexBufferObjects(useVBO);
262
263    return geode;
264}
265
266int main(int argc, char *argv[])
267{
268    // use an ArgumentParser object to manage the program arguments.
269    osg::ArgumentParser arguments(&argc,argv);
270
271    // set up the usage document, in case we need to print out how to use this program.
272    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrate support for ARB_vertex_program.");
273    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
274    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
275   
276    // construct the viewer.
277    osgViewer::Viewer viewer;
278
279    // add the stats handler
280    viewer.addEventHandler(new osgViewer::StatsHandler);
281
282    std::string shader("simple");
283    while(arguments.read("-s",shader)) {}
284   
285    std::string textureFileName("Images/lz.rgb");
286    while(arguments.read("-t",textureFileName)) {}
287
288    std::string terrainFileName("");
289    while(arguments.read("-d",terrainFileName)) {}
290
291    bool dynamic = true;
292    while(arguments.read("--static")) { dynamic = false; }
293
294    bool vbo = false;
295    while(arguments.read("--vbo")) { vbo = true; }
296
297    // if user request help write it out to cout.
298    if (arguments.read("-h") || arguments.read("--help"))
299    {
300        arguments.getApplicationUsage()->write(std::cout);
301        return 1;
302    }
303   
304    // load the nodes from the commandline arguments.
305    osg::Node* model = createModel(shader,textureFileName,terrainFileName, dynamic, vbo);
306    if (!model)
307    {
308        return 1;
309    }
310   
311    viewer.setSceneData(model);
312
313    return viewer.run();
314}
Note: See TracBrowser for help on using the browser.