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

Revision 6864, 9.1 kB (checked in by robert, 8 years ago)

Fixed includes

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