├── osglogger.cpp ├── .gitignore ├── QtWorkbench.png ├── screenshots ├── sample1.png └── sample2.png ├── .directory ├── QtWorkbench.desktop ├── shaders ├── myShader.frag ├── myShader.vert └── myShader.geom ├── main.cpp ├── appbuild.sh ├── README.md ├── osglogger.h ├── mainwindow.h ├── LICENSE ├── Qtworkbench.pro ├── CMakeLists.txt ├── qtosgwidget.h ├── osghelper.h ├── mainwindow.cpp ├── mainwindow.ui ├── qtosgwidget.cpp ├── osghelper.cpp ├── Qtworkbench.pro.user.2bd5f8f └── Qtworkbench.pro.user /osglogger.cpp: -------------------------------------------------------------------------------- 1 | #include "osglogger.h" 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | bin/* 2 | avatar.osg 3 | test.osg 4 | Makefile 5 | -------------------------------------------------------------------------------- /QtWorkbench.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VasanthBalguri/QtWorkbench/HEAD/QtWorkbench.png -------------------------------------------------------------------------------- /screenshots/sample1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VasanthBalguri/QtWorkbench/HEAD/screenshots/sample1.png -------------------------------------------------------------------------------- /screenshots/sample2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VasanthBalguri/QtWorkbench/HEAD/screenshots/sample2.png -------------------------------------------------------------------------------- /.directory: -------------------------------------------------------------------------------- 1 | [Dolphin] 2 | Timestamp=2019,10,13,18,11,31 3 | Version=4 4 | 5 | [Settings] 6 | HiddenFilesShown=true 7 | -------------------------------------------------------------------------------- /QtWorkbench.desktop: -------------------------------------------------------------------------------- 1 | [Desktop Entry] 2 | Version=1.0 3 | Name=QtWorkbench 4 | GenericName=QtWorkbench 5 | Comment=QtWorkbench 6 | Exec=Qtworkbench 7 | Icon=QtWorkbench 8 | Terminal=false 9 | Type=Application 10 | -------------------------------------------------------------------------------- /shaders/myShader.frag: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in VertexData{ 4 | vec4 mColor; 5 | } VertexIn; 6 | 7 | //out vec4 FragColor; 8 | 9 | void main(void) 10 | { 11 | 12 | gl_FragColor = VertexIn.mColor; 13 | } 14 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | 8 | QApplication a(argc, argv); 9 | MainWindow w; 10 | w.show(); 11 | 12 | return a.exec(); 13 | } 14 | -------------------------------------------------------------------------------- /appbuild.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | mkdir appdir 3 | mkdir appdir/usr 4 | mkdir appdir/usr/lib 5 | mkdir appdir/usr/bin 6 | 7 | cp build/Qtworkbench appdir/usr/bin 8 | cp QtWorkbench.desktop appdir 9 | cp QtWorkbench.png appdir 10 | ln -s $PWD/appdir/usr/bin/Qtworkbench appdir/AppRun 11 | 12 | ARCH=x86_64 ./linuxdeploy-x86_64.AppImage --appdir appdir --output appimage 13 | -------------------------------------------------------------------------------- /shaders/myShader.vert: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | 4 | uniform mat4 osg_ModelViewProjectionMatrix; 5 | 6 | layout(location = 0) in vec3 osg_Vertex; 7 | layout(location = 1) in vec3 osg_Normal; 8 | layout(location = 2) in vec4 osg_Color; 9 | 10 | out VertexData{ 11 | vec4 mColor; 12 | 13 | } VertexOut; 14 | 15 | void main(void) 16 | { 17 | VertexOut.mColor = osg_Color; 18 | //VertexOut.mVertex = vec4(osg_Vertex,1); 19 | gl_Position = osg_ModelViewProjectionMatrix * vec4(osg_Vertex,1); 20 | } 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # QtWorkbench 2 | 3 | Created for qt learning purpose 4 | 5 | prequesites: 6 | install opencascade, openscenegraph and qt 7 | 8 | sudo apt-get install build-essential oce openscenegraph qt5-default boost 9 | 10 | build steps: 11 | 12 | * create bin directory 13 | * go to bin directory 14 | * run cmake .. 15 | * run make 16 | * Qtworkbench executable will be generated in build folder 17 | 18 | Create appimage: 19 | * run appbuild.sh 20 | 21 | 22 | # Current usage 23 | 24 | * Added sources from Opencascade tutorial to create bottle(should be removed) 25 | * There is a feature to open .step files 26 | 27 | # Screenshots 28 | ![Alt text](/screenshots/sample1.png?raw=true "Opencascade Bottle") 29 | ![Alt text](/screenshots/sample2.png?raw=true "osg file") 30 | # note: 31 | 32 | * Need to test CMake build on windows to confirm cross compatibility 33 | -------------------------------------------------------------------------------- /osglogger.h: -------------------------------------------------------------------------------- 1 | #ifndef OSGLOGGER_H 2 | #define OSGLOGGER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | class OsgLogger : public osg::NotifyHandler{ 10 | public: 11 | OsgLogger(const std::string& file_path) 12 | { 13 | _log.open(file_path.c_str()); 14 | } 15 | 16 | virtual ~OsgLogger(){_log.close();} 17 | 18 | virtual void notify(osg::NotifySeverity severity,const char* msg) 19 | { 20 | std::time_t current_time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); 21 | _log<<'['< 5 | #include 6 | #include 7 | 8 | #include "qtosgwidget.h" 9 | #include "osghelper.h" 10 | 11 | namespace Ui { 12 | class MainWindow; 13 | } 14 | 15 | class MainWindow : public QMainWindow 16 | { 17 | Q_OBJECT 18 | 19 | public: 20 | explicit MainWindow(QWidget *parent = 0); 21 | ~MainWindow(); 22 | 23 | private: 24 | Ui::MainWindow *ui; 25 | QColor _color; 26 | QtOSGWidget* qOsg; 27 | 28 | public slots: 29 | 30 | void changedOrientation(std::vector); 31 | void changedR(int); 32 | void changedG(int); 33 | void changedB(int); 34 | void createFileDialog(); 35 | void saveFileDialog(); 36 | void updateBottle(); 37 | 38 | signals: 39 | void colorChanged(QColor); 40 | void openFile(std::string); 41 | void saveFile(std::string); 42 | }; 43 | 44 | #endif // MAINWINDOW_H 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 VasanthBalguri 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Qtworkbench.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2019-10-06T16:28:30 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Qtworkbench 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which has been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | 26 | SOURCES += \ 27 | main.cpp \ 28 | mainwindow.cpp \ 29 | qtosgwidget.cpp \ 30 | osghelper.cpp 31 | 32 | HEADERS += \ 33 | mainwindow.h \ 34 | qtosgwidget.h \ 35 | osghelper.h 36 | 37 | FORMS += \ 38 | mainwindow.ui 39 | 40 | unix:!macx: LIBS += -L$$PWD/../../../usr/lib/x86_64-linux-gnu/ -losg -losgDB -losgGA -losgViewer 41 | 42 | INCLUDEPATH += $$PWD/../../../usr/include/osg 43 | DEPENDPATH += $$PWD/../../../usr/include/osg 44 | INCLUDEPATH += /usr/include/oce 45 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.1.0) 2 | 3 | project(Qtworkbench) 4 | 5 | set(CMAKE_AUTOMOC ON) 6 | set(CMAKE_AUTORCC ON) 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 9 | 10 | if(CMAKE_VERSION VERSION_LESS "3.7.0") 11 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 12 | endif() 13 | 14 | find_package(Qt5 COMPONENTS Widgets OpenGL REQUIRED) 15 | find_package (OpenCASCADE REQUIRED) 16 | FIND_PACKAGE( OpenSceneGraph COMPONENTS osgDB osgGA osgViewer osg osgUtil osgText) 17 | find_package(Boost REQUIRED) 18 | #include_directories(${OCE_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS}) 19 | include_directories(${OpenCASCADE_INCLUDE_DIR} ${Boost_INCLUDE_DIRS} ${OPENSCENEGRAPH_INCLUDE_DIRS}) 20 | 21 | message(STATUS "OSG Incl dirs: ${OPENCASCADE_LIBRARIES} ;${OpenCASCADE_INCLUDE_DIR} ; ${Boost_INCLUDE_DIRS} ; ${OPENSCENEGRAPH_INCLUDE_DIRS} ") 22 | 23 | add_executable(Qtworkbench 24 | mainwindow.ui 25 | qtosgwidget.cpp 26 | osghelper.cpp 27 | mainwindow.cpp 28 | main.cpp 29 | ) 30 | 31 | #target_link_libraries(Qtworkbench Qt5::Widgets ${OPENSCENEGRAPH_LIBRARIES} ${OPENCASCADE_LIBRARIES}) 32 | 33 | target_link_libraries(Qtworkbench Qt5::Widgets ${OPENSCENEGRAPH_LIBRARIES} TKBinL TKBin TKBinTObj TKBinXCAF TKBool TKBO TKBRep TKCAF TKCDF TKernel TKFeat TKFillet TKGeomAlgo TKGeomBase TKG2d TKG3d TKHLR TKIGES TKLCAF TKMath TKMesh TKMeshVS TKOffset TKOpenGl TKPrim TKService TKShHealing TKSTEPAttr TKSTEPBase TKSTEP TKSTEP209 TKSTL TKTObj TKTopAlgo TKVRML TKV3d TKXCAF TKXDEIGES TKXDESTEP TKXMesh TKXmlL TKXml TKXmlTObj TKXmlXCAF TKXSBase) 34 | -------------------------------------------------------------------------------- /qtosgwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef QTOSGWIDGET_H 2 | #define QTOSGWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | struct ModelViewProjectionMatrixCallback: public osg::Uniform::Callback 35 | { 36 | ModelViewProjectionMatrixCallback(osg::Camera* camera) : 37 | _camera(camera) { 38 | } 39 | 40 | virtual void operator()(osg::Uniform* uniform, osg::NodeVisitor* nv) { 41 | osg::Matrixd viewMatrix = _camera->getViewMatrix(); 42 | osg::Matrixd modelMatrix = osg::computeLocalToWorld(nv->getNodePath()); 43 | osg::Matrixd modelViewProjectionMatrix = modelMatrix * viewMatrix * _camera->getProjectionMatrix(); 44 | uniform->set(modelViewProjectionMatrix); 45 | } 46 | 47 | osg::Camera* _camera; 48 | }; 49 | 50 | struct ViewportCallback: public osg::Uniform::Callback 51 | { 52 | ViewportCallback(osg::Camera* camera) : 53 | _camera(camera) { 54 | } 55 | 56 | virtual void operator()(osg::Uniform* uniform, osg::NodeVisitor* /*nv*/) { 57 | const osg::Viewport* viewport = _camera->getViewport(); 58 | osg::Vec2f viewportVector = osg::Vec2f(viewport->width(), viewport->height()); 59 | uniform->set(viewportVector); 60 | } 61 | 62 | osg::Camera* _camera; 63 | }; 64 | 65 | class QtOSGWidget : public QOpenGLWidget 66 | { 67 | private: 68 | 69 | osgGA::EventQueue* getEventQueue() const; 70 | 71 | osg::ref_ptr _mGraphicsWindow; 72 | osg::ref_ptr _mViewer; 73 | qreal m_scaleX, m_scaleY; 74 | 75 | public: 76 | QtOSGWidget(qreal scaleX = 1, qreal scaleY = 1, QWidget* parent = 0); 77 | 78 | void setScene(osg::ref_ptr root); 79 | osg::ref_ptr getScene(); 80 | 81 | virtual ~QtOSGWidget(){} 82 | 83 | void setScale(qreal X, qreal Y); 84 | osg::ref_ptr getView() 85 | { 86 | return _mViewer; 87 | } 88 | Q_OBJECT 89 | public slots: 90 | void changedColor(QColor color); 91 | void openScene(std::string path); 92 | void saveScene(std::string path); 93 | 94 | signals: 95 | void orientationChanged(std::vector); 96 | 97 | 98 | protected: 99 | 100 | virtual void paintGL(); 101 | 102 | virtual void resizeGL( int width, int height ); 103 | 104 | virtual void initializeGL(); 105 | 106 | virtual void mouseMoveEvent(QMouseEvent* event); 107 | 108 | virtual void mousePressEvent(QMouseEvent* event); 109 | 110 | virtual void mouseReleaseEvent(QMouseEvent* event); 111 | 112 | virtual void wheelEvent(QWheelEvent* event); 113 | 114 | virtual bool event(QEvent* event); 115 | 116 | }; 117 | #endif // QTOSGWIDGET_H 118 | -------------------------------------------------------------------------------- /osghelper.h: -------------------------------------------------------------------------------- 1 | #ifndef OSGHELPER_H 2 | #define OSGHELPER_H 3 | 4 | //osg includes 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | //oce includes 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | 35 | #include 36 | #include 37 | 38 | #include 39 | #include 40 | 41 | #include 42 | 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | #include 55 | #include 56 | #include 57 | #include 58 | 59 | #include 60 | #include 61 | 62 | #include 63 | 64 | #include 65 | #include 66 | #include 67 | #include 68 | #include 69 | #include 70 | 71 | #include 72 | 73 | #include 74 | #include 75 | #include 76 | #include 77 | #include 78 | #include 79 | #include 80 | 81 | #include 82 | #include 83 | #include 84 | 85 | # include 86 | # include 87 | # include 88 | # include 89 | # include 90 | # include 91 | # include 92 | # include 93 | # include 94 | # include 95 | # include 96 | # include 97 | # include 98 | # include 99 | # include 100 | # include 101 | # include 102 | # include 103 | # include 104 | 105 | osg::Group* createScene(); 106 | 107 | TopoDS_Shape MakeBottle(const Standard_Real myWidth, const Standard_Real myHeight, 108 | const Standard_Real myThickness); 109 | osg::Geometry* createGeometryFromShape(TopoDS_Shape& shape, 110 | const osg::Vec3& geomColor, gp_Trsf& transformation); 111 | osg::Group* readSceneFile(std::string filePath); 112 | osg::Geode* createBottle(double width, double height, double thickness, 113 | const osg::Vec3f &geomColor/*, const Matrixd &viewMatrix*/); 114 | osg::Geode* readStepFile(std::string path); 115 | 116 | #endif // OSGHELPER_H 117 | -------------------------------------------------------------------------------- /mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | 4 | #include 5 | 6 | MainWindow::MainWindow(QWidget *parent) : 7 | QMainWindow(parent), 8 | ui(new Ui::MainWindow) 9 | { 10 | qOsg = new QtOSGWidget(1,1,this); 11 | ui->setupUi(this); 12 | ui->verticalLayout_2->addWidget(qOsg); 13 | this->setWindowTitle(QString("Qt osg step Viewer")); 14 | resize(1024,768); 15 | connect(qOsg,SIGNAL(orientationChanged(std::vector)),this,SLOT(changedOrientation(std::vector))); 16 | connect(this,SIGNAL(colorChanged(QColor)),qOsg,SLOT(changedColor(QColor))); 17 | connect(this,SIGNAL(openFile(std::string)),qOsg,SLOT(openScene(std::string))); 18 | connect(this,SIGNAL(saveFile(std::string)),qOsg,SLOT(saveScene(std::string))); 19 | 20 | connect(ui->height_value,SIGNAL(valueChanged(double)),this,SLOT(updateBottle())); 21 | connect(ui->width_value,SIGNAL(valueChanged(double)),this,SLOT(updateBottle())); 22 | connect(ui->thickness_value,SIGNAL(valueChanged(double)),this,SLOT(updateBottle())); 23 | connect(ui->red_slider,SIGNAL(sliderMoved(int)),this,SLOT(changedR(int))); 24 | connect(ui->green_slider,SIGNAL(sliderMoved(int)),this,SLOT(changedG(int))); 25 | connect(ui->blue_slider,SIGNAL(sliderMoved(int)),this,SLOT(changedB(int))); 26 | connect(ui->actionOpen,SIGNAL(triggered(bool)),this,SLOT(createFileDialog())); 27 | connect(ui->actionsave,SIGNAL(triggered(bool)),this,SLOT(saveFileDialog())); 28 | 29 | //qOsg->addScene(createScene()); 30 | } 31 | 32 | MainWindow::~MainWindow() 33 | { 34 | delete ui; 35 | } 36 | 37 | void MainWindow::changedOrientation(std::vector values) 38 | { 39 | ui->eye_x->setText(QString::number(values[0])); 40 | ui->eye_y->setText(QString::number(values[1])); 41 | ui->eye_z->setText(QString::number(values[2])); 42 | 43 | ui->center_x->setText(QString::number(values[3])); 44 | ui->center_y->setText(QString::number(values[4])); 45 | ui->center_z->setText(QString::number(values[5])); 46 | 47 | ui->up_x->setText(QString::number(values[6])); 48 | ui->up_y->setText(QString::number(values[7])); 49 | ui->up_z->setText(QString::number(values[8])); 50 | } 51 | 52 | void MainWindow::changedR(int value) 53 | { 54 | _color.setRed(value); 55 | _color.setAlpha(255); 56 | emit colorChanged(_color); 57 | } 58 | 59 | 60 | void MainWindow::changedG(int value) 61 | { 62 | _color.setGreen(value); 63 | _color.setAlpha(255); 64 | emit colorChanged(_color); 65 | } 66 | 67 | void MainWindow::changedB(int value) 68 | { 69 | _color.setBlue(value); 70 | _color.setAlpha(255); 71 | emit colorChanged(_color); 72 | } 73 | 74 | void MainWindow::createFileDialog() 75 | { 76 | QFileDialog fileDialog(this); 77 | 78 | fileDialog.setFileMode(QFileDialog::AnyFile); 79 | fileDialog.setViewMode(QFileDialog::Detail); 80 | 81 | QStringList fileNames; 82 | if (fileDialog.exec()) 83 | if(fileDialog.selectedFiles().size() != 0) 84 | { 85 | fileNames = fileDialog.selectedFiles(); 86 | QString filePath = fileNames.first(); 87 | emit openFile(std::string(filePath.toUtf8().constData())); 88 | } 89 | 90 | 91 | } 92 | 93 | void MainWindow::saveFileDialog() 94 | { 95 | QFileDialog fileDialog(this); 96 | fileDialog.setAcceptMode(QFileDialog::AcceptSave); 97 | fileDialog.setFileMode(QFileDialog::AnyFile); 98 | fileDialog.setViewMode(QFileDialog::Detail); 99 | 100 | QStringList fileNames; 101 | if (fileDialog.exec()) 102 | fileNames = fileDialog.selectedFiles(); 103 | 104 | QString filePath = fileNames.first(); 105 | 106 | emit saveFile(std::string(filePath.toUtf8().constData())); 107 | } 108 | 109 | void MainWindow::updateBottle() 110 | { 111 | osg::ref_ptr root; 112 | osg::ref_ptr bottle; 113 | 114 | double mythickness = ui->thickness_value->value(); 115 | double mywidth = ui->width_value->value(); 116 | double myheight = ui->height_value->value(); 117 | osg::Vec3 myColor = osg::Vec3( 1.0f, 1.0f, 1.0f ); 118 | 119 | 120 | qOsg->getScene()->removeChild(0,1); 121 | qOsg->getScene()->addChild(createBottle(mywidth,myheight,mythickness,myColor/*,viewMatrix*/)); 122 | 123 | 124 | } 125 | -------------------------------------------------------------------------------- /mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 400 10 | 300 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | true 19 | 20 | 21 | 22 | 0 23 | 0 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | TextLabel 33 | 34 | 35 | 36 | 37 | 38 | 39 | TextLabel 40 | 41 | 42 | 43 | 44 | 45 | 46 | 255 47 | 48 | 49 | Qt::Horizontal 50 | 51 | 52 | 53 | 54 | 55 | 56 | 255 57 | 58 | 59 | Qt::Horizontal 60 | 61 | 62 | 63 | 64 | 65 | 66 | 255 67 | 68 | 69 | Qt::Horizontal 70 | 71 | 72 | 73 | 74 | 75 | 76 | TextLabel 77 | 78 | 79 | 80 | 81 | 82 | 83 | TextLabel 84 | 85 | 86 | 87 | 88 | 89 | 90 | TextLabel 91 | 92 | 93 | 94 | 95 | 96 | 97 | TextLabel 98 | 99 | 100 | 101 | 102 | 103 | 104 | TextLabel 105 | 106 | 107 | 108 | 109 | 110 | 111 | TextLabel 112 | 113 | 114 | 115 | 116 | 117 | 118 | TextLabel 119 | 120 | 121 | 122 | 123 | 124 | 125 | 2.000000000000000 126 | 127 | 128 | 0.100000000000000 129 | 130 | 131 | 132 | 133 | 134 | 135 | 2.000000000000000 136 | 137 | 138 | 0.100000000000000 139 | 140 | 141 | 142 | 143 | 144 | 145 | 2.000000000000000 146 | 147 | 148 | 0.100000000000000 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 0 160 | 0 161 | 400 162 | 30 163 | 164 | 165 | 166 | 167 | Fi&le 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | &Open 177 | 178 | 179 | 180 | 181 | save 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | -------------------------------------------------------------------------------- /qtosgwidget.cpp: -------------------------------------------------------------------------------- 1 | #include "qtosgwidget.h" 2 | #include "osghelper.h" 3 | #include 4 | #include 5 | #include 6 | 7 | QtOSGWidget::QtOSGWidget(qreal scaleX, qreal scaleY, QWidget* parent) 8 | : QOpenGLWidget(parent) 9 | , _mGraphicsWindow(new osgViewer::GraphicsWindowEmbedded( this->x(), this->y(), 10 | this->width(), this->height() ) ) 11 | , _mViewer(new osgViewer::Viewer) 12 | , m_scaleX(scaleX) 13 | , m_scaleY(scaleY) 14 | { 15 | 16 | try{ 17 | // osg::setNotifyLevel(osg::WARN); 18 | // osg::setNotifyHandler(new OsgLogger("mylog.txt")); 19 | //initialize camera 20 | osg::ref_ptr camera = new osg::Camera; 21 | camera->setViewport( this->x(), this->y(), this->width(), this->height() ); 22 | camera->setClearColor( osg::Vec4( 0.9f, 0.9f, 1.f, 1.f ) ); 23 | float aspectRatio = static_cast( this->width()) / static_cast( this->height() ); 24 | camera->setProjectionMatrixAsPerspective( 30.f, aspectRatio, 1.f, 1000.f ); 25 | camera->setGraphicsContext( _mGraphicsWindow ); 26 | _mViewer->setCamera(camera); 27 | 28 | 29 | 30 | //intitalize hud 31 | osg::ref_ptr hud_camera = new osg::Camera; 32 | hud_camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1280,0,1024));// set the view matrix 33 | hud_camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); 34 | hud_camera->setViewMatrix(osg::Matrix::identity());// only clear the depth buffer 35 | hud_camera->setClearMask(GL_DEPTH_BUFFER_BIT);// draw subgraph after main camera view. 36 | hud_camera->setRenderOrder(osg::Camera::POST_RENDER); 37 | 38 | hud_camera->setGraphicsContext(_mGraphicsWindow); 39 | hud_camera->setViewport(this->x(), this->y(),this->width(), this->height()); 40 | 41 | _mViewer->addSlave(hud_camera, false); 42 | //trackball navigation 43 | osg::ref_ptr manipulator = new osgGA::TrackballManipulator; 44 | manipulator->setAllowThrow( false ); 45 | this->setMouseTracking(true); 46 | _mViewer->setCameraManipulator(manipulator); 47 | 48 | 49 | osg::Vec3d eye( 60.0, 40.0, 60.0 ); 50 | osg::Vec3d center( 0.0, 0.0, 0.0 ); 51 | osg::Vec3d up( 0.0, 1.0, 0.0 ); 52 | 53 | manipulator->setHomePosition(eye,center,up); 54 | 55 | osg::ref_ptr root = new osg::Group; 56 | // root->addChild(hud_camera); 57 | _mViewer->setSceneData(root); 58 | _mViewer->setThreadingModel(osgViewer::Viewer::SingleThreaded); 59 | 60 | _mViewer->realize(); 61 | } 62 | catch(std::exception e) 63 | { 64 | std::cout< root) 70 | { 71 | _mViewer->setSceneData(root); 72 | } 73 | 74 | osg::ref_ptr QtOSGWidget::getScene() 75 | { 76 | osg::ref_ptr root = dynamic_cast(_mViewer->getSceneData()); 77 | return root; 78 | } 79 | 80 | void QtOSGWidget::setScale(qreal X, qreal Y) 81 | { 82 | m_scaleX = X; 83 | m_scaleY = Y; 84 | this->resizeGL(this->width(), this->height()); 85 | } 86 | 87 | 88 | void QtOSGWidget::paintGL() { 89 | _mViewer->frame(); 90 | } 91 | 92 | void QtOSGWidget::resizeGL( int width, int height ) 93 | { 94 | this->getEventQueue()->windowResize(this->x()*m_scaleX, this->y() * m_scaleY, width*m_scaleX, height*m_scaleY); 95 | _mGraphicsWindow->resized(this->x()*m_scaleX, this->y() * m_scaleY, width*m_scaleX, height*m_scaleY); 96 | osg::ref_ptr camera = _mViewer->getCamera(); 97 | camera->setViewport(0, 0, this->width()*m_scaleX, this->height()* m_scaleY); 98 | } 99 | 100 | void QtOSGWidget::initializeGL(){ 101 | 102 | //basic shader setup 103 | // disabled shader as of now, but in essence this is how it will work 104 | // osg::ref_ptr stateSet = this->getScene()->getOrCreateStateSet(); 105 | // osg::ref_ptr program = new osg::Program; 106 | 107 | // osg::ref_ptr vertShader = new osg::Shader(osg::Shader::VERTEX); 108 | // if (!vertShader->loadShaderSourceFromFile("../shaders/myShader.vert")) 109 | // std::cerr << "Could not read VERTEX shader from file" << std::endl; 110 | // program->addShader(vertShader); 111 | 112 | // osg::ref_ptr geomShader = new osg::Shader(osg::Shader::GEOMETRY); 113 | // if (!geomShader->loadShaderSourceFromFile("../shaders/myShader.geom")) 114 | // std::cerr << "Could not read GEOM shader from file" << std::endl; 115 | // program->addShader(geomShader); 116 | 117 | // osg::ref_ptr fragShader = new osg::Shader(osg::Shader::FRAGMENT); 118 | // if (!fragShader->loadShaderSourceFromFile("../shaders/myShader.frag")) 119 | // std::cerr << "Could not read FRAGMENT shader from file" << std::endl; 120 | // program->addShader(fragShader); 121 | 122 | // stateSet->setAttributeAndModes(program.get(), osg::StateAttribute::ON); 123 | // stateSet->setMode(GL_BLEND, osg::StateAttribute::ON); 124 | 125 | // osg::Uniform* modelViewProjectionMatrix = new osg::Uniform(osg::Uniform::FLOAT_MAT4, "osg_ModelViewProjectionMatrix"); 126 | // modelViewProjectionMatrix->setUpdateCallback(new ModelViewProjectionMatrixCallback(_mViewer->getCamera())); 127 | // stateSet->addUniform(modelViewProjectionMatrix); 128 | } 129 | 130 | /*void QtOSGWidget::addHud(osg::Group hud) 131 | { 132 | hud_camera->addChild(hud); 133 | hud_camera->setGraphicsContext(_mGraphicsWindow); 134 | hud_camera->setViewport(this->x(), this->y(),this->width(), this->height()); 135 | }*/ 136 | void QtOSGWidget::mouseMoveEvent(QMouseEvent* event) 137 | { 138 | this->getEventQueue()->mouseMotion(event->x()*m_scaleX, event->y()*m_scaleY); 139 | osg::Matrixd viewMatrix = _mViewer->getCamera()->getViewMatrix(); 140 | osg::Vec3d center; 141 | osg::Vec3d eye; 142 | osg::Vec3d up; 143 | viewMatrix.getLookAt(eye,center,up); 144 | std::vector values; 145 | 146 | values.push_back(eye.x()); 147 | values.push_back(eye.y()); 148 | values.push_back(eye.z()); 149 | 150 | values.push_back(center.x()); 151 | values.push_back(center.y()); 152 | values.push_back(center.z()); 153 | 154 | values.push_back(up.x()); 155 | values.push_back(up.y()); 156 | values.push_back(up.z()); 157 | 158 | emit orientationChanged(values); 159 | } 160 | 161 | void QtOSGWidget::mousePressEvent(QMouseEvent* event) 162 | { 163 | unsigned int button = 0; 164 | switch (event->button()){ 165 | case Qt::LeftButton: 166 | button = 1; 167 | break; 168 | case Qt::MiddleButton: 169 | button = 2; 170 | break; 171 | case Qt::RightButton: 172 | button = 3; 173 | break; 174 | default: 175 | break; 176 | } 177 | this->getEventQueue()->mouseButtonPress(event->x()*m_scaleX, event->y()*m_scaleY, button); 178 | } 179 | 180 | void QtOSGWidget::mouseReleaseEvent(QMouseEvent* event) 181 | { 182 | unsigned int button = 0; 183 | switch (event->button()){ 184 | case Qt::LeftButton: 185 | button = 1; 186 | break; 187 | case Qt::MiddleButton: 188 | button = 2; 189 | break; 190 | case Qt::RightButton: 191 | button = 3; 192 | break; 193 | default: 194 | break; 195 | } 196 | this->getEventQueue()->mouseButtonRelease(event->x()*m_scaleX, event->y()*m_scaleY, button); 197 | } 198 | 199 | void QtOSGWidget::wheelEvent(QWheelEvent* event) 200 | { 201 | int delta = event->delta(); 202 | osgGA::GUIEventAdapter::ScrollingMotion motion = delta > 0 ? 203 | osgGA::GUIEventAdapter::SCROLL_UP : osgGA::GUIEventAdapter::SCROLL_DOWN; 204 | this->getEventQueue()->mouseScroll(motion); 205 | osg::Matrixd viewMatrix = _mViewer->getCamera()->getViewMatrix(); 206 | osg::Vec3d center; 207 | osg::Vec3d eye; 208 | osg::Vec3d up; 209 | viewMatrix.getLookAt(eye,center,up); 210 | std::vector values; 211 | 212 | values.push_back(eye.x()); 213 | values.push_back(eye.y()); 214 | values.push_back(eye.z()); 215 | 216 | values.push_back(center.x()); 217 | values.push_back(center.y()); 218 | values.push_back(center.z()); 219 | 220 | values.push_back(up.x()); 221 | values.push_back(up.y()); 222 | values.push_back(up.z()); 223 | 224 | emit orientationChanged(values); 225 | } 226 | 227 | bool QtOSGWidget::event(QEvent* event) 228 | { 229 | bool handled = QOpenGLWidget::event(event); 230 | this->update(); 231 | return handled; 232 | } 233 | 234 | osgGA::EventQueue* QtOSGWidget::getEventQueue() const { 235 | osgGA::EventQueue* eventQueue = _mGraphicsWindow->getEventQueue(); 236 | return eventQueue; 237 | } 238 | 239 | void QtOSGWidget::changedColor(QColor color) 240 | { 241 | int r = 0,b = 0,g = 0,a = 255; 242 | osg::Vec4 vColor; 243 | color.getRgb(&r,&b,&g,&a); 244 | vColor[0] = ((float)r)/255.0f; 245 | vColor[1] = ((float)g)/255.0f; 246 | vColor[2] = ((float)b)/255.0f; 247 | vColor[3] = ((float)a)/255.0f; 248 | 249 | _mViewer->getCamera()->setClearColor(vColor); 250 | } 251 | 252 | void QtOSGWidget::openScene(std::string path) 253 | { 254 | 255 | if(boost::ends_with(path,".osg")) 256 | { 257 | osg::ref_ptr loadedModel = osgDB::readRefNodeFile(path); 258 | if(loadedModel == NULL) 259 | { 260 | std::cout<<"failed to load model"; 261 | throw std::exception(); 262 | } 263 | 264 | osg::ref_ptr root = new osg::Group(); 265 | root->addChild(loadedModel); 266 | 267 | _mViewer->setSceneData(root); 268 | }else if(boost::ends_with(path,".step")) 269 | { 270 | osg::ref_ptr loadedModel = readStepFile(path); 271 | if(loadedModel == NULL) 272 | { 273 | std::cout<<"failed to load model"; 274 | throw std::exception(); 275 | } 276 | osg::ref_ptr root = new osg::Group(); 277 | root->addChild(loadedModel); 278 | 279 | _mViewer->setSceneData(root); 280 | } 281 | 282 | } 283 | 284 | void QtOSGWidget::saveScene(std::string path) 285 | { 286 | osg::ref_ptr root = _mViewer->getSceneData(); 287 | osgDB::writeNodeFile(*root,path); 288 | 289 | } 290 | -------------------------------------------------------------------------------- /osghelper.cpp: -------------------------------------------------------------------------------- 1 | #include "osghelper.h" 2 | 3 | using namespace osg; 4 | 5 | //need to be removed 6 | osg::Group* createScene() 7 | { 8 | osg::ref_ptr cylinder = new osg::Cylinder( osg::Vec3( 0.f, 0.f, 0.f ), 0.25f, 0.5f ); 9 | osg::ref_ptr sd = new osg::ShapeDrawable( cylinder ); 10 | sd->setColor( osg::Vec4( 0.8f, 0.5f, 0.2f, 1.f ) ); 11 | osg::ref_ptr geode = new osg::Geode(); 12 | geode->addDrawable(sd); 13 | osg::ref_ptr root = new osg::Group(); 14 | 15 | root->addChild(geode); 16 | 17 | osg::ref_ptr stateSet = root->getOrCreateStateSet(); 18 | osg::ref_ptr material = new osg::Material; 19 | material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE); 20 | stateSet->setAttributeAndModes( material, osg::StateAttribute::ON ); 21 | stateSet->setMode( GL_DEPTH_TEST, osg::StateAttribute::ON ); 22 | 23 | return root.release(); 24 | } 25 | 26 | //sample code to create bottle in osg, will remove it later 27 | TopoDS_Shape MakeBottle(const Standard_Real myWidth, const Standard_Real myHeight, 28 | const Standard_Real myThickness) 29 | { 30 | // Profile : Define Support Points 31 | gp_Pnt aPnt1(-myWidth / 2., 0, 0); 32 | gp_Pnt aPnt2(-myWidth / 2., -myThickness / 4., 0); 33 | gp_Pnt aPnt3(0, -myThickness / 2., 0); 34 | gp_Pnt aPnt4(myWidth / 2., -myThickness / 4., 0); 35 | gp_Pnt aPnt5(myWidth / 2., 0, 0); 36 | 37 | // Profile : Define the Geometry 38 | Handle(Geom_TrimmedCurve) anArcOfCircle = GC_MakeArcOfCircle(aPnt2,aPnt3,aPnt4); 39 | Handle(Geom_TrimmedCurve) aSegment1 = GC_MakeSegment(aPnt1, aPnt2); 40 | Handle(Geom_TrimmedCurve) aSegment2 = GC_MakeSegment(aPnt4, aPnt5); 41 | 42 | // Profile : Define the Topology 43 | TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge(aSegment1); 44 | TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge(anArcOfCircle); 45 | TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge(aSegment2); 46 | TopoDS_Wire aWire = BRepBuilderAPI_MakeWire(anEdge1, anEdge2, anEdge3); 47 | 48 | // Complete Profile 49 | gp_Ax1 xAxis = gp::OX(); 50 | gp_Trsf aTrsf; 51 | 52 | aTrsf.SetMirror(xAxis); 53 | BRepBuilderAPI_Transform aBRepTrsf(aWire, aTrsf); 54 | TopoDS_Shape aMirroredShape = aBRepTrsf.Shape(); 55 | TopoDS_Wire aMirroredWire = TopoDS::Wire(aMirroredShape); 56 | 57 | BRepBuilderAPI_MakeWire mkWire; 58 | mkWire.Add(aWire); 59 | mkWire.Add(aMirroredWire); 60 | TopoDS_Wire myWireProfile = mkWire.Wire(); 61 | 62 | // Body : Prism the Profile 63 | TopoDS_Face myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile); 64 | gp_Vec aPrismVec(0, 0, myHeight); 65 | TopoDS_Shape myBody = BRepPrimAPI_MakePrism(myFaceProfile, aPrismVec); 66 | 67 | // Body : Apply Fillets 68 | BRepFilletAPI_MakeFillet mkFillet(myBody); 69 | TopExp_Explorer anEdgeExplorer(myBody, TopAbs_EDGE); 70 | while(anEdgeExplorer.More()){ 71 | TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExplorer.Current()); 72 | //Add edge to fillet algorithm 73 | mkFillet.Add(myThickness / 12., anEdge); 74 | anEdgeExplorer.Next(); 75 | 76 | } 77 | 78 | myBody = mkFillet.Shape(); 79 | 80 | // Body : Add the Neck 81 | gp_Pnt neckLocation(0, 0, myHeight); 82 | gp_Dir neckAxis = gp::DZ(); 83 | gp_Ax2 neckAx2(neckLocation, neckAxis); 84 | 85 | Standard_Real myNeckRadius = myThickness / 4.; 86 | Standard_Real myNeckHeight = myHeight / 10.; 87 | 88 | BRepPrimAPI_MakeCylinder MKCylinder(neckAx2, myNeckRadius, myNeckHeight); 89 | TopoDS_Shape myNeck = MKCylinder.Shape(); 90 | 91 | myBody = BRepAlgoAPI_Fuse(myBody, myNeck); 92 | 93 | // Body : Create a Hollowed Solid 94 | TopoDS_Face faceToRemove; 95 | Standard_Real zMax = -1; 96 | 97 | for(TopExp_Explorer aFaceExplorer(myBody, TopAbs_FACE); aFaceExplorer.More(); aFaceExplorer.Next()){ 98 | TopoDS_Face aFace = TopoDS::Face(aFaceExplorer.Current()); 99 | // Check if is the top face of the bottle’s neck 100 | Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace); 101 | if(aSurface->DynamicType() == STANDARD_TYPE(Geom_Plane)){ 102 | Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(aSurface); 103 | gp_Pnt aPnt = aPlane->Location(); 104 | Standard_Real aZ = aPnt.Z(); 105 | if(aZ > zMax){ 106 | zMax = aZ; 107 | faceToRemove = aFace; 108 | } 109 | } 110 | } 111 | 112 | TopTools_ListOfShape facesToRemove; 113 | facesToRemove.Append(faceToRemove); 114 | //myBody = BRepOffsetAPI_MakeThickSolid(myBody, facesToRemove, -myThickness / 50, 1.e-3); 115 | BRepOffsetAPI_MakeThickSolid aSolidMaker; 116 | aSolidMaker.MakeThickSolidByJoin(myBody, facesToRemove, -myThickness / 50, 1.e-3); 117 | myBody = aSolidMaker.Shape(); 118 | //myBody = BRepOffsetAPI_MakeThickSolidByJoin(myBody,facesToRemove, -myThickness / 50, 1.e-3,0.01); 119 | // Threading : Create Surfaces 120 | Handle(Geom_CylindricalSurface) aCyl1 = new Geom_CylindricalSurface(neckAx2, myNeckRadius * 0.99); 121 | Handle(Geom_CylindricalSurface) aCyl2 = new Geom_CylindricalSurface(neckAx2, myNeckRadius * 1.05); 122 | 123 | // Threading : Define 2D Curves 124 | gp_Pnt2d aPnt(2. * M_PI, myNeckHeight / 2.); 125 | gp_Dir2d aDir(2. * M_PI, myNeckHeight / 4.); 126 | gp_Ax2d anAx2d(aPnt, aDir); 127 | 128 | Standard_Real aMajor = 2. * M_PI; 129 | Standard_Real aMinor = myNeckHeight / 10; 130 | 131 | Handle(Geom2d_Ellipse) anEllipse1 = new Geom2d_Ellipse(anAx2d, aMajor, aMinor); 132 | Handle(Geom2d_Ellipse) anEllipse2 = new Geom2d_Ellipse(anAx2d, aMajor, aMinor / 4); 133 | Handle(Geom2d_TrimmedCurve) anArc1 = new Geom2d_TrimmedCurve(anEllipse1, 0, M_PI); 134 | Handle(Geom2d_TrimmedCurve) anArc2 = new Geom2d_TrimmedCurve(anEllipse2, 0, M_PI); 135 | gp_Pnt2d anEllipsePnt1 = anEllipse1->Value(0); 136 | gp_Pnt2d anEllipsePnt2 = anEllipse1->Value(M_PI); 137 | 138 | Handle(Geom2d_TrimmedCurve) aSegment = GCE2d_MakeSegment(anEllipsePnt1, anEllipsePnt2); 139 | // Threading : Build Edges and Wires 140 | TopoDS_Edge anEdge1OnSurf1 = BRepBuilderAPI_MakeEdge(anArc1, aCyl1); 141 | TopoDS_Edge anEdge2OnSurf1 = BRepBuilderAPI_MakeEdge(aSegment, aCyl1); 142 | TopoDS_Edge anEdge1OnSurf2 = BRepBuilderAPI_MakeEdge(anArc2, aCyl2); 143 | TopoDS_Edge anEdge2OnSurf2 = BRepBuilderAPI_MakeEdge(aSegment, aCyl2); 144 | TopoDS_Wire threadingWire1 = BRepBuilderAPI_MakeWire(anEdge1OnSurf1, anEdge2OnSurf1); 145 | TopoDS_Wire threadingWire2 = BRepBuilderAPI_MakeWire(anEdge1OnSurf2, anEdge2OnSurf2); 146 | BRepLib::BuildCurves3d(threadingWire1); 147 | BRepLib::BuildCurves3d(threadingWire2); 148 | 149 | // Create Threading 150 | BRepOffsetAPI_ThruSections aTool(Standard_True); 151 | aTool.AddWire(threadingWire1); 152 | aTool.AddWire(threadingWire2); 153 | aTool.CheckCompatibility(Standard_False); 154 | 155 | TopoDS_Shape myThreading = aTool.Shape(); 156 | 157 | // Building the Resulting Compound 158 | TopoDS_Compound aRes; 159 | BRep_Builder aBuilder; 160 | aBuilder.MakeCompound (aRes); 161 | aBuilder.Add (aRes, myBody); 162 | aBuilder.Add (aRes, myThreading); 163 | 164 | return aRes; 165 | } 166 | 167 | //function to convert opencascade shape to osg geometry 168 | osg::Geometry* createGeometryFromShape(TopoDS_Shape& shape, const osg::Vec3& geomColor, gp_Trsf& transformation) 169 | { 170 | // vector to save vertices 171 | osg::ref_ptr vertexList = new osg::Vec3Array(); 172 | 173 | 174 | osg::Array::Binding colorBinding = osg::Array::BIND_OVERALL; 175 | 176 | // vector to save _colorTool 177 | osg::ref_ptr colorList = new osg::Vec3Array(); 178 | if (colorBinding==osg::Array::BIND_OVERALL) 179 | { 180 | colorList->push_back(geomColor); 181 | } 182 | 183 | // create one osg primitive set 184 | osg::ref_ptr triangleStrip = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0); 185 | 186 | //osg::ref_ptr normalArray = new osg::Vec3Array; 187 | 188 | unsigned int noOfTriangles = 0; 189 | 190 | osg::ref_ptr geom = new osg::Geometry; 191 | 192 | // large vertex datasets work best with VBO. 193 | geom->setUseVertexBufferObjects(true); 194 | 195 | if(!shape.IsNull()) 196 | { 197 | // clean any previous triangulation 198 | BRepTools::Clean(shape); 199 | 200 | //_healShape(shape); 201 | 202 | /// call to incremental mesh on this shape 203 | /// \todo not sure why this 1 is passed. Its called deflection BTW 204 | /// need to find a way to calculate it 205 | double linearDeflection = 1.0; 206 | BRepMesh_IncrementalMesh(shape, linearDeflection); 207 | 208 | ///iterate faces 209 | // this variable will help in keeping track of face indices 210 | unsigned int index = 0; 211 | for (TopExp_Explorer ex(shape, TopAbs_FACE); ex.More(); ex.Next()) 212 | { 213 | TopoDS_Face face = TopoDS::Face(ex.Current()); 214 | TopLoc_Location location; 215 | 216 | /// triangulate current face 217 | Handle (Poly_Triangulation) triangulation = BRep_Tool::Triangulation(face, location); 218 | if (!triangulation.IsNull()) 219 | { 220 | int noOfNodes = triangulation->NbNodes(); 221 | 222 | // Store vertices. Build vertex array here 223 | for(int j = 1; j <= triangulation->NbNodes(); j++) 224 | { 225 | // populate vertex list 226 | // Ref: http://www.opencascade.org/org/forum/thread_16694/?forum=3 227 | gp_Pnt pt = triangulation->Node(j).Transformed(transformation * location.Transformation()); 228 | //gp_Pnt pt = (triangulation->Nodes())(j).Transformed(transformation * location.Transformation()); 229 | vertexList->push_back(osg::Vec3(pt.X(), pt.Y(), pt.Z())); 230 | // std::cout<push_back(geomColor); 235 | } 236 | } 237 | 238 | /// now we need to get face indices for triangles 239 | // get list of triangle first 240 | const Poly_Array1OfTriangle& triangles = triangulation->Triangles(); 241 | 242 | //No of triangles in this triangulation 243 | noOfTriangles = triangulation->NbTriangles(); 244 | 245 | Standard_Integer v1, v2, v3; 246 | for (unsigned int j = 1; j <= noOfTriangles; j++) 247 | { 248 | /// If face direction is reversed then we add verticews in reverse order 249 | /// order of vertices is important for normal calculation later 250 | if (face.Orientation() == TopAbs_REVERSED) 251 | { 252 | triangles(j).Get(v1, v3, v2); 253 | } 254 | else 255 | { 256 | triangles(j).Get(v1, v2, v3); 257 | } 258 | 259 | //gp_XYZ aPnt1 = (triangulation->Nodes())(v1).XYZ(); 260 | //gp_XYZ aPnt2 = (triangulation->Nodes())(v2).XYZ(); 261 | //gp_XYZ aPnt3 = (triangulation->Nodes())(v3).XYZ(); 262 | 263 | gp_XYZ aPnt1 = triangulation->Node(v1).XYZ(); 264 | gp_XYZ aPnt2 = triangulation->Node(v2).XYZ(); 265 | gp_XYZ aPnt3 = triangulation->Node(v3).XYZ(); 266 | // gp_XYZ aV12 = aPnt2 - aPnt1; 267 | // gp_XYZ aV23 = aPnt3 - aPnt2; 268 | // gp_XYZ aNor = aV12^aV23; 269 | 270 | // normalArray->push_back(osg::Vec3(aNor.X(),aNor.Y(),aNor.Z())); 271 | 272 | 273 | triangleStrip->push_back(index + v1 - 1); 274 | triangleStrip->push_back(index + v2 - 1); 275 | triangleStrip->push_back(index + v3 - 1); 276 | } 277 | index = index + noOfNodes; 278 | } 279 | } 280 | 281 | #ifdef _LOG_DEBUG_ 282 | std::cout << "Creating a geometry.." << std::endl; 283 | #endif 284 | 285 | geom->setVertexArray(vertexList.get()); 286 | //geom->setVertexAttribArray(0, geom->getVertexArray(), osg::Array::BIND_PER_VERTEX); 287 | geom->setColorArray(colorList.get(), colorBinding); 288 | //geom->setVertexAttribArray(2, geom->getColorArray(), osg::Array::BIND_OVERALL); 289 | //geom->setNormalArray(normalArray.get()); 290 | //geom->setVertexAttribArray(1, geom->getNormalArray(), osg::Array::BIND_PER_VERTEX); 291 | geom->addPrimitiveSet(triangleStrip.get()); 292 | //osgUtil::SmoothingVisitor::smooth(*geom); 293 | osgUtil::SmoothingVisitor::smooth(*geom,osg::PI/6.0); 294 | } 295 | 296 | return geom.release(); 297 | } 298 | 299 | osg::Geode* readStepFile(std::string path) 300 | { 301 | STEPControl_Reader aReader; 302 | TopoDS_Shape aShape; 303 | gp_Trsf transform; 304 | aReader.ReadFile(path.c_str()); 305 | 306 | osg::ref_ptr geode = new osg::Geode(); 307 | 308 | 309 | Standard_Integer nbr = aReader.NbRootsForTransfer(); 310 | 311 | for (Standard_Integer n = 1; n<= nbr; n++) { 312 | aReader.TransferRoot(n); 313 | } 314 | 315 | // Collecting resulting entities 316 | Standard_Integer nbs = aReader.NbShapes(); 317 | if (nbs == 0) { 318 | 319 | } 320 | else 321 | { 322 | for (Standard_Integer i=1; i<=nbs; i++) { 323 | aShape = aReader.Shape(i); 324 | 325 | osg::ref_ptr geom = createGeometryFromShape(aShape,osg::Vec3f(1.0,1.0,1.0),transform); 326 | osg::ref_ptr sd = (osg::ref_ptr)geom->asDrawable(); 327 | 328 | geode->addDrawable(sd); 329 | } 330 | } 331 | 332 | osg::ref_ptr stateSet = geode->getOrCreateStateSet(); 333 | 334 | // osg::ref_ptr program = new osg::Program; 335 | 336 | // osg::ref_ptr vertShader = new osg::Shader(osg::Shader::VERTEX); 337 | // if (!vertShader->loadShaderSourceFromFile("../shaders/myShader.vert")) 338 | // std::cerr << "Could not read VERTEX shader from file" << std::endl; 339 | // program->addShader(vertShader); 340 | 341 | // osg::ref_ptr geomShader = new osg::Shader(osg::Shader::GEOMETRY); 342 | // if (!geomShader->loadShaderSourceFromFile("../shaders/myShader.geom")) 343 | // std::cerr << "Could not read GEOM shader from file" << std::endl; 344 | // program->addShader(geomShader); 345 | 346 | // osg::ref_ptr fragShader = new osg::Shader(osg::Shader::FRAGMENT); 347 | // if (!fragShader->loadShaderSourceFromFile("../shaders/myShader.frag")) 348 | // std::cerr << "Could not read FRAGMENT shader from file" << std::endl; 349 | // program->addShader(fragShader); 350 | 351 | // stateSet->setAttributeAndModes(program.get(), osg::StateAttribute::ON); 352 | // stateSet->setMode(GL_BLEND, osg::StateAttribute::ON); 353 | 354 | osg::ref_ptr material = new osg::Material; 355 | material->setColorMode( osg::Material::AMBIENT_AND_DIFFUSE ); 356 | stateSet->setAttributeAndModes( material, osg::StateAttribute::ON ); 357 | stateSet->setMode( GL_DEPTH_TEST, osg::StateAttribute::ON ); 358 | 359 | return geode.release(); 360 | } 361 | 362 | //must be clubbed as a single class instead of helper functions 363 | osg::Geode* createBottle(double width,double height,double thickness,const osg::Vec3f& geomColor/*,const osg::Matrixd& viewMatrix*/) 364 | { 365 | gp_Trsf transform; 366 | 367 | TopoDS_Shape shape = MakeBottle((Standard_Real)width,(Standard_Real)height,(Standard_Real)thickness); 368 | osg::ref_ptr geom = createGeometryFromShape(shape,geomColor,transform); 369 | osg::ref_ptr sd = (osg::ref_ptr)geom->asDrawable(); 370 | osg::ref_ptr geode = new osg::Geode(); 371 | 372 | geode->addDrawable(sd); 373 | 374 | osg::ref_ptr stateSet = geode->getOrCreateStateSet(); 375 | osg::ref_ptr material = new osg::Material; 376 | material->setColorMode( osg::Material::AMBIENT_AND_DIFFUSE ); 377 | stateSet->setAttributeAndModes( material, osg::StateAttribute::ON ); 378 | stateSet->setMode( GL_DEPTH_TEST, osg::StateAttribute::ON ); 379 | return geode.release(); 380 | } 381 | -------------------------------------------------------------------------------- /Qtworkbench.pro.user.2bd5f8f: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {2bd5f8f1-ca6b-4581-b5ea-313c760e7365} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | true 41 | false 42 | 0 43 | true 44 | true 45 | 0 46 | 8 47 | true 48 | 1 49 | true 50 | true 51 | true 52 | false 53 | 54 | 55 | 56 | ProjectExplorer.Project.PluginSettings 57 | 58 | 59 | 60 | ProjectExplorer.Project.Target.0 61 | 62 | Desktop 63 | Desktop 64 | {1f143a7f-09a2-43ea-9393-5b7eddaae485} 65 | 0 66 | 0 67 | 0 68 | 69 | /home/nodei/build-Qtworkbench-Desktop-Debug 70 | 71 | 72 | true 73 | qmake 74 | 75 | QtProjectManager.QMakeBuildStep 76 | true 77 | 78 | false 79 | false 80 | false 81 | 82 | 83 | true 84 | Make 85 | 86 | Qt4ProjectManager.MakeStep 87 | 88 | -w 89 | -r 90 | 91 | false 92 | 93 | 94 | 95 | 2 96 | Build 97 | 98 | ProjectExplorer.BuildSteps.Build 99 | 100 | 101 | 102 | true 103 | Make 104 | 105 | Qt4ProjectManager.MakeStep 106 | 107 | -w 108 | -r 109 | 110 | true 111 | clean 112 | 113 | 114 | 1 115 | Clean 116 | 117 | ProjectExplorer.BuildSteps.Clean 118 | 119 | 2 120 | false 121 | 122 | Debug 123 | 124 | Qt4ProjectManager.Qt4BuildConfiguration 125 | 2 126 | true 127 | 128 | 129 | /home/nodei/build-Qtworkbench-Desktop-Release 130 | 131 | 132 | true 133 | qmake 134 | 135 | QtProjectManager.QMakeBuildStep 136 | false 137 | 138 | false 139 | false 140 | false 141 | 142 | 143 | true 144 | Make 145 | 146 | Qt4ProjectManager.MakeStep 147 | 148 | -w 149 | -r 150 | 151 | false 152 | 153 | 154 | 155 | 2 156 | Build 157 | 158 | ProjectExplorer.BuildSteps.Build 159 | 160 | 161 | 162 | true 163 | Make 164 | 165 | Qt4ProjectManager.MakeStep 166 | 167 | -w 168 | -r 169 | 170 | true 171 | clean 172 | 173 | 174 | 1 175 | Clean 176 | 177 | ProjectExplorer.BuildSteps.Clean 178 | 179 | 2 180 | false 181 | 182 | Release 183 | 184 | Qt4ProjectManager.Qt4BuildConfiguration 185 | 0 186 | true 187 | 188 | 189 | /home/nodei/build-Qtworkbench-Desktop-Profile 190 | 191 | 192 | true 193 | qmake 194 | 195 | QtProjectManager.QMakeBuildStep 196 | true 197 | 198 | false 199 | true 200 | false 201 | 202 | 203 | true 204 | Make 205 | 206 | Qt4ProjectManager.MakeStep 207 | 208 | -w 209 | -r 210 | 211 | false 212 | 213 | 214 | 215 | 2 216 | Build 217 | 218 | ProjectExplorer.BuildSteps.Build 219 | 220 | 221 | 222 | true 223 | Make 224 | 225 | Qt4ProjectManager.MakeStep 226 | 227 | -w 228 | -r 229 | 230 | true 231 | clean 232 | 233 | 234 | 1 235 | Clean 236 | 237 | ProjectExplorer.BuildSteps.Clean 238 | 239 | 2 240 | false 241 | 242 | Profile 243 | 244 | Qt4ProjectManager.Qt4BuildConfiguration 245 | 0 246 | true 247 | 248 | 3 249 | 250 | 251 | 0 252 | Deploy 253 | 254 | ProjectExplorer.BuildSteps.Deploy 255 | 256 | 1 257 | Deploy locally 258 | 259 | ProjectExplorer.DefaultDeployConfiguration 260 | 261 | 1 262 | 263 | 264 | false 265 | false 266 | 1000 267 | 268 | true 269 | 270 | false 271 | false 272 | false 273 | false 274 | true 275 | 0.01 276 | 10 277 | true 278 | 1 279 | 25 280 | 281 | 1 282 | true 283 | false 284 | true 285 | valgrind 286 | 287 | 0 288 | 1 289 | 2 290 | 3 291 | 4 292 | 5 293 | 6 294 | 7 295 | 8 296 | 9 297 | 10 298 | 11 299 | 12 300 | 13 301 | 14 302 | 303 | 2 304 | 305 | Qtworkbench 306 | Qtworkbench2 307 | Qt4ProjectManager.Qt4RunConfiguration:/home/nodei/practice/Qtworkbench/QtWorkbench/Qtworkbench.pro 308 | true 309 | 310 | Qtworkbench.pro 311 | false 312 | 313 | /home/nodei/build-Qtworkbench-Desktop-Debug 314 | 3768 315 | false 316 | true 317 | false 318 | false 319 | true 320 | 321 | 1 322 | 323 | 324 | 325 | ProjectExplorer.Project.TargetCount 326 | 1 327 | 328 | 329 | ProjectExplorer.Project.Updater.FileVersion 330 | 18 331 | 332 | 333 | Version 334 | 18 335 | 336 | 337 | -------------------------------------------------------------------------------- /Qtworkbench.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {ee762cfe-28a7-434e-aeda-8fe2d0b3108c} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | true 41 | false 42 | 0 43 | true 44 | true 45 | 0 46 | 8 47 | true 48 | 1 49 | true 50 | true 51 | true 52 | false 53 | 54 | 55 | 56 | ProjectExplorer.Project.PluginSettings 57 | 58 | 59 | 60 | ProjectExplorer.Project.Target.0 61 | 62 | Desktop 63 | Desktop 64 | {680290fd-6b2a-4582-a239-406cd8b91c4c} 65 | 0 66 | 0 67 | 0 68 | 69 | /home/vasanth/practice/Qtworkbench/build-Qtworkbench-Desktop-Debug 70 | 71 | 72 | true 73 | qmake 74 | 75 | QtProjectManager.QMakeBuildStep 76 | true 77 | 78 | false 79 | false 80 | false 81 | 82 | 83 | true 84 | Make 85 | 86 | Qt4ProjectManager.MakeStep 87 | 88 | -w 89 | -r 90 | 91 | false 92 | 93 | 94 | 95 | 2 96 | Build 97 | 98 | ProjectExplorer.BuildSteps.Build 99 | 100 | 101 | 102 | true 103 | Make 104 | 105 | Qt4ProjectManager.MakeStep 106 | 107 | -w 108 | -r 109 | 110 | true 111 | clean 112 | 113 | 114 | 1 115 | Clean 116 | 117 | ProjectExplorer.BuildSteps.Clean 118 | 119 | 2 120 | false 121 | 122 | Debug 123 | 124 | Qt4ProjectManager.Qt4BuildConfiguration 125 | 2 126 | true 127 | 128 | 129 | /home/vasanth/practice/Qtworkbench/build-Qtworkbench-Desktop-Release 130 | 131 | 132 | true 133 | qmake 134 | 135 | QtProjectManager.QMakeBuildStep 136 | false 137 | 138 | false 139 | false 140 | false 141 | 142 | 143 | true 144 | Make 145 | 146 | Qt4ProjectManager.MakeStep 147 | 148 | -w 149 | -r 150 | 151 | false 152 | 153 | 154 | 155 | 2 156 | Build 157 | 158 | ProjectExplorer.BuildSteps.Build 159 | 160 | 161 | 162 | true 163 | Make 164 | 165 | Qt4ProjectManager.MakeStep 166 | 167 | -w 168 | -r 169 | 170 | true 171 | clean 172 | 173 | 174 | 1 175 | Clean 176 | 177 | ProjectExplorer.BuildSteps.Clean 178 | 179 | 2 180 | false 181 | 182 | Release 183 | 184 | Qt4ProjectManager.Qt4BuildConfiguration 185 | 0 186 | true 187 | 188 | 189 | /home/vasanth/practice/Qtworkbench/build-Qtworkbench-Desktop-Profile 190 | 191 | 192 | true 193 | qmake 194 | 195 | QtProjectManager.QMakeBuildStep 196 | true 197 | 198 | false 199 | true 200 | false 201 | 202 | 203 | true 204 | Make 205 | 206 | Qt4ProjectManager.MakeStep 207 | 208 | -w 209 | -r 210 | 211 | false 212 | 213 | 214 | 215 | 2 216 | Build 217 | 218 | ProjectExplorer.BuildSteps.Build 219 | 220 | 221 | 222 | true 223 | Make 224 | 225 | Qt4ProjectManager.MakeStep 226 | 227 | -w 228 | -r 229 | 230 | true 231 | clean 232 | 233 | 234 | 1 235 | Clean 236 | 237 | ProjectExplorer.BuildSteps.Clean 238 | 239 | 2 240 | false 241 | 242 | Profile 243 | 244 | Qt4ProjectManager.Qt4BuildConfiguration 245 | 0 246 | true 247 | 248 | 3 249 | 250 | 251 | 0 252 | Deploy 253 | 254 | ProjectExplorer.BuildSteps.Deploy 255 | 256 | 1 257 | Deploy locally 258 | 259 | ProjectExplorer.DefaultDeployConfiguration 260 | 261 | 1 262 | 263 | 264 | false 265 | false 266 | 1000 267 | 268 | true 269 | 270 | false 271 | false 272 | false 273 | false 274 | true 275 | 0.01 276 | 10 277 | true 278 | 1 279 | 25 280 | 281 | 1 282 | true 283 | false 284 | true 285 | valgrind 286 | 287 | 0 288 | 1 289 | 2 290 | 3 291 | 4 292 | 5 293 | 6 294 | 7 295 | 8 296 | 9 297 | 10 298 | 11 299 | 12 300 | 13 301 | 14 302 | 303 | 2 304 | 305 | Qtworkbench 306 | 307 | Qt4ProjectManager.Qt4RunConfiguration:/home/vasanth/practice/Qtworkbench/QtWorkbench/Qtworkbench.pro 308 | true 309 | 310 | Qtworkbench.pro 311 | false 312 | 313 | 314 | 3768 315 | false 316 | true 317 | false 318 | false 319 | true 320 | 321 | 1 322 | 323 | 324 | 325 | ProjectExplorer.Project.TargetCount 326 | 1 327 | 328 | 329 | ProjectExplorer.Project.Updater.FileVersion 330 | 18 331 | 332 | 333 | Version 334 | 18 335 | 336 | 337 | --------------------------------------------------------------------------------