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