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