QOpenGLShaderProgram

目录

  • 前言
  • 一 .类型
  • 二、使用代码

前言

可以使用QOpenGLShader QOpenGLShaderProgram来代替复杂的gl原生函数

一 .类型

类型 说明
QOpenGLShader shader 可以代表vertex shader/fragment shader
QOpenGLShaderProgram program

二、使用代码

  1. 编译代码
 	QOpenGLShader* vertex = new QOpenGLShader(QOpenGLShader::Vertex);
    vertex->compileSourceCode(vertexShaderSource) ;

    QOpenGLShader* fragment = new QOpenGLShader(QOpenGLShader::Fragment);
    fragment->compileSourceCode(fragmentShaderSource) ;
  1. program 添加并连接
	shaderProgram.addShader(vertex);
    shaderProgram.addShader(fragment);

    if(! shaderProgram.link())
    {
    	shaderProgram.log();
    }
  1. 使用program bind
	glClearColor(0.2f,0.2f,0.3f,1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    shaderProgram.bind();
    glBindVertexArray(VAO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);

    glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);

三、 全代码:

#include "zlopenglwidget.h"

#include "qdebug.h"
#include "QOpenGLShader"

float vertices[] = {
    //右上三角形
    0.5f,0.5f,0.0f, //右上
    0.5f,-0.5f,0.0f,//右下
    -0.5f,-0.5f,0.0f,//左下
    -0.5f,0.5f,0.0f//左上
    }; //只有四个点了

unsigned int indices[]=
{
    0,1,3, //右上三角形 右上 - 右下 - 左上
    1,2,3 //左下三角形  右下 - 左下-左上
};

const char *vertexShaderSource =
   "#version 330 core \n"
   "layout(location=0) in vec3 aPos;\n"
   "void main()\n"
   "{\n"
   "gl_Position = vec4   (aPos.x,aPos.y,aPos.z,1.0f);\n"
   "}\n\0";


const char *fragmentShaderSource =
   "#version 330 core \n"
   "out vec4 FragColor;\n"
   "void main()\n"
   "{\n"
   "    FragColor = vec4 (1.0f,0.5f,0.2f,1.0f);\n"
   "}\n\0";




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


}

ZLOpenGLWidget::~ZLOpenGLWidget()
{
    glDeleteVertexArrays(1,&VAO);
    glDeleteBuffers(1,&VBO);
    glDeleteBuffers(1,&EBO);
    //glDeleteProgram();
}



void ZLOpenGLWidget::initializeGL()
{
    initializeOpenGLFunctions();

    //产生Gen
    glGenVertexArrays(1,&VAO);
    glGenBuffers(1,&VBO);

    //绑定bind
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER,VBO);

    //设置
    glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void*)0);
    glEnableVertexAttribArray(0);
    //解绑
   glBindVertexArray(0);
   glBindBuffer(GL_ARRAY_BUFFER,0);


    //EBO

    glGenBuffers(1,&EBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);

 
    QOpenGLShader* vertex = new QOpenGLShader(QOpenGLShader::Vertex);
    vertex->compileSourceCode(vertexShaderSource) ;

    QOpenGLShader* fragment = new QOpenGLShader(QOpenGLShader::Fragment);
    fragment->compileSourceCode(fragmentShaderSource) ;


    shaderProgram.addShader(vertex);
    shaderProgram.addShader(fragment);


    if(! shaderProgram.link())
    {
        qDebug()<<"Error"<<shaderProgram.log();
    }



}

void ZLOpenGLWidget::resizeGL(int w, int h)
{
    Q_UNUSED(w);
    Q_UNUSED(h);


  //  glDeleteProgram(shaderProgram);

}

void ZLOpenGLWidget::paintGL()
{
    glClearColor(0.2f,0.2f,0.3f,1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    shaderProgram.bind();
    glBindVertexArray(VAO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);

    glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
}

你可能感兴趣的:(openGL+QT,qt,opengl)