osg中关于矢量旋转的计算

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <osgViewer/Viewer>
#include <osgGA/GUIEventHandler>
#include <osgGA/TrackballManipulator>
#include <osg/Group>
#include <osg/Shape>
#include <osg/ShapeDrawable>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osg/observer_ptr>
#include <osg/Timer>
#include <osg/Geode>
#include <osg/Texture2D>
#include <osgUtil/CullVisitor>
#include <osg/Program>
#include <osg/Shader>
#include <osg/Uniform>
#include <osg/Geometry>
#include <osg/BlendFunc>
#include <time.h>
#include <stdlib.h>
#include <osgViewer/ViewerEventHandlers>
#include <osgDB/WriteFile>
#include <igGraph/CameraManipulator>
#include <osg/Plane>
#include <osg/Geometry>
#include <osg/ShapeDrawable>
#include <osg/MatrixTransform>
#include <osgViewer/Viewer>
#include <iostream>
#include <cmath>


struct Plane {
	double a, b, c, d;
};

osg::Node* createPlane()
{
	osg::Group* root = new osg::Group;

	osg::Geode* lpGeode = new osg::Geode;
	osg::Geode* lpGeodeLine = new osg::Geode;
	osg::Geometry* lpGeometry = new osg::Geometry;
	lpGeode->addDrawable(lpGeometry);
	osg::Geometry* lpGeometryLine = new osg::Geometry;
	lpGeodeLine->addDrawable(lpGeometryLine);

	osg::Vec3Array* lpVertexArray = new osg::Vec3Array;
	osg::Vec4Array* lpColorArray = new osg::Vec4Array;

	osg::Vec3Array* lpVertexArrayLine = new osg::Vec3Array;


	float scale = 1;
	lpVertexArray->push_back(osg::Vec3(-2, -2, 0) * scale);
	lpVertexArray->push_back(osg::Vec3(-2, 2, 0) * scale);
	lpVertexArray->push_back(osg::Vec3(2, 2, 0) * scale);
	lpVertexArray->push_back(osg::Vec3(2, -2, 0) * scale);

	lpGeometry->setVertexArray(lpVertexArray);
	lpColorArray->push_back(osg::Vec4(1.0, 1.0, 0.0, 0.5));
	lpGeometry->setColorArray(lpColorArray, osg::Array::BIND_OVERALL);
	lpGeometry->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, lpVertexArray->size()));


	osg::MatrixTransform* mt = new osg::MatrixTransform;
	mt->addChild(lpGeode);


	float Distance = 5;
	osg::Plane plane(0,0,1,Distance);
	osg::Vec3 normal = plane.getNormal();
	normal.normalize();
	osg::Vec3 xdir(0.0, 0.0, 1.0);
	osg::Quat q;
	q.makeRotate(xdir, normal);  //从(0,0,1)点旋转到 normal 便得到旋转四元数
	osg::Vec3f pos = normal * Distance;


	lpVertexArrayLine->push_back(osg::Vec3(0,0,0));
	lpVertexArrayLine->push_back(pos);
	lpGeometryLine->setVertexArray(lpVertexArrayLine);
	lpGeometryLine->setColorArray(lpColorArray, osg::Array::BIND_OVERALL);
	lpGeometryLine->addPrimitiveSet(new osg::DrawArrays(GL_LINES, 0, lpVertexArrayLine->size()));

	mt->setMatrix(osg::Matrix::rotate(q) * osg::Matrix::translate(pos));

	root->addChild(mt);
	root->addChild(lpGeodeLine);
	return root;
}


osg::ref_ptr<osg::Node> caculateTest()
{
	osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("glider.osg");

	osg::Vec3d zeroDir(0, 0, 0);
	osg::Vec3d curDir(0, 0, 10);
	osg::Vec3d toDir = (curDir - zeroDir);

	osg::Vec3d curPos(0, 0, 0);
	curPos + toDir * 10;

	toDir.normalize();

	return node;
}


int main(int argc, char *argv[])
{

    osg::ref_ptr<osg::Group> root = new osg::Group;
    osg::ref_ptr<osg::Node> Axis = osgDB::readNodeFile("axes.osgt");

    root->addChild(Axis);
	root->addChild(createPlane());

    osgViewer::Viewer v;
    v.setSceneData(root);

    v.realize();

    v.run();

	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值