OsgWidget.h
#ifndef OSGWIDGET_H
#define OSGWIDGET_H
#include <QWidget>
#include "OsgViewerWidget.h"
#include "MyManipulator.h"
#include "osg/PolygonMode"
class AnimationPathCameraMainpulator;
namespace Ui {
class OsgWidget;
}
class OsgWidget : public QWidget
{
Q_OBJECT
public:
struct Element_Shell
{
Element_Shell() {
}
qint64 eid;
qint64 pid;
qint64 n1;
qint64 n2;
qint64 n3;
qint64 n4;
qint64 n5;
qint64 n6;
qint64 n7;
qint64 n8;
};
struct Part
{
Part() {
}
qint64 pid;
qint64 secid;
QList<Element_Shell> listElementShell;
qint64 mid;
qint64 eosid;
qint64 hgid;
qint64 grav;
qint64 adpopt;
qint64 tmid;
};
struct Node {
Node() {
}
qint64 nid;
double x;
double y;
double z;
int tc;
int rc;
};
struct K_Mode
{
K_Mode() {}
QList<Part> listPart;
QList<Node> listNode;
QHash<int, Node> hashNid2Node;
};
K_Mode kMode;
public:
explicit OsgWidget(QWidget *parent = 0);
~OsgWidget();
public:
bool getFixXAxis() const;
bool getFixYAxis() const;
bool getFixZAxis() const;
void getCenter(double &x, double &y, double &z);
void getPersonPoint(double &x, double &y, double &z);
public:
void setFixXAxis(bool fixXAxis);
void setFixYAxis(bool fixYAxis);
void setFixZAxis(bool fixZAxis);
void setCenter(double x, double y, double z);
void setPersonPoint(double x, double y, double z);
void setEnablePolygonMode(bool enable);
void startAnimation();
void pauseAnimation();
void stopAnimation();
public:
bool loadKFile(QString filePath);
bool loadK2File(QString filePath, int num, int x, int y, int z);
void clear();
void resetCoordinate();
protected:
void initOsg();
void loadNode(osg::ref_ptr<osg::Node> pNode);
protected:
osg::ref_ptr<osg::Node> createScene();
osg::ref_ptr<osg::Node> createAnimation();
protected:
void resizeEvent(QResizeEvent *event);
void keyPressEvent(QKeyEvent* event);
void keyReleaseEvent(QKeyEvent* event);
void mousePressEvent(QMouseEvent* event);
void mouseReleaseEvent(QMouseEvent* event);
void mouseDoubleClickEvent(QMouseEvent* event);
void mouseMoveEvent(QMouseEvent* event);
void wheelEvent(QWheelEvent* event);
void timerEvent(QTimerEvent *event);
private:
Ui::OsgWidget *ui;
private:
OsgViewerWidget *_pViewer;
osg::ref_ptr<osg::MatrixTransform> _pRoot;
private:
float _xDistance;
int _xTickNumber;
float _yDistance;
int _yTickNumber;
float _zDistance;
int _zTickNumber;
QString _zUnit;
float _zTickLabelOffset;
QString _yUnit;
float _zTickUnitLabelOffset;
QColor _gridColor;
QColor _labelColor;
osg::ref_ptr<osg::Node> _pNode;
osg::ref_ptr<osg::Node> _pNode2;
osg::ref_ptr<MyManipulator> _pManipulator;
osg::Vec3d _eyeVect3D;
osg::Vec3d _centerVect3D;
osg::Vec3d _upVect3D;
K_Mode _kMode;
int _timerId;
osg::ref_ptr<osg::StateSet> _pStateSet;
osg::ref_ptr<osg::PolygonMode> _pPolygonMode;
osg::ref_ptr<osg::Vec3Array> _pVec3Array;
bool _animationPausing;
};
#endif
OsgWidget.cpp
bool OsgWidget::loadK2File(QString filePath, int num, int x, int y, int z)
{
if(!QFile::exists(filePath))
{
LOG << "Not exist file:" << filePath;
QMessageBox::information(0, "错误", QString("Not exist file: %1").arg(filePath));
return false;
}
QFile file(filePath);
if(!file.open(QIODevice::ReadOnly))
{
LOG << "Failed to open file:" << filePath;
QMessageBox::information(0, "错误", QString("Failed to open file: %1").arg(filePath));
return false;
}
kMode = K_Mode();
QTextStream textStream(&file);
QString context;
qint64 rowIndex = -1;
context = textStream.readLine();
rowIndex++;
LOG;
...
file.close();
LOG;
osg::ref_ptr<osg::Group> pGroup = new osg::Group;
for(int index = 0; index < num; index++)
{
LOG << index;
{
for(int partIndex = 0; partIndex < kMode.listPart.size(); partIndex++)
{
osg::ref_ptr<osg::Geometry> pGeometry = new osg::Geometry;
osg::ref_ptr<osg::Vec3Array> pVec3Array = new osg::Vec3Array;
pGeometry->setVertexArray(pVec3Array.get());
osg::ref_ptr<osg::Vec4Array> pVec4Array = new osg::Vec4Array;
pGeometry->setColorArray(pVec4Array.get());
pGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
double r, g, b;
r = qrand() % 100 * 1.0f / 100;
g = qrand() % 100 * 1.0f / 100;
b = qrand() % 100 * 1.0f / 100;
for(int elementShellIndex = 0; elementShellIndex < kMode.listPart.at(partIndex).listElementShell.size(); elementShellIndex++)
{
pVec3Array->push_back(osg::Vec3(kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n1).x + index * x,
kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n1).y + index * y,
kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n1).z + index * z));
pVec3Array->push_back(osg::Vec3(kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n2).x + index * x,
kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n2).y + index * y,
kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n2).z + index * z));
pVec3Array->push_back(osg::Vec3(kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n3).x + index * x,
kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n3).y + index * y,
kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n3).z + index * z));
pVec3Array->push_back(osg::Vec3(kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n4).x + index * x,
kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n4).y + index * y,
kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n4).z + index * z));
pVec4Array->push_back(osg::Vec4(r, g, b, 1.0));
pVec4Array->push_back(osg::Vec4(r, g, b, 1.0));
pVec4Array->push_back(osg::Vec4(r, g, b, 1.0));
pVec4Array->push_back(osg::Vec4(r, g, b, 1.0));
}
osg::ref_ptr<osg::Vec3Array> pVec3ArrayNormal = new osg::Vec3Array;
pGeometry->setNormalArray(pVec3ArrayNormal.get());
pGeometry->setNormalBinding(osg::Geometry::BIND_OVERALL);
pVec3ArrayNormal->push_back(osg::Vec3(0.0, -1.0, 0.0));
pGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, kMode.listPart.at(partIndex).listElementShell.size() * 4));
osg::ref_ptr<osg::Geode> pGeode = new osg::Geode;
pGeode->addDrawable(pGeometry.get());
#if 0
{
_pStateSet = pGeometry->getOrCreateStateSet();
_pPolygonMode = new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL);
_pStateSet->setAttribute(_pPolygonMode, osg::StateAttribute::ON);
}
#endif
pGroup->addChild(pGeode.get());
}
}
}
{
osg::StateSet *pStateSet = pGroup->getOrCreateStateSet();
pStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
}
_pNode = pGroup.get();
if(_pNode.get() == 0)
{
return false;
}
_pRoot->addChild(_pNode);
return true;
}