- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 QGLWidget (Qt 5.2.1)。
GLWidget 实例化另外两个类,每个类都包含自己的 QOpenGLShaderPrograms,加载自己的着色器,并处理自己的绘图。这形成了一个很好的封装。
但是——问题是来自轴网格(第一个正在初始化的)的数据出现在第二类的着色器中。因此,它从不使用自己的着色器绘制自己的对象。如果我颠倒 init() 调用的顺序,同样的事情也会反过来发生。
这两个绘图类的结构相同,因此为简洁起见,我只包含 Mesh 类。
这是结果。绘制线条的 AxisMesh 将它们绘制在 Scatter 类上,使用相同的颜色,而不是它们自己的颜色和应该绘制的位置。
这张图中的线条应该是不同的颜色和不同的位置:
问题:
是否允许在一个QGLWidget 中有两个QOpenGLShaderPrograms?
下面的方法有问题吗?
GLWidget initialzeGL 方法:
void GLWidget::initializeGL()
{
// Instantiate our drawing objects
m_axisMesh = new PlotItemAxisMesh(m_plotManager, m_plotSelection, &m_axisScale);
m_surfaceScatter = new PlotItemSurfaceScatter(m_plotManager, m_plotSelection, &m_axisScale);
...
// Initialize the axis mesh class
m_axisMesh->init();
// Initialize the scatter points class
m_surfaceScatter->init();
}
GLWidget paintGL方法:
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
...
// Draw the mesh
m_axisMesh->draw(m_camera.data(), modelMatrix);
// Draw the points
m_surfaceScatter->draw(m_camera.data(), modelMatrix);
}
Axis Mesh 初始化方法:
void PlotItemAxisMesh::init()
{
initializeOpenGLFunctions();
// Initialize the shaders
initShaders();
m_program->link();
m_program->bind();
// Load the data into the local VBO
build();
// Release (unbind) all
m_program->release();
}
Axis Mesh 构建方法:
void PlotItemAxisMesh::build()
{
ShmooPlotMatrix *matrix = m_plotManager->getPlotPointMatrix();
// Calculate the y-axis height in OpenGL terms
uint32_t yHeight = (m_xMax + m_yMax)/2;
float yScale = yHeight / fabs(m_axisScale->getMax() - m_axisScale->getMin());
float yOffset = 0 ? m_axisScale->getMin() > 0 : -m_axisScale->getMin();
// Since we swept X/Y but are plotting the points as X/Z, then Y becomes the value
m_xMax = matrix->getXMax();
m_yMax = yHeight;
m_zMax = matrix->getYMax();
m_vertexArray.clear();
m_vertexArray.reserve(4*(m_xMax + m_yMax));
... (load vertexAray with data)
m_vertexBuffer.create();
m_vertexBuffer.bind();
m_vertexBuffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
m_vertexBuffer.allocate(&(m_vertexArray.front()), vertexSize);
// Tell VBO how to read the data
m_positionAttrIndex = m_program->attributeLocation(m_positionAttr);
m_colorAttrIndex = m_program->attributeLocation(m_colorAttr);
int offset = 0;
m_program->enableAttributeArray(m_positionAttrIndex);
m_program->setAttributeBuffer(m_positionAttrIndex, GL_FLOAT, offset, 3, sizeof(VertexData));
offset = sizeof(QVector3D);
m_program->enableAttributeArray(m_colorAttrIndex);
m_program->setAttributeBuffer(m_colorAttrIndex, GL_FLOAT, offset, 3, sizeof(VertexData));
// Release (unbind) all
m_vertexBuffer.release();
}
Axis Mesh绘制方法:
void PlotItemAxisMesh::draw(PlotCamera *camera, const QMatrix4x4 &modelMatrix)
{
m_program->bind();
{
// Set modelview-projection matrix
m_program->setUniformValue("mvpMatrix", camera->getProjection() * camera->getView() * modelMatrix);
m_vertexBuffer.bind();
glDrawArrays(GL_LINES, 0, m_vertexArray.size());
m_vertexBuffer.release();
}
m_program->release();
}
最佳答案
感谢@Reto Koradi 提供的解决方案
解决方法是更改绘制方法以在绘制时启用/设置属性缓冲区。
void PlotItemAxisMesh::draw(PlotCamera *camera, const QMatrix4x4 &modelMatrix)
{
m_program->bind();
{
m_vertexBuffer.bind();
int offset = 0;
m_program->enableAttributeArray(m_positionAttrIndex);
m_program->setAttributeBuffer(m_positionAttrIndex, GL_FLOAT, offset, 3, sizeof(VertexData));
offset = sizeof(QVector3D);
m_program->enableAttributeArray(m_colorAttrIndex);
m_program->setAttributeBuffer(m_colorAttrIndex, GL_FLOAT, offset, 3, sizeof(VertexData));
// Set modelview-projection matrix
m_program->setUniformValue("mvpMatrix", camera->getProjection() * camera->getView() * modelMatrix);
glDrawArrays(GL_LINES, 0, m_vertexArray.size());
m_vertexBuffer.release();
}
m_program->release();
}
正确的绘图
关于c++ - QOpenGLShaderProgram - 多个实例产生损坏的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29879866/
我有一个 QGLWidget (Qt 5.2.1)。 GLWidget 实例化另外两个类,每个类都包含自己的 QOpenGLShaderPrograms,加载自己的着色器,并处理自己的绘图。这形成了一
我正在通过OpenGL和Qt在GPU上实现一些数值算法。 但是我不是很熟悉。 我想从当前着色器中提取一些函数到某个“着色器库”,并通过字符串插值在其他着色器中使用它。实现起来并不难,但我不知道如何处理
我是一名优秀的程序员,十分优秀!