QT OpenGL 配置问题解决

直接去官网下载 Qt 5.2.0 for Windows 64-bit (VS 2012, OpenGL, 589 MB)

安装后,建立工程,选择Qt Widgets Application, 点击选择然后输入名字,完成.

更改widget.h 文件为:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QGLWidget>
#include <QKeyEvent>
#include <QTimerEvent>

namespace Ui {
class Widget;
}

class Widget : public QGLWidget
{
    Q_OBJECT

public:
    explicit Widget(QGLWidget *parent = 0, const char* name = 0, bool fs = false);
    ~Widget();
    void timerEvent(QTimerEvent *e);

protected:
    void initializeGL();
    void resizeGL(int w, int h);
    void paintGL();
    void keyPressEvent(QKeyEvent *e);
  //  void keyReleaseEvent(QKeyEvent *e);
    void loadTextures();

protected:
    bool bfullscreen;
    GLuint texId[1];

private:
    Ui::Widget *ui;
    bool light;
    bool blend;
};

#endif // WIDGET_H

Widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <GL/glu.h>

GLfloat light_ambient[4] = {0.5, 0.5, 0.5, 1.0};
GLfloat light_diffuse[4] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_position[4] = {0, 0, 2, 0};

Widget::Widget(QGLWidget *parent, const char *name, bool fs) :
    QGLWidget(parent),
    ui(new Ui::Widget)
{
    light = false;
    blend = false;
    bfullscreen = fs;
    setGeometry(0, 0, 800, 600);
    setWindowTitle("openGL Framework");
    if (fs)
        showFullScreen();
    startTimer(33);
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::initializeGL()
{
    loadTextures();
    glEnable(GL_TEXTURE_2D);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_SMOOTH);
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
    glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
    glLightfv(GL_LIGHT1, GL_POSITION, light_position);
    glEnable(GL_LIGHT1);

    glColor4f(1.0, 1.0, 1.0, 0.5);
    glBlendFunc(GL_SRC0_ALPHA, GL_ONE);

}

void Widget::resizeGL(int w, int h)
{
    if (h == 0)
        h = 1;
    glViewport(0, 0, (GLint)w, (GLint)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1, 100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void Widget::paintGL()
{
    static GLfloat rTri = 0, rQuad = 0;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    glRotatef(rQuad, -0.2f, 0.2f, -0.3f);
    glBindTexture(GL_TEXTURE_2D, texId[0]);
    glBegin(GL_QUADS);
    //top
    glNormal3f(0, 1, 0);
    glTexCoord2f(0.0, 1.0);
    glVertex3f(-0.3f, 0.3f, -0.3f);
    glTexCoord2f(0.0, 0.0);
    glVertex3f(-0.3f, 0.3f, 0.3f);
    glTexCoord2f(1.0, 0.0);
    glVertex3f(0.3f, 0.3f, 0.3f);
    glTexCoord2f(1.0, 1.0);
    glVertex3f(0.3f, 0.3f, -0.3f);
    //bottom
    glNormal3f(0, -1, 0);
    glTexCoord2f(0.0, 1.0);
    glVertex3f(-0.3f, -0.3f, -0.3f);
    glTexCoord2f(0.0, 0.0);
    glVertex3f(-0.3f, -0.3f, 0.3f);
    glTexCoord2f(1.0, 0.0);
    glVertex3f(0.3f, -0.3f, 0.3f);
    glTexCoord2f(1.0, 1.0);
    glVertex3f(0.3f, -0.3f, -0.3f);
    //front
    glNormal3f(0, 0, 1);
    glTexCoord2f(0.0, 1.0);
    glVertex3f(-0.3f, 0.3f, 0.3f);
    glTexCoord2f(0.0, 0.0);
    glVertex3f(-0.3f, -0.3f, 0.3f);
    glTexCoord2f(1.0, 0.0);
    glVertex3f(0.3f, -0.3f, 0.3f);
    glTexCoord2f(1.0, 1.0);
    glVertex3f(0.3f, 0.3f, 0.3f);
    //right
    glNormal3f(1, 0, 0);
    glTexCoord2f(0.0, 1.0);
    glVertex3f(0.3f, 0.3f, 0.3f);
    glTexCoord2f(0.0, 0.0);
    glVertex3f(0.3f, -0.3f, 0.3f);
    glTexCoord2f(1.0, 0.0);
    glVertex3f(0.3f, -0.3f, -0.3f);
    glTexCoord2f(1.0, 1.0);
    glVertex3f(0.3f, 0.3f, -0.3f);
    //back
    glNormal3f(0, 0, -1);
    glTexCoord2f(0.0, 1.0);
    glVertex3f(-0.3f, 0.3f, -0.3f);
    glTexCoord2f(0.0, 0.0);
    glVertex3f(0.3f, 0.3f, -0.3f);
    glTexCoord2f(1.0, 0.0);
    glVertex3f(0.3f, -0.3f, -0.3f);
    glTexCoord2f(1.0, 1.0);
    glVertex3f(-0.3f, -0.3f, -0.3f);
    //left
    glNormal3f(-1, 0, 0);
    glTexCoord2f(0.0, 1.0);
    glVertex3f(-0.3f, 0.3f, -0.3f);
    glTexCoord2f(0.0, 0.0);
    glVertex3f(-0.3f, -0.3f, -0.3f);
    glTexCoord2f(1.0, 0.0);
    glVertex3f(-0.3f, -0.3f, 0.3f);
    glTexCoord2f(1.0, 1.0);
    glVertex3f(-0.3f, 0.3f, 0.3f);

    glEnd();


    rTri += 2;
    rQuad -= 3;
}

void Widget::keyPressEvent(QKeyEvent *e)
{
    switch (e->key()) {
    case Qt::Key_F2:
        bfullscreen = !bfullscreen;
        if (bfullscreen)
            showFullScreen();
        else
        {
            showNormal();
            setGeometry(0, 0, 640, 480);
        }
        updateGL();
        break;
    case Qt::Key_L:
        light = !light;
        if (light)
            glEnable(GL_LIGHTING);
        else
            glDisable(GL_LIGHTING);
        updateGL();
        break;
    case Qt::Key_B:

        blend = !blend;
        if (blend)
        {
            glEnable(GL_BLEND);
            glDisable(GL_DEPTH_TEST);
        }
        else
        {
            glDisable(GL_BLEND);
            glEnable(GL_DEPTH_TEST);
        }
        break;
case Qt::Key_Escape: close(); default: break; }}void Widget::timerEvent(QTimerEvent *e){ updateGL();}void Widget::loadTextures(){ QImage tex, buf; if (!buf.load("1.jpg")) { qWarning("Can't open the image."); QImage dumy(128, 128, QImage::Format_RGB32); dumy.fill(Qt::green); buf = dumy; } tex = convertToGLFormat(buf); glGenTextures(1, &texId[0]); glBindTexture(GL_TEXTURE_2D, texId[0]); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, tex.width(), tex.height(), GL_RGBA, GL_UNSIGNED_BYTE, tex.bits()); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);}

main.cpp

#include "widget.h"
#include <QApplication>
#include <qmessagebox.h>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    bool fs = false;
    switch (QMessageBox::information(0, "Start FullScreen?", "Info",
                                     QMessageBox::Yes,
                                     QMessageBox::No | QMessageBox::Default))
    {
    case QMessageBox::Yes:
        fs = true;
        break;
    }


    Widget w(0, 0, fs);
    a.setActiveWindow(&w);
    w.show();

    return a.exec();
}




编译会提示什么什么无法解析这个那个的,很多错误,或者No such Directory 等等,

我找到一种解决方法: 如果是Debug编译,则找到工程目录build-testQt-Desktop_Qt_5_2_0_MSVC2012_OpenGL_64bit-Debug

里面有个这个文件 Makefile.Debug, 用记事本打开, 找到INCPATH  在等号后面加入 

-I"..\..\QT\5.2.0\msvc2012_64_opengl\include\QtOpenGL"  

找到 LIBS 在最后面加入  D:\QT\5.2.0\msvc2012_64_opengl\lib\Qt5OpenGLd.lib 前面为安装路径 主要天剑Qt5OpenGLd.lib 后面的d表示Debug, 如果为Release 则不用加d

保存后在编译就可以通过了.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值