2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osgtexturerectangle.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2003-05-20 22:05:19 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* demonstrates usage of osg::TextureRectangle.
|
|
|
|
*
|
|
|
|
* Actually there isn't much difference to the rest of the osg::Texture*
|
|
|
|
* bunch only this:
|
|
|
|
* - texture coordinates for texture rectangles must be in image
|
|
|
|
* coordinates instead of normalized coordinates (0-1). So for a 500x250
|
|
|
|
* image the coordinates for the entire image would be
|
|
|
|
* 0,250 0,0 500,0 500,250 instead of 0,1 0,0 1,0 1,1
|
|
|
|
* - only the following wrap modes are supported (but not enforced)
|
|
|
|
* CLAMP, CLAMP_TO_EDGE, CLAMP_TO_BORDER
|
|
|
|
* - a border is not supported
|
|
|
|
* - mipmap is not supported
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <osg/Notify>
|
|
|
|
#include <osg/TextureRectangle>
|
|
|
|
#include <osg/Geometry>
|
|
|
|
#include <osg/Geode>
|
2006-09-04 20:46:49 +08:00
|
|
|
#include <osg/TexMat>
|
2003-05-20 22:05:19 +08:00
|
|
|
|
2003-06-24 23:40:09 +08:00
|
|
|
#include <osg/Group>
|
2003-05-20 22:05:19 +08:00
|
|
|
#include <osg/Projection>
|
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
#include <osgText/Text>
|
|
|
|
|
|
|
|
#include <osgDB/Registry>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
2007-01-11 23:19:59 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2003-05-20 22:05:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
*
|
|
|
|
* Texture pan animation callback
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
class TexturePanCallback : public osg::NodeCallback
|
|
|
|
{
|
|
|
|
public:
|
2006-09-04 20:46:49 +08:00
|
|
|
TexturePanCallback(osg::TexMat* texmat,
|
2003-05-20 22:05:19 +08:00
|
|
|
double delay = 0.05) :
|
2006-09-04 20:46:49 +08:00
|
|
|
_texmat(texmat),
|
2003-05-20 22:05:19 +08:00
|
|
|
_phaseS(35.0f),
|
|
|
|
_phaseT(18.0f),
|
|
|
|
_phaseScale(5.0f),
|
|
|
|
_delay(delay),
|
|
|
|
_prevTime(0.0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void operator()(osg::Node*, osg::NodeVisitor* nv)
|
|
|
|
{
|
2006-09-04 20:46:49 +08:00
|
|
|
if (!_texmat)
|
2003-05-20 22:05:19 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (nv->getFrameStamp()) {
|
2007-01-25 20:02:51 +08:00
|
|
|
double currTime = nv->getFrameStamp()->getSimulationTime();
|
2003-05-20 22:05:19 +08:00
|
|
|
if (currTime - _prevTime > _delay) {
|
|
|
|
|
|
|
|
float rad = osg::DegreesToRadians(currTime);
|
|
|
|
|
|
|
|
// zoom scale (0.2 - 1.0)
|
|
|
|
float scale = sin(rad * _phaseScale) * 0.4f + 0.6f;
|
|
|
|
float scaleR = 1.0f - scale;
|
|
|
|
|
|
|
|
// calculate new texture coordinates
|
|
|
|
float s, t;
|
2006-09-04 20:46:49 +08:00
|
|
|
s = ((sin(rad * _phaseS) + 1) * 0.5f) * (scaleR);
|
|
|
|
t = ((sin(rad * _phaseT) + 1) * 0.5f) * (scaleR);
|
2003-05-20 22:05:19 +08:00
|
|
|
|
2006-09-04 20:46:49 +08:00
|
|
|
|
|
|
|
_texmat->setMatrix(osg::Matrix::translate(s,t,1.0)*osg::Matrix::scale(scale,scale,1.0));
|
2003-05-20 22:05:19 +08:00
|
|
|
|
|
|
|
// record time
|
|
|
|
_prevTime = currTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2006-09-04 20:46:49 +08:00
|
|
|
osg::TexMat* _texmat;
|
2003-05-20 22:05:19 +08:00
|
|
|
|
|
|
|
float _phaseS, _phaseT, _phaseScale;
|
|
|
|
|
|
|
|
double _delay;
|
|
|
|
double _prevTime;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
osg::Node* createRectangle(osg::BoundingBox& bb,
|
|
|
|
const std::string& filename)
|
|
|
|
{
|
|
|
|
osg::Vec3 top_left(bb.xMin(),bb.yMax(),bb.zMax());
|
|
|
|
osg::Vec3 bottom_left(bb.xMin(),bb.yMax(),bb.zMin());
|
|
|
|
osg::Vec3 bottom_right(bb.xMax(),bb.yMax(),bb.zMin());
|
|
|
|
osg::Vec3 top_right(bb.xMax(),bb.yMax(),bb.zMax());
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-05-20 22:05:19 +08:00
|
|
|
// create geometry
|
|
|
|
osg::Geometry* geom = new osg::Geometry;
|
|
|
|
|
|
|
|
osg::Vec3Array* vertices = new osg::Vec3Array(4);
|
|
|
|
(*vertices)[0] = top_left;
|
|
|
|
(*vertices)[1] = bottom_left;
|
|
|
|
(*vertices)[2] = bottom_right;
|
|
|
|
(*vertices)[3] = top_right;
|
|
|
|
geom->setVertexArray(vertices);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-05-20 22:05:19 +08:00
|
|
|
osg::Vec2Array* texcoords = new osg::Vec2Array(4);
|
|
|
|
(*texcoords)[0].set(0.0f, 0.0f);
|
|
|
|
(*texcoords)[1].set(1.0f, 0.0f);
|
|
|
|
(*texcoords)[2].set(1.0f, 1.0f);
|
|
|
|
(*texcoords)[3].set(0.0f, 1.0f);
|
|
|
|
geom->setTexCoordArray(0,texcoords);
|
|
|
|
|
|
|
|
osg::Vec3Array* normals = new osg::Vec3Array(1);
|
2004-03-11 17:04:02 +08:00
|
|
|
(*normals)[0].set(0.0f,-1.0f,0.0f);
|
2013-06-27 17:54:12 +08:00
|
|
|
geom->setNormalArray(normals, osg::Array::BIND_OVERALL);
|
2003-05-20 22:05:19 +08:00
|
|
|
|
|
|
|
osg::Vec4Array* colors = new osg::Vec4Array(1);
|
|
|
|
(*colors)[0].set(1.0f,1.0f,1.0f,1.0f);
|
2013-06-27 17:54:12 +08:00
|
|
|
geom->setColorArray(colors, osg::Array::BIND_OVERALL);
|
2003-05-20 22:05:19 +08:00
|
|
|
|
|
|
|
geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));
|
|
|
|
|
|
|
|
// disable display list so our modified tex coordinates show up
|
|
|
|
geom->setUseDisplayList(false);
|
|
|
|
|
|
|
|
// load image
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Image> img = osgDB::readRefImageFile(filename);
|
2003-06-24 23:40:09 +08:00
|
|
|
|
2006-09-04 20:52:09 +08:00
|
|
|
// setup texture
|
|
|
|
osg::TextureRectangle* texture = new osg::TextureRectangle(img);
|
2003-05-20 22:05:19 +08:00
|
|
|
|
2006-09-04 20:46:49 +08:00
|
|
|
osg::TexMat* texmat = new osg::TexMat;
|
|
|
|
texmat->setScaleByTextureRectangleSize(true);
|
2006-09-04 20:52:09 +08:00
|
|
|
|
|
|
|
// setup state
|
|
|
|
osg::StateSet* state = geom->getOrCreateStateSet();
|
|
|
|
state->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
|
2006-09-04 20:46:49 +08:00
|
|
|
state->setTextureAttributeAndModes(0, texmat, osg::StateAttribute::ON);
|
|
|
|
|
2013-06-27 17:54:12 +08:00
|
|
|
// turn off lighting
|
2003-05-20 22:05:19 +08:00
|
|
|
state->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
|
|
|
|
|
|
|
|
// install 'update' callback
|
2003-06-24 23:40:09 +08:00
|
|
|
osg::Geode* geode = new osg::Geode;
|
|
|
|
geode->addDrawable(geom);
|
2006-09-04 20:46:49 +08:00
|
|
|
geode->setUpdateCallback(new TexturePanCallback(texmat));
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-05-20 22:05:19 +08:00
|
|
|
return geode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
osg::Geode* createText(const std::string& str,
|
|
|
|
const osg::Vec3& pos)
|
|
|
|
{
|
|
|
|
static std::string font("fonts/arial.ttf");
|
|
|
|
|
|
|
|
osg::Geode* geode = new osg::Geode;
|
|
|
|
|
|
|
|
osgText::Text* text = new osgText::Text;
|
|
|
|
geode->addDrawable(text);
|
|
|
|
|
|
|
|
text->setFont(font);
|
|
|
|
text->setPosition(pos);
|
|
|
|
text->setText(str);
|
|
|
|
|
|
|
|
return geode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
osg::Node* createHUD()
|
|
|
|
{
|
|
|
|
osg::Group* group = new osg::Group;
|
|
|
|
|
|
|
|
// turn off lighting and depth test
|
|
|
|
osg::StateSet* state = group->getOrCreateStateSet();
|
|
|
|
state->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
|
|
|
|
state->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
|
|
|
|
|
|
|
|
// add text
|
2003-06-24 23:40:09 +08:00
|
|
|
osg::Vec3 pos(120.0f, 800.0f, 0.0f);
|
2003-05-20 22:05:19 +08:00
|
|
|
const osg::Vec3 delta(0.0f, -80.0f, 0.0f);
|
|
|
|
|
|
|
|
const char* text[] = {
|
|
|
|
"TextureRectangle Mini-HOWTO",
|
|
|
|
"- essentially behaves like Texture2D, *except* that:",
|
2006-09-04 20:46:49 +08:00
|
|
|
"- tex coords must be non-normalized (0..pixel) instead of (0..1),\nalternatively you can use osg::TexMat to scale normal non dimensional texcoords.",
|
2003-06-24 23:40:09 +08:00
|
|
|
"- wrap modes must be CLAMP, CLAMP_TO_EDGE, or CLAMP_TO_BORDER\n repeating wrap modes are not supported",
|
|
|
|
"- filter modes must be NEAREST or LINEAR since\n mipmaps are not supported",
|
|
|
|
"- texture borders are not supported",
|
|
|
|
"- defaults should be fine",
|
2003-05-20 22:05:19 +08:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
const char** t = text;
|
|
|
|
while (*t) {
|
|
|
|
group->addChild(createText(*t++, pos));
|
|
|
|
pos += delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create HUD
|
|
|
|
osg::MatrixTransform* modelview_abs = new osg::MatrixTransform;
|
2004-10-25 04:04:00 +08:00
|
|
|
modelview_abs->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
|
2003-05-20 22:05:19 +08:00
|
|
|
modelview_abs->setMatrix(osg::Matrix::identity());
|
|
|
|
modelview_abs->addChild(group);
|
|
|
|
|
|
|
|
osg::Projection* projection = new osg::Projection;
|
|
|
|
projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
|
|
|
|
projection->addChild(modelview_abs);
|
|
|
|
|
|
|
|
return projection;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
osg::Node* createModel(const std::string& filename)
|
|
|
|
{
|
|
|
|
osg::Group* root = new osg::Group;
|
2003-06-24 23:40:09 +08:00
|
|
|
|
|
|
|
if (filename != "X") {
|
|
|
|
osg::BoundingBox bb(0.0f,0.0f,0.0f,1.0f,1.0f,1.0f);
|
|
|
|
root->addChild(createRectangle(bb, filename)); // XXX
|
|
|
|
}
|
2003-05-20 22:05:19 +08:00
|
|
|
|
|
|
|
root->addChild(createHUD());
|
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
// use an ArgumentParser object to manage the program arguments.
|
|
|
|
osg::ArgumentParser arguments(&argc,argv);
|
|
|
|
|
|
|
|
// construct the viewer.
|
2007-01-11 23:19:59 +08:00
|
|
|
osgViewer::Viewer viewer;
|
2003-05-20 22:05:19 +08:00
|
|
|
|
|
|
|
// create a model from the images.
|
2003-07-22 16:42:14 +08:00
|
|
|
osg::Node* rootNode = createModel((arguments.argc() > 1 ? arguments[1] : "Images/lz.rgb"));
|
2003-05-20 22:05:19 +08:00
|
|
|
|
|
|
|
// add model to viewer.
|
|
|
|
viewer.setSceneData(rootNode);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-01-11 23:19:59 +08:00
|
|
|
return viewer.run();
|
2003-05-20 22:05:19 +08:00
|
|
|
}
|