【发布时间】:2015-03-30 23:27:54
【问题描述】:
我尝试将 Bullet Physics 的调试绘图接口集成到 QML 中,所以我必须实现一个 drawLine() 方法。
void drawLine(const btVector3 &from, const btVector3 &to, const btVector3 &color);
我尝试的是从 QQuickItem3D 和 btIDebugDraw 继承了一个在场景中使用的项目。在drawLine() 中,我将这些行添加到成员向量中。在 Qt 的drawItem() 中,我遍历这些行并使用 OpenGL 调用来渲染它们。但是,它们不会出现在屏幕上。
如何在 3D 空间和正确的相机视图中绘制线条?
void DebugDrawer::drawItem(QGLPainter *painter)
{
if (lines_.size() < 1)
return;
// Draw current lines
painter->modelViewMatrix().push();
glBegin(GL_LINES);
for (auto &line : lines_) {
glColor3f(line.color.getX(), line.color.getY(), line.color.getZ());
glVertex3f(line.from.getX(), line.from.getY(), line.from.getZ());
glVertex3f(line.to.getX(), line.to.getY(), line.to.getZ());
}
glEnd();
painter->modelViewMatrix().pop();
// Reset buffer
lines_.clear();
}
【问题讨论】:
-
modelViewMatrix 是 Projection * View 矩阵吗?除此之外,您的代码看起来还不错。
-
另外,我认为您正在从 btIDebugDraw 继承的 drawLine 函数中推新行,在此之前您已调用:
setDebugMode(btIDebugDraw::DBG_DrawWireframe)? -
最后一件事,btDiscreteDynamicsWorld->debugDrawWorld();必须在您的主要绘图功能中调用。您的问题忽略了 lines_ 是否被填充,但这些都是您所需要的,尤其是当您使用旧版 GL 时。
标签: c++ qt opengl qml qtquick3d