| 1 | #include <osg/AlphaFunc> |
|---|
| 2 | #include <osg/Billboard> |
|---|
| 3 | #include <osg/BlendFunc> |
|---|
| 4 | #include <osg/Depth> |
|---|
| 5 | #include <osg/Geode> |
|---|
| 6 | #include <osg/Geometry> |
|---|
| 7 | #include <osg/GL2Extensions> |
|---|
| 8 | #include <osg/Material> |
|---|
| 9 | #include <osg/Math> |
|---|
| 10 | #include <osg/MatrixTransform> |
|---|
| 11 | #include <osg/PolygonOffset> |
|---|
| 12 | #include <osg/Program> |
|---|
| 13 | #include <osg/Projection> |
|---|
| 14 | #include <osg/Shader> |
|---|
| 15 | #include <osg/ShapeDrawable> |
|---|
| 16 | #include <osg/StateSet> |
|---|
| 17 | #include <osg/Switch> |
|---|
| 18 | #include <osg/Texture2D> |
|---|
| 19 | #include <osg/Uniform> |
|---|
| 20 | |
|---|
| 21 | #include <osgDB/ReadFile> |
|---|
| 22 | #include <osgDB/FileUtils> |
|---|
| 23 | |
|---|
| 24 | #include <osgUtil/IntersectVisitor> |
|---|
| 25 | #include <osgUtil/SmoothingVisitor> |
|---|
| 26 | |
|---|
| 27 | #include <osgText/Text> |
|---|
| 28 | |
|---|
| 29 | #include <osgViewer/Viewer> |
|---|
| 30 | |
|---|
| 31 | #include <iostream> |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | #include "../osghangglide/terrain_coords.h" |
|---|
| 35 | |
|---|
| 36 | osg::Node* createScene() |
|---|
| 37 | { |
|---|
| 38 | osg::Group* scene = new osg::Group; |
|---|
| 39 | |
|---|
| 40 | unsigned int numColumns = 38; |
|---|
| 41 | unsigned int numRows = 39; |
|---|
| 42 | unsigned int r; |
|---|
| 43 | unsigned int c; |
|---|
| 44 | |
|---|
| 45 | osg::Vec3 origin(0.0f,0.0f,0.0f); |
|---|
| 46 | osg::Vec3 size(1000.0f,1000.0f,250.0f); |
|---|
| 47 | osg::Vec3 scaleDown(1.0f/size.x(),1.0f/size.y(),1.0f/size.z()); |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | osg::StateSet* stateset = new osg::StateSet(); |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | osg::Uniform* originUniform = new osg::Uniform("terrainOrigin",origin); |
|---|
| 56 | stateset->addUniform(originUniform); |
|---|
| 57 | |
|---|
| 58 | osg::Uniform* sizeUniform = new osg::Uniform("terrainSize",size); |
|---|
| 59 | stateset->addUniform(sizeUniform); |
|---|
| 60 | |
|---|
| 61 | osg::Uniform* scaleDownUniform = new osg::Uniform("terrainScaleDown",scaleDown); |
|---|
| 62 | stateset->addUniform(scaleDownUniform); |
|---|
| 63 | |
|---|
| 64 | osg::Uniform* terrainTextureSampler = new osg::Uniform("terrainTexture",0); |
|---|
| 65 | stateset->addUniform(terrainTextureSampler); |
|---|
| 66 | |
|---|
| 67 | osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",1); |
|---|
| 68 | stateset->addUniform(baseTextureSampler); |
|---|
| 69 | |
|---|
| 70 | osg::Uniform* treeTextureSampler = new osg::Uniform("treeTexture",1); |
|---|
| 71 | stateset->addUniform(treeTextureSampler); |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | float min_z = FLT_MAX; |
|---|
| 76 | float max_z = -FLT_MAX; |
|---|
| 77 | for(r=0;r<numRows;++r) |
|---|
| 78 | { |
|---|
| 79 | for(c=0;c<numColumns;++c) |
|---|
| 80 | { |
|---|
| 81 | min_z = osg::minimum(min_z,vertex[r+c*numRows][2]); |
|---|
| 82 | max_z = osg::maximum(max_z,vertex[r+c*numRows][2]); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | float scale_z = size.z()/(max_z-min_z); |
|---|
| 87 | |
|---|
| 88 | osg::Image* terrainImage = new osg::Image; |
|---|
| 89 | terrainImage->allocateImage(numColumns,numRows,1,GL_LUMINANCE, GL_FLOAT); |
|---|
| 90 | terrainImage->setInternalTextureFormat(GL_LUMINANCE_FLOAT32_ATI); |
|---|
| 91 | for(r=0;r<numRows;++r) |
|---|
| 92 | { |
|---|
| 93 | for(c=0;c<numColumns;++c) |
|---|
| 94 | { |
|---|
| 95 | *((float*)(terrainImage->data(c,r))) = (vertex[r+c*numRows][2]-min_z)*scale_z; |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | osg::Texture2D* terrainTexture = new osg::Texture2D; |
|---|
| 100 | terrainTexture->setImage(terrainImage); |
|---|
| 101 | terrainTexture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST); |
|---|
| 102 | terrainTexture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST); |
|---|
| 103 | terrainTexture->setResizeNonPowerOfTwoHint(false); |
|---|
| 104 | stateset->setTextureAttributeAndModes(0,terrainTexture,osg::StateAttribute::ON); |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | osg::Image* image = osgDB::readImageFile("Images/lz.rgb"); |
|---|
| 108 | if (image) |
|---|
| 109 | { |
|---|
| 110 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 111 | |
|---|
| 112 | texture->setImage(image); |
|---|
| 113 | stateset->setTextureAttributeAndModes(1,texture,osg::StateAttribute::ON); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | { |
|---|
| 117 | std::cout<<"Creating terrain..."; |
|---|
| 118 | |
|---|
| 119 | osg::Geode* geode = new osg::Geode(); |
|---|
| 120 | geode->setStateSet( stateset ); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | { |
|---|
| 124 | osg::Program* program = new osg::Program; |
|---|
| 125 | stateset->setAttribute(program); |
|---|
| 126 | |
|---|
| 127 | #if 1 |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | char vertexShaderSource[] = |
|---|
| 133 | "uniform float osg_FrameTime;\n" |
|---|
| 134 | "uniform sampler2D terrainTexture;\n" |
|---|
| 135 | "uniform vec3 terrainOrigin;\n" |
|---|
| 136 | "uniform vec3 terrainScaleDown;\n" |
|---|
| 137 | "\n" |
|---|
| 138 | "varying vec2 texcoord;\n" |
|---|
| 139 | "\n" |
|---|
| 140 | "void main(void)\n" |
|---|
| 141 | "{\n" |
|---|
| 142 | " texcoord = gl_Vertex.xy - terrainOrigin.xy;\n" |
|---|
| 143 | " texcoord.x *= terrainScaleDown.x;\n" |
|---|
| 144 | " texcoord.y *= terrainScaleDown.y;\n" |
|---|
| 145 | "\n" |
|---|
| 146 | " vec4 position;\n" |
|---|
| 147 | " position.x = gl_Vertex.x;\n" |
|---|
| 148 | " position.y = gl_Vertex.y;\n" |
|---|
| 149 | " position.z = texture2D(terrainTexture, texcoord).r;\n" |
|---|
| 150 | " position.w = 1.0;\n" |
|---|
| 151 | " \n" |
|---|
| 152 | " gl_Position = gl_ModelViewProjectionMatrix * position;\n" |
|---|
| 153 | " gl_FrontColor = vec4(1.0,1.0,1.0,1.0);\n" |
|---|
| 154 | "}\n"; |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | char fragmentShaderSource[] = |
|---|
| 160 | "uniform sampler2D baseTexture; \n" |
|---|
| 161 | "varying vec2 texcoord;\n" |
|---|
| 162 | "\n" |
|---|
| 163 | "void main(void) \n" |
|---|
| 164 | "{\n" |
|---|
| 165 | " gl_FragColor = texture2D( baseTexture, texcoord); \n" |
|---|
| 166 | "}\n"; |
|---|
| 167 | |
|---|
| 168 | program->addShader(new osg::Shader(osg::Shader::VERTEX, vertexShaderSource)); |
|---|
| 169 | program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource)); |
|---|
| 170 | |
|---|
| 171 | #else |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | program->addShader(osg::Shader::readShaderFile(osg::Shader::VERTEX, osgDB::findDataFile("shaders/terrain.vert"))); |
|---|
| 175 | program->addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, osgDB::findDataFile("shaders/terrain.frag"))); |
|---|
| 176 | |
|---|
| 177 | #endif |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | { |
|---|
| 184 | osg::Geometry* geometry = new osg::Geometry; |
|---|
| 185 | |
|---|
| 186 | osg::Vec3Array& v = *(new osg::Vec3Array(numColumns*numRows)); |
|---|
| 187 | osg::Vec4ubArray& color = *(new osg::Vec4ubArray(1)); |
|---|
| 188 | |
|---|
| 189 | color[0].set(255,255,255,255); |
|---|
| 190 | |
|---|
| 191 | float rowCoordDelta = size.y()/(float)(numRows-1); |
|---|
| 192 | float columnCoordDelta = size.x()/(float)(numColumns-1); |
|---|
| 193 | |
|---|
| 194 | float rowTexDelta = 1.0f/(float)(numRows-1); |
|---|
| 195 | float columnTexDelta = 1.0f/(float)(numColumns-1); |
|---|
| 196 | |
|---|
| 197 | osg::Vec3 pos = origin; |
|---|
| 198 | osg::Vec2 tex(0.0f,0.0f); |
|---|
| 199 | int vi=0; |
|---|
| 200 | for(r=0;r<numRows;++r) |
|---|
| 201 | { |
|---|
| 202 | pos.x() = origin.x(); |
|---|
| 203 | tex.x() = 0.0f; |
|---|
| 204 | for(c=0;c<numColumns;++c) |
|---|
| 205 | { |
|---|
| 206 | v[vi].set(pos.x(),pos.y(),pos.z()); |
|---|
| 207 | pos.x()+=columnCoordDelta; |
|---|
| 208 | tex.x()+=columnTexDelta; |
|---|
| 209 | ++vi; |
|---|
| 210 | } |
|---|
| 211 | pos.y() += rowCoordDelta; |
|---|
| 212 | tex.y() += rowTexDelta; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | geometry->setVertexArray(&v); |
|---|
| 216 | geometry->setColorArray(&color); |
|---|
| 217 | geometry->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 218 | |
|---|
| 219 | for(r=0;r<numRows-1;++r) |
|---|
| 220 | { |
|---|
| 221 | osg::DrawElementsUShort& drawElements = *(new osg::DrawElementsUShort(GL_QUAD_STRIP,2*numColumns)); |
|---|
| 222 | geometry->addPrimitiveSet(&drawElements); |
|---|
| 223 | int ei=0; |
|---|
| 224 | for(c=0;c<numColumns;++c) |
|---|
| 225 | { |
|---|
| 226 | drawElements[ei++] = (r+1)*numColumns+c; |
|---|
| 227 | drawElements[ei++] = (r)*numColumns+c; |
|---|
| 228 | } |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | geometry->setInitialBound(osg::BoundingBox(origin, origin+size)); |
|---|
| 232 | |
|---|
| 233 | geode->addDrawable(geometry); |
|---|
| 234 | |
|---|
| 235 | scene->addChild(geode); |
|---|
| 236 | } |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | std::cout<<"done."<<std::endl; |
|---|
| 240 | |
|---|
| 241 | return scene; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | #if 0 |
|---|
| 245 | class TestSupportCallback : public osgProducer::OsgCameraGroup::RealizeCallback |
|---|
| 246 | { |
|---|
| 247 | public: |
|---|
| 248 | TestSupportCallback():_supported(true),_errorMessage() {} |
|---|
| 249 | |
|---|
| 250 | virtual void operator()( osgProducer::OsgCameraGroup&, osgProducer::OsgSceneHandler& sh, const Producer::RenderSurface& ) |
|---|
| 251 | { |
|---|
| 252 | { |
|---|
| 253 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 254 | |
|---|
| 255 | unsigned int contextID = sh.getSceneView()->getState()->getContextID(); |
|---|
| 256 | osg::GL2Extensions* gl2ext = osg::GL2Extensions::Get(contextID,true); |
|---|
| 257 | if( gl2ext ) |
|---|
| 258 | { |
|---|
| 259 | if( !gl2ext->isGlslSupported() ) |
|---|
| 260 | { |
|---|
| 261 | _supported = false; |
|---|
| 262 | _errorMessage = "ERROR: GLSL not supported by OpenGL driver."; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | GLint numVertexTexUnits = 0; |
|---|
| 266 | glGetIntegerv( GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &numVertexTexUnits ); |
|---|
| 267 | if( numVertexTexUnits <= 0 ) |
|---|
| 268 | { |
|---|
| 269 | _supported = false; |
|---|
| 270 | _errorMessage = "ERROR: vertex texturing not supported by OpenGL driver."; |
|---|
| 271 | } |
|---|
| 272 | } |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | sh.init(); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | OpenThreads::Mutex _mutex; |
|---|
| 279 | bool _supported; |
|---|
| 280 | std::string _errorMessage; |
|---|
| 281 | |
|---|
| 282 | }; |
|---|
| 283 | #endif |
|---|
| 284 | |
|---|
| 285 | int main(int, char **) |
|---|
| 286 | { |
|---|
| 287 | |
|---|
| 288 | osgViewer::Viewer viewer; |
|---|
| 289 | |
|---|
| 290 | osg::Node* node = createScene(); |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | viewer.setSceneData( node ); |
|---|
| 294 | |
|---|
| 295 | #if 0 |
|---|
| 296 | |
|---|
| 297 | osg::ref_ptr<TestSupportCallback> testSupportCallback = new TestSupportCallback(); |
|---|
| 298 | viewer.setRealizeCallback(testSupportCallback.get()); |
|---|
| 299 | |
|---|
| 300 | viewer.realize(); |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | if (!testSupportCallback->_supported) |
|---|
| 304 | { |
|---|
| 305 | osg::notify(osg::WARN)<<testSupportCallback->_errorMessage<<std::endl; |
|---|
| 306 | |
|---|
| 307 | exit(1); |
|---|
| 308 | } |
|---|
| 309 | #else |
|---|
| 310 | |
|---|
| 311 | osg::notify(osg::NOTICE)<<"osgshaderterrain OpenGL support test not implemented yet"<<std::endl; |
|---|
| 312 | |
|---|
| 313 | #endif |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | return viewer.run(); |
|---|
| 317 | } |
|---|