2005-08-15 21:54:25 +08:00
|
|
|
#include <osg/AlphaFunc>
|
|
|
|
#include <osg/Billboard>
|
|
|
|
#include <osg/BlendFunc>
|
|
|
|
#include <osg/Depth>
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osg/Geometry>
|
2005-10-28 16:35:55 +08:00
|
|
|
#include <osg/GL2Extensions>
|
2005-08-15 21:54:25 +08:00
|
|
|
#include <osg/Material>
|
|
|
|
#include <osg/Math>
|
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
#include <osg/PolygonOffset>
|
2005-10-28 16:35:55 +08:00
|
|
|
#include <osg/Program>
|
2005-08-15 21:54:25 +08:00
|
|
|
#include <osg/Projection>
|
2005-10-28 16:35:55 +08:00
|
|
|
#include <osg/Shader>
|
2005-08-15 21:54:25 +08:00
|
|
|
#include <osg/ShapeDrawable>
|
|
|
|
#include <osg/StateSet>
|
|
|
|
#include <osg/Switch>
|
|
|
|
#include <osg/Texture2D>
|
2005-10-28 16:35:55 +08:00
|
|
|
#include <osg/Uniform>
|
2005-08-15 21:54:25 +08:00
|
|
|
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
#include <osgDB/FileUtils>
|
|
|
|
|
|
|
|
#include <osgUtil/IntersectVisitor>
|
|
|
|
#include <osgUtil/SmoothingVisitor>
|
|
|
|
|
|
|
|
#include <osgText/Text>
|
|
|
|
|
2007-01-11 05:05:26 +08:00
|
|
|
#include <osgViewer/Viewer>
|
|
|
|
|
|
|
|
#include <iostream>
|
2005-08-15 21:54:25 +08:00
|
|
|
|
|
|
|
// for the grid data..
|
|
|
|
#include "../osghangglide/terrain_coords.h"
|
|
|
|
|
2007-01-02 02:20:10 +08:00
|
|
|
osg::Node* createScene()
|
2005-08-15 21:54:25 +08:00
|
|
|
{
|
|
|
|
osg::Group* scene = new osg::Group;
|
|
|
|
|
|
|
|
unsigned int numColumns = 38;
|
|
|
|
unsigned int numRows = 39;
|
|
|
|
unsigned int r;
|
|
|
|
unsigned int c;
|
|
|
|
|
|
|
|
osg::Vec3 origin(0.0f,0.0f,0.0f);
|
|
|
|
osg::Vec3 size(1000.0f,1000.0f,250.0f);
|
|
|
|
osg::Vec3 scaleDown(1.0f/size.x(),1.0f/size.y(),1.0f/size.z());
|
|
|
|
|
|
|
|
// ---------------------------------------
|
|
|
|
// Set up a StateSet to texture the objects
|
|
|
|
// ---------------------------------------
|
|
|
|
osg::StateSet* stateset = new osg::StateSet();
|
|
|
|
|
|
|
|
|
|
|
|
osg::Uniform* originUniform = new osg::Uniform("terrainOrigin",origin);
|
|
|
|
stateset->addUniform(originUniform);
|
|
|
|
|
|
|
|
osg::Uniform* sizeUniform = new osg::Uniform("terrainSize",size);
|
|
|
|
stateset->addUniform(sizeUniform);
|
|
|
|
|
|
|
|
osg::Uniform* scaleDownUniform = new osg::Uniform("terrainScaleDown",scaleDown);
|
|
|
|
stateset->addUniform(scaleDownUniform);
|
|
|
|
|
|
|
|
osg::Uniform* terrainTextureSampler = new osg::Uniform("terrainTexture",0);
|
|
|
|
stateset->addUniform(terrainTextureSampler);
|
|
|
|
|
|
|
|
osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",1);
|
|
|
|
stateset->addUniform(baseTextureSampler);
|
|
|
|
|
|
|
|
osg::Uniform* treeTextureSampler = new osg::Uniform("treeTexture",1);
|
|
|
|
stateset->addUniform(treeTextureSampler);
|
|
|
|
|
|
|
|
|
|
|
|
// compute z range of z values of grid data so we can scale it.
|
|
|
|
float min_z = FLT_MAX;
|
|
|
|
float max_z = -FLT_MAX;
|
|
|
|
for(r=0;r<numRows;++r)
|
|
|
|
{
|
2005-10-28 16:35:55 +08:00
|
|
|
for(c=0;c<numColumns;++c)
|
|
|
|
{
|
|
|
|
min_z = osg::minimum(min_z,vertex[r+c*numRows][2]);
|
|
|
|
max_z = osg::maximum(max_z,vertex[r+c*numRows][2]);
|
|
|
|
}
|
2005-08-15 21:54:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float scale_z = size.z()/(max_z-min_z);
|
|
|
|
|
|
|
|
osg::Image* terrainImage = new osg::Image;
|
|
|
|
terrainImage->allocateImage(numColumns,numRows,1,GL_LUMINANCE, GL_FLOAT);
|
|
|
|
terrainImage->setInternalTextureFormat(GL_LUMINANCE_FLOAT32_ATI);
|
|
|
|
for(r=0;r<numRows;++r)
|
|
|
|
{
|
2005-10-28 16:35:55 +08:00
|
|
|
for(c=0;c<numColumns;++c)
|
|
|
|
{
|
|
|
|
*((float*)(terrainImage->data(c,r))) = (vertex[r+c*numRows][2]-min_z)*scale_z;
|
|
|
|
}
|
2005-08-15 21:54:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
osg::Texture2D* terrainTexture = new osg::Texture2D;
|
|
|
|
terrainTexture->setImage(terrainImage);
|
|
|
|
terrainTexture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST);
|
|
|
|
terrainTexture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST);
|
2005-11-24 18:28:09 +08:00
|
|
|
terrainTexture->setResizeNonPowerOfTwoHint(false);
|
2005-08-15 21:54:25 +08:00
|
|
|
stateset->setTextureAttributeAndModes(0,terrainTexture,osg::StateAttribute::ON);
|
|
|
|
|
|
|
|
|
|
|
|
osg::Image* image = osgDB::readImageFile("Images/lz.rgb");
|
|
|
|
if (image)
|
|
|
|
{
|
2005-10-28 16:35:55 +08:00
|
|
|
osg::Texture2D* texture = new osg::Texture2D;
|
2005-08-15 21:54:25 +08:00
|
|
|
|
2005-10-28 16:35:55 +08:00
|
|
|
texture->setImage(image);
|
|
|
|
stateset->setTextureAttributeAndModes(1,texture,osg::StateAttribute::ON);
|
2005-08-15 21:54:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::cout<<"Creating terrain...";
|
|
|
|
|
|
|
|
osg::Geode* geode = new osg::Geode();
|
|
|
|
geode->setStateSet( stateset );
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
osg::Program* program = new osg::Program;
|
|
|
|
stateset->setAttribute(program);
|
|
|
|
|
2007-01-11 05:05:26 +08:00
|
|
|
#if 1
|
2005-08-15 21:54:25 +08:00
|
|
|
// use inline shaders
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
// vertex shader using just Vec4 coefficients
|
|
|
|
char vertexShaderSource[] =
|
|
|
|
"uniform sampler2D terrainTexture;\n"
|
|
|
|
"uniform vec3 terrainOrigin;\n"
|
|
|
|
"uniform vec3 terrainScaleDown;\n"
|
|
|
|
"\n"
|
|
|
|
"varying vec2 texcoord;\n"
|
|
|
|
"\n"
|
|
|
|
"void main(void)\n"
|
|
|
|
"{\n"
|
|
|
|
" texcoord = gl_Vertex.xy - terrainOrigin.xy;\n"
|
|
|
|
" texcoord.x *= terrainScaleDown.x;\n"
|
|
|
|
" texcoord.y *= terrainScaleDown.y;\n"
|
|
|
|
"\n"
|
2007-01-11 05:05:26 +08:00
|
|
|
" vec4 position;\n"
|
2005-08-15 21:54:25 +08:00
|
|
|
" position.x = gl_Vertex.x;\n"
|
|
|
|
" position.y = gl_Vertex.y;\n"
|
2006-09-02 22:27:42 +08:00
|
|
|
" position.z = texture2D(terrainTexture, texcoord).r;\n"
|
|
|
|
" position.w = 1.0;\n"
|
2005-08-15 21:54:25 +08:00
|
|
|
" \n"
|
|
|
|
" gl_Position = gl_ModelViewProjectionMatrix * position;\n"
|
|
|
|
" gl_FrontColor = vec4(1.0,1.0,1.0,1.0);\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// fragment shader
|
|
|
|
//
|
|
|
|
char fragmentShaderSource[] =
|
|
|
|
"uniform sampler2D baseTexture; \n"
|
|
|
|
"varying vec2 texcoord;\n"
|
|
|
|
"\n"
|
|
|
|
"void main(void) \n"
|
|
|
|
"{\n"
|
|
|
|
" gl_FragColor = texture2D( baseTexture, texcoord); \n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
program->addShader(new osg::Shader(osg::Shader::VERTEX, vertexShaderSource));
|
|
|
|
program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource));
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// get shaders from source
|
|
|
|
program->addShader(osg::Shader::readShaderFile(osg::Shader::VERTEX, osgDB::findDataFile("shaders/terrain.vert")));
|
|
|
|
program->addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, osgDB::findDataFile("shaders/terrain.frag")));
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// get shaders from source
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
osg::Geometry* geometry = new osg::Geometry;
|
|
|
|
|
|
|
|
osg::Vec3Array& v = *(new osg::Vec3Array(numColumns*numRows));
|
|
|
|
osg::Vec4ubArray& color = *(new osg::Vec4ubArray(1));
|
|
|
|
|
|
|
|
color[0].set(255,255,255,255);
|
|
|
|
|
|
|
|
float rowCoordDelta = size.y()/(float)(numRows-1);
|
|
|
|
float columnCoordDelta = size.x()/(float)(numColumns-1);
|
|
|
|
|
|
|
|
float rowTexDelta = 1.0f/(float)(numRows-1);
|
|
|
|
float columnTexDelta = 1.0f/(float)(numColumns-1);
|
|
|
|
|
|
|
|
osg::Vec3 pos = origin;
|
|
|
|
osg::Vec2 tex(0.0f,0.0f);
|
|
|
|
int vi=0;
|
|
|
|
for(r=0;r<numRows;++r)
|
|
|
|
{
|
|
|
|
pos.x() = origin.x();
|
|
|
|
tex.x() = 0.0f;
|
2005-10-28 16:35:55 +08:00
|
|
|
for(c=0;c<numColumns;++c)
|
|
|
|
{
|
|
|
|
v[vi].set(pos.x(),pos.y(),pos.z());
|
2005-08-15 21:54:25 +08:00
|
|
|
pos.x()+=columnCoordDelta;
|
|
|
|
tex.x()+=columnTexDelta;
|
|
|
|
++vi;
|
2005-10-28 16:35:55 +08:00
|
|
|
}
|
2005-08-15 21:54:25 +08:00
|
|
|
pos.y() += rowCoordDelta;
|
|
|
|
tex.y() += rowTexDelta;
|
|
|
|
}
|
|
|
|
|
|
|
|
geometry->setVertexArray(&v);
|
|
|
|
geometry->setColorArray(&color);
|
|
|
|
geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
|
|
|
|
|
|
|
|
for(r=0;r<numRows-1;++r)
|
|
|
|
{
|
|
|
|
osg::DrawElementsUShort& drawElements = *(new osg::DrawElementsUShort(GL_QUAD_STRIP,2*numColumns));
|
|
|
|
geometry->addPrimitiveSet(&drawElements);
|
|
|
|
int ei=0;
|
2005-10-28 16:35:55 +08:00
|
|
|
for(c=0;c<numColumns;++c)
|
|
|
|
{
|
|
|
|
drawElements[ei++] = (r+1)*numColumns+c;
|
|
|
|
drawElements[ei++] = (r)*numColumns+c;
|
|
|
|
}
|
2005-08-15 21:54:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
geometry->setInitialBound(osg::BoundingBox(origin, origin+size));
|
|
|
|
|
|
|
|
geode->addDrawable(geometry);
|
|
|
|
|
|
|
|
scene->addChild(geode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout<<"done."<<std::endl;
|
2005-11-24 18:28:09 +08:00
|
|
|
|
2005-08-15 21:54:25 +08:00
|
|
|
return scene;
|
|
|
|
}
|
|
|
|
|
2007-02-03 06:30:36 +08:00
|
|
|
class TestSupportOperation: public osg::Operation
|
2005-12-01 22:09:38 +08:00
|
|
|
{
|
2007-01-12 18:28:36 +08:00
|
|
|
public:
|
|
|
|
|
|
|
|
TestSupportOperation():
|
2007-02-03 06:30:36 +08:00
|
|
|
osg::Operation("TestSupportOperation",false),
|
2007-01-12 18:28:36 +08:00
|
|
|
_supported(true),
|
|
|
|
_errorMessage() {}
|
|
|
|
|
2007-02-03 06:30:36 +08:00
|
|
|
virtual void operator () (osg::Object* object)
|
2007-01-12 18:28:36 +08:00
|
|
|
{
|
2007-02-03 06:30:36 +08:00
|
|
|
osg::GraphicsContext* gc = dynamic_cast<osg::GraphicsContext*>(object);
|
|
|
|
if (!gc) return;
|
2007-01-12 18:28:36 +08:00
|
|
|
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
|
|
|
|
|
|
|
|
unsigned int contextID = gc->getState()->getContextID();
|
|
|
|
osg::GL2Extensions* gl2ext = osg::GL2Extensions::Get(contextID,true);
|
|
|
|
if( gl2ext )
|
2006-07-05 21:31:38 +08:00
|
|
|
{
|
2007-01-12 18:28:36 +08:00
|
|
|
if( !gl2ext->isGlslSupported() )
|
2005-12-01 22:09:38 +08:00
|
|
|
{
|
2007-01-12 18:28:36 +08:00
|
|
|
_supported = false;
|
|
|
|
_errorMessage = "ERROR: GLSL not supported by OpenGL driver.";
|
|
|
|
}
|
2005-12-01 22:09:38 +08:00
|
|
|
|
2007-01-12 18:28:36 +08:00
|
|
|
GLint numVertexTexUnits = 0;
|
|
|
|
glGetIntegerv( GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &numVertexTexUnits );
|
|
|
|
if( numVertexTexUnits <= 0 )
|
|
|
|
{
|
|
|
|
_supported = false;
|
|
|
|
_errorMessage = "ERROR: vertex texturing not supported by OpenGL driver.";
|
2005-12-01 22:09:38 +08:00
|
|
|
}
|
|
|
|
}
|
2007-02-03 06:30:36 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_supported = false;
|
|
|
|
_errorMessage = "ERROR: GLSL not supported.";
|
|
|
|
}
|
2007-01-12 18:28:36 +08:00
|
|
|
}
|
2005-12-01 22:09:38 +08:00
|
|
|
|
2007-01-12 18:28:36 +08:00
|
|
|
OpenThreads::Mutex _mutex;
|
|
|
|
bool _supported;
|
|
|
|
std::string _errorMessage;
|
2005-12-01 22:09:38 +08:00
|
|
|
};
|
|
|
|
|
2007-01-11 05:05:26 +08:00
|
|
|
int main(int, char **)
|
2005-08-15 21:54:25 +08:00
|
|
|
{
|
|
|
|
// construct the viewer.
|
2007-01-11 05:05:26 +08:00
|
|
|
osgViewer::Viewer viewer;
|
2005-08-15 21:54:25 +08:00
|
|
|
|
2007-01-02 02:20:10 +08:00
|
|
|
osg::Node* node = createScene();
|
2005-08-15 21:54:25 +08:00
|
|
|
|
|
|
|
// add model to viewer.
|
|
|
|
viewer.setSceneData( node );
|
|
|
|
|
2007-01-12 18:28:36 +08:00
|
|
|
viewer.setUpViewAcrossAllScreens();
|
|
|
|
|
|
|
|
osg::ref_ptr<TestSupportOperation> testSupportOperation = new TestSupportOperation;
|
2007-02-03 06:30:36 +08:00
|
|
|
#if 0
|
|
|
|
// temporily commenting out as its causing the viewer to crash... no clue yet to why
|
|
|
|
viewer.setRealizeOperation(testSupportOperation.get());
|
|
|
|
#endif
|
2007-01-12 18:28:36 +08:00
|
|
|
// create the windows and run the threads.
|
|
|
|
viewer.realize();
|
2005-08-15 21:54:25 +08:00
|
|
|
|
2007-01-12 18:28:36 +08:00
|
|
|
if (!testSupportOperation->_supported)
|
|
|
|
{
|
|
|
|
osg::notify(osg::WARN)<<testSupportOperation->_errorMessage<<std::endl;
|
2006-08-03 03:55:03 +08:00
|
|
|
|
2007-01-12 18:28:36 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2005-08-15 21:54:25 +08:00
|
|
|
|
2007-01-11 05:05:26 +08:00
|
|
|
return viewer.run();
|
2005-08-15 21:54:25 +08:00
|
|
|
}
|