简体   繁体   中英

Using OpenGl code to the FreeCAD COIN3D scene?

I would like to be able to use OpenGL inside the QuarterWidget, FreeCAD use to glue Coin3D to OpenCascade scene. I can retrieve the following objects via python:

    def GetQuarterWidget(self): #From FreeCAD forum
       views = []
       self.mainWindow=Gui.getMainWindow()
       for w in self.mainWindow.findChild(QtGui.QMdiArea).findChildren(QtGui.QWidget):
           if w.inherits("SIM::Coin3D::Quarter::QuarterWidget"):
               views.append(w)
       return views

    def getMdiWindow(self)  #From FreeCAD forum
        mw = Gui.getMainWindow()
        mdi = mw.findChild(PySide2.QtWidgets.QMdiArea)

but I don't know how to be able to draw to the scene using OpenGL code... Say hello world code (drawing only a triangle)?

My goal is to be able to make a link to the scene so I can draw all my new object using OpenGL directly, not COIN3D, or using SDL2 library..etc.

I appreciate any hint to achieve that. I use python but I accept getting cpp code also.

Thank you very much

EDIT: I managed to draw hello world triangle inside the scene.. how good is the code? I am not sure yet. below is the code.

from OpenGL.GL import *
from OpenGL.GLU import *
import PySide2
import FreeCADGui as Gui
import pivy.coin as coin
import PySide.QtCore as QtCore
import PySide.QtGui as QtGui
from PySide2.QtOpenGL import * #as QtOPENGL
from OpenGL.WGL import *

def drawOpenGl(arg1,arg2):
    glTranslatef(-2.5, 0.5, -6.0)
    glColor3f( 1.0, 1.5, 0.0 )
    glPolygonMode(GL_FRONT, GL_FILL)
    glBegin(GL_TRIANGLES)
    glVertex3f(2.0,-1.2,0.0)
    glVertex3f(2.6,0.0,0.0)
    glVertex3f(2.9,-1.2,0.0)
    glEnd()

def drawsomething():
    w_view = Gui.ActiveDocument.ActiveView
    Root_SceneGraph = w_view.getSceneGraph()
    calback_=coin.SoCallback()
    calback_.setCallback(drawOpenGl)
    Root_SceneGraph.addChild(calback_)

drawsomething()

please notice that you need to install pyopengl inside freecad (not your pc/linux/mac version of pip or python) by running FreeCAD's python.

FREECAD_DIR/bin/Scripts/pip install pyopengl

Your code looks very similar to the sample in the Invertor Mentor book. I think you should store current state with glPushMatrix and glPopMatrix. Otherwise transformations may behave incorrectly.

def drawOpenGl(arg1,arg2):
    glPushMatrix()
    glTranslatef(-2.5, 0.5, -6.0)
    glColor3f( 1.0, 1.5, 0.0 )
    glPolygonMode(GL_FRONT, GL_FILL)
    glBegin(GL_TRIANGLES)
    glVertex3f(2.0,-1.2,0.0)
    glVertex3f(2.6,0.0,0.0)
    glVertex3f(2.9,-1.2,0.0)
    glEnd()
    glPopMatrix()

Not sure if this can be useful for you in any way, but there is a C++ sample that mixes a pure OpenGL geometry (a rectangle) and a Coin3D geometry (a cone) and uses Quarter:

#include <QApplication>
#include <Inventor/nodes/SoBaseColor.h>
#include <Inventor/nodes/SoCone.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoCallback.h>
#include <Quarter/Quarter.h>
#include <Quarter/QuarterWidget.h>
#include <GL/gl.h>

using namespace SIM::Coin3D::Quarter;

// Callback routine to render using OpenGL
void
myCallbackRoutine(void *, SoAction *)
{
   glPushMatrix();
   glTranslatef(0.0, -3.0, 0.0);
   glColor3f(1.0, 0.0, 0.0);
   glDisable(GL_LIGHTING);  // so we don't have to set normals
    glBegin(GL_POLYGON);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.5, 0.0, 0.0);
        glVertex3f(0.5, 0.5, 0.0);
        glVertex3f(0.0, 0.5, 0.0);
    glEnd();
    glEnable(GL_LIGHTING);
   glPopMatrix();
}

int
main(int argc, char ** argv)
{
    QApplication app(argc, argv);
    // Initializes Quarter library (and implicitly also the Coin and Qt
    // libraries).
    Quarter::init();
    // Make a dead simple scene graph by using the Coin library, only
    // containing a single yellow cone under the scenegraph root.
    SoSeparator * root = new SoSeparator;
    root->ref();
    SoBaseColor * col = new SoBaseColor;
    col->rgb = SbColor(1, 1, 0);
    root->addChild(col);
    root->addChild(new SoCone);
    
    SoCallback *myCallback = new SoCallback;
    myCallback->setCallback(myCallbackRoutine);
    root->addChild(myCallback);
    
    // Create a QuarterWidget for displaying a Coin scene graph
    QuarterWidget * viewer = new QuarterWidget;
    viewer->setSceneGraph(root);
    // make the viewer react to input events similar to the good old
    // ExaminerViewer
     viewer->setNavigationModeFile(QUrl("coin:///scxml/navigation/examiner.xml"));
    // Pop up the QuarterWidget
    viewer->show();
    // Loop until exit.
    app.exec();
    // Clean up resources.
    root->unref();
    delete viewer;
    Quarter::clean();
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM