簡體   English   中英

無法在QWIndow(Qt5)中使用OpenGL啟用深度測試

[英]Can't enable depth test with OpenGL in QWIndow (Qt5)

我正在嘗試使用QWindow在Qt 5.2中編寫一個簡單的OpenGL應用程序。 我似乎無法啟用深度測試。 我已經大大簡化了QWindow OpenGL示例 :我繪制了一個帶有彩色頂點的三角形,然后是一個帶有白色頂點的三角形。 白色三角形的Z坐標較大,因此應出現在彩色三角形的后面。 沒有。

我將QWindow的表面格式的深度緩沖區大小明確設置為24,但是當我使用QWindow::format()函數檢查時,深度緩沖區大小為0。使用QGLFormat時,我知道有一個setDepth()函數可以用於打開深度緩沖區,但是QSurfaceFormat中沒有類似的功能。

我究竟做錯了什么?

我的代碼...

testWindow.cpp:

#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QResizeEvent>
#include <QSurfaceFormat>
#include <QWidget>
#include <QWindow>
#include <QVBoxLayout>

#include "SphereWindow.h"

class GLDialog : public QDialog
{
public:
  GLDialog(QWidget *parent = 0, Qt::WindowFlags f = 0) : QDialog(parent, f)
  {
    QVBoxLayout *layout = new QVBoxLayout(this);

    QSurfaceFormat format;
    format.setSamples(16);
    format.setDepthBufferSize(24);

    qDebug() << "requested format:" << format;

    window = new SphereWindow;
    window->setFormat(format);

    qDebug() << "actual format:" << window->format();

    window->render();

    QWidget *glWidget = QWidget::createWindowContainer(window, this);
    layout->addWidget(glWidget);

  }

  ~GLDialog()
  {
    delete window;
  }

protected:
  void resizeEvent(QResizeEvent *event)
  {
    window->resize(event->size());
    window->render();
  }

private:
  SphereWindow *window;



};


int main(int argc, char **argv)
{
  QApplication app(argc, argv);

  QDialog *dlg = new GLDialog;

  dlg->resize(640,480);
  dlg->show();

  return app.exec();
}

SphereWindow.h:

#include <QColor>
#include <QEvent>
#include <QExposeEvent>
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#include <QOpenGLPaintDevice>
#include <QOpenGLShaderProgram>
#include <QPainter>
#include <QResizeEvent>
#include <QSize>
#include <QWindow>


class SphereWindow : public QWindow, protected QOpenGLFunctions
{
  Q_OBJECT
public:
  SphereWindow(QWindow * = 0);
  virtual ~SphereWindow();
  virtual void render();
  virtual void initialize();

public slots:
  void resizeViewport(const QSize &);

protected:
  virtual void resizeEvent(QResizeEvent *); 


private:
  bool _initialized;
  QOpenGLContext *_context;
  QOpenGLPaintDevice *_device;
  QOpenGLShaderProgram *_program;

  QColor _backgroundColour;

  GLuint _posAttr;
  GLuint _colAttr;
};

SphereWindow.cpp:

#include <QCoreApplication>
#include <QMatrix4x4>
#include <QOpenGLShader>
#include <QScreen>
#include <QSurfaceFormat>

#include <QDebug>

#include "SphereWindow.h"

SphereWindow::SphereWindow(QWindow *parent)
: QWindow(parent), 
_initialized(0), 
_program(0), 
_backgroundColour(Qt::black) 
{
  setSurfaceType(QWindow::OpenGLSurface);
  create();      

  _context = new QOpenGLContext(this);
  _context->setFormat(requestedFormat());
  _context->create();
}

SphereWindow::~SphereWindow()
{ }


void SphereWindow::resizeEvent(QResizeEvent *event)
{
  resizeViewport(event->size());
}

void SphereWindow::resizeViewport(const QSize &size)
{
  int width = size.width();
  int height = size.height();
  int side = qMin(width, height);

  int hoffset = (int)((width - side) / 2.0 + 0.5);
  int voffset = (int)((height - side) / 2.0 + 0.5);

  glViewport(hoffset, voffset, side, side);
}

void SphereWindow::render()
{

  if (! _initialized)
    initialize();

  if (! isExposed()) return;
  glEnable(GL_DEPTH_TEST);

  _context->makeCurrent(this);
  glClearColor(_backgroundColour.redF(), _backgroundColour.greenF(), _backgroundColour.blueF(), 1.0);

  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  _program->bind();

    GLfloat vertices[] = {
        -0.75f, 0.75f, 0.0f,
        -0.75f, -0.75f, 0.0f,
        0.75f, -0.75f, 0.0f,

        0.75f, 0.75f, 0.5f,
        0.75f, -0.75f, 0.5f,
        -0.75f, -0.75f, 0.5f,

    };

    GLfloat colors[] = {
        1.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 1.0f,
        1.0f, 1.0f, 1.0f,
        1.0f, 1.0f, 1.0f,
        1.0f, 1.0f, 1.0f,
    };

    glVertexAttribPointer(_posAttr, 3, GL_FLOAT, GL_FALSE, 0, vertices);
    glVertexAttribPointer(_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    glDrawArrays(GL_TRIANGLES, 0, 6);

    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);   

    _program->release();
    _context->swapBuffers(this);
    _context->doneCurrent();
}

static const char *vertexShaderSource =
    "attribute highp vec4 posAttr;\n"
    "attribute lowp vec4 colAttr;\n"
    "varying lowp vec4 col;\n"
    "void main() {\n"
    "   col = colAttr;\n"
    "   gl_Position = posAttr;\n"
    "}\n";

static const char *fragmentShaderSource =
    "varying lowp vec4 col;\n"
    "void main() {\n"
    "   gl_FragColor = col;\n"
    "}\n";


void SphereWindow::initialize()
{
  _context->makeCurrent(this);
  _program = new QOpenGLShaderProgram(this);
  _program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
  _program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
  _program->link();

  _program->bind();

  _posAttr = _program->attributeLocation("posAttr");
  _colAttr = _program->attributeLocation("colAttr");

  _program->release();



  initializeOpenGLFunctions();
}

testqwindow.pro:

######################################################################
# Automatically generated by qmake (3.0) Sat May 3 05:01:55 2014
######################################################################

TEMPLATE = app
TARGET = testqwindow
INCLUDEPATH += .

QT += widgets
CONFIG += debug

# Input
HEADERS += SphereWindow.h
SOURCES += SphereWindow.cpp testWindow.cpp

問題是這樣的:我在調用SphereWindow構造函數后設置了表面格式,但是創建了一個QOpenGLContext並在構造函數內設置了其格式。 結果是上下文沒有獲得深度緩沖區。 我已經QOpenGLContext::setFormat()QOpenGLContext::create()的調用移到了SphereWindow::initialize()函數中,並且深度緩沖現在對我SphereWindow::initialize()

SphereWindow::SphereWindow(QWindow *parent)
: QWindow(parent), 
_initialized(0), 
_program(0), 
_backgroundColour(Qt::black) 
{
  setSurfaceType(QWindow::OpenGLSurface);
  create();

  qDebug() << "format:" << format();


  _context = new QOpenGLContext(this);
  /* Move these lines into initialize() */
//   _context->setFormat(requestedFormat());
//   _context->create();
}

...

void SphereWindow::initialize()
{
  _context->setFormat(requestedFormat());
  _context->create();

  _context->makeCurrent(this);
  _program = new QOpenGLShaderProgram(this);
  _program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
  _program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
  _program->link();

  _program->bind();

  _posAttr = _program->attributeLocation("posAttr");
  _colAttr = _program->attributeLocation("colAttr");

  _program->release();



  initializeOpenGLFunctions();
}

正如我在上面的評論中提到的那樣,我在基於QGLWidget的設置中具有幾乎相同的代碼,並且深度緩沖“開箱即用”。 我猜QGLWidget(或QGLFormat?)默認情況下必須打開深度緩沖區,而QWindow(或QSurfaceFormat?)卻沒有。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM