这节课主要介绍了如何使用OpenGL中的模板缓冲区和裁剪平面来产生镜子反射的效果,关于模板缓冲区的内容可以参考另一篇文章OpenGL模板缓冲区---Stencil Buffer
首先我们需要在模板缓冲区中将地面(类似镜子)绘制出来,同时并不会在颜色缓冲区中绘制出镜面。通过设置颜色掩码glColorMask可以做到这一点
//创建模板缓冲区中的镜面(不可见) //深度测试关闭意味着深度测试总是通过 osg::Geode *stencilFloorGeode = createFloor(); osg::Stencil *floorStencil = new osg::Stencil; floorStencil->setFunction(osg::Stencil::ALWAYS, 1, 1); floorStencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::REPLACE); osg::ColorMask *colorMask1 = new osg::ColorMask(0, 0, 0, 0); stencilFloorGeode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, false); stencilFloorGeode->getOrCreateStateSet()->setAttribute(colorMask1, osg::StateAttribute::ON); stencilFloorGeode->getOrCreateStateSet()->setAttributeAndModes(floorStencil); stencilFloorGeode->getOrCreateStateSet()->setMode(GL_LIGHTING, false);
osg::Group *reflectionBall = createSphere(); osg::Stencil *stencilBall = new osg::Stencil; stencilBall->setFunction(osg::Stencil::EQUAL, 1, 1); stencilBall->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::KEEP); osg::ClipPlane *clipPlane = new osg::ClipPlane(0, 0.0, -1.0, 0.0, 0.0); reflectionBall->getOrCreateStateSet()->setAttributeAndModes(stencilBall, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); reflectionBall->getOrCreateStateSet()->setAttributeAndModes(clipPlane);接下来在颜色缓冲区(可见的屏幕上)绘制真实的地面(镜子)
osg::Geode *colorBufferFloor = createFloor(); osg::BlendFunc *blendFunc = new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA); osg::Geometry *floorGeometry = colorBufferFloor->getDrawable(0)->asGeometry(); if (floorGeometry) { osg::Vec4Array *colorArray = new osg::Vec4Array; colorArray->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 0.8f)); floorGeometry->setColorArray(colorArray, osg::Array::BIND_OVERALL); floorGeometry->getOrCreateStateSet()->setAttributeAndModes(blendFunc); floorGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); }最后我们绘制出真实的球体:
osg::Group *realBall = createSphere(); transMT->addChild(rotateX); rotateX->addChild(rotateY); rotateY->addChild(realBall);
//所有部分添加到场景节点 root->addChild(tiltMT); tiltMT->addChild(stencilFloorGeode); tiltMT->addChild(reflectScaleMT); tiltMT->addChild(colorBufferFloor); tiltMT->addChild(transMT);
在我们操作真实球体的时候,镜像的球体实际上也在跟着上下运动,通过反转Y轴实现了这样的效果:
osg::MatrixTransform *reflectScaleMT = new osg::MatrixTransform; reflectScaleMT->setMatrix(osg::Matrix::scale(1.0, -1.0, 1.0));与键盘交互部分的代码与第二十五课中一样
编译运行程序:
附:本课源码(源码中可能存在着错误和不足,仅供参考)
/************************************************************************\ * osgNeHe - Copyright (C) 2013-2014 Frank He * * This library is open source and may be redistributed and/or modified under * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or * (at your option) any later version. The full license is in LICENSE file * included with this distribution, and on the openscenegraph.org website. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * A HUGE thanks to NeHe for the OpenGL tutorials< http://nehe.gamedev.net > * * If you've found this code useful, Please let me know. * My E-mail: < [email protected] > * My Blog: < http://blog.csdn.net/csxiaoshui > * \************************************************************************/ #include "../osgNeHe.h" #include <QtCore/QTimer> #include <QtGui/QApplication> #include <QtGui/QVBoxLayout> #include <osgViewer/Viewer> #include <osgDB/ReadFile> #include <osgQt/GraphicsWindowQt> #include <osg/MatrixTransform> #include <osg/ShapeDrawable> #include <osg/TexGen> #include <osg/BlendFunc> #include <osg/Texture2D> #include <osg/ColorMask> #include <osg/Stencil> #include <osg/ClipPlane> ////////////////////////////////////////////////////////////////////////// static GLfloat LightAmb[] = {0.7f, 0.7f, 0.7f, 1.0f}; static GLfloat LightDif[] = {1.0f, 1.0f, 1.0f, 1.0f}; static GLfloat LightPos[] = {4.0f, 4.0f, 6.0f, 1.0f}; GLfloat xrot = 0.0f; GLfloat yrot = 0.0f; GLfloat xrotspeed = 0.0f; GLfloat yrotspeed = 0.0f; GLfloat zoom = -7.0f; GLfloat height = 2.0f; osg::MatrixTransform *g_ZoomMT = NULL; osg::MatrixTransform *g_ReflectTrans = NULL; osg::MatrixTransform *g_ReflectRotX = NULL; osg::MatrixTransform *g_ReflectRotY = NULL; osg::MatrixTransform *g_Trans = NULL; osg::MatrixTransform *g_RotX = NULL; osg::MatrixTransform *g_RotY = NULL; osg::Group* createSphere() { osg::Group *sphereGroup = new osg::Group; osg::Geode *sphereGeode1 = new osg::Geode; osg::Geode *sphereGeode2 = new osg::Geode; osg::ShapeDrawable *shapeDrawable1 = new osg::ShapeDrawable; shapeDrawable1->setShape(new osg::Sphere(osg::Vec3(0,0,0), 0.5f)); shapeDrawable1->setColor(osg::Vec4(1.0, 1.0, 1.0, 1.0)); osg::Texture2D *texture1 = new osg::Texture2D; texture1->setImage(osgDB::readImageFile("Data/Ball.bmp")); texture1->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); texture1->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); shapeDrawable1->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture1); sphereGeode1->addDrawable(shapeDrawable1); osg::ShapeDrawable *shapeDrawable2 = new osg::ShapeDrawable; shapeDrawable2->setShape(new osg::Sphere(osg::Vec3(0,0,0), 0.5f)); shapeDrawable2->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.4f)); osg::Texture2D *texture2 = new osg::Texture2D; texture2->setImage(osgDB::readImageFile("Data/EnvRoll.bmp")); texture2->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); texture2->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); shapeDrawable2->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture2); osg::BlendFunc *blendFunc = new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE); shapeDrawable2->getOrCreateStateSet()->setAttributeAndModes(blendFunc); osg::TexGen *texGen = new osg::TexGen; texGen->setMode(osg::TexGen::SPHERE_MAP); shapeDrawable2->getOrCreateStateSet()->setTextureAttributeAndModes(0, texGen); sphereGeode2->addDrawable(shapeDrawable2); sphereGroup->addChild(sphereGeode1); sphereGroup->addChild(sphereGeode2); return sphereGroup; } osg::Geode* createFloor() { osg::Geode *geode1 = new osg::Geode; osg::Geometry *geometry1 = new osg::Geometry; osg::Vec3Array *vertexArray1 = new osg::Vec3Array; vertexArray1->push_back(osg::Vec3(-2.0, 0.0, 2.0)); vertexArray1->push_back(osg::Vec3(-2.0, 0.0,-2.0)); vertexArray1->push_back(osg::Vec3(2.0, 0.0,-2.0)); vertexArray1->push_back(osg::Vec3(2.0, 0.0, 2.0)); geometry1->setVertexArray(vertexArray1); osg::Vec2Array *textureArray1 = new osg::Vec2Array; textureArray1->push_back(osg::Vec2(0.0f, 1.0f)); textureArray1->push_back(osg::Vec2(0.0f, 0.0f)); textureArray1->push_back(osg::Vec2(1.0f, 0.0f)); textureArray1->push_back(osg::Vec2(1.0f, 1.0f)); osg::Texture2D *texture1 = new osg::Texture2D; texture1->setImage(osgDB::readImageFile("Data/EnvWall.bmp")); texture1->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); texture1->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); geometry1->setTexCoordArray(0, textureArray1); geometry1->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4)); geometry1->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture1); geode1->addDrawable(geometry1); return geode1; } ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// class RotAxisCallback : public osg::NodeCallback { public: RotAxisCallback(const osg::Vec3& axis, double rotSpeed = 0.0, double currentAngle = 0.0) : _rotAxis(axis), _rotSpeed(rotSpeed), _currentAngle(currentAngle){ } virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) { osg::MatrixTransform *rotMT = dynamic_cast<osg::MatrixTransform*>(node); if (!rotMT) return; rotMT->setMatrix(osg::Matrix::rotate(_currentAngle, _rotAxis)); _currentAngle += _rotSpeed; traverse(node, nv); } void setRotateSpeed(double speed) { _rotSpeed = speed; } double getRotateSpeed() const { return _rotSpeed; } private: osg::Vec3 _rotAxis; double _currentAngle; double _rotSpeed; }; ////////////////////////////////////////////////////////////////////////// class ManipulatorSceneHandler : public osgGA::GUIEventHandler { public: ManipulatorSceneHandler() { } virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) { osgViewer::Viewer *viewer = dynamic_cast<osgViewer::Viewer*>(&aa); if (!viewer) return false; if (!viewer->getSceneData()) return false; if (ea.getHandled()) return false; osg::Group *root = viewer->getSceneData()->asGroup(); switch(ea.getEventType()) { case(osgGA::GUIEventAdapter::KEYDOWN): { if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Left) { if (!g_RotY|| !g_ReflectRotY) return false; RotAxisCallback *rotCallback = dynamic_cast<RotAxisCallback*>(g_RotY->getUpdateCallback()); RotAxisCallback *rotCallbackReflect = dynamic_cast<RotAxisCallback*>(g_ReflectRotY->getUpdateCallback()); if (!rotCallback || !rotCallbackReflect) return false; double speed = rotCallback->getRotateSpeed(); speed -= 0.02; rotCallback->setRotateSpeed(speed); speed = rotCallbackReflect->getRotateSpeed(); speed -= 0.02; rotCallbackReflect->setRotateSpeed(speed); } if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Right) { if (!g_RotY|| !g_ReflectRotY) return false; RotAxisCallback *rotCallback = dynamic_cast<RotAxisCallback*>(g_RotY->getUpdateCallback()); RotAxisCallback *rotCallbackReflect = dynamic_cast<RotAxisCallback*>(g_ReflectRotY->getUpdateCallback()); if (!rotCallback || !rotCallbackReflect) return false; double speed = rotCallback->getRotateSpeed(); speed += 0.02; rotCallback->setRotateSpeed(speed); speed = rotCallbackReflect->getRotateSpeed(); speed += 0.02; rotCallbackReflect->setRotateSpeed(speed); } if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Up) { if (!g_RotX|| !g_ReflectRotX) return false; RotAxisCallback *rotCallback = dynamic_cast<RotAxisCallback*>(g_RotX->getUpdateCallback()); RotAxisCallback *rotCallbackReflect = dynamic_cast<RotAxisCallback*>(g_ReflectRotX->getUpdateCallback()); if (!rotCallback || !rotCallbackReflect) return false; double speed = rotCallback->getRotateSpeed(); speed -= 0.02; rotCallback->setRotateSpeed(speed); speed = rotCallbackReflect->getRotateSpeed(); speed -= 0.02; rotCallbackReflect->setRotateSpeed(speed); } if (ea.getKey()== osgGA::GUIEventAdapter::KEY_Down) { if (!g_RotX|| !g_ReflectRotX) return false; RotAxisCallback *rotCallback = dynamic_cast<RotAxisCallback*>(g_RotX->getUpdateCallback()); RotAxisCallback *rotCallbackReflect = dynamic_cast<RotAxisCallback*>(g_ReflectRotX->getUpdateCallback()); if (!rotCallback || !rotCallbackReflect) return false; double speed = rotCallback->getRotateSpeed(); speed += 0.02; rotCallback->setRotateSpeed(speed); speed = rotCallbackReflect->getRotateSpeed(); speed += 0.02; rotCallbackReflect->setRotateSpeed(speed); } if (ea.getKey()== osgGA::GUIEventAdapter::KEY_Page_Up) { if (!g_Trans || !g_ReflectTrans) return false; osg::Matrix transMatrix = g_Trans->getMatrix(); transMatrix.setTrans(transMatrix.getTrans() + osg::Vec3(0, 0.1, 0)); g_Trans->setMatrix(transMatrix); osg::Matrix reflectTransMatrix = g_ReflectTrans->getMatrix(); reflectTransMatrix.setTrans(reflectTransMatrix.getTrans() + osg::Vec3(0, 0.1, 0)); g_ReflectTrans->setMatrix(reflectTransMatrix); } if (ea.getKey()== osgGA::GUIEventAdapter::KEY_Page_Down) { if (!g_Trans || !g_ReflectTrans) return false; osg::Matrix transMatrix = g_Trans->getMatrix(); transMatrix.setTrans(transMatrix.getTrans() + osg::Vec3(0, -0.1, 0)); g_Trans->setMatrix(transMatrix); osg::Matrix reflectTransMatrix = g_ReflectTrans->getMatrix(); reflectTransMatrix.setTrans(reflectTransMatrix.getTrans() + osg::Vec3(0, -0.1, 0)); g_ReflectTrans->setMatrix(reflectTransMatrix); } if (ea.getKey()== osgGA::GUIEventAdapter::KEY_A) { if (!g_ZoomMT) return false; osg::Matrix transMatrix = g_ZoomMT->getMatrix(); transMatrix.setTrans(transMatrix.getTrans() + osg::Vec3(0, 0, -0.1)); g_ZoomMT->setMatrix(transMatrix); } if (ea.getKey()== osgGA::GUIEventAdapter::KEY_Z) { if (!g_ZoomMT) return false; osg::Matrix transMatrix = g_ZoomMT->getMatrix(); transMatrix.setTrans(transMatrix.getTrans() + osg::Vec3(0, 0, 0.1)); g_ZoomMT->setMatrix(transMatrix); } } default: break; } return false; } }; ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// class ViewerWidget : public QWidget, public osgViewer::Viewer { public: ViewerWidget(osg::Node *scene = NULL) { QWidget* renderWidget = getRenderWidget( createGraphicsWindow(0,0,640,480), scene); QVBoxLayout* layout = new QVBoxLayout; layout->addWidget(renderWidget); layout->setContentsMargins(0, 0, 0, 1); setLayout( layout ); connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) ); _timer.start( 10 ); } QWidget* getRenderWidget( osgQt::GraphicsWindowQt* gw, osg::Node* scene ) { osg::Camera* camera = this->getCamera(); camera->setGraphicsContext( gw ); const osg::GraphicsContext::Traits* traits = gw->getTraits(); camera->setClearColor( osg::Vec4(0.2f, 0.5f, 1.0f, 1.0f) ); camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) ); camera->setProjectionMatrixAsPerspective(45.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 0.1f, 100.0f ); camera->setViewMatrixAsLookAt(osg::Vec3d(0, 0, 1), osg::Vec3d(0, 0, 0), osg::Vec3d(0, 1, 0)); unsigned int clearMask = camera->getClearMask(); camera->setClearMask(clearMask | GL_STENCIL_BUFFER_BIT); this->setSceneData( scene ); this->addEventHandler(new ManipulatorSceneHandler); return gw->getGLWidget(); } osgQt::GraphicsWindowQt* createGraphicsWindow( int x, int y, int w, int h, const std::string& name="", bool windowDecoration=false ) { osg::DisplaySettings* ds = osg::DisplaySettings::instance().get(); osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; traits->windowName = name; traits->windowDecoration = windowDecoration; traits->x = x; traits->y = y; traits->width = w; traits->height = h; traits->doubleBuffer = true; traits->alpha = ds->getMinimumNumAlphaBits(); traits->stencil = ds->getMinimumNumStencilBits(); traits->sampleBuffers = ds->getMultiSamples(); traits->samples = ds->getNumMultiSamples(); return new osgQt::GraphicsWindowQt(traits.get()); } virtual void paintEvent( QPaintEvent* event ) { frame(); } protected: QTimer _timer; }; osg::Node* buildScene() { osg::Group *root = new osg::Group; osg::Light *light = new osg::Light; light->setAmbient(osg::Vec4(LightAmb[0], LightAmb[1], LightAmb[2], LightAmb[3])); light->setDiffuse(osg::Vec4(LightDif[0], LightDif[1], LightDif[2], LightDif[3])); light->setPosition(osg::Vec4(LightPos[0], LightPos[1], LightPos[2], LightPos[3])); light->setLightNum(0); osg::LightSource *lightSource = new osg::LightSource; lightSource->setLight(light); osg::MatrixTransform *tiltMT = new osg::MatrixTransform; g_ZoomMT = tiltMT; tiltMT->setMatrix(osg::Matrix::translate(0.0f, -0.6f, -7.0f)); tiltMT->addChild(lightSource); //创建模板缓冲区中的镜面(不可见) //深度测试关闭意味着深度测试总是通过 osg::Geode *stencilFloorGeode = createFloor(); osg::Stencil *floorStencil = new osg::Stencil; floorStencil->setFunction(osg::Stencil::ALWAYS, 1, 1); floorStencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::REPLACE); osg::ColorMask *colorMask1 = new osg::ColorMask(0, 0, 0, 0); stencilFloorGeode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, false); stencilFloorGeode->getOrCreateStateSet()->setAttribute(colorMask1, osg::StateAttribute::ON); stencilFloorGeode->getOrCreateStateSet()->setAttributeAndModes(floorStencil); stencilFloorGeode->getOrCreateStateSet()->setMode(GL_LIGHTING, false); //创建镜面中的球体(球体镜像) osg::MatrixTransform *reflectScaleMT = new osg::MatrixTransform; reflectScaleMT->setMatrix(osg::Matrix::scale(1.0, -1.0, 1.0)); reflectScaleMT->addChild(lightSource); osg::MatrixTransform *reflectTransMT = new osg::MatrixTransform; reflectTransMT->setMatrix(osg::Matrix::translate(0.0, 2.0, 0.0)); g_ReflectTrans = reflectTransMT; osg::MatrixTransform *reflectRotateX = new osg::MatrixTransform; g_ReflectRotX = reflectRotateX; reflectRotateX->addUpdateCallback(new RotAxisCallback(osg::X_AXIS)); osg::MatrixTransform *reflectRotateY = new osg::MatrixTransform; g_ReflectRotY = reflectRotateY; reflectRotateY->addUpdateCallback(new RotAxisCallback(osg::Y_AXIS)); osg::Group *reflectionBall = createSphere(); osg::Stencil *stencilBall = new osg::Stencil; stencilBall->setFunction(osg::Stencil::EQUAL, 1, 1); stencilBall->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::KEEP); osg::ClipPlane *clipPlane = new osg::ClipPlane(0, 0.0, -1.0, 0.0, 0.0); reflectionBall->getOrCreateStateSet()->setAttributeAndModes(stencilBall, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); reflectionBall->getOrCreateStateSet()->setAttributeAndModes(clipPlane); reflectScaleMT->addChild(reflectTransMT); reflectTransMT->addChild(reflectRotateX); reflectRotateX->addChild(reflectRotateY); reflectRotateY->addChild(reflectionBall); //创建颜色缓冲区镜面(可见) osg::Geode *colorBufferFloor = createFloor(); osg::BlendFunc *blendFunc = new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA); osg::Geometry *floorGeometry = colorBufferFloor->getDrawable(0)->asGeometry(); if (floorGeometry) { osg::Vec4Array *colorArray = new osg::Vec4Array; colorArray->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 0.8f)); floorGeometry->setColorArray(colorArray, osg::Array::BIND_OVERALL); floorGeometry->getOrCreateStateSet()->setAttributeAndModes(blendFunc); floorGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); } osg::MatrixTransform *transMT = new osg::MatrixTransform; transMT->setMatrix(osg::Matrix::translate(0, 2, 0)); g_Trans = transMT; osg::MatrixTransform *rotateX = new osg::MatrixTransform; g_RotX = rotateX; rotateX->addUpdateCallback(new RotAxisCallback(osg::X_AXIS)); osg::MatrixTransform *rotateY = new osg::MatrixTransform; g_RotY = rotateY; rotateY->addUpdateCallback(new RotAxisCallback(osg::Y_AXIS)); osg::Group *realBall = createSphere(); transMT->addChild(rotateX); rotateX->addChild(rotateY); rotateY->addChild(realBall); //所有部分添加到场景节点 root->addChild(tiltMT); tiltMT->addChild(stencilFloorGeode); tiltMT->addChild(reflectScaleMT); tiltMT->addChild(colorBufferFloor); tiltMT->addChild(transMT); return root; } int main( int argc, char** argv ) { QApplication app(argc, argv); ViewerWidget* viewWidget = new ViewerWidget(buildScene()); viewWidget->setGeometry( 100, 100, 640, 480 ); viewWidget->show(); return app.exec(); }