| 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 | #include "../osghangglide/terrain_coords.h" |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | char vertexShaderSource_simple[] = |
|---|
| 23 | "uniform vec4 coeff; \n" |
|---|
| 24 | "\n" |
|---|
| 25 | "void main(void) \n" |
|---|
| 26 | "{ \n" |
|---|
| 27 | "\n" |
|---|
| 28 | " gl_TexCoord[0] = gl_Vertex; \n" |
|---|
| 29 | " vec4 vert = gl_Vertex; \n" |
|---|
| 30 | " vert.z = gl_Vertex.x*coeff[0] + gl_Vertex.x*gl_Vertex.x* coeff[1] + \n" |
|---|
| 31 | " gl_Vertex.y*coeff[2] + gl_Vertex.y*gl_Vertex.y* coeff[3]; \n" |
|---|
| 32 | " gl_Position = gl_ModelViewProjectionMatrix * vert;\n" |
|---|
| 33 | "}\n"; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | char vertexShaderSource_matrix[] = |
|---|
| 39 | "uniform vec4 origin; \n" |
|---|
| 40 | "uniform mat4 coeffMatrix; \n" |
|---|
| 41 | "\n" |
|---|
| 42 | "void main(void) \n" |
|---|
| 43 | "{ \n" |
|---|
| 44 | "\n" |
|---|
| 45 | " gl_TexCoord[0] = gl_Vertex; \n" |
|---|
| 46 | " vec4 v = vec4(gl_Vertex.x, gl_Vertex.x*gl_Vertex.x, gl_Vertex.y, gl_Vertex.y*gl_Vertex.y ); \n" |
|---|
| 47 | " gl_Position = gl_ModelViewProjectionMatrix * (origin + coeffMatrix * v);\n" |
|---|
| 48 | "}\n"; |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | char vertexShaderSource_texture[] = |
|---|
| 53 | "uniform sampler2D vertexTexture; \n" |
|---|
| 54 | "\n" |
|---|
| 55 | "void main(void) \n" |
|---|
| 56 | "{ \n" |
|---|
| 57 | "\n" |
|---|
| 58 | " gl_TexCoord[0] = gl_Vertex; \n" |
|---|
| 59 | " vec4 vert = gl_Vertex; \n" |
|---|
| 60 | " vert.z = texture2D( vertexTexture, gl_TexCoord[0].xy).x*0.0001; \n" |
|---|
| 61 | " gl_Position = gl_ModelViewProjectionMatrix * vert;\n" |
|---|
| 62 | "}\n"; |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | char fragmentShaderSource[] = |
|---|
| 69 | "uniform sampler2D baseTexture; \n" |
|---|
| 70 | "\n" |
|---|
| 71 | "void main(void) \n" |
|---|
| 72 | "{ \n" |
|---|
| 73 | " gl_FragColor = texture2D( baseTexture, gl_TexCoord[0].xy); \n" |
|---|
| 74 | "}\n"; |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | class UniformVarying : public osg::Uniform::Callback |
|---|
| 79 | { |
|---|
| 80 | virtual void operator () (osg::Uniform* uniform, osg::NodeVisitor* nv) |
|---|
| 81 | { |
|---|
| 82 | const osg::FrameStamp* fs = nv->getFrameStamp(); |
|---|
| 83 | float value = sinf(fs->getReferenceTime()); |
|---|
| 84 | uniform->set(osg::Vec4(value,-value,-value,value)); |
|---|
| 85 | } |
|---|
| 86 | }; |
|---|
| 87 | |
|---|
| 88 | osg::Node* createModel(const std::string& shader, const std::string& textureFileName, const std::string& terrainFileName) |
|---|
| 89 | { |
|---|
| 90 | osg::Geode* geode = new osg::Geode; |
|---|
| 91 | |
|---|
| 92 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 93 | geode->addDrawable(geom); |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | unsigned int num_x = 708; |
|---|
| 97 | unsigned int num_y = 708; |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | { |
|---|
| 101 | |
|---|
| 102 | osg::StateSet* stateset = geom->getOrCreateStateSet(); |
|---|
| 103 | |
|---|
| 104 | osg::Program* program = new osg::Program; |
|---|
| 105 | stateset->setAttribute(program); |
|---|
| 106 | |
|---|
| 107 | if (shader=="simple") |
|---|
| 108 | { |
|---|
| 109 | osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_simple); |
|---|
| 110 | program->addShader(vertex_shader); |
|---|
| 111 | |
|---|
| 112 | osg::Uniform* coeff = new osg::Uniform("coeff",osg::Vec4(1.0,-1.0f,-1.0f,1.0f)); |
|---|
| 113 | coeff->setUpdateCallback(new UniformVarying); |
|---|
| 114 | stateset->addUniform(coeff); |
|---|
| 115 | } |
|---|
| 116 | else if (shader=="matrix") |
|---|
| 117 | { |
|---|
| 118 | osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_matrix); |
|---|
| 119 | program->addShader(vertex_shader); |
|---|
| 120 | |
|---|
| 121 | osg::Uniform* origin = new osg::Uniform("origin",osg::Vec4(0.0f,0.0f,0.0f,1.0f)); |
|---|
| 122 | stateset->addUniform(origin); |
|---|
| 123 | |
|---|
| 124 | osg::Uniform* coeffMatrix = new osg::Uniform("coeffMatrix", |
|---|
| 125 | osg::Matrix(1.0f,0.0f,1.0f,0.0f, |
|---|
| 126 | 0.0f,0.0f,-1.0f,0.0f, |
|---|
| 127 | 0.0f,1.0f,-1.0f,0.0f, |
|---|
| 128 | 0.0f,0.0f,1.0f,0.0f)); |
|---|
| 129 | |
|---|
| 130 | stateset->addUniform(coeffMatrix); |
|---|
| 131 | } |
|---|
| 132 | else if (shader=="texture") |
|---|
| 133 | { |
|---|
| 134 | osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_texture); |
|---|
| 135 | program->addShader(vertex_shader); |
|---|
| 136 | |
|---|
| 137 | osg::Image* image = 0; |
|---|
| 138 | |
|---|
| 139 | if (terrainFileName.empty()) |
|---|
| 140 | { |
|---|
| 141 | image = new osg::Image; |
|---|
| 142 | unsigned int tx = 38; |
|---|
| 143 | unsigned int ty = 39; |
|---|
| 144 | image->allocateImage(tx,ty,1,GL_LUMINANCE,GL_FLOAT,1); |
|---|
| 145 | for(unsigned int r=0;r<ty;++r) |
|---|
| 146 | { |
|---|
| 147 | for(unsigned int c=0;c<tx;++c) |
|---|
| 148 | { |
|---|
| 149 | *((float*)image->data(c,r)) = vertex[r+c*39][2]*0.1; |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | num_x = tx; |
|---|
| 154 | num_y = tx; |
|---|
| 155 | } |
|---|
| 156 | else |
|---|
| 157 | { |
|---|
| 158 | image = osgDB::readImageFile(terrainFileName); |
|---|
| 159 | |
|---|
| 160 | num_x = image->s(); |
|---|
| 161 | num_y = image->t(); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | osg::Texture2D* vertexTexture = new osg::Texture2D(image); |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | vertexTexture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST); |
|---|
| 168 | vertexTexture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST); |
|---|
| 169 | vertexTexture->setInternalFormat(GL_LUMINANCE_FLOAT32_ATI); |
|---|
| 170 | stateset->setTextureAttributeAndModes(1,vertexTexture); |
|---|
| 171 | |
|---|
| 172 | osg::Uniform* vertexTextureSampler = new osg::Uniform("vertexTexture",1); |
|---|
| 173 | stateset->addUniform(vertexTextureSampler); |
|---|
| 174 | |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource); |
|---|
| 178 | program->addShader(fragment_shader); |
|---|
| 179 | |
|---|
| 180 | osg::Texture2D* texture = new osg::Texture2D(osgDB::readImageFile(textureFileName)); |
|---|
| 181 | stateset->setTextureAttributeAndModes(0,texture); |
|---|
| 182 | |
|---|
| 183 | osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0); |
|---|
| 184 | stateset->addUniform(baseTextureSampler); |
|---|
| 185 | |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | osg::Vec3Array* vertices = new osg::Vec3Array( num_x * num_y ); |
|---|
| 192 | |
|---|
| 193 | float dx = 1.0f/(float)(num_x-1); |
|---|
| 194 | float dy = 1.0f/(float)(num_y-1); |
|---|
| 195 | osg::Vec3 row(0.0f,0.0f,0.0); |
|---|
| 196 | |
|---|
| 197 | unsigned int vert_no = 0; |
|---|
| 198 | for(unsigned int iy=0; iy<num_y; ++iy) |
|---|
| 199 | { |
|---|
| 200 | osg::Vec3 column = row; |
|---|
| 201 | for(unsigned int ix=0;ix<num_x;++ix) |
|---|
| 202 | { |
|---|
| 203 | (*vertices)[vert_no++] = column; |
|---|
| 204 | column.x() += dx; |
|---|
| 205 | } |
|---|
| 206 | row.y() += dy; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | geom->setVertexArray(vertices); |
|---|
| 210 | |
|---|
| 211 | for(unsigned int iy=0; iy<num_y-1; ++iy) |
|---|
| 212 | { |
|---|
| 213 | unsigned int element_no = 0; |
|---|
| 214 | osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLE_STRIP, num_x*2); |
|---|
| 215 | unsigned int index = iy * num_x; |
|---|
| 216 | for(unsigned int ix = 0; ix<num_x; ++ix) |
|---|
| 217 | { |
|---|
| 218 | (*elements)[element_no++] = index + num_x; |
|---|
| 219 | (*elements)[element_no++] = index++; |
|---|
| 220 | } |
|---|
| 221 | geom->addPrimitiveSet(elements); |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | geom->setUseVertexBufferObjects(true); |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | return geode; |
|---|
| 230 | |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | int main(int argc, char *argv[]) |
|---|
| 234 | { |
|---|
| 235 | |
|---|
| 236 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 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 | |
|---|
| 244 | osgProducer::Viewer viewer(arguments); |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 248 | |
|---|
| 249 | std::string shader("simple"); |
|---|
| 250 | while(arguments.read("-s",shader)) {} |
|---|
| 251 | |
|---|
| 252 | std::string textureFileName("Images/lz.rgb"); |
|---|
| 253 | while(arguments.read("-t",textureFileName)) {} |
|---|
| 254 | |
|---|
| 255 | std::string terrainFileName(""); |
|---|
| 256 | while(arguments.read("-d",terrainFileName)) {} |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 263 | { |
|---|
| 264 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 265 | return 1; |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | if (arguments.errors()) |
|---|
| 273 | { |
|---|
| 274 | arguments.writeErrorMessages(std::cout); |
|---|
| 275 | return 1; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | osg::Node* model = createModel(shader,textureFileName,terrainFileName); |
|---|
| 280 | if (!model) |
|---|
| 281 | { |
|---|
| 282 | return 1; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | viewer.setSceneData(model); |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | viewer.realize(); |
|---|
| 290 | |
|---|
| 291 | while( !viewer.done() ) |
|---|
| 292 | { |
|---|
| 293 | |
|---|
| 294 | viewer.sync(); |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | viewer.update(); |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | viewer.frame(); |
|---|
| 302 | |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | viewer.sync(); |
|---|
| 307 | |
|---|
| 308 | return 0; |
|---|
| 309 | } |
|---|