在Qt5

时间:2017-01-24 02:17:32

标签: c++ opengl qt5

我正在尝试使用可编程管道在Qt 5.7.0上使用Qt Creator 4.0.2实现QOpenGLWidget类,但我无法显示基本渲染。我以前能够使用带有glBegin / End的固定功能管道进行渲染,但可编程管道不能正常工作。这段代码的目标是在我提升到DrawingWidget类的QOpenGLWidget中显示一个非阴影三角形(我还没有开发着色器文件)。使用下面的代码,我在Qt中呈现以下输出: OpenGLWidget Output

如果我添加,glClearColor(1.0,0.0,0.0,0.0);我得到一个红色窗口,因此上下文和提升的类窗口小部件似乎正在工作,只是不渲染任何给定的顶点。

project1.pro

QT       += core gui opengl

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = untitled1
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    drawingwidget.cpp

HEADERS  += mainwindow.h \
    drawingwidget.h

FORMS    += mainwindow.ui

DrawingWidget.h

#ifndef DRAWINGWIDGET_H
#define DRAWINGWIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLExtraFunctions>

class DrawingWidget : public QOpenGLWidget, public QOpenGLExtraFunctions
{

Q_OBJECT

public:
    DrawingWidget(QWidget *parent);

protected:
    void initializeGL();
    void paintGL();

private:
    GLuint m_vao;

private slots:


};

#endif // DRAWINGWIDGET_H

drawingWidget.cpp

#include "drawingwidget.h"
#include <QOpenGLWidget>
#include <QOpenGLExtraFunctions>

DrawingWidget::DrawingWidget(QWidget *parent)
    : QOpenGLWidget(parent)
{

}

void DrawingWidget::initializeGL(){
    initializeOpenGLFunctions();
    GLfloat verts[] = {0.0f, 0.75f, 0.75f, -0.75f, -0.75f, -0.75f};
    GLuint vertexBufferID;
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glGenBuffers(1, &vertexBufferID);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
    glGenVertexArrays(1, &m_vao);
    glBindVertexArray(m_vao);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
    glBindVertexArray(0);
}

void DrawingWidget::paintGL(){
    glBindVertexArray(m_vao);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glBindVertexArray(0);
}

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>769</width>
    <height>518</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="DrawingWidget" name="openGLWidget">
    <property name="geometry">
     <rect>
      <x>220</x>
      <y>40</y>
      <width>300</width>
      <height>200</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>769</width>
     <height>19</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <customwidgets>
  <customwidget>
   <class>DrawingWidget</class>
   <extends>QOpenGLWidget</extends>
   <header>drawingwidget.h</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

提前感谢您的帮助。

0 个答案:

没有答案