如何在缓冲区中将一些顶点渲染为点,其余的顶点在 qopenglwidget 中使用线条渲染?

How to render some vertices in a buffer as points and the rest using lines in qopenglwidget?

本文关键字:顶点 qopenglwidget 缓冲区      更新时间:2023-10-16

我在缓冲区中存储了一组顶点和法线。我想将大部分顶点显示为点,但我想为其余几个顶点绘制线条。这些都存储在一个向量中,点部分在前面,我知道缓冲区的位置,直到它们使用点显示。我还知道每个绘图任务的元素计数。我只使用一个 vao 和一个缓冲区对象来完成此任务。

我使用以下代码初始化 GLWidget:

void GLWidget::initializeGL()
{
connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &GLWidget::cleanup);
initializeOpenGLFunctions();
glClearColor(0, 0, 0, m_transparent ? 0 : 1);
m_program = new QOpenGLShaderProgram;
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, m_core ? vertexShaderSourceCore : vertexShaderSource);
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, m_core ? fragmentShaderSourceCore : fragmentShaderSource);
m_program->bindAttributeLocation("vertex", 0);
m_program->bindAttributeLocation("normal", 1);
m_program->link();
m_program->bind();
m_projMatrixLoc = m_program->uniformLocation("projMatrix");
m_mvMatrixLoc = m_program->uniformLocation("mvMatrix");
m_normalMatrixLoc = m_program->uniformLocation("normalMatrix");
m_lightPosLoc = m_program->uniformLocation("lightPos");
m_vao.create();
QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
m_obj.create();
setupBuffer();
setupVertexAttribs();
m_camera.setToIdentity();
QVector3D camPos = QVector3D(0.0, 0.0, 15.0);
m_camera.translate(-camPos);
QVector3D camTarget = QVector3D(0.0, 0.0, 0.0);
QVector3D camDirection = QVector3D(camPos - camTarget).normalized();
QVector3D worldUp = QVector3D(0.0, 1.0, 0.0);
QVector3D camRight = QVector3D::crossProduct(worldUp, camDirection).normalized();
QVector3D camUp = QVector3D::crossProduct(camDirection, camRight);
m_camera.lookAt(camPos, camTarget, camUp);
// Light position is fixed.
m_program->setUniformValue(m_lightPosLoc, QVector3D(0, 0, 200));
m_program->release();
}

其中函数setupBuffer(( setupVertexAtrribs((执行与其名称所暗示的任务相同的任务。顶点布置在缓冲区中,顶点的 xyz 位置后跟其关联法线的 xyz 位置。它们的实现方式如下

void GLWidget::setupBuffer()
{
m_obj.bind();
m_obj.allocate(vertices.constData(), vertices.size() * sizeof(GLfloat));
}
void GLWidget::setupVertexAttribs()
{
m_obj.bind();
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->glEnableVertexAttribArray(0);
f->glEnableVertexAttribArray(1);
f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), reinterpret_cast<void *>(0));
f->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), reinterpret_cast<void *>(3 * sizeof(GLfloat)));
m_obj.release();
}

现在,QVector 顶点是传递给 opengl 的缓冲区。此向量中的最后几个条目是需要使用GL_LINES绘制的顶点。

我的paintGL((函数看起来像这样:

void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_POINT_SIZE);
glEnable(GL_LINE_WIDTH);
glPointSize(2);
glLineWidth(10);
m_world.setToIdentity();
m_world.rotate(180.0f - (m_xRot / 16.0f), 1, 0, 0);
m_world.rotate(m_yRot / 16.0f, 0, 1, 0);
m_world.rotate(m_zRot / 16.0f, 0, 0, 1);
m_world.scale(m_dispScale);
QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
m_program->bind();
m_program->setUniformValue(m_projMatrixLoc, m_proj);
m_program->setUniformValue(m_mvMatrixLoc, m_camera * m_world);
QMatrix3x3 normalMatrix = m_world.normalMatrix();
m_program->setUniformValue(m_normalMatrixLoc, normalMatrix);
glDrawArrays(GL_POINTS, 0, vertices.size() - camVertices.size());
// Draw camera frustums
glDrawArrays(GL_LINES, vertices.size() - camVertices.size(), camVertices.size());
//glDrawElements(GL_POINTS, vecIndices.size(), GL_UNSIGNED_INT, 0);
m_program->release();
}

QVector camVertices是另一个包含需要使用直线绘制的点的矢量。在渲染之前,camVertices 中的数据将附加到矢量"顶点"的末尾。如上面的代码所示,我调用 glDrawArrays 函数两次 - 第一次,从缓冲区的 0 索引开始,第二次,从上一次调用结束的地方开始显示剩余的点。

问题是点显示正常。但是,第二次调用仅显示点,但不绘制任何线条。

下面是指向所显示输出的屏幕截图的链接 - https://drive.google.com/open?id=1i7CjO1qkBALw78KKYGvBteydhfAWh3wh

该图显示了一个示例,其中在顶部与其他点(许多点的框(一起看到的亮绿色点是要用线条绘制的点。但是,我只看到点,看不到任何线条。

我做了一个简单的测试,我能够使用以下代码绘制点和线:

glDrawArrays(GL_POINTS, 0, verticesCount() - 10);
glDrawArrays(GL_LINES, 10, 10);

除了变量之外,它与您的没有太大区别。我也在使用 1 VAO。因此,绝对有可能像我们期望的那样在点之后画线。

你能尝试同样的方法吗(使用整数而不是变量(

你能在你的顶点上显示调试信息吗?

你能上传一个最小的可编译示例吗?