Various work on osgViewer library, including warp point and graphics window resize support

This commit is contained in:
Robert Osfield
2007-01-01 18:20:10 +00:00
parent 88fc4ee986
commit 7155f7d1b0
36 changed files with 1624 additions and 1030 deletions

View File

@@ -36,10 +36,73 @@ int main( int argc, char **argv )
viewer.setCameraManipulator(new osgGA::TrackballManipulator());
viewer.getCamera()->setClearColor(osg::Vec4f(0.6f,0.6f,0.8f,1.0f));
#if 1
osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
if (!wsi)
{
osg::notify(osg::NOTICE)<<"View::setUpViewAcrossAllScreens() : Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
return 0;
}
unsigned int width, height;
wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->x = 0;
traits->y = 0;
traits->width = width;
traits->height = height;
#if 0
traits->windowDecoration = false;
#else
traits->windowDecoration = true;
#endif
traits->doubleBuffer = true;
traits->sharedContext = 0;
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get());
if (gw)
{
osg::notify(osg::NOTICE)<<" GraphicsWindow has been created successfully."<<gw<<std::endl;
gw->getEventQueue()->getCurrentEventState()->setWindowRectangle(0, 0, width, height );
}
else
{
osg::notify(osg::NOTICE)<<" GraphicsWindow has not been created successfully."<<std::endl;
}
unsigned int numCameras = 2;
double aspectRatioScale = 1.0;///(double)numCameras;
for(unsigned int i=0; i<numCameras;++i)
{
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setGraphicsContext(gc.get());
camera->setViewport(new osg::Viewport((i*width)/numCameras,(i*height)/numCameras, width/numCameras, height/numCameras));
GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
camera->setDrawBuffer(buffer);
camera->setReadBuffer(buffer);
viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::scale(aspectRatioScale,1.0,1.0));
}
viewer.setUpRenderingSupport();
viewer.assignSceneDataToCameras();
#else
viewer.setUpViewAcrossAllScreens();
#endif
viewer.realize();
bool limitNumberOfFrames = true;
gw->requestWarpPointer(100,100);
bool limitNumberOfFrames = false;
unsigned int numFrames = 0;
unsigned int maxFrames = 10;

View File

@@ -7,7 +7,7 @@ using namespace osgGA;
GliderManipulator::GliderManipulator()
{
_modelScale = 0.01f;
_velocity = 0.0f;
_velocity = 0.2f;
_yawMode = YAW_AUTOMATICALLY_WHEN_BANKED;
_distance = 1.0f;
@@ -74,7 +74,7 @@ void GliderManipulator::init(const GUIEventAdapter& ea,GUIActionAdapter& us)
us.requestContinuousUpdate(false);
_velocity = 0.0f;
_velocity = 0.2f;
if (ea.getEventType()!=GUIEventAdapter::RESIZE)
{

View File

@@ -143,10 +143,71 @@ int main( int argc, char **argv )
viewer.setSceneData( rootnode );
// set up the value with sensible default event handlers.
viewer.setUpViewAcrossAllScreens();
#if 0
osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
if (!wsi)
{
osg::notify(osg::NOTICE)<<"View::setUpViewAcrossAllScreens() : Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
return 0;
}
unsigned int width, height;
wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
width -= 500;
height -= 500;
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->x = 500;
traits->y = 500;
traits->width = width;
traits->height = height;
#if 0
traits->windowDecoration = false;
#else
traits->windowDecoration = true;
#endif
traits->doubleBuffer = true;
traits->sharedContext = 0;
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get());
if (gw)
{
osg::notify(osg::NOTICE)<<" GraphicsWindow has been created successfully."<<gw<<std::endl;
gw->getEventQueue()->getCurrentEventState()->setWindowRectangle(0, 0, width, height );
}
else
{
osg::notify(osg::NOTICE)<<" GraphicsWindow has not been created successfully."<<std::endl;
}
unsigned int numCameras = 2;
double aspectRatioScale = 1.0/(double)numCameras;
for(unsigned int i=0; i<numCameras;++i)
{
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setGraphicsContext(gc.get());
camera->setViewport(new osg::Viewport((i*width)/numCameras,(i*height)/numCameras, width/numCameras, height/numCameras));
GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
camera->setDrawBuffer(buffer);
camera->setReadBuffer(buffer);
viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::scale(aspectRatioScale,1.0,1.0));
}
viewer.setUpRenderingSupport();
viewer.assignSceneDataToCameras();
#else
viewer.setUpViewAcrossAllScreens();
#endif
// create the windows and run the threads.
viewer.realize();
while( !viewer.done() )

View File

@@ -11,6 +11,7 @@
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osgUtil/TriStripVisitor>
#include <osgProducer/Viewer>
@@ -57,10 +58,26 @@ char vertexShaderSource_texture[] =
"\n"
" gl_TexCoord[0] = gl_Vertex; \n"
" vec4 vert = gl_Vertex; \n"
" vert.z = texture2D( vertexTexture, gl_TexCoord[0].xy).x*0.0001; \n"
" vert.z = texture2D( vertexTexture, gl_TexCoord[0].xy).x*0.1; \n"
" gl_Position = gl_ModelViewProjectionMatrix * vert;\n"
"}\n";
//////////////////////////////////////////////////////////////////
// vertex shader using texture read
char vertexShaderSource_texture2[] =
"uniform sampler2D vertexTexture; \n"
"uniform sampler2D vertexTexture2; \n"
"uniform float osg_FrameTime;\n"
"\n"
"void main(void) \n"
"{ \n"
"\n"
" gl_TexCoord[0] = gl_Vertex; \n"
" vec4 vert = gl_Vertex; \n"
" float r = 1.0 + 0.5 * sin(osg_FrameTime);\n"
" vert.z = (texture2D( vertexTexture, gl_TexCoord[0].xy).x * (1-r) + texture2D( vertexTexture2, gl_TexCoord[0].xy).x * r)*0.1; \n"
" gl_Position = gl_ModelViewProjectionMatrix * vert;\n"
"}\n";
//////////////////////////////////////////////////////////////////
// fragment shader
@@ -73,7 +90,33 @@ char fragmentShaderSource[] =
" gl_FragColor = texture2D( baseTexture, gl_TexCoord[0].xy); \n"
"}\n";
#if 0
char fragmentShaderNormalSource[] =
"uniform sampler2D baseTexture; \n"
"uniform sampler2D vertexTexture; \n"
"\n"
"void main(void) \n"
"{ \n"
" const float dx = 0.0010; \n"
" const float dy = 0.0010; \n"
" float dz_dx = texture2D( vertexTexture, gl_TexCoord[0].xy + vec2(dx,0.0)).x - texture2D( vertexTexture, gl_TexCoord[0].xy + vec2(-dx,0.0)).x; \n"
" float dz_dy = texture2D( vertexTexture, gl_TexCoord[0].xy + vec2(0.0,dy)).x - texture2D( vertexTexture, gl_TexCoord[0].xy + vec2(0.0,-dy)).x; \n"
" vec3 normal = normalize(vec3(-dz_dx, -dz_dy, dx*50));\n"
" \n"
" gl_FragColor = vec4(normal.z,normal.z,normal.z,1.0); \n"
"}\n";
#else
char fragmentShaderNormalSource[] =
"uniform sampler2D baseTexture; \n"
"uniform sampler2D vertexTexture; \n"
"\n"
"void main(void) \n"
"{ \n"
" vec3 normal = normalize(texture2D( baseTexture, gl_TexCoord[0].xy).xyz);\n"
" \n"
" gl_FragColor = vec4(normal.z,normal.z,normal.z,1.0); \n"
"}\n";
#endif
class UniformVarying : public osg::Uniform::Callback
{
@@ -85,7 +128,7 @@ class UniformVarying : public osg::Uniform::Callback
}
};
osg::Node* createModel(const std::string& shader, const std::string& textureFileName, const std::string& terrainFileName)
osg::Node* createModel(const std::string& shader, const std::string& textureFileName, const std::string& terrainFileName, const std::string& terrainFileName2, unsigned int cacheSize, unsigned int maxSize, bool joinStrips, const std::string& mesh)
{
osg::Geode* geode = new osg::Geode;
@@ -131,8 +174,6 @@ osg::Node* createModel(const std::string& shader, const std::string& textureFile
}
else if (shader=="texture")
{
osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_texture);
program->addShader(vertex_shader);
osg::Image* image = 0;
@@ -166,17 +207,54 @@ osg::Node* createModel(const std::string& shader, const std::string& textureFile
vertexTexture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST);
vertexTexture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST);
vertexTexture->setInternalFormat(GL_LUMINANCE_FLOAT32_ATI);
vertexTexture->setResizeNonPowerOfTwoHint(false);
stateset->setTextureAttributeAndModes(1,vertexTexture);
osg::Uniform* vertexTextureSampler = new osg::Uniform("vertexTexture",1);
stateset->addUniform(vertexTextureSampler);
if (!terrainFileName2.empty())
{
osg::Image* image2 = osgDB::readImageFile(terrainFileName2);
osg::Texture2D* vertexTexture2 = new osg::Texture2D(image2);
vertexTexture2->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST);
vertexTexture2->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST);
vertexTexture2->setInternalFormat(GL_LUMINANCE_FLOAT32_ATI);
vertexTexture2->setResizeNonPowerOfTwoHint(false);
stateset->setTextureAttributeAndModes(2,vertexTexture2);
osg::Uniform* vertexTextureSampler2 = new osg::Uniform("vertexTexture2",2);
stateset->addUniform(vertexTextureSampler2);
}
}
osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource);
program->addShader(fragment_shader);
if (terrainFileName2.empty())
{
osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderNormalSource);
program->addShader(fragment_shader);
osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_texture);
program->addShader(vertex_shader);
}
else
{
osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource);
program->addShader(fragment_shader);
osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_texture2);
program->addShader(vertex_shader);
}
osg::Texture2D* texture = new osg::Texture2D(osgDB::readImageFile(textureFileName));
stateset->setTextureAttributeAndModes(0,texture);
@@ -209,20 +287,215 @@ osg::Node* createModel(const std::string& shader, const std::string& textureFile
geom->setVertexArray(vertices);
for(iy=0; iy<num_y-1; ++iy)
unsigned int totalIndices = 0;
if (mesh=="triangles" || mesh=="tristrip")
{
unsigned int element_no = 0;
osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLE_STRIP, num_x*2);
unsigned int index = iy * num_x;
for(unsigned int ix = 0; ix<num_x; ++ix)
if (cacheSize)
{
(*elements)[element_no++] = index + num_x;
(*elements)[element_no++] = index++;
unsigned int index=0;
osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLES);
geom->addPrimitiveSet(elements);
elements->dirty();
for(unsigned int is=0; is<num_x; is += cacheSize-1)
{
for(iy=0; iy<num_y-1; ++iy)
{
unsigned int num_in_stripe = osg::minimum(cacheSize, num_x-is);
bool rightToLeft = true;
if (elements->size() > maxSize)
{
osg::notify(osg::NOTICE)<<"elements->size() = "<<elements->size()<<std::endl;
totalIndices += elements->size();
index=0;
elements = new osg::DrawElementsUInt(GL_TRIANGLES);
geom->addPrimitiveSet(elements);
elements->dirty();
}
if (rightToLeft)
{
index = iy * num_x + is;
for(unsigned int ix = 0; ix<num_in_stripe-1; ++ix)
{
(*elements).push_back(index + num_x);
(*elements).push_back(index);
(*elements).push_back(index+1);
(*elements).push_back(index + num_x);
(*elements).push_back(index+1);
(*elements).push_back(index + num_x + 1);
++index;
}
}
else
{
index = iy * num_x + is + (num_x-2);
for(unsigned int ix = 0; ix<num_in_stripe-1; ++ix)
{
(*elements).push_back(index + num_x);
(*elements).push_back(index);
(*elements).push_back(index+1);
(*elements).push_back(index + num_x);
(*elements).push_back(index+1);
(*elements).push_back(index + num_x + 1);
--index;
}
}
rightToLeft = !rightToLeft;
}
}
totalIndices += elements->size();
osg::notify(osg::NOTICE)<<"elements->size() = "<<elements->size()<<std::endl;
}
else
{
osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLES, (num_x-1)*(num_y-1)*6);
elements->dirty();
unsigned int element_no = 0;
for(iy=0; iy<num_y-1; ++iy)
{
unsigned int index = iy * num_x;
for(unsigned int ix = 0; ix<num_x-1; ++ix)
{
(*elements)[element_no++] = index + num_x;
(*elements)[element_no++] = index;
(*elements)[element_no++] = index+1;
(*elements)[element_no++] = index + num_x;
(*elements)[element_no++] = index+1;
(*elements)[element_no++] = index + num_x + 1;
++index;
}
}
geom->addPrimitiveSet(elements);
osg::notify(osg::NOTICE)<<"elements->size() = "<<elements->size()<<std::endl;
}
geom->addPrimitiveSet(elements);
}
else
{
// geom->setUseVertexBufferObjects(true);
if (cacheSize)
{
bool needToJoin = false;
unsigned int index=0;
osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLE_STRIP);
geom->addPrimitiveSet(elements);
elements->dirty();
for(unsigned int is=0; is<num_x; is += cacheSize-1)
{
for(iy=0; iy<num_y-1; ++iy)
{
unsigned int num_in_stripe = osg::minimum(cacheSize, num_x-is);
bool rightToLeft = true;
if (elements->size() > maxSize)
{
osg::notify(osg::NOTICE)<<"elements->size() = "<<elements->size()<<std::endl;
totalIndices += elements->size();
needToJoin = false;
index=0;
elements = new osg::DrawElementsUInt(GL_TRIANGLE_STRIP);
geom->addPrimitiveSet(elements);
elements->dirty();
}
if (needToJoin) (*elements).push_back(elements->back());
if (rightToLeft)
{
index = iy * num_x + is;
if (needToJoin) (*elements).push_back(index + num_x);
for(unsigned int ix = 0; ix<num_in_stripe; ++ix)
{
(*elements).push_back(index + num_x);
(*elements).push_back(index++);
}
}
else
{
index = iy * num_x + is + (num_x-1);
if (needToJoin) (*elements).push_back(index);
for(unsigned int ix = 0; ix<num_in_stripe; ++ix)
{
(*elements).push_back(index);
(*elements).push_back(index-- + num_x);
}
}
rightToLeft = !rightToLeft;
needToJoin = true;
}
}
totalIndices += elements->size();
}
else
{
osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLE_STRIP);
elements->dirty();
geom->addPrimitiveSet(elements);
bool needToJoin = false;
unsigned int index=0;
for(iy=0; iy<num_y-1; ++iy)
{
if (needToJoin) (*elements).push_back(index-1);
index = iy * num_x;
if (needToJoin) (*elements).push_back(index + num_x);
for(unsigned int ix = 0; ix<num_x; ++ix)
{
(*elements).push_back(index + num_x);
(*elements).push_back(index++);
}
needToJoin = true;
}
}
}
osg::notify(osg::NOTICE)<<"totalIndices = "<<totalIndices<<std::endl;
if (mesh=="tristrip")
{ osgUtil::TriStripVisitor stripper;
stripper.stripify(*geom);
}
#if 0
geom->setUseVertexBufferObjects(true);
#else
geom->setUseVertexBufferObjects(false);
#endif
return geode;
}
@@ -252,6 +525,21 @@ int main(int argc, char *argv[])
std::string terrainFileName("");
while(arguments.read("-d",terrainFileName)) {}
std::string terrainFileName2("");
while(arguments.read("-d2",terrainFileName2)) {}
unsigned int cacheSize = 0;
while(arguments.read("--cache",cacheSize)) {}
unsigned int maxSize = 2048;
while(arguments.read("--max",maxSize)) {}
std::string mesh("strip");
while(arguments.read("--mesh",mesh)) {}
bool joinStrips = false;
while(arguments.read("--join")) { joinStrips = true; }
// get details on keyboard and mouse bindings used by the viewer.
viewer.getUsage(*arguments.getApplicationUsage());
@@ -273,7 +561,7 @@ int main(int argc, char *argv[])
}
// load the nodes from the commandline arguments.
osg::Node* model = createModel(shader,textureFileName,terrainFileName);
osg::Node* model = createModel(shader,textureFileName,terrainFileName,terrainFileName2, cacheSize, maxSize, joinStrips,mesh);
if (!model)
{
return 1;

View File

@@ -31,442 +31,7 @@
// for the grid data..
#include "../osghangglide/terrain_coords.h"
// class to create the forest and manage the movement between various techniques.
class ForestTechniqueManager : public osg::Referenced
{
public:
ForestTechniqueManager() {}
class Tree : public osg::Referenced
{
public:
Tree():
_color(255,255,255,255),
_width(1.0f),
_height(1.0f),
_type(0) {}
Tree(const osg::Vec3& position, const osg::Vec4ub& color, float width, float height, unsigned int type):
_position(position),
_color(color),
_width(width),
_height(height),
_type(type) {}
osg::Vec3 _position;
osg::Vec4ub _color;
float _width;
float _height;
unsigned int _type;
};
typedef std::vector< osg::ref_ptr<Tree> > TreeList;
class Cell : public osg::Referenced
{
public:
typedef std::vector< osg::ref_ptr<Cell> > CellList;
Cell():_parent(0) {}
Cell(osg::BoundingBox& bb):_parent(0), _bb(bb) {}
void addCell(Cell* cell) { cell->_parent=this; _cells.push_back(cell); }
void addTree(Tree* tree) { _trees.push_back(tree); }
void addTrees(const TreeList& trees) { _trees.insert(_trees.end(),trees.begin(),trees.end()); }
void computeBound();
bool contains(const osg::Vec3& position) const { return _bb.contains(position); }
bool divide(unsigned int maxNumTreesPerCell=10);
bool devide(bool xAxis, bool yAxis, bool zAxis);
void bin();
Cell* _parent;
osg::BoundingBox _bb;
CellList _cells;
TreeList _trees;
};
float random(float min,float max) { return min + (max-min)*(float)rand()/(float)RAND_MAX; }
int random(int min,int max) { return min + (int)((float)(max-min)*(float)rand()/(float)RAND_MAX); }
osg::Geode* createTerrain(const osg::Vec3& origin, const osg::Vec3& size);
void createTreeList(osg::Node* terrain,const osg::Vec3& origin, const osg::Vec3& size,unsigned int numTreesToCreate,TreeList& trees);
osg::Geometry* createOrthogonalQuadsNoColor( const osg::Vec3& pos, float w, float h );
osg::Node* createShaderGraph(Cell* cell,osg::StateSet* stateset);
osg::Node* createScene(unsigned int numTreesToCreates);
};
// event handler to capture keyboard events and use them to advance the technique used for rendering
void ForestTechniqueManager::Cell::computeBound()
{
_bb.init();
for(CellList::iterator citr=_cells.begin();
citr!=_cells.end();
++citr)
{
(*citr)->computeBound();
_bb.expandBy((*citr)->_bb);
}
for(TreeList::iterator titr=_trees.begin();
titr!=_trees.end();
++titr)
{
_bb.expandBy((*titr)->_position);
}
}
bool ForestTechniqueManager::Cell::divide(unsigned int maxNumTreesPerCell)
{
if (_trees.size()<=maxNumTreesPerCell) return false;
computeBound();
float radius = _bb.radius();
float divide_distance = radius*0.7f;
if (devide((_bb.xMax()-_bb.xMin())>divide_distance,(_bb.yMax()-_bb.yMin())>divide_distance,(_bb.zMax()-_bb.zMin())>divide_distance))
{
// recusively divide the new cells till maxNumTreesPerCell is met.
for(CellList::iterator citr=_cells.begin();
citr!=_cells.end();
++citr)
{
(*citr)->divide(maxNumTreesPerCell);
}
return true;
}
else
{
return false;
}
}
bool ForestTechniqueManager::Cell::devide(bool xAxis, bool yAxis, bool zAxis)
{
if (!(xAxis || yAxis || zAxis)) return false;
if (_cells.empty())
_cells.push_back(new Cell(_bb));
if (xAxis)
{
unsigned int numCellsToDivide=_cells.size();
for(unsigned int i=0;i<numCellsToDivide;++i)
{
Cell* orig_cell = _cells[i].get();
Cell* new_cell = new Cell(orig_cell->_bb);
float xCenter = (orig_cell->_bb.xMin()+orig_cell->_bb.xMax())*0.5f;
orig_cell->_bb.xMax() = xCenter;
new_cell->_bb.xMin() = xCenter;
_cells.push_back(new_cell);
}
}
if (yAxis)
{
unsigned int numCellsToDivide=_cells.size();
for(unsigned int i=0;i<numCellsToDivide;++i)
{
Cell* orig_cell = _cells[i].get();
Cell* new_cell = new Cell(orig_cell->_bb);
float yCenter = (orig_cell->_bb.yMin()+orig_cell->_bb.yMax())*0.5f;
orig_cell->_bb.yMax() = yCenter;
new_cell->_bb.yMin() = yCenter;
_cells.push_back(new_cell);
}
}
if (zAxis)
{
unsigned int numCellsToDivide=_cells.size();
for(unsigned int i=0;i<numCellsToDivide;++i)
{
Cell* orig_cell = _cells[i].get();
Cell* new_cell = new Cell(orig_cell->_bb);
float zCenter = (orig_cell->_bb.zMin()+orig_cell->_bb.zMax())*0.5f;
orig_cell->_bb.zMax() = zCenter;
new_cell->_bb.zMin() = zCenter;
_cells.push_back(new_cell);
}
}
bin();
return true;
}
void ForestTechniqueManager::Cell::bin()
{
// put trees in apprpriate cells.
TreeList treesNotAssigned;
for(TreeList::iterator titr=_trees.begin();
titr!=_trees.end();
++titr)
{
Tree* tree = titr->get();
bool assigned = false;
for(CellList::iterator citr=_cells.begin();
citr!=_cells.end() && !assigned;
++citr)
{
if ((*citr)->contains(tree->_position))
{
(*citr)->addTree(tree);
assigned = true;
}
}
if (!assigned) treesNotAssigned.push_back(tree);
}
// put the unassigned trees back into the original local tree list.
_trees.swap(treesNotAssigned);
// prune empty cells.
CellList cellsNotEmpty;
for(CellList::iterator citr=_cells.begin();
citr!=_cells.end();
++citr)
{
if (!((*citr)->_trees.empty()))
{
cellsNotEmpty.push_back(*citr);
}
}
_cells.swap(cellsNotEmpty);
}
void ForestTechniqueManager::createTreeList(osg::Node* terrain,const osg::Vec3& origin, const osg::Vec3& size,unsigned int numTreesToCreate,TreeList& trees)
{
float max_TreeHeight = sqrtf(size.length2()/(float)numTreesToCreate);
float max_TreeWidth = max_TreeHeight*0.5f;
float min_TreeHeight = max_TreeHeight*0.3f;
float min_TreeWidth = min_TreeHeight*0.5f;
trees.reserve(trees.size()+numTreesToCreate);
for(unsigned int i=0;i<numTreesToCreate;++i)
{
Tree* tree = new Tree;
tree->_position.set(random(origin.x(),origin.x()+size.x()),random(origin.y(),origin.y()+size.y()),origin.z());
tree->_color.set(random(128,255),random(128,255),random(128,255),255);
tree->_width = random(min_TreeWidth,max_TreeWidth);
tree->_height = random(min_TreeHeight,max_TreeHeight);
tree->_type = 0;
if (terrain)
{
osgUtil::IntersectVisitor iv;
osg::ref_ptr<osg::LineSegment> segDown = new osg::LineSegment;
segDown->set(tree->_position,tree->_position+osg::Vec3(0.0f,0.0f,size.z()));
iv.addLineSegment(segDown.get());
terrain->accept(iv);
if (iv.hits())
{
osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segDown.get());
if (!hitList.empty())
{
osg::Vec3 ip = hitList.front().getWorldIntersectPoint();
osg::Vec3 np = hitList.front().getWorldIntersectNormal();
tree->_position = ip;
}
}
}
trees.push_back(tree);
}
}
osg::Geometry* ForestTechniqueManager::createOrthogonalQuadsNoColor( const osg::Vec3& pos, float w, float h)
{
// set up the coords
osg::Vec3Array& v = *(new osg::Vec3Array(8));
osg::Vec2Array& t = *(new osg::Vec2Array(8));
float rotation = random(0.0f,osg::PI/2.0f);
float sw = sinf(rotation)*w*0.5f;
float cw = cosf(rotation)*w*0.5f;
v[0].set(pos.x()-sw,pos.y()-cw,pos.z()+0.0f);
v[1].set(pos.x()+sw,pos.y()+cw,pos.z()+0.0f);
v[2].set(pos.x()+sw,pos.y()+cw,pos.z()+h);
v[3].set(pos.x()-sw,pos.y()-cw,pos.z()+h);
v[4].set(pos.x()-cw,pos.y()+sw,pos.z()+0.0f);
v[5].set(pos.x()+cw,pos.y()-sw,pos.z()+0.0f);
v[6].set(pos.x()+cw,pos.y()-sw,pos.z()+h);
v[7].set(pos.x()-cw,pos.y()+sw,pos.z()+h);
t[0].set(0.0f,0.0f);
t[1].set(1.0f,0.0f);
t[2].set(1.0f,1.0f);
t[3].set(0.0f,1.0f);
t[4].set(0.0f,0.0f);
t[5].set(1.0f,0.0f);
t[6].set(1.0f,1.0f);
t[7].set(0.0f,1.0f);
osg::Geometry *geom = new osg::Geometry;
geom->setVertexArray( &v );
geom->setTexCoordArray( 0, &t );
geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,8) );
return geom;
}
class ShaderGeometry : public osg::Drawable
{
public:
ShaderGeometry() { setUseDisplayList(false); }
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
ShaderGeometry(const ShaderGeometry& ShaderGeometry,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
osg::Drawable(ShaderGeometry,copyop) {}
META_Object(osg,ShaderGeometry)
typedef std::vector<osg::Vec4> PositionSizeList;
virtual void drawImplementation(osg::RenderInfo& renderInfo) const
{
for(PositionSizeList::const_iterator itr = _trees.begin();
itr != _trees.end();
++itr)
{
glColor4fv(itr->ptr());
_geometry->draw(renderInfo);
}
}
virtual osg::BoundingBox computeBound() const
{
osg::BoundingBox geom_box = _geometry->getBound();
osg::BoundingBox bb;
for(PositionSizeList::const_iterator itr = _trees.begin();
itr != _trees.end();
++itr)
{
bb.expandBy(geom_box.corner(0)*(*itr)[3] +
osg::Vec3( (*itr)[0], (*itr)[1], (*itr)[2] ));
bb.expandBy(geom_box.corner(7)*(*itr)[3] +
osg::Vec3( (*itr)[0], (*itr)[1], (*itr)[2] ));
}
return bb;
}
void setGeometry(osg::Geometry* geometry)
{
_geometry = geometry;
}
void addTree(ForestTechniqueManager::Tree& tree)
{
_trees.push_back(osg::Vec4(tree._position.x(), tree._position.y(), tree._position.z(), tree._height));
}
osg::ref_ptr<osg::Geometry> _geometry;
PositionSizeList _trees;
protected:
virtual ~ShaderGeometry() {}
};
osg::Geometry* shared_geometry = 0;
osg::Node* ForestTechniqueManager::createShaderGraph(Cell* cell,osg::StateSet* stateset)
{
if (shared_geometry==0)
{
shared_geometry = createOrthogonalQuadsNoColor(osg::Vec3(0.0f,0.0f,0.0f),1.0f,1.0f);
//shared_geometry->setUseDisplayList(false);
}
bool needGroup = !(cell->_cells.empty());
bool needTrees = !(cell->_trees.empty());
osg::Geode* geode = 0;
osg::Group* group = 0;
if (needTrees)
{
geode = new osg::Geode;
ShaderGeometry* shader_geometry = new ShaderGeometry;
shader_geometry->setGeometry(shared_geometry);
for(TreeList::iterator itr=cell->_trees.begin();
itr!=cell->_trees.end();
++itr)
{
Tree& tree = **itr;
shader_geometry->addTree(tree);
}
geode->setStateSet(stateset);
geode->addDrawable(shader_geometry);
}
if (needGroup)
{
group = new osg::Group;
for(Cell::CellList::iterator itr=cell->_cells.begin();
itr!=cell->_cells.end();
++itr)
{
group->addChild(createShaderGraph(itr->get(),stateset));
}
if (geode) group->addChild(geode);
}
if (group) return group;
else return geode;
}
osg::Node* ForestTechniqueManager::createScene(unsigned int /*numTreesToCreates*/)
osg::Node* createScene()
{
osg::Group* scene = new osg::Group;
@@ -557,7 +122,7 @@ osg::Node* ForestTechniqueManager::createScene(unsigned int /*numTreesToCreates*
osg::Program* program = new osg::Program;
stateset->setAttribute(program);
#if 1
#if 0
// use inline shaders
///////////////////////////////////////////////////////////////////
@@ -576,7 +141,7 @@ osg::Node* ForestTechniqueManager::createScene(unsigned int /*numTreesToCreates*
" texcoord.x *= terrainScaleDown.x;\n"
" texcoord.y *= terrainScaleDown.y;\n"
"\n"
" vec4 position;\n"
" vec4 position;\n"ttm->
" position.x = gl_Vertex.x;\n"
" position.y = gl_Vertex.y;\n"
" position.z = texture2D(terrainTexture, texcoord).r;\n"
@@ -671,62 +236,6 @@ osg::Node* ForestTechniqueManager::createScene(unsigned int /*numTreesToCreates*
std::cout<<"done."<<std::endl;
#if 0
std::cout<<"Creating tree locations...";std::cout.flush();
TreeList trees;
createTreeList(0,origin,size,numTreesToCreates,trees);
std::cout<<"done."<<std::endl;
std::cout<<"Creating cell subdivision...";
osg::ref_ptr<Cell> cell = new Cell;
cell->addTrees(trees);
cell->divide();
std::cout<<"done."<<std::endl;
osg::Texture2D *tex = new osg::Texture2D;
tex->setWrap( osg::Texture2D::WRAP_S, osg::Texture2D::CLAMP );
tex->setWrap( osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP );
tex->setImage(osgDB::readImageFile("Images/tree0.rgba"));
tex->setResizeNonPowerOfTwoHint(false);
osg::StateSet *dstate = new osg::StateSet;
{
dstate->setTextureAttributeAndModes(1, tex, osg::StateAttribute::ON );
dstate->setAttributeAndModes( new osg::BlendFunc, osg::StateAttribute::ON );
osg::AlphaFunc* alphaFunc = new osg::AlphaFunc;
alphaFunc->setFunction(osg::AlphaFunc::GEQUAL,0.05f);
dstate->setAttributeAndModes( alphaFunc, osg::StateAttribute::ON );
dstate->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
dstate->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
}
{
osg::StateSet* stateset = new osg::StateSet;
stateset->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON );
stateset->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
{
osg::Program* program = new osg::Program;
stateset->setAttribute(program);
// get shaders from source
program->addShader(osg::Shader::readShaderFile(osg::Shader::VERTEX, osgDB::findDataFile("shaders/forest2.vert")));
program->addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, osgDB::findDataFile("shaders/forest2.frag")));
}
std::cout<<"Creating billboard based forest...";
scene->addChild(createShaderGraph(cell.get(),stateset));
}
#endif
return scene;
}
@@ -784,14 +293,9 @@ int main( int argc, char **argv )
// construct the viewer.
osgProducer::Viewer viewer(arguments);
float numTreesToCreates = 10000;
arguments.read("--trees",numTreesToCreates);
// set up the value with sensible default event handlers.
viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
osg::ref_ptr<ForestTechniqueManager> ttm = new ForestTechniqueManager;
// get details on keyboard and mouse bindings used by the viewer.
viewer.getUsage(*arguments.getApplicationUsage());
@@ -812,7 +316,7 @@ int main( int argc, char **argv )
return 1;
}
osg::Node* node = ttm->createScene((unsigned int)numTreesToCreates);
osg::Node* node = createScene();
// add model to viewer.
viewer.setSceneData( node );