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

Revision 6108, 8.4 kB (checked in by robert, 8 years ago)

Set the DataVariance? to DYNAIMC for the StateSet? of with the dynamically updating Uniform

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