2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osgparametric.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2005-06-21 05:10:32 +08:00
|
|
|
#include <osg/Vec3>
|
|
|
|
#include <osg/Vec4>
|
|
|
|
#include <osg/Quat>
|
|
|
|
#include <osg/Matrix>
|
|
|
|
#include <osg/ShapeDrawable>
|
|
|
|
#include <osg/Geometry>
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osg/Texture2D>
|
|
|
|
|
|
|
|
#include <osgDB/FileUtils>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
|
|
|
#include <osgUtil/Optimizer>
|
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2007-06-07 00:23:20 +08:00
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
2007-01-08 18:00:16 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
2005-06-21 05:10:32 +08:00
|
|
|
|
2005-06-22 00:10:04 +08:00
|
|
|
// for the grid data..
|
|
|
|
#include "../osghangglide/terrain_coords.h"
|
|
|
|
|
2005-06-21 23:24:23 +08:00
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
// vertex shader using just Vec4 coefficients
|
2015-10-22 21:42:19 +08:00
|
|
|
char vertexShaderSource_simple[] =
|
2005-06-21 23:13:42 +08:00
|
|
|
"uniform vec4 coeff; \n"
|
2005-06-21 05:10:32 +08:00
|
|
|
"\n"
|
|
|
|
"void main(void) \n"
|
|
|
|
"{ \n"
|
|
|
|
"\n"
|
|
|
|
" gl_TexCoord[0] = gl_Vertex; \n"
|
2005-06-22 19:26:56 +08:00
|
|
|
" vec4 vert = gl_Vertex; \n"
|
|
|
|
" vert.z = gl_Vertex.x*coeff[0] + gl_Vertex.x*gl_Vertex.x* coeff[1] + \n"
|
|
|
|
" gl_Vertex.y*coeff[2] + gl_Vertex.y*gl_Vertex.y* coeff[3]; \n"
|
|
|
|
" gl_Position = gl_ModelViewProjectionMatrix * vert;\n"
|
2005-06-21 05:10:32 +08:00
|
|
|
"}\n";
|
2015-10-22 21:42:19 +08:00
|
|
|
|
|
|
|
|
2005-06-21 23:24:23 +08:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// vertex shader using full Matrix4 coefficients
|
2015-10-22 21:42:19 +08:00
|
|
|
char vertexShaderSource_matrix[] =
|
2005-06-21 23:24:23 +08:00
|
|
|
"uniform vec4 origin; \n"
|
|
|
|
"uniform mat4 coeffMatrix; \n"
|
|
|
|
"\n"
|
|
|
|
"void main(void) \n"
|
|
|
|
"{ \n"
|
|
|
|
"\n"
|
|
|
|
" gl_TexCoord[0] = gl_Vertex; \n"
|
|
|
|
" vec4 v = vec4(gl_Vertex.x, gl_Vertex.x*gl_Vertex.x, gl_Vertex.y, gl_Vertex.y*gl_Vertex.y ); \n"
|
|
|
|
" gl_Position = gl_ModelViewProjectionMatrix * (origin + coeffMatrix * v);\n"
|
|
|
|
"}\n";
|
|
|
|
|
2005-06-22 00:10:04 +08:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// vertex shader using texture read
|
2015-10-22 21:42:19 +08:00
|
|
|
char vertexShaderSource_texture[] =
|
2005-06-22 00:10:04 +08:00
|
|
|
"uniform sampler2D vertexTexture; \n"
|
|
|
|
"\n"
|
|
|
|
"void main(void) \n"
|
|
|
|
"{ \n"
|
|
|
|
"\n"
|
|
|
|
" gl_TexCoord[0] = gl_Vertex; \n"
|
2005-06-22 19:26:56 +08:00
|
|
|
" vec4 vert = gl_Vertex; \n"
|
2007-01-15 17:14:36 +08:00
|
|
|
" vert.z = texture2D( vertexTexture, gl_TexCoord[0].xy).x*0.0001; \n"
|
2005-06-22 19:26:56 +08:00
|
|
|
" gl_Position = gl_ModelViewProjectionMatrix * vert;\n"
|
2005-06-22 00:10:04 +08:00
|
|
|
"}\n";
|
|
|
|
|
2005-06-21 23:24:23 +08:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// fragment shader
|
|
|
|
//
|
2015-10-22 21:42:19 +08:00
|
|
|
char fragmentShaderSource[] =
|
2005-06-21 05:10:32 +08:00
|
|
|
"uniform sampler2D baseTexture; \n"
|
|
|
|
"\n"
|
|
|
|
"void main(void) \n"
|
|
|
|
"{ \n"
|
|
|
|
" gl_FragColor = texture2D( baseTexture, gl_TexCoord[0].xy); \n"
|
|
|
|
"}\n";
|
|
|
|
|
2007-01-15 17:14:36 +08:00
|
|
|
|
2005-06-21 23:24:23 +08:00
|
|
|
|
2015-06-03 17:07:41 +08:00
|
|
|
class UniformVarying : public osg::UniformCallback
|
2005-06-21 05:10:32 +08:00
|
|
|
{
|
|
|
|
virtual void operator () (osg::Uniform* uniform, osg::NodeVisitor* nv)
|
|
|
|
{
|
|
|
|
const osg::FrameStamp* fs = nv->getFrameStamp();
|
2007-01-25 20:02:51 +08:00
|
|
|
float value = sinf(fs->getSimulationTime());
|
2005-06-21 23:13:42 +08:00
|
|
|
uniform->set(osg::Vec4(value,-value,-value,value));
|
2005-06-21 05:10:32 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-10-02 04:19:42 +08:00
|
|
|
osg::Node* createModel(const std::string& shader, const std::string& textureFileName, const std::string& terrainFileName, bool dynamic, bool useVBO)
|
2005-06-21 05:10:32 +08:00
|
|
|
{
|
|
|
|
osg::Geode* geode = new osg::Geode;
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2005-06-21 05:10:32 +08:00
|
|
|
osg::Geometry* geom = new osg::Geometry;
|
|
|
|
geode->addDrawable(geom);
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2005-06-21 05:10:32 +08:00
|
|
|
// dimensions for ~one million triangles :-)
|
|
|
|
unsigned int num_x = 708;
|
|
|
|
unsigned int num_y = 708;
|
|
|
|
|
2005-06-22 00:10:04 +08:00
|
|
|
// set up state
|
|
|
|
{
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2005-06-22 00:10:04 +08:00
|
|
|
osg::StateSet* stateset = geom->getOrCreateStateSet();
|
|
|
|
|
|
|
|
osg::Program* program = new osg::Program;
|
|
|
|
stateset->setAttribute(program);
|
|
|
|
|
|
|
|
if (shader=="simple")
|
|
|
|
{
|
|
|
|
osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_simple);
|
|
|
|
program->addShader(vertex_shader);
|
|
|
|
|
|
|
|
osg::Uniform* coeff = new osg::Uniform("coeff",osg::Vec4(1.0,-1.0f,-1.0f,1.0f));
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2005-06-22 00:10:04 +08:00
|
|
|
stateset->addUniform(coeff);
|
2007-04-25 23:32:33 +08:00
|
|
|
|
|
|
|
if (dynamic)
|
|
|
|
{
|
|
|
|
coeff->setUpdateCallback(new UniformVarying);
|
|
|
|
coeff->setDataVariance(osg::Object::DYNAMIC);
|
|
|
|
stateset->setDataVariance(osg::Object::DYNAMIC);
|
|
|
|
}
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2005-06-22 00:10:04 +08:00
|
|
|
}
|
|
|
|
else if (shader=="matrix")
|
|
|
|
{
|
|
|
|
osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_matrix);
|
|
|
|
program->addShader(vertex_shader);
|
|
|
|
|
|
|
|
osg::Uniform* origin = new osg::Uniform("origin",osg::Vec4(0.0f,0.0f,0.0f,1.0f));
|
|
|
|
stateset->addUniform(origin);
|
|
|
|
|
|
|
|
osg::Uniform* coeffMatrix = new osg::Uniform("coeffMatrix",
|
|
|
|
osg::Matrix(1.0f,0.0f,1.0f,0.0f,
|
|
|
|
0.0f,0.0f,-1.0f,0.0f,
|
|
|
|
0.0f,1.0f,-1.0f,0.0f,
|
|
|
|
0.0f,0.0f,1.0f,0.0f));
|
|
|
|
|
|
|
|
stateset->addUniform(coeffMatrix);
|
|
|
|
}
|
|
|
|
else if (shader=="texture")
|
|
|
|
{
|
2007-01-15 17:14:36 +08:00
|
|
|
osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_texture);
|
|
|
|
program->addShader(vertex_shader);
|
2005-06-22 00:10:04 +08:00
|
|
|
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Image> image;
|
2005-06-22 00:10:04 +08:00
|
|
|
|
|
|
|
if (terrainFileName.empty())
|
|
|
|
{
|
|
|
|
image = new osg::Image;
|
|
|
|
unsigned int tx = 38;
|
|
|
|
unsigned int ty = 39;
|
|
|
|
image->allocateImage(tx,ty,1,GL_LUMINANCE,GL_FLOAT,1);
|
|
|
|
for(unsigned int r=0;r<ty;++r)
|
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
for(unsigned int c=0;c<tx;++c)
|
|
|
|
{
|
|
|
|
*((float*)image->data(c,r)) = vertex[r+c*39][2]*0.1;
|
|
|
|
}
|
2005-06-22 00:10:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
num_x = tx;
|
|
|
|
num_y = tx;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-22 21:42:19 +08:00
|
|
|
image = osgDB::readRefImageFile(terrainFileName);
|
2005-06-22 00:10:04 +08:00
|
|
|
|
|
|
|
num_x = image->s();
|
|
|
|
num_y = image->t();
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Texture2D* vertexTexture = new osg::Texture2D(image);
|
|
|
|
|
|
|
|
|
|
|
|
vertexTexture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST);
|
|
|
|
vertexTexture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST);
|
|
|
|
vertexTexture->setInternalFormat(GL_LUMINANCE_FLOAT32_ATI);
|
|
|
|
stateset->setTextureAttributeAndModes(1,vertexTexture);
|
|
|
|
|
|
|
|
osg::Uniform* vertexTextureSampler = new osg::Uniform("vertexTexture",1);
|
|
|
|
stateset->addUniform(vertexTextureSampler);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-01-15 17:14:36 +08:00
|
|
|
osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource);
|
|
|
|
program->addShader(fragment_shader);
|
2005-06-22 00:10:04 +08:00
|
|
|
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::Texture2D* texture = new osg::Texture2D(osgDB::readRefImageFile(textureFileName));
|
2005-06-22 00:10:04 +08:00
|
|
|
stateset->setTextureAttributeAndModes(0,texture);
|
|
|
|
|
|
|
|
osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0);
|
|
|
|
stateset->addUniform(baseTextureSampler);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// set up geometry data.
|
|
|
|
|
2005-06-21 05:10:32 +08:00
|
|
|
osg::Vec3Array* vertices = new osg::Vec3Array( num_x * num_y );
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2005-06-21 05:10:32 +08:00
|
|
|
float dx = 1.0f/(float)(num_x-1);
|
|
|
|
float dy = 1.0f/(float)(num_y-1);
|
|
|
|
osg::Vec3 row(0.0f,0.0f,0.0);
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2005-06-21 05:10:32 +08:00
|
|
|
unsigned int vert_no = 0;
|
2005-11-11 03:32:37 +08:00
|
|
|
unsigned int iy;
|
|
|
|
for(iy=0; iy<num_y; ++iy)
|
2005-06-21 05:10:32 +08:00
|
|
|
{
|
|
|
|
osg::Vec3 column = row;
|
|
|
|
for(unsigned int ix=0;ix<num_x;++ix)
|
|
|
|
{
|
|
|
|
(*vertices)[vert_no++] = column;
|
|
|
|
column.x() += dx;
|
2015-10-22 21:42:19 +08:00
|
|
|
}
|
2005-06-21 05:10:32 +08:00
|
|
|
row.y() += dy;
|
|
|
|
}
|
|
|
|
|
|
|
|
geom->setVertexArray(vertices);
|
|
|
|
|
2009-10-02 04:19:42 +08:00
|
|
|
osg::VertexBufferObject* vbo = useVBO ? new osg::VertexBufferObject : 0;
|
|
|
|
if (vbo) vertices->setVertexBufferObject(vbo);
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2009-10-02 04:19:42 +08:00
|
|
|
osg::ElementBufferObject* ebo = useVBO ? new osg::ElementBufferObject : 0;
|
2007-04-27 00:50:35 +08:00
|
|
|
|
2007-01-15 17:14:36 +08:00
|
|
|
for(iy=0; iy<num_y-1; ++iy)
|
2005-06-21 05:10:32 +08:00
|
|
|
{
|
2007-01-15 17:14:36 +08:00
|
|
|
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)
|
2007-01-02 02:20:10 +08:00
|
|
|
{
|
2007-01-15 17:14:36 +08:00
|
|
|
(*elements)[element_no++] = index + num_x;
|
|
|
|
(*elements)[element_no++] = index++;
|
2005-06-21 05:10:32 +08:00
|
|
|
}
|
2015-10-22 21:42:19 +08:00
|
|
|
geom->addPrimitiveSet(elements);
|
|
|
|
|
2009-10-02 04:19:42 +08:00
|
|
|
if (ebo) elements->setElementBufferObject(ebo);
|
2005-06-21 05:10:32 +08:00
|
|
|
}
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2009-12-15 22:45:33 +08:00
|
|
|
geom->setUseVertexBufferObjects(useVBO);
|
2005-06-21 20:12:50 +08:00
|
|
|
|
2005-06-21 05:10:32 +08:00
|
|
|
return geode;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
// use an ArgumentParser object to manage the program arguments.
|
|
|
|
osg::ArgumentParser arguments(&argc,argv);
|
|
|
|
|
|
|
|
// set up the usage document, in case we need to print out how to use this program.
|
|
|
|
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrate support for ARB_vertex_program.");
|
|
|
|
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
|
|
|
|
arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2005-06-21 05:10:32 +08:00
|
|
|
// construct the viewer.
|
2007-01-08 18:00:16 +08:00
|
|
|
osgViewer::Viewer viewer;
|
2005-06-21 05:10:32 +08:00
|
|
|
|
2007-04-25 23:32:33 +08:00
|
|
|
// add the stats handler
|
|
|
|
viewer.addEventHandler(new osgViewer::StatsHandler);
|
|
|
|
|
2005-06-21 23:24:23 +08:00
|
|
|
std::string shader("simple");
|
|
|
|
while(arguments.read("-s",shader)) {}
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2005-11-02 22:24:29 +08:00
|
|
|
std::string textureFileName("Images/lz.rgb");
|
2005-06-22 00:10:04 +08:00
|
|
|
while(arguments.read("-t",textureFileName)) {}
|
|
|
|
|
|
|
|
std::string terrainFileName("");
|
|
|
|
while(arguments.read("-d",terrainFileName)) {}
|
2005-06-21 05:10:32 +08:00
|
|
|
|
2007-04-25 23:32:33 +08:00
|
|
|
bool dynamic = true;
|
|
|
|
while(arguments.read("--static")) { dynamic = false; }
|
|
|
|
|
|
|
|
bool vbo = false;
|
|
|
|
while(arguments.read("--vbo")) { vbo = true; }
|
|
|
|
|
2005-06-21 05:10:32 +08:00
|
|
|
// if user request help write it out to cout.
|
|
|
|
if (arguments.read("-h") || arguments.read("--help"))
|
|
|
|
{
|
|
|
|
arguments.getApplicationUsage()->write(std::cout);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2005-06-21 05:10:32 +08:00
|
|
|
// load the nodes from the commandline arguments.
|
2007-04-25 23:32:33 +08:00
|
|
|
osg::Node* model = createModel(shader,textureFileName,terrainFileName, dynamic, vbo);
|
2005-06-21 05:10:32 +08:00
|
|
|
if (!model)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
viewer.setSceneData(model);
|
2005-06-21 05:10:32 +08:00
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
return viewer.run();
|
2005-06-21 05:10:32 +08:00
|
|
|
}
|