-
UID:146765
-
- 注册时间2013-10-20
- 最后登录2023-03-10
- 在线时间11小时
-
- 发帖3
- 搜Ta的帖子
- 精华0
- 金钱30
- 威望13
- 贡献值0
- 好评度3
-
访问TA的空间加好友用道具
|
我看到一篇文章 http://blog.qt.digia.com/blog/2014/11/20/qt-weekly-20-completing-the-offering-qopenglwindow-and-qrasterwindow/?utm_source=tuicool 里面有例子。我按照例子做了,但是得不到正确结果。代码贴出来。 - class Render : public QOpenGLWindow
- {
- public:
- Render();
- ~Render();
- protected:
- void initializeGL();
- void paintGL();
- void resizeGL(int w ,int h);
- QOpenGLFunctions * f;
- QOpenGLBuffer * triangle;
- QOpenGLVertexArrayObject * vao;
- QOpenGLShaderProgram * program;
- QMatrix4x4 mv,p;
- QTimer * time;
- float rota;
- //QPainter * painter;
- };
- GLfloat tri[] =
- {
- 0.0f,1.0f,-1.0f,1.0f,
- 1.0f,1.0f,-1.0f,1.0f,
- 1.0f,0.0f,-1.0f,1.0f,
- };
- Render::Render()
- {
- QSurfaceFormat format;
- format.setDepthBufferSize(24);
- format.setStencilBufferSize(8);
- format.setVersion(3,3);
- format.setProfile(QSurfaceFormat::CoreProfile);
- setFormat(format);
- time = new QTimer;
- connect(time,SIGNAL(timeout()),this,SLOT(update()));
- time->start(50);
- rota = 0;
- }
- Render::~Render()
- {
- }
- void Render::initializeGL()
- {
- // f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
- // f->initializeOpenGLFunctions();
- f = context()->functions();
- program = new QOpenGLShaderProgram;
- program->addShaderFromSourceCode(QOpenGLShader::Vertex,
- "#version 330 core \n\
- layout(location = 0) in vec4 vertex;\
- uniform mat4 mvp;\
- void main() \
- {\
- gl_Position = mvp * vertex;\
- }");
- program->addShaderFromSourceCode(QOpenGLShader::Fragment,
- "#version 330 core \n\
- out vec4 fragColor;\
- void main() \
- { \
- fragColor = vec4(0.0f,1.0f,0.0f,1.0f);\
- }");
- program->link();
- vao = new QOpenGLVertexArrayObject;
- vao->create();
- vao->bind();
- triangle = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
- triangle->create();
- triangle->bind();
- triangle->setUsagePattern(QOpenGLBuffer::StaticDraw);
- triangle->allocate(tri,sizeof(tri));
- program->enableAttributeArray(0);
- program->setAttributeBuffer(0,GL_FLOAT,0,4,0);
- f->glEnable(GL_DEPTH_TEST);
- }
- void Render::resizeGL(int w ,int h)
- {
- p.setToIdentity();
- p.perspective(35.0f,float(w)/float(h),1.0f,30.0f);
- }
- void Render::paintGL()
- {
- program->bind();
- f->glClearColor(0.5,0.5f,0.5f,1.0f);
- f->glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- f->glViewport(0,0,width(),height());
- mv.setToIdentity();
- mv.lookAt(QVector3D(0.0f,0.0f,5.0f),QVector3D(0.0f,0.0f,0.0f),QVector3D(0.0f,1.0f,0.0f));
- mv.rotate(0.5+rota,0,1,0);
- program->setUniformValue("mvp",p*mv);
- vao->bind();
- f->glDrawArrays(GL_TRIANGLES,0,3);
- rota=rota+1.5;
- rota=rota>360.0?0:rota;
- QPainter pp(this);
- pp.drawText(10,20,"dsdsds");
- update();
- }
|