我想编写一个单线程应用程序,它将显示两个QGLWidgets(一个用于3D,一个用于2D)。我试图找到两个QGLWidgets同时显示的信息,但我没有找到任何可以帮助我的。
现在,我认为我不需要共享上下文-显示的数据在有用的方式上没有关联。这两个应该放在QMainWindow中,最好用QSplitter来分割。出于测试目的,我将两个QGLWidgets放入一个QHBoxLayout中,但由于某种原因,一次只显示其中一个。这取决于创建小部件实例的顺序,就好像一个以某种方式覆盖了另一个一样。QSplitter根本不显示任何东西。如果我尝试对它们两个调用show(),那么只会显示一个窗口。(我将QGLWidgets放入QMainWindow中,通过QWidget将布局放在一起。)
是我错过了什么,还是它与我如何使用小部件有关?
主窗口:
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);
}
QGLWidget:
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类几乎是相同的代码(目前)。我想他们不会自己分享任何东西。
我忘记在ViewsWidget中调用setLayout(),我在那里把布局放在一起。当我直接将QSplitter设置为QMainWindow的中心Widget时,它就开始工作了。它似乎已经正确地呈现,但没有办法正确地将小部件连接在一起。