Domanda

I'd like to program a single threaded application that would show two QGLWidgets (one for 3D, one for 2D). I've tried to find information on two QGLWidgets displaying at once, but I haven't found anything that would help me.

Now, I think I don't need shared contexts - the data displayed aren't related in a useful way. The two should be put inside a QMainWindow, preferably divided by a QSplitter. For testing purposes, I put the two QGLWidgets into a QHBoxLayout, but for some reason, only one of them is showing at a time. It depends on the order of creating the instances of widgets, as if one somehow overwrites the other. QSplitter doesn't show anything at all. If I try to call show() on both of them, only one window ever shows up. (I put the QGLWidgets into the QMainWindow through a QWidget that puts the layout together.)

Am I missing something or is it something related to how I use the widgets?

Main Window:

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 ) );
}

The ModelView and UnfoldedView classes are pretty much the same code (for now). I assume they aren't sharing anything by themselves.

È stato utile?

Soluzione

I forgot to call setLayout() in the ViewsWidget, where I was putting the layout together. QSplitter started to work as soon as I directly set it as the central Widget of the QMainWindow. It appears to have been rendering correctly, but didn't have a way of properly connecting the widgets together.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top