整理 qt opengl,自己的基础框架 —— 绘制一个彩色三角形

网上很多都是QGLWidget例子,而cube,虽然说明文档上说再简单不过了,但是对于一个0基础的来说,还是像看天书一样。opengl的例子还有一个hellgl2,看着比较简单,但是那个log生成的代码使用了QVector3D显得有点复杂。

      所以决定看QOpenGLWidget Class文档和QGLWidget的例子来自己写。

    类文档中又说

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Code Examples  
  2.   
  3. To get started, the simplest QOpenGLWidget subclass could like like the following:  
  4.   
  5. class MyGLWidget : public QOpenGLWidget  
  6. {  
  7. public:  
  8.     MyGLWidget(QWidget *parent) : QOpenGLWidget(parent) { }  
  9.   
  10. protected:  
  11.     void initializeGL()  
  12.     {  
  13.         // Set up the rendering context, load shaders and other resources, etc.:  
  14.         QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();  
  15.         f->glClearColor(1.0f, 1.0f, 1.0f, 1.0f);  
  16.         ...  
  17.     }  
  18.   
  19.     void resizeGL(int w, int h)  
  20.     {  
  21.         // Update projection matrix and other size related settings:  
  22.         m_projection.setToIdentity();  
  23.         m_projection.perspective(45.0f, w / float(h), 0.01f, 100.0f);  
  24.         ...  
  25.     }  
  26.   
  27.     void paintGL()  
  28.     {  
  29.         // Draw the scene:  
  30.         QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();  
  31.         f->glClear(GL_COLOR_BUFFER_BIT);  
  32.         ...  
  33.     }  
  34.   
  35. };  
  36. Alternatively, the prefixing of each and every OpenGL call can be avoided by deriving from QOpenGLFunctions instead:  
  37.   
  38. class MyGLWidget : public QOpenGLWidget, protected QOpenGLFunctions  
  39. {  
  40.     ...  
  41.     void initializeGL()  
  42.     {  
  43.         initializeOpenGLFunctions();  
  44.         glClearColor(...);  
  45.         ...  
  46.     }  
  47.     ...  
  48. };  
  49. To get a context compatible with a given OpenGL version or profile, or to request depth and stencil buffers, call setFormat():  
  50.   
  51. QOpenGLWidget *widget = new QOpenGLWidget(parent);  
  52. QSurfaceFormat format;  
  53. format.setDepthBufferSize(24);  
  54. format.setStencilBufferSize(8);  
  55. format.setVersion(3, 2);  
  56. format.setProfile(QSurfaceFormat::CoreProfile);  
  57. widget->setFormat(format); // must be called before the widget or its parent window gets shown  
  58. With OpenGL 3.0+ contexts, when portability is not important, the versioned QOpenGLFunctions variants give easy access to all the modern OpenGL functions available in a given version:  
  59.   
  60.     ...  
  61.     void paintGL()  
  62.     {  
  63.         QOpenGLFunctions_3_2_Core *f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_2_Core>();  
  64.         ...  
  65.         f->glDrawArraysInstanced(...);  
  66.         ...  
  67.     }  
  68.     ...  
  69. As described above, it is simpler and more robust to set the requested format globally so that it applies to all windows and contexts during the lifetime of the application. Below is an example of this:  
  70.   
  71. int main(int argc, char **argv)  
  72. {  
  73.     QApplication app(argc, argv);  
  74.   
  75.     QSurfaceFormat format;  
  76.     format.setDepthBufferSize(24);  
  77.     format.setStencilBufferSize(8);  
  78.     format.setVersion(3, 2);  
  79.     format.setProfile(QSurfaceFormat::CoreProfile);  
  80.     QSurfaceFormat::setDefaultFormat(format);  
  81.   
  82.     MyWidget widget;  
  83.     widget.show();  
  84.   
  85.     return app.exec();  
  86. }  
      

      所以第一个程序,由上面得来。

main.cpp

#include "myglwidget.h"
 
#include <QApplication>
#include <QSurfaceFormat>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    a.setApplicationName("My First openGL Widget");
   
 
    QSurfaceFormat format;
    format.setDepthBufferSize(24);
    format.setStencilBufferSize(8);
    format.setVersion(3, 2);
    format.setProfile(QSurfaceFormat::CoreProfile);
    QSurfaceFormat::setDefaultFormat(format);
 
    MyGLWidget w;
    w.show();
 
    return a.exec();
}

myglwidget.h

#ifndef MYGLWIDGET_H
#define MYGLWIDGET_H
 
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
 
class MyGLWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT
 
public:
    MyGLWidget(QWidget *parent = 0);
    ~MyGLWidget();
 
protected:
    void initializeGL();
};
 
#endif // MYGLWIDGET_H

myglwidget.cpp

#include "myglwidget.h"
 
MyGLWidget::MyGLWidget(QWidget *parent)
    : QOpenGLWidget(parent)
{
}
 
MyGLWidget::~MyGLWidget()
{
 
}
 
void MyGLWidget::initializeGL()
{
    initializeOpenGLFunctions();
    glClearColor(0, 255, 0, 1);
}
 


      RGBA,所以初始化涂成绿色。

      

 



from: http://blog.csdn.net/fu851523125/article/details/51169534



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值