osg.TextureRectangle
类封装了用于2D纹理映射的纹理。它提供了一种方便的方式来加载和使用图片、图形和其他纹理映射资源。
创建一个新的osg.TextureRectangle
对象:
osg::ref_ptr<osg::TextureRectangle> texture = new osg::TextureRectangle();
将一个图片文件加载到纹理中:
osg::ref_ptr<osg::Image> image = osgDB::readImageFile("image.png");
texture->setImage(image.get());
设置纹理的过滤方式:
texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
将纹理应用于节点:
osg::ref_ptr<osg::Geometry> geometry = createRectangleGeometry();
osg::StateSet* state = geometry->getOrCreateStateSet();
state->setTextureAttributeAndModes(0, texture.get());
TextureRectangle()
:默认构造函数。TextureRectangle(unsigned int texnum)
:构造函数,使用指定的纹理单元号码。TextureRectangle(const TextureRectangle& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY)
:拷贝构造函数,使用指定纹理和拷贝操作。void setImage(osg::Image* image)
:设置纹理的图像。osg::Image* getImage()
:获取纹理的图像。void setBorderColor(const osg::Vec4& color)
:设置纹理边框的颜色。osg::Vec4 getBorderColor() const
:获取纹理边框的颜色。void setWrap(osg::Texture::WrapParameter wrapS, osg::Texture::WrapParameter wrapT)
:设置纹理的环绕方式。void setFilter(osg::Texture::FilterMode magFilter, osg::Texture::FilterMode minFilter)
:设置纹理的过滤模式。void setMaxAnisotropy(float anisotropy)
:设置纹理的最大各向异性。bool getUseHardwareMipMapGeneration() const
:获取纹理是否使用硬件生成。void setUseHardwareMipMapGeneration(bool on_off)
:设置纹理是否使用硬件生成。void setUnRefImageDataAfterApply(bool flag)
:设置在应用纹理后是否释放图像数据。bool getUnRefImageDataAfterApply() const
:获在应用纹理后是否释放图像数据。#include <osg/Geode>
#include <osg/Geometry>
#include <osg/TextureRectangle>
#include <osgDB/ReadFile>
osg::ref_ptr<osg::Geometry> createRectangleGeometry()
{
osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry();
osg::Vec3Array* vertices = new osg::Vec3Array(4);
(*vertices)[0] = osg::Vec3(-1.0f, -1.0f, 0.0f);
(*vertices)[1] = osg::Vec3( 1.0f, -1.0f, 0.0f);
(*vertices)[2] = osg::Vec3( 1.0f, 1.0f, 0.0f);
(*vertices)[3] = osg::Vec3(-1.0f, 1.0f, 0.0f);
geometry->setVertexArray(vertices);
osg::Vec2Array* texcoords = new osg::Vec2Array(4);
(*texcoords)[0] = osg::Vec2(0.0f, 0.0f);
(*texcoords)[1] = osg::Vec2(1.0f, 0.0f);
(*texcoords)[2] = osg::Vec2(1.0f, 1.0f);
(*texcoords)[3] = osg::Vec2(0.0f, 1.0f);
geometry->setTexCoordArray(0, texcoords);
osg::DrawElementsUShort* indices = new osg::DrawElementsUShort(osg::PrimitiveSet::QUADS, 4);
(*indices)[0] = 0;
(*indices)[1] = 1;
(*indices)[2] = 2;
(*indices)[3] = 3;
geometry->addPrimitiveSet(indices);
return geometry;
}
int main(int argc, char** argv)
{
osg::ref_ptr<osg::Referenced> init = osg::Referenced::newRef();
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits();
traits->windowName = "osgtexrect";
traits->screenNum = 0;
traits->x = 50;
traits->y = 50;
traits->width = 800;
traits->height = 600;
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
osg::ref_ptr<osg::Camera> camera = new osg::Camera();
camera->setClearColor(osg::Vec4(0.5, 0.5, 1.0, 1.0));
camera->setGraphicsContext(gc.get());
camera->setViewport(0, 0, traits->width, traits->height);
camera->setProjectionMatrixAsPerspective(45.0, (double)traits->width / (double)traits->height, 0.1, 100.0);
osg::ref_ptr<osg::Group> root = new osg::Group();
root->addChild(camera);
osg::ref_ptr<osg::Geometry> geometry = createRectangleGeometry();
osg::ref_ptr<osg::TextureRectangle> texture = new osg::TextureRectangle();
osg::ref_ptr<osg::Image> image = osgDB::readImageFile("image.png");
texture->setImage(image.get());
texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
osg::StateSet* state = geometry->getOrCreateStateSet();
state->setTextureAttributeAndModes(0, texture.get());
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
geode->addDrawable(geometry.get());
root->addChild(geode.get());
osgViewer::Viewer viewer;
viewer.setSceneData(root.get());
return viewer.run();
}