当前位置:首页 > 芯闻号 > 充电吧
[导读]我们添加了一个三角形和一个四边形。也许你认为这很简单,但你已经迈出了一大步,要知道任何在OpenGL中绘制的模型都会被分解为这两种简单的图形。lesson1.h#ifndef LESSON1_H #d

我们添加了一个三角形和一个四边形。也许你认为这很简单,但你已经迈出了一大步,要知道任何在OpenGL中绘制的模型都会被分解为这两种简单的图形。

lesson1.h

#ifndef LESSON1_H
#define LESSON1_H


#include#includeclass QPainter;
class QOpenGLContext;
class QOpenGLPaintDevice;

class Lesson1 : public QWindow, QOpenGLFunctions_1_0
{
    Q_OBJECT
public:
    explicit Lesson1(QWindow *parent = 0);
    ~Lesson1();

    virtual void render(QPainter *);
    virtual void render();
    virtual void initialize();

public slots:
    void renderNow();

protected:
    void exposeEvent(QExposeEvent *);
    void resizeEvent(QResizeEvent *);

private:
    void myPerspective( GLdouble fov, GLdouble aspectRatio, GLdouble zNear, GLdouble zFar );

private:
    QOpenGLContext *m_context;

};

#endif // LESSON1_H

lesson1.cpp

#include "lesson1.h"

#include#include#include#includeLesson1::Lesson1(QWindow *parent) :
    QWindow(parent)
  , m_context(0)
{
    setSurfaceType(QWindow::OpenGLSurface);
}

Lesson1::~Lesson1()
{

}

void Lesson1::render(QPainter *painter)
{
    Q_UNUSED(painter);
}

void Lesson1::myPerspective( GLdouble fov, GLdouble aspectRatio, GLdouble zNear, GLdouble zFar )
{
    // 使用glu库函数,需要添加glu.h头文件
    // gluPerspective(fov, aspectRatio, zNear, zFar);
    GLdouble rFov = fov * 3.14159265 / 180.0;
    glFrustum( -zNear * tan( rFov / 2.0 ) * aspectRatio,
               zNear * tan( rFov / 2.0 ) * aspectRatio,
               -zNear * tan( rFov / 2.0 ),
               zNear * tan( rFov / 2.0 ),
               zNear, zFar );
}

void Lesson1::render()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glViewport(0,0,(GLint)width(),(GLint)height()); // 重置当前视口
    glMatrixMode(GL_PROJECTION);                    // 选择投影矩阵
    glLoadIdentity();                               // 重置投影矩阵为单位矩阵
    // glu库函数Qt不支持,但是glu库函数是对gl库函数的封装,方便使用。因此我们可以自己
    // 写一个类似gluPerspective的函数myPerspective,用于设置透视。
    //gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
    myPerspective(45.0,(GLfloat)width()/(GLfloat)height(),0.1,100.0);

    glMatrixMode(GL_MODELVIEW);// 选择模型视图矩阵
    glLoadIdentity();          // 重置模型视图矩阵为单位矩阵

    glTranslatef(-1.5f,0.0f,-6.0f);
    glBegin(GL_TRIANGLES);			// 绘制三角形
    glVertex3f( 0.0f, 1.0f, 0.0f);	// 上顶点
    glVertex3f(-1.0f,-1.0f, 0.0f);	// 左下
    glVertex3f( 1.0f,-1.0f, 0.0f);	// 右下
    glEnd();                        // 三角形绘制结束
    glTranslatef(3.0f,0.0f,0.0f);
    glBegin(GL_QUADS);				// 绘制正方形
    glVertex3f(-1.0f, 1.0f, 0.0f);	// 左上
    glVertex3f( 1.0f, 1.0f, 0.0f);	// 右上
    glVertex3f( 1.0f,-1.0f, 0.0f);	// 左下
    glVertex3f(-1.0f,-1.0f, 0.0f);	// 右下
    glEnd();					    // 正方形绘制结束
}

void Lesson1::initialize()
{
    glShadeModel(GL_SMOOTH);              // 启用平滑着色
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // 黑色背景
    glClearDepth(1.0f);                   // 设置深度缓存
    glEnable(GL_DEPTH_TEST);              // 启用深度测试
    glDepthFunc(GL_LEQUAL);               // 深度测试类型
    // 接着告诉OpenGL我们希望进行最好的透视修正。这会十分轻微的影响性能。但使得透视图看起来好一点。
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void Lesson1::renderNow()
{
    if (!isExposed())
        return;

    bool needsInitialize = false;

    if (!m_context) {
        m_context = new QOpenGLContext(this);
        m_context->setFormat(requestedFormat());
        m_context->create();

        needsInitialize = true;
    }

    m_context->makeCurrent(this);

    if (needsInitialize) {
        initializeOpenGLFunctions();
        initialize();
    }

    render();

    m_context->swapBuffers(this);
}

void Lesson1::exposeEvent(QExposeEvent *event)
{
    Q_UNUSED(event);

    if (isExposed())
        renderNow();
}

void Lesson1::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);

    if (isExposed())
        renderNow();
}

main.cpp

#include#includeint main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QSurfaceFormat format;
    format.setSamples(16);

    Lesson1 window;
    window.setFormat(format);
    window.resize(640, 480);
    window.show();

    return app.exec();
}

运行效果


源码中用到的OpenGL库函数可以参考:

OpenGL之glViewPort函数的用法

OpenGL之glLoadIdentity函数的用法

OpenGL之glMatrixMode函数的用法

实际上仅用如下代码就能画出一个三角形

void Lesson1::render()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glViewport(0,0,(GLint)width(),(GLint)height()); // 重置当前视口
    glBegin(GL_TRIANGLES);			// 绘制三角形
    glVertex3f( 0.0f, 1.0f, 0.0f);	// 上顶点
    glVertex3f(-1.0f,-1.0f, 0.0f);	// 左下
    glVertex3f( 1.0f,-1.0f, 0.0f);	// 右下
    glEnd();                        // 三角形绘制结束
}


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

  PowerVR GPU系列现可提供从单簇到六簇内核的多种组合   全球移动通信大会,西班牙巴塞罗那 —— 2013 年 2 月 25 日 —&md

关键字: 内核 GPU ip powervr g6100 opengl es3.0

长期以来,Android一直为开发人员提供免费的用户界面,以补充背景图片的多样化创建,甚至在多年前,甚至还支持在背景图片中使用openGL。 但是,旧的移动设备无法很好地扩展背景图像,因此移动实时背景图像的开发主要由“出...

关键字: Android opengl 壁纸

函数原型:     void glLoadIdentity(void)函数说明:      OpenGL为我们提供了一个非常简单的恢复初始坐标系的手段,那就是调用glLoadIdentity()命令。

关键字: opengl glloadidentity

pro文件QT -=gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = lesson1 TEMPLATE = app

关键字: opengl QT

在上节课的内容上作些扩展,我们现在开始生成真正的3D对象,而不是象前两节课中那样3D世界中的2D对象。我们给三角形增加一个左侧面,一个右侧面,一个后侧面来生成一个金字塔(四棱锥)。给正方形增加左、右、

关键字: nehe opengl

坐标系统想要弄懂几何变换,一定要搞清楚OpenGL中的坐标系统。从我们构造模型的局部坐标系(Local/Object Space)经过一系列处理最终渲染到屏幕坐标(Screen Space)下,这过程

关键字: opengl 坐标系

被用户诟病20年后,NVIDIA终于做出让步,在SIGGRAPH开幕活动中,NVIDIA发布Studio Driver: SIGGRAPH Edition驱动程序(v431.70),正式为所有GeFo

关键字: NVIDIA opengl

在这一课里,将学会如何将纹理映射到立方体的六个面。学习texture map纹理映射(贴图)有很多好处。比方说您想让一颗导弹飞过屏幕。根据前几课的知识,我们最可行的办法可能是很多个多边形来构建导弹的轮

关键字: nehe opengl

当前光栅位置:    当前光栅位置就是开始绘制下一幅位图/图像的屏幕位置。  //左下角glRasterPos2f(GLfloat x, GLfloat y);glRasterPos3f(GLfloa

关键字: opengl

这一课将把如下图片做成一个飘动的旗帜,其实主要还是用到了纹理映射。lesson10.h#ifndef LESSON10_H #define LESSON10_H #include#include#i

关键字: opengl QT
关闭
关闭