完整代码:\nmyopenglwidget.h:\n\n#ifndef MYOPENGLWIDGET_H\n#define MYOPENGLWIDGET_H\n\n#include \u003CQObject>\n#include \u003CQWidget>\n\n#include \u003CQOpenGLWidget>\n#include \u003CQOpenGLFunctions_3_3_Core>\n#include \u003CQOpenGLShaderProgram>\n#include \u003CQOpenGLTexture>\n\nclass MyOpenGLWidget : public QOpenGLWidget,QOpenGLFunctions_3_3_Core\n{\n Q_OBJECT\npublic:\n\n enum Shape{None,Rect,Circle,Triangle};\n\n explicit MyOpenGLWidget(QWidget *parent = nullptr);\n\n void drawShape(Shape shape);\n void clearGraphic();\n void setWireFrame(bool wireFrame);\n\nprotected:\n virtual void initializeGL() override;\n virtual void resizeGL(int w, int h) override;\n virtual void paintGL() override;\n\nsignals:\n\nprivate:\n Shape m_shape;\n\n QOpenGLShaderProgram shaderProgram;\n\n QOpenGLTexture *textureStar;\n};\n\n#endif // MYOPENGLWIDGET_H\n\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\nmyopenglwidget.cpp:\n\n#include \"myopenglwidget.h\"\n\nunsigned int VBO,VAO;\n\n//添加一个索引控制器\nunsigned int EBO;\n\nfloat verticesColorTexture[] = {\t//每一行数据的前三个是位置坐标,后三个是颜色值\n 0.5f,0.5f,0.5f,1.0f,0.0f,0.0f,1.0f,1.0f,\n 0.5f,-0.5f,0.0f,0.0f,1.0f,0.0f,1.0f,0.0f,\n -0.5f,-0.5f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f,\n -0.5f,0.5f,0.0f,0.5f,0.5f,0.5f,0.0f,1.0f\n};\n\n//添加索引数据\nunsigned int indices[]={\n 0,1,3,\n 1,2,3\n};\n\nMyOpenGLWidget::MyOpenGLWidget(QWidget *parent) : QOpenGLWidget(parent)\n{\n\n}\n\nvoid MyOpenGLWidget::drawShape(MyOpenGLWidget::Shape shape)\n{\n makeCurrent();\n m_shape = shape;\n update();\n doneCurrent();\n}\n\nvoid MyOpenGLWidget::clearGraphic()\n{\n makeCurrent();\n drawShape(MyOpenGLWidget::None);\n makeCurrent();\n glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);\n update();\n doneCurrent();\n}\n\nvoid MyOpenGLWidget::setWireFrame(bool wireFrame)\n{\n makeCurrent();\n if(wireFrame){\n glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);\n }else{\n glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);\n }\n update();\n doneCurrent();\n}\n\n\n\nvoid MyOpenGLWidget::initializeGL()\n{\n initializeOpenGLFunctions();\n\n //void glGenVertexArrays(GLsizei n, GLuint *arrays)生成顶点数组对象名称\n // n: 要产生的VAO对象的数量\n // arrays: 存放产生的VAO对象的名称\n glGenVertexArrays(1,&VAO);\n\n // void glGenBuffers(GLsizei n,GLuint *buffers)生成顶点缓冲对象\n // n: 要产生的VBO对象的数量\n // arrays: 存放产生的VBO对象的名称\n glGenBuffers(1,&VBO);\n\n //初始化索引器\n glGenBuffers(1,&EBO);\n glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);\n glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof (indices),indices,GL_STATIC_DRAW);\n\n //绑定VAO和VBO\n glBindVertexArray(VAO);\n glBindBuffer(GL_ARRAY_BUFFER,VBO);\n //在VBO中存入顶点数据\n glBufferData(GL_ARRAY_BUFFER,sizeof (verticesColorTexture),verticesColorTexture,GL_STATIC_DRAW);\n\n //告诉VAO怎么在VBO中拿数据(顶点位置数据)\n glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,8*sizeof (float),(void*)0);\n //开启第一个VAO\n glEnableVertexAttribArray(0);\n //告诉VAO怎么在VBO中拿数据(颜色数据)\n glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,8*sizeof (float),(void*)(3*sizeof(float)));\n //开启第二个VAO\n glEnableVertexAttribArray(1);\n //告诉VAO怎么在VBO中拿数据(纹理数据)\n glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,8*sizeof (float),(void*)(6*sizeof(float)));\n //开启第三个VAO\n glEnableVertexAttribArray(2);\n\n //用完之后解除绑定(信息已经被记录下来了)\n glBindBuffer(GL_ARRAY_BUFFER,0);\n glBindVertexArray(0);\n\n //添加着色器\n shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,\":/shaders/Shaders/shape.vert\");\n shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,\":/shaders/Shaders/shape.frag\");\n shaderProgram.link();\n\n //添加纹理数据\n textureStar = new QOpenGLTexture(QImage(\":/img/images/water.jpg\").mirrored());\n shaderProgram.setUniformValue(\"textureStar\",0);\n}\n\nvoid MyOpenGLWidget::resizeGL(int w, int h)\n{\n Q_UNUSED(w);\n Q_UNUSED(h);\n\n}\n\nvoid MyOpenGLWidget::paintGL()\n{\n glClearColor(0.5f,0.9f,0.4f,1.0f);\n glClear(GL_COLOR_BUFFER_BIT);\n\n shaderProgram.bind();\n textureStar->bind(0);\n //在渲染前只需开启对应的VAO即可\n glBindVertexArray(VAO);\n switch (m_shape) {\n case Rect:\n glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,&indices);\n break;\n default:\n break;
完整代码\nmyopenglwidget
最新推荐文章于 2024-11-02 15:22:06 发布