opengl - QOpenGLWidget,QOpenGLWindow的问题
问题描述
谁能告诉我为什么我的paintGL中的以下代码在使用DrawArrays函数时不绘制?我在最后一个 Windows 10 上的 Qt Creator 中使用 Qt 5.14.2。
一些评论;
我正在尝试使用以下声明来了解 Qt 中 OpenGL 实现之间的差异。随着时间的推移,我将使用我最喜欢的实现来编写我的应用程序。
- 类 OpenGLWindow:公共 QWindow,公共 QOpenGLFunctions
当我将以下代码放在 render() 函数中时,这工作正常,完全没有问题,非常好!!
- 类 myOpenGLWidget:公共 QOpenGLWidget,公共 QOpenGLFunctions
我将代码放在paintGL 函数中。我可以为背景着色,但 glDrawArrays() 什么也不做。但是,我可以画一个三角形,glBegin 和 glEnd 语句之间的代码是成功的。我没有收到任何错误。我测试 m_program->bind() 调用的结果,结果返回 true。
- 类 myQOpenGLWindow:公共 QOpenGLWindow,受保护的 QOpenGLFunctions
与 #2 相同,只是我将代码放在 render() 函数中。我可以为背景着色,但 glDrawArrays() 什么也不做。但是,我可以画一个三角形,glBegin 和 glEnd 语句之间的代码是成功的。我测试 m_program->bind() 调用的结果,结果返回 true。
如果有人需要问,我是在阅读了几十个不同的教程之后这样做的,这是我能找到的最好的信息。
谢谢你!!
{ // 绘制场景:
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
const qreal retinaScale = devicePixelRatio();
f->glViewport(0, 0, width() * retinaScale, height() * retinaScale);
f->glClearColor(red, green, blue, 1.0f);
f->glClear(GL_COLOR_BUFFER_BIT);
QMatrix4x4 matrix;
matrix.perspective(60.0f, 4.0f / 3.0f, 0.1f, 100.0f);
matrix.translate(0, 0, 0);
matrix.rotate(0, 1, 0, 0);
m_program->setUniformValue(m_matrixUniform, matrix);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-0.5, -0.5, 0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 0.5, -0.5, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f( 0.0, 0.5, 0);
glEnd();
//f->glBindTexture(GL_TEXTURE_2D, 0);
bound = m_program->bind();
GLfloat line_vertices[2160];
for (int v=0;v<360;v++)
{
line_vertices[(6*v)]=-3.5+float(v)/25;
line_vertices[(6*v)+1]=1.1+qSin(5*2*v*(M_PI)/180);
line_vertices[(6*v)+2]=-5;
line_vertices[(6*v)+3]=-3.5+float(v+1)/25;
line_vertices[(6*v)+4]=1.1+qSin(5*2*(v+1)*(M_PI)/180);;
line_vertices[(6*v)+5]=-5;
}
GLfloat line_colors[2160];
for (int v=0;v<360;v++)
{
line_colors[(6*v)]=1.0;
line_colors[(6*v)+1]=0;
line_colors[(6*v)+2]=0;
line_colors[(6*v)+3]=1.0;
line_colors[(6*v)+4]=0;
line_colors[(6*v)+5]=0;
}
f->glVertexAttribPointer(m_posAttr, 3, GL_FLOAT, GL_FALSE, 0, line_vertices);
f->glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, line_colors);
f->glEnableVertexAttribArray(m_posAttr);
f->glEnableVertexAttribArray(m_colAttr);
f->glDrawArrays(GL_LINES, 0, 360);
f->glDisableVertexAttribArray(m_colAttr);
f->glDisableVertexAttribArray(m_posAttr);
m_program->release();
}
解决方案
GL_VERSION = 4.6.0
这是我定义位置和颜色顶点、VBO、位置和颜色缓冲区的各种设置,然后是 glDrawArrays 函数的代码部分。我在 render() 和 paintgl() 函数中都尝试过这段代码。我没有得到任何错误,但我也没有得到漂亮的线条。不过,在 begin() 和 end() 之间定义的三角形顶点确实会显示出来。
GLfloat line_vertices[2160];
for (int v=0;v<360;v++)
{
line_vertices[(6*v)]=-3.5+float(v)/25;
line_vertices[(6*v)+1]=1.1+qSin(5*2*v*(M_PI)/180);
line_vertices[(6*v)+2]=-5;
line_vertices[(6*v)+3]=-3.5+float(v+1)/25;
line_vertices[(6*v)+4]=1.1+qSin(5*2*(v+1)*(M_PI)/180);;
line_vertices[(6*v)+5]=-5;
}
GLfloat line_colors[2160];
for (int v=0;v<360;v++)
{
line_colors[(6*v)]=1.0;
line_colors[(6*v)+1]=0;
line_colors[(6*v)+2]=0;
line_colors[(6*v)+3]=1.0;
line_colors[(6*v)+4]=0;
line_colors[(6*v)+5]=0;
}
QOpenGLBuffer m_vertexBufferCube;
QOpenGLBuffer m_colorBufferCube;
m_program->setUniformValue(m_matrixUniform, matrix);
m_vertexBufferCube.create();
m_vertexBufferCube.setUsagePattern(QOpenGLBuffer::StaticDraw);
m_vertexBufferCube.allocate(line_vertices, 3 * 360 * sizeof(float));
m_colorBufferCube.create();
m_colorBufferCube.setUsagePattern(QOpenGLBuffer::StaticDraw);
m_colorBufferCube.allocate(line_colors, 3 * 360 * sizeof(float));
bound = m_vertexBufferCube.bind();
//m_program->setAttributeBuffer("vertexPosition", GL_FLOAT,0,3);
m_program->setAttributeBuffer(m_posAttr, GL_FLOAT,0,3);
m_colorBufferCube.bind();
//m_program->setAttributeBuffer("colorPosition", GL_FLOAT,0,3);
m_program->setAttributeBuffer(m_colAttr, GL_FLOAT,0,3);
//glVertexAttribPointer(m_posAttr, 3, GL_FLOAT, GL_FALSE, 0, line_vertices);
//glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, line_colors);
//glEnableVertexAttribArray(m_posAttr);
//glEnableVertexAttribArray(m_colAttr);
glDrawArrays(GL_LINES, 0, 360);
推荐阅读
- jquery - Wordpress Woocommerce 商店页面:使用 swiper 查看产品的模式 - 如何提高加载速度?
- dax - 是否可以从 Power BI 中的度量中删除钻取筛选器?
- python - PySpark Pytest 错误:sc = SparkContext(conf=conf)
- junit - 用于 OnFailure 回调的 Kafka Junit
- c++ - 如何解决 registerPOintPickingCallback 错误?
- c++ - 为什么在 C++ 中编译 64 位时会出现 32 位溢出?
- ios - Swift - Google 地图的 OpenUrl 不显示方向(永远加载)
- amazon-web-services - AWS - 如何在负载均衡器前创建通用 dns
- spring-boot - 添加 spring 集成导致 Spring boot 中的冲突
- swift - 使用 Firebase Auth 在 swiftUI TextField 和 SecureField 中读取登录表单的值