我想编写一个单线程应用程序,它会显示两个 QGLWidget(一个用于 3D,一个用于 2D)。我试图查找同时显示的两个 QGLWidgets 的信息,但我没有找到任何对我有帮助的东西。
现在,我认为我不需要共享上下文 - 显示的数据没有以有用的方式相关。两者应该放在 QMainWindow 内,最好用 QSplitter 分开。出于测试目的,我将两个 QGLWidget 放入一个 QHBoxLayout,但由于某种原因,一次只显示其中一个。它取决于创建小部件实例的顺序,就好像一个以某种方式覆盖另一个。QSplitter 根本不显示任何内容。如果我尝试对它们都调用 show(),则只会出现一个窗口。(我通过将布局放在一起的 QWidget 将 QGLWidgets 放入 QMainWindow。)
我错过了什么还是与我如何使用小部件有关?
主窗口:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ViewsWidget *widget = new ViewsWidget();
this->setCentralWidget(widget);
this->resize(640,480);
}
ViewsWidget::ViewsWidget()
{
//create two QGLWidgets
ModelView *modelView2 = new ModelView(this);
UnfoldedView *unfoldedView2 = new UnfoldedView(this);
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(modelView2);
layout->addWidget(unfoldedView2);
unfoldedView2->setMaximumSize(200,100);
modelView2->setMaximumSize(200,100);
}
QGL小部件:
typedef struct
{
float XYZW[4];
float RGBA[4];
} Vertex;
GLuint bufferId;
Vertex Vertices[] = //vertices
{
};
GLubyte Indices[] = { //indices
};
const size_t BufferSize = sizeof(Vertices);
const size_t VertexSize = sizeof(Vertices[0]);
const size_t RgbOffset = sizeof(Vertices[0].XYZW);
static const char *vertexShaderSource =
"attribute highp vec4 posAttr;\n"
"attribute lowp vec4 colAttr;\n"
"varying lowp vec4 col;\n"
"uniform highp mat4 matrix;\n"
"void main() {\n"
" col = colAttr;\n"
" gl_Position = matrix * posAttr;\n"
"}\n";
static const char *fragmentShaderSource =
"varying lowp vec4 col;\n"
"void main() {\n"
" gl_FragColor = col;\n"
"}\n";
ModelView::ModelView(QWidget *parent)
{
this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
this->setParent(parent);
}
void ModelView::initializeGL()
{
m_program = new QGLShaderProgram(this);
m_program->addShaderFromSourceCode(QGLShader::Vertex, vertexShaderSource);
m_program->addShaderFromSourceCode(QGLShader::Fragment, fragmentShaderSource);
m_program->link();
}
void ModelView::paintGL()
{
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, 200, 160);
m_program->bind();
QMatrix4x4 matrix;
matrix.perspective(60, 4.0/3.0, 0.1, 100.0);
matrix.translate(0, 0, -2);
vertices = (GLfloat *)fileSystem->getVertices();
colors = (GLfloat *)fileSystem->getColors();
m_program->setUniformValue(m_matrixUniform, matrix);
m_posAttr = m_program->attributeLocation("posAttr");
m_colAttr = m_program->attributeLocation("colAttr");
m_matrixUniform = m_program->uniformLocation("matrix");
glGenBuffers(1, &BufferId);
glBindBuffer(GL_ARRAY_BUFFER, BufferId);
glBufferData(GL_ARRAY_BUFFER, BufferSize, Vertices, GL_STATIC_DRAW);
glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, VertexSize, 0);
glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, VertexSize, (GLvoid *)RgbOffset);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glGenBuffers(1, &IndexBufferId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBufferId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
glDrawElements(GL_TRIANGLES, 48, GL_UNSIGNED_BYTE, NULL);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);
m_program->release();
}
void ModelView::resizeGL(int w, int h)
{
glViewport( 0, 0, w, qMax( h, 1 ) );
}
ModelView 和 UnfoldedView 类的代码几乎相同(目前)。我认为他们自己不会分享任何东西。