#ifndef MAINWIDGET_H#define MAINWIDGET_H#include <QWidget>#include <QOpenGLWidget>#include <QOpenGLFunctions>#include <QString>#include <QOpenGLShader>#include <QOpenGLShaderProgram>#include <QOpenGLBuffer>#include <QVector>class MainWidget : public QOpenGLWidget, protected QOpenGLFunctions{ Q_OBJECTpublic: MainWidget(QWidget *parent = 0); ~MainWidget();protected: void initializeGL(); void resizeGL(int width, int height); void paintGL();private: QOpenGLShaderProgram program; void initShaders(); void initData();//data GLuint vbo_position; GLuint attribute_vpos;};#endif // MAINWIDGET_H
#include "mainwidget.h"#include <QMessageBox>MainWidget::MainWidget(QWidget *parent) : QOpenGLWidget(parent){}MainWidget::~MainWidget(){}void MainWidget::initData(){ QVector3D vectdata[] = {QVector3D(-1.0f, -1.0f, 0.0f), QVector3D( 1.0f, -1.0f, 0.0f), QVector3D( 1.0f, 1.0f, 0.0f), QVector3D(-1.0f, 1.0f, 0.0f)}; glGenBuffers(1, &vbo_position); glBindBuffer(GL_ARRAY_BUFFER, vbo_position); glBufferData(GL_ARRAY_BUFFER, sizeof(vectdata), vectdata, GL_STATIC_DRAW); glVertexAttribPointer(attribute_vpos, 3, GL_FLOAT, false, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);}void MainWidget::initializeGL(){ initializeOpenGLFunctions(); glClearColor(0, 0, 0, 1); initShaders();}void MainWidget::initShaders(){ // Compile vertex shader if (!program.addShaderFromSourceFile((QOpenGLShader::ShaderType)QOpenGLShader::Vertex, ":/vshader.vert")) close(); // Compile fragment shader if (!program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fshader.frag")) close(); initData(); // Link shader pipeline if (!program.link()) close(); // Bind shader pipeline for use if (!program.bind()) close();}void MainWidget::resizeGL(int width, int height){ //glViewport(0, 0, width, height);}void MainWidget::paintGL(){ glClear(GL_COLOR_BUFFER_BIT); update();}
#version 430out vec4 FragColor;in vec3 glPosition;void main ( void ){ FragColor = vec4 ( abs(glPosition.xy), 0, 1.0);}
#version 430in vec3 vPosition;out vec3 glPosition;void main (void){ gl_Position = vec4(vPosition, 1.0); glPosition = vPosition;}
void MainWidget::resizeGL(int width, int height){ glViewport(0, 0, width, height);}void MainWidget::paintGL(){ /* glClear(GL_COLOR_BUFFER_BIT); update(); */}