首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法启用OpenGL in QWIndow (Qt5)的深度测试

无法启用OpenGL in QWIndow (Qt5)的深度测试
EN

Stack Overflow用户
提问于 2014-05-04 22:44:06
回答 1查看 1.8K关注 0票数 1

我正在尝试使用OpenGL在QT5.2中编写一个简单的QWindow应用程序。我似乎无法进行深度测试。我已经大大简化了QWindow OpenGL示例:我用彩色顶点画了一个三角形,然后用白色顶点画了一个三角形。白色三角形有较大的Z坐标,因此应该出现在彩色三角形后面。事实并非如此。

我显式地将QWindow的表面格式的深度缓冲区大小设置为24,但是当我检查QWindow::format()函数时,深度缓冲区大小是0。使用QGLFormat,我知道有一个setDepth()函数可以用来打开深度缓冲区,但是QSurfaceFormat中没有类似的函数。

我做错了什么?

我的密码..。

testWindow.cpp:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
######################################################################
# 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-05 03:20:35

问题是:我在调用SphereWindow构造函数之后设置了表面格式,但是创建了一个QOpenGLContext并在构造函数中设置了它的格式。结果是上下文没有得到深度缓冲区。我已经将对QOpenGLContext::setFormat()QOpenGLContext::create()的调用转移到了我的SphereWindow::initialize()函数中,深度缓冲现在正在为我工作。

代码语言:javascript
复制
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?)不会的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23462420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档