#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;
}
osg中关于矢量旋转的计算
最新推荐文章于 2024-10-02 22:25:35 发布