使用OpenGl代码到FreeCAD COIN3D场景中?

bz4sfanl  于 2022-12-12  发布在  其他
关注(0)|答案(1)|浏览(180)

我希望能够在QuarterWidget中使用OpenGL,FreeCAD使用它将Coin 3D粘贴到OpenCascade场景中。我可以通过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)

但我不知道如何能够绘制到场景使用OpenGL代码...说你好世界代码(只画一个三角形)?
我的目标是能够制作一个场景链接,这样我就可以直接使用OpenGL绘制我所有的新对象,而不是COIN 3D,或者使用SDL 2库等。
我很感激任何实现这一点的提示。我使用python,但我也接受获得cpp代码。
非常感谢您的光临
编辑:我成功地在场景里面画出了你好世界的三角形。。代码有多好?我还不确定。下面是代码。

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()

请注意,您需要通过运行FreeCAD的python在freecad中安装pyopengl(而不是您的pc/linux/mac版本的pip或python)。

FREECAD_DIR/bin/Scripts/pip install pyopengl
jv4diomz

jv4diomz1#

你的代码看起来很像sample in the Invertor Mentor书。我认为你应该用glPushMatrix和glPopMatrix存储当前状态。否则转换可能会不正确。

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()

不确定这是否对您有用,但有一个C++示例混合了纯OpenGL几何体(矩形)和Coin3D几何体(圆锥体),并使用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;
}

相关问题