前言一、数据二、 shader三、初始化四、全部代码
前言本文是根据AXB的视频完成了。链接: 视频
要完成的内容:1.“绘制矩形” 2.“绘制三角形”。使用同一个VAO,但使用不同的EBO
- vertices是一个矩形的四个点右上-右下-左下-左上indicesRect (EBO)的矩形的indexindicesTri (EBO)的三角形的index
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 indicesRect[]= { 0,1,3, //右上三角形 右上 - 右下 - 左上 1,2,3 //左下三角形 右下 - 左下-左上 }; unsigned int indicesTri[]= { 0,1,3 //右上三角形 右上 - 右下 - 左上 };二、 shader
- vertexShaderSource = 顶点渲染器fragmentShaderSource = 片段渲染器
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"; 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";三、初始化
glGen函数
速记:产生对象是不需要指定类型,仅需要说明个数
glGenBuffers(2,EBO);
绑定对象VBO EBO等,才需要类型
a.绑定时需要指定类型
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO[0]);
传递数据
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indicesTri),indicesTri,GL_STATIC_DRAW);
QOpenGLShader
QOpenGLShader::compileSourceCode(vertexShaderSource
初始化代码
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(2,EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO[0]); glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indicesRect),indicesRect,GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indicesTri),indicesTri,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"<四、全部代码 #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 indicesRect[]= { 0,1,3, //右上三角形 右上 - 右下 - 左上 1,2,3 //左下三角形 右下 - 左下-左上 }; unsigned int indicesTri[]= { 0,1,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"; 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"; ZLOpenGLWidget::ZLOpenGLWidget(QWidget *parent) :QOpenGLWidget(parent) { } ZLOpenGLWidget::~ZLOpenGLWidget() { glDeleteVertexArrays(1,&VAO); glDeleteBuffers(1,&VBO); glDeleteBuffers(2,EBO); //glDeleteProgram(); } void ZLOpenGLWidget::draw(int type) { auto f_clear=[&]() { glClearColor(0.2f,0.2f,0.3f,1.0f); glClear(GL_COLOR_BUFFER_BIT); }; auto f_Rect=[&]() { glClearColor(0.2f,0.2f,0.3f,1.0f); glClear(GL_COLOR_BUFFER_BIT); shaderProgram.bind(); glBindVertexArray(VAO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO[0]); glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0); }; auto f_Tri=[&]() { f_clear(); shaderProgram.bind(); glBindVertexArray(VAO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO[1]); glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_INT,0); }; makeCurrent(); switch(type) { case eClear: f_clear(); break; case eRect: f_Rect(); break; case eTri: f_Tri(); break; } doneCurrent(); //update(); } 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(2,EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO[0]); glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indicesRect),indicesRect,GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indicesTri),indicesTri,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"<欢迎分享,转载请注明来源:内存溢出
评论列表(0条)