OSG局部坐标系和世界坐标系的理解

对坐标系这个概念相对比较熟悉,但是应用于实践时,发现理解有点偏差,现记录如下:
首先测试代码如下简单的使用了pick显示鼠标点击的位置以及利用computerinterections函数计算与鼠标相交的模型点,从而输出鼠标的窗口值以及这些模型点的全局和局部点坐标。
结果一、如果不设置MatrixTransform矩阵只简单显示一头牛发现,局部坐标和世界坐标的坐标值是完全相同的。

结果二、设置MatrixTranform矩阵出现两头牛,点击经过矩阵变换的牛,点击与第一头牛大致相同的位置,会发现局部坐标是相同的世界坐标发生了变化。

结论:一般情况下用顶点(世界坐标系)建立好模型后,全局和局部坐标系都属于世界坐标系,但是将模型平移旋转和缩放后,世界坐标系会随之发生变化但是局部坐标系不发生变化,局部坐标系应该是属于平移之前的位置
#include"…/common/common.h"

#include
#include <osg/ref_ptr>
#include <osgViewer/Viewer>
#include<osg/ShapeDrawable>
#include<osg/Node>
#include<osg/Geometry>
#include<osg/Geode>
#include<osg/Group>
#include<osg/ClipNode>
#include<osg/PolygonMode>
#include<osg/MatrixTransform>
#include<osg/AnimationPath>
#include<osgUtil/DelaunayTriangulator>
#include <osgDB/ReadFile>
#include<osgDB/WriteFile>
#include <osgFX/Scribe>
#include<osgGA/StateSetManipulator>
#include<osgViewer/ViewerEventHandlers>
#include<osgUtil/Optimizer>
#include<osgUtil/PlaneIntersector>

#include<osg/LineWidth>
#include<osg/PrimitiveSet>

#ifdef _DEBUG
#pragma comment(lib,"…/Debug/common.lib")
#else
#pragma comment(lib,"…/Release/common.lib")
#endif
class mPickHandler:public osgGA::GUIEventHandler
{
public:
virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor*)
{
_viewer = dynamic_castosgViewer::Viewer*(&aa);
switch (ea.getEventType())
{
case osgGA::GUIEventAdapter::PUSH:
if (ea.getButton() == 1)
{

			float X = ea.getX();
			float Y = ea.getY();
			std::cout << "x " << X <<" "<<"y " << Y << std::endl;
			pick(X, Y);

		}
		if (ea.getButton() == 2)
		{
			float X = ea.getXnormalized();
			float Y = ea.getYnormalized();
			std::cout << "x " << X << " " << "y " << Y << std::endl;
			//pick(X, Y);
		}
		return true;
	default:
		break;
	}
	return false;
}
mPickHandler():_viewer(0) {}

~mPickHandler() {}
void pick(float x, float y)
{
	osgUtil::LineSegmentIntersector::Intersections intersections;
	_viewer->computeIntersections(x, y, intersections);
	if (!intersections.size()) { std::cout << "error" << std::endl; }
	for (osgUtil::LineSegmentIntersector::Intersections::iterator itr = intersections.begin(); itr != intersections.end(); ++itr)
	{
		std::cout << "局部坐标" <<itr->getLocalIntersectPoint().x() << " " << itr->getLocalIntersectPoint().y() << " " << itr->getLocalIntersectPoint().z() << std::endl;

		std::cout<<"世界坐标" << itr->getWorldIntersectPoint().x() << " "<<itr->getWorldIntersectPoint().y() << " "<<itr->getWorldIntersectPoint().z() << std::endl;
	}

	
}

private:
osgViewer::Viewer* _viewer;

};

int main()
{
osgViewer::Viewer viewer;
osg::ref_ptrosg::Group root = new osg::Group;
osg::ref_ptrosg::MatrixTransform mat = new osg::MatrixTransform;
osg::ref_ptrosg::Node cow = osgDB::readNodeFile(“cow.osg”);
mat->addChild(cow);
mat->setMatrix(osg::Matrix::translate(10, 0, 0));
root->addChild(mat);
osg::ref_ptrosgFX::Scribe sc = new osgFX::Scribe();
sc->addChild(cow);
root->addChild(cow);
root->addChild(sc);
viewer.setSceneData(root);
viewer.addEventHandler(new mPickHandler);
viewer.addEventHandler(new osgViewer::WindowSizeHandler);
return viewer.run();
}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值