欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

02——qtopengl画三角形

时间:2023-05-27

qmyopenglwidget.h

#ifndef QMYOPENGLWIDGET_H#define QMYOPENGLWIDGET_H#include #include #include #include class QMyOpenglWidget : public QOpenGLWidget, QOpenGLFunctions_3_3_Core{ Q_OBJECTpublic: explicit QMyOpenglWidget(QWidget* parent = nullptr);protected: virtual void initializeGL(); virtual void resizeGL(int w, int h); virtual void paintGL();public: void cretaeShader();private: unsigned int vao1; QOpenGLShaderProgram program;signals:public slots:};#endif // QMYOPENGLWIDGET_H

qmyopenglwidget.cpp

#include "qmyopenglwidget.h"QMyOpenglWidget::QMyOpenglWidget(QWidget* parent):QOpenGLWidget(parent){}void QMyOpenglWidget::initializeGL(){ initializeOpenGLFunctions(); glGenVertexArrays(1, &vao1); unsigned int vbo1; glGenBuffers(1, &vbo1); glBindVertexArray(vao1); glBindBuffer(GL_ARRAY_BUFFER, vbo1); float vertices[] = { -1.0, 0, 0, 1.0, 0, 0, 0, 1.0f, 0 }; //为当前绑定到target 的缓冲区对象创建一个新的数据存储 // glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); //告知显卡如何解析缓冲区里的属性值 glVertexAttribPointer(0, 3, GL_FLOAT, FALSE, 3*sizeof (GL_FLOAT), nullptr); //开启VAO管理的第一个属性 glEnableVertexAttribArray(0); cretaeShader();}void QMyOpenglWidget::resizeGL(int w, int h){}void QMyOpenglWidget::paintGL(){ glClearColor(0.2f, 0.2f, 0.1f, 0.1f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindVertexArray(vao1); glDrawArrays(GL_TRIANGLES, 0, 3); update();}void QMyOpenglWidget::cretaeShader(){ program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shader/vertex.vert"); program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shader/frag.frag"); program.bind(); program.link();}

vertex.vert

#version 330 corein vec3 pos;out vec3 fcolors;void main(void){ gl_Position = vec4(pos, 1.0f);}

frag.frag

#version 330 coreout vec4 FragColor; //片段着色器输出void main(void){ FragColor = vec4(0.5, 0.8, 0.1, 1.0);}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。