| 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 | char vertexShaderSource_simple[] = |
|---|
| 18 | "uniform vec4 coeff; \n" |
|---|
| 19 | "\n" |
|---|
| 20 | "void main(void) \n" |
|---|
| 21 | "{ \n" |
|---|
| 22 | "\n" |
|---|
| 23 | " gl_TexCoord[0] = gl_Vertex; \n" |
|---|
| 24 | " gl_Vertex.z = gl_Vertex.x*coeff[0] + gl_Vertex.x*gl_Vertex.x* coeff[1] + \n" |
|---|
| 25 | " gl_Vertex.y*coeff[2] + gl_Vertex.y*gl_Vertex.y* coeff[3]; \n" |
|---|
| 26 | " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" |
|---|
| 27 | "}\n"; |
|---|
| 28 | |
|---|
| 29 | char fragmentShaderSource[] = |
|---|
| 30 | "uniform sampler2D baseTexture; \n" |
|---|
| 31 | "\n" |
|---|
| 32 | "void main(void) \n" |
|---|
| 33 | "{ \n" |
|---|
| 34 | " gl_FragColor = texture2D( baseTexture, gl_TexCoord[0].xy); \n" |
|---|
| 35 | "}\n"; |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | class UniformVarying : public osg::Uniform::Callback |
|---|
| 39 | { |
|---|
| 40 | virtual void operator () (osg::Uniform* uniform, osg::NodeVisitor* nv) |
|---|
| 41 | { |
|---|
| 42 | const osg::FrameStamp* fs = nv->getFrameStamp(); |
|---|
| 43 | float value = sinf(fs->getReferenceTime()); |
|---|
| 44 | uniform->set(osg::Vec4(value,-value,-value,value)); |
|---|
| 45 | } |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | osg::Node* createModel() |
|---|
| 49 | { |
|---|
| 50 | osg::Geode* geode = new osg::Geode; |
|---|
| 51 | |
|---|
| 52 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 53 | geode->addDrawable(geom); |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | unsigned int num_x = 708; |
|---|
| 57 | unsigned int num_y = 708; |
|---|
| 58 | |
|---|
| 59 | osg::Vec3Array* vertices = new osg::Vec3Array( num_x * num_y ); |
|---|
| 60 | |
|---|
| 61 | float dx = 1.0f/(float)(num_x-1); |
|---|
| 62 | float dy = 1.0f/(float)(num_y-1); |
|---|
| 63 | osg::Vec3 row(0.0f,0.0f,0.0); |
|---|
| 64 | |
|---|
| 65 | unsigned int vert_no = 0; |
|---|
| 66 | for(unsigned int iy=0; iy<num_y; ++iy) |
|---|
| 67 | { |
|---|
| 68 | osg::Vec3 column = row; |
|---|
| 69 | for(unsigned int ix=0;ix<num_x;++ix) |
|---|
| 70 | { |
|---|
| 71 | (*vertices)[vert_no++] = column; |
|---|
| 72 | column.x() += dx; |
|---|
| 73 | } |
|---|
| 74 | row.y() += dy; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | geom->setVertexArray(vertices); |
|---|
| 78 | |
|---|
| 79 | for(unsigned int iy=0; iy<num_y-1; ++iy) |
|---|
| 80 | { |
|---|
| 81 | unsigned int element_no = 0; |
|---|
| 82 | osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLE_STRIP, num_x*2); |
|---|
| 83 | unsigned int index = iy * num_x; |
|---|
| 84 | for(unsigned int ix = 0; ix<num_x; ++ix) |
|---|
| 85 | { |
|---|
| 86 | (*elements)[element_no++] = index + num_x; |
|---|
| 87 | (*elements)[element_no++] = index++; |
|---|
| 88 | } |
|---|
| 89 | geom->addPrimitiveSet(elements); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | geom->setUseVertexBufferObjects(true); |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | osg::StateSet* stateset = geom->getOrCreateStateSet(); |
|---|
| 96 | |
|---|
| 97 | osg::Program* program = new osg::Program; |
|---|
| 98 | stateset->setAttribute(program); |
|---|
| 99 | |
|---|
| 100 | osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_simple); |
|---|
| 101 | program->addShader(vertex_shader); |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource); |
|---|
| 105 | program->addShader(fragment_shader); |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | osg::Uniform* xCoeff = new osg::Uniform("coeff",osg::Vec4(1.0,-1.0f,-1.0f,1.0f)); |
|---|
| 109 | xCoeff->setUpdateCallback(new UniformVarying); |
|---|
| 110 | stateset->addUniform(xCoeff); |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | osg::Texture2D* texture = new osg::Texture2D(osgDB::readImageFile("lz.rgb")); |
|---|
| 114 | texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST); |
|---|
| 115 | texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST); |
|---|
| 116 | stateset->setTextureAttributeAndModes(0,texture); |
|---|
| 117 | |
|---|
| 118 | osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0); |
|---|
| 119 | stateset->addUniform(baseTextureSampler); |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | return geode; |
|---|
| 123 | |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | int main(int argc, char *argv[]) |
|---|
| 127 | { |
|---|
| 128 | |
|---|
| 129 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrate support for ARB_vertex_program."); |
|---|
| 133 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 134 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | osgProducer::Viewer viewer(arguments); |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 147 | { |
|---|
| 148 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 149 | return 1; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | if (arguments.errors()) |
|---|
| 157 | { |
|---|
| 158 | arguments.writeErrorMessages(std::cout); |
|---|
| 159 | return 1; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | osg::Node* model = createModel(); |
|---|
| 164 | if (!model) |
|---|
| 165 | { |
|---|
| 166 | return 1; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | viewer.setSceneData(model); |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | viewer.realize(); |
|---|
| 174 | |
|---|
| 175 | while( !viewer.done() ) |
|---|
| 176 | { |
|---|
| 177 | |
|---|
| 178 | viewer.sync(); |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | viewer.update(); |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | viewer.frame(); |
|---|
| 186 | |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | viewer.sync(); |
|---|
| 191 | |
|---|
| 192 | return 0; |
|---|
| 193 | } |
|---|