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

Revision 6051, 8.3 kB (checked in by robert, 8 years ago)

Added osg::FrameStamp::set/getSimulationTime().

Added setting of osg_SimulationTime and osg_DeltaSimulationTime to the uniforms set by SceneView?

Added frame(double simulationTime) and advance(double simulationTime) parameters to
osgViewer::SimpleViewer?, Vewer and CompositeViewer?.

Updated various examples and Nodes to use SimulationTime? where appropriate.

  • 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        }
118        else if (shader=="matrix")
119        {
120            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_matrix);
121            program->addShader(vertex_shader);
122
123            osg::Uniform* origin = new osg::Uniform("origin",osg::Vec4(0.0f,0.0f,0.0f,1.0f));
124            stateset->addUniform(origin);
125
126            osg::Uniform* coeffMatrix = new osg::Uniform("coeffMatrix",
127                osg::Matrix(1.0f,0.0f,1.0f,0.0f,
128                            0.0f,0.0f,-1.0f,0.0f,
129                            0.0f,1.0f,-1.0f,0.0f,
130                            0.0f,0.0f,1.0f,0.0f));
131
132            stateset->addUniform(coeffMatrix);
133        }
134        else if (shader=="texture")
135        {
136            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_texture);
137            program->addShader(vertex_shader);
138
139            osg::Image* image = 0;
140
141            if (terrainFileName.empty())
142            {
143                image = new osg::Image;
144                unsigned int tx = 38;
145                unsigned int ty = 39;
146                image->allocateImage(tx,ty,1,GL_LUMINANCE,GL_FLOAT,1);
147                for(unsigned int r=0;r<ty;++r)
148                {
149                    for(unsigned int c=0;c<tx;++c)
150                    {
151                        *((float*)image->data(c,r)) = vertex[r+c*39][2]*0.1;
152                    }
153                }
154
155                num_x = tx;
156                num_y = tx;
157            }
158            else
159            {
160                image = osgDB::readImageFile(terrainFileName);
161
162                num_x = image->s();
163                num_y = image->t();
164            }
165
166            osg::Texture2D* vertexTexture = new osg::Texture2D(image);
167
168
169            vertexTexture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST);
170            vertexTexture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST);
171            vertexTexture->setInternalFormat(GL_LUMINANCE_FLOAT32_ATI);
172            stateset->setTextureAttributeAndModes(1,vertexTexture);
173
174            osg::Uniform* vertexTextureSampler = new osg::Uniform("vertexTexture",1);
175            stateset->addUniform(vertexTextureSampler);
176
177        }
178
179        osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource);
180        program->addShader(fragment_shader);
181
182        osg::Texture2D* texture = new osg::Texture2D(osgDB::readImageFile(textureFileName));
183        stateset->setTextureAttributeAndModes(0,texture);
184
185        osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0);
186        stateset->addUniform(baseTextureSampler);
187
188    }
189
190
191    // set up geometry data.
192
193    osg::Vec3Array* vertices = new osg::Vec3Array( num_x * num_y );
194   
195    float dx = 1.0f/(float)(num_x-1);
196    float dy = 1.0f/(float)(num_y-1);
197    osg::Vec3 row(0.0f,0.0f,0.0);
198   
199    unsigned int vert_no = 0;
200    unsigned int iy;
201    for(iy=0; iy<num_y; ++iy)
202    {
203        osg::Vec3 column = row;
204        for(unsigned int ix=0;ix<num_x;++ix)
205        {
206            (*vertices)[vert_no++] = column;
207            column.x() += dx;
208        }       
209        row.y() += dy;
210    }
211
212    geom->setVertexArray(vertices);
213
214    for(iy=0; iy<num_y-1; ++iy)
215    {
216        unsigned int element_no = 0;
217        osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLE_STRIP, num_x*2);
218        unsigned int index = iy * num_x;
219        for(unsigned int ix = 0; ix<num_x; ++ix)
220        {
221            (*elements)[element_no++] = index + num_x;
222            (*elements)[element_no++] = index++;
223        }
224        geom->addPrimitiveSet(elements);   
225    }
226   
227    // geom->setUseVertexBufferObjects(true);
228
229    return geode;
230}
231
232int main(int argc, char *argv[])
233{
234    // use an ArgumentParser object to manage the program arguments.
235    osg::ArgumentParser arguments(&argc,argv);
236
237    // set up the usage document, in case we need to print out how to use this program.
238    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrate support for ARB_vertex_program.");
239    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
240    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
241   
242    // construct the viewer.
243    osgViewer::Viewer viewer;
244
245    std::string shader("simple");
246    while(arguments.read("-s",shader)) {}
247   
248    std::string textureFileName("Images/lz.rgb");
249    while(arguments.read("-t",textureFileName)) {}
250
251    std::string terrainFileName("");
252    while(arguments.read("-d",terrainFileName)) {}
253
254    // if user request help write it out to cout.
255    if (arguments.read("-h") || arguments.read("--help"))
256    {
257        arguments.getApplicationUsage()->write(std::cout);
258        return 1;
259    }
260
261    // load the nodes from the commandline arguments.
262    osg::Node* model = createModel(shader,textureFileName,terrainFileName);
263    if (!model)
264    {
265        return 1;
266    }
267   
268    viewer.setSceneData(model);
269
270    return viewer.run();
271}
Note: See TracBrowser for help on using the browser.