查看: 1682|回复: 1

关于节点更新问题

[复制链接]

该用户从未签到

发表于 2009-12-10 10:28:16 | 显示全部楼层 |阅读模式
我的思路是:定义一个从osgGA::GUIEventHandler继承的回调函数,按下某个键,触发更新节点,相关代码如下所示
void CEmendation:perator ()(osg::Node *node, osg::NodeVisitor *nv)
{
        if(bAjust)  //按下某键,此值变为true
        {
                node=AdjustToRectangle(node);
        }
        traverse(node, nv);
}
osg::Node* CEmendation::AdjustToRectangle(osg::Node* node)
{
        osg::Group* distortionNode = new osg::Group;
        //osg::ref_ptr<osg::Group> distortionNode = new osg::Group;

        //osg::Texture2D* texture = new osg::Texture2D;
        osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
    texture->setTextureSize(width, height);
        texture->setWrap(osg::Texture::WRAP_S,osg::Texture::REPEAT);
        texture->setWrap(osg::Texture::WRAP_T,osg::Texture::REPEAT);
    texture->setInternalFormat(GL_RGBA);
    texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D:INEAR);
    texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
       
        // set up the render to texture camera.
    {
        //osg::Camera* camera = new osg::Camera;
                osg::ref_ptr<osg::Camera> camera = new osg::Camera;
        // set clear the color and depth buffer
                camera->setClearColor(osg::Vec4(0.0,0.0,0.0,0.2));
        camera->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        // just inherit the main cameras view
        camera->setReferenceFrame(osg::Transform::RELATIVE_RF);
        camera->setProjectionMatrix(osg::Matrixd::identity());
        camera->setViewMatrix(osg::Matrixd::identity());
        // set viewport
        camera->setViewport(0,0,width,height);
        // set the camera to render before the main camera.
        camera->setRenderOrder(osg::Camera:RE_RENDER);
        // tell the camera to use OpenGL frame buffer object where supported.
        camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
        // attach the texture and use it as the color buffer.
        camera->attach(osg::Camera::COLOR_BUFFER, texture);
        // add subgraph to render
        camera->addChild(node);      
        distortionNode->addChild(camera);
        }
        {
                //osg::Geometry* polyGeom = new osg::Geometry();
                osg::ref_ptr<osg::Geometry> polyGeom = new osg::Geometry();
                polyGeom->setSupportsDisplayList(false);

                /*osg::Vec3Array* vertices = new osg::Vec3Array;
        osg::Vec2Array* texcoords = new osg::Vec2Array;
        osg::Vec4Array* colors = new osg::Vec4Array;*/
                osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
                osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array;
                osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;

                int nPiece = param.getColumnNum();
                float deltaX1;
                float deltaY1;
                float deltaX2;
                float deltaY2;
                deltaX1 = (param.getPointD().PosX - param.getPointA().PosX)/nPiece;
                deltaY1 = (param.getPointD().PosY - param.getPointA().PosY)/nPiece;
                deltaX2 = (param.getPointC().PosX - param.getPointB().PosX)/nPiece;
                deltaY2 = (param.getPointC().PosY - param.getPointB().PosY)/nPiece;
                float x1;
                float y1;
                float x2;
                float y2;
                //osg:rawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLE_STRIP);
                osg::ref_ptr<osg::DrawElementsUShort> elements = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLE_STRIP);
                int k=0;
                for(int i=0;i<=nPiece;i++)
                {
                        x1 = param.getPointA().PosX + i * deltaX1;
                        y1 = param.getPointA().PosY + i * deltaY1;
                        x2 = param.getPointB().PosX + i * deltaX2;
                        y2 = param.getPointB().PosY + i * deltaY2;
                        vertices->push_back(osg::Vec3(x1,y1,param.getPointA().PosZ));
                        texcoords->push_back(osg::Vec2(0.0, i*1.0 / nPiece));
                        colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));

                        vertices->push_back(osg::Vec3(x2,y2,param.getPointB().PosZ));
                        texcoords->push_back(osg::Vec2(1.0, i*1.0 / nPiece));
                        colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
                        elements->push_back(k);
                        k++;
                        elements->push_back(k);
                        k++;
                }
                polyGeom->setVertexArray(vertices);
        polyGeom->setColorArray(colors);
        polyGeom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
        polyGeom->setTexCoordArray(0,texcoords);
                polyGeom->addPrimitiveSet(elements);

                //osg::Geode* geode = new osg::Geode();
                osg::ref_ptr<osg::Geode> geode = new osg::Geode();
        geode->addDrawable(polyGeom);

                //osg::StateSet* stateset = polyGeom->getOrCreateStateSet();
                osg::ref_ptr<osg::StateSet> stateset = polyGeom->getOrCreateStateSet();
        stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON);
                stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);

               
                 // set up the camera to render the textured quad
        //osg::Camera* camera = new osg::Camera;
                osg::ref_ptr<osg::Camera> camera = new osg::Camera;
        // just inherit the main cameras view
        camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
        camera->setViewMatrix(osg::Matrix::identity());
                camera->setProjectionMatrixAsPerspective(33.7157, 1.33333, 0.1, 1000);
                camera->setViewMatrixAsLookAt(osg::Vec3(0,0,0),osg::Vec3(0,0,-1),osg::Vec3(0,1,0));
        // set the camera to render before the main camera.
        camera->setRenderOrder(osg::Camera::NESTED_RENDER);
        camera->addChild(geode);      
        distortionNode->addChild(camera);
        }
        return distortionNode;
}
运行出错,中断在onexit.c函数中的
   /*
         * Put the new entry into the table and update the end-of-table
         * pointer.
         */
         *(onexitend++) = (_PVFV)_encode_pointer(func);
        __onexitend = (_PVFV *)_encode_pointer(onexitend);

        return func;

不知道是什么原因导致,觉得应该是和指针有关,但是我改为osg::ref_ptr的话,又会中断在traverse(node, nv);处。???

该用户从未签到

发表于 2009-12-10 10:35:31 | 显示全部楼层
请检查运行堆栈中与OSG相关的位置,您给出的 onexit.c中断 对于排错没有意义。

此外您的代码实在有些长~~如果希望大家帮助您阅读代码来检查的话,请将其缩减到30行以内;毕竟我们并没有足够的时间去阅读他人的冗长的程序。相信这个缩减代码的过程中,您自己也可以更进一步检查问题的所在
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表