Qt_OpenGL:3D贴不同纹理小测

68 篇文章 24 订阅
23 篇文章 0 订阅

Qt_OpenGL:3D贴不同纹理小测


//.h

#ifndef TEXTURETEST_H
#define TEXTURETEST_H

#include <QMainWindow>
#include <QtGui>
#include <QtOpenGL/QtOpenGL>

class TextureTest : public QGLWidget
{
    Q_OBJECT

public:
    TextureTest(QWidget *parent = 0);
    ~TextureTest();

    void createMask();
    void loadTexture(GLuint *addrTextureID, QString picturePaths);

protected:
    void synthesizeTexture();
    void initializeGL();
    void paintGL();
    void resizeGL(int width, int height);
    void wheelEvent(QWheelEvent *);
    void mousePressEvent(QMouseEvent*);
    void mouseMoveEvent(QMouseEvent*);

private:
    GLfloat xRot, yRot, zRot;
    GLuint texture[10];
    QPoint lastPos;
    GLfloat zoomFactor;
    GLubyte image[128][128][3];
};

#endif // TEXTURETEST_H

//.cpp

#include "texturetest.h"
#include <QString>
#include <QStringList>
#include <QTextStream>
#include <iostream>
#include <math.h>

TextureTest::TextureTest(QWidget *parent)
    : QGLWidget(parent)
{
    xRot = 0.0;
    yRot = 0.0;
    zRot = 0.0;
    zoomFactor = 1.0;
    setGeometry(100,100,600,600);
    setWindowTitle(" my Texture test");
}

void TextureTest::createMask(){

    int i, j, c;
    for(i = 0; i< 128; ++i){
        for(j = 0; j<128; ++j){
            c = ((((i&0x08)==0)^((j&0x08))==0))*255;
            image[i][j][0]= (GLubyte) c;
            image[i][j][1]= (GLubyte) c;
            image[i][j][2]= (GLubyte) c;
        }
    }
}

void TextureTest::initializeGL(){

    glShadeModel(GL_SMOOTH);
    glClearColor(1.0,0.5,0.8,1.0);
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    loadTexture(&texture[0],":/b.jpg :/c.jpg :/d.jpg");
}

void TextureTest::resizeGL(int width, int height){

    if(height == 0)
        height = 1;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if(width <= height){
        glOrtho(-10.0,10.0,-10.0*(GLfloat)(height)/(GLfloat)(width),
                10.0*(GLfloat)(height)/(GLfloat)(width),-100.0,100.0);
    }else{
        glOrtho(-10.0*(GLfloat)(width)/(GLfloat)(height),
                10.0*(GLfloat)(width)/(GLfloat)(height),-10.0,10.0,-100.0,100.0);
    }
    glViewport(0, 0, width, height);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void TextureTest::paintGL(){

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glRotatef(xRot, 1, 0, 0);
    glRotatef(yRot, 0, 1, 0);
    glRotatef(zRot, 0, 0, 1);

    glScalef(zoomFactor, zoomFactor, zoomFactor);

    synthesizeTexture();

}

void TextureTest::synthesizeTexture(){

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glBegin(GL_QUADS);  //底面
    glTexCoord2f(0.0,0.0); glVertex3f(-1,0,1);
    glTexCoord2f(1.0,0.0); glVertex3f(1,0,1);
    glTexCoord2f(1.0,1.0); glVertex3f(1,0,-1);
    glTexCoord2f(0.0,1.0); glVertex3f(-1,0,-1);
    glEnd();

    glBindTexture(GL_TEXTURE_2D, texture[1]);
    glBegin(GL_QUADS);      //前面
    glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0);
    glTexCoord2f(1.0, 0.0); glVertex3f(1.0, -1.0, 1.0);
    glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 1.0);
    glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0);
    glEnd();

    glBindTexture(GL_TEXTURE_2D, texture[2]);
    glBegin(GL_QUADS);    //右面
    glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 1.0);
    glTexCoord2f(2.0, 0.0); glVertex3f(1.0, -1.0, -1.0);
    glTexCoord2f(2.0, 2.0); glVertex3f(1.0, 1.0, -1.0);
    glTexCoord2f(0.0, 2.0); glVertex3f(1.0, 1.0, 1.0);
    glEnd();

}

void TextureTest::wheelEvent(QWheelEvent *event){

    GLdouble numDegree = event->delta()/120.0;
    zoomFactor *= pow(1.25, numDegree);
    if(zoomFactor <= 0.1){
        zoomFactor = 0.1;
    }
    updateGL();
}

void TextureTest::mousePressEvent(QMouseEvent *event){

    lastPos = event->pos();
}

void TextureTest::mouseMoveEvent(QMouseEvent *event){

    GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
    GLfloat dy = GLfloat(event->y() - lastPos.y()) / width();
    if(event->buttons() & Qt::LeftButton){
        xRot += 180 * dy;
        zRot -= 180 * dx;
        updateGL();
    }else if( event->buttons() & Qt::RightButton){
        xRot += 180 * dy;
        yRot += 180 *dx;
        updateGL();
    }
    lastPos = event->pos();
}

void TextureTest::loadTexture(GLuint *addrTextureID, QString picturePaths){

    picturePaths = picturePaths.simplified();
    QStringList strList = picturePaths.split(' ');

    int textureCounter = 0;
    int numTextures = strList.size();
    glGenTextures(numTextures, addrTextureID);

    QTextStream paths(&picturePaths);
    QString aPath;

    while(!paths.atEnd()){
        paths >> aPath;
        std::cout << aPath.toStdString() << std::endl;

        QImage tex, buf;
        if(!buf.load(aPath)){
            qWarning("Couldn't open image file...");
            QImage dummy(128, 128, QImage::Format_RGB32);
            dummy.fill(12);
            buf = dummy;
        }
        tex = QGLWidget::convertToGLFormat(buf);

        glBindTexture(GL_TEXTURE_2D, addrTextureID[textureCounter]);
        glTexImage2D(GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(),
                     0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        textureCounter += 1;
    }
}

TextureTest::~TextureTest()
{

}

//main.cpp

#include "texturetest.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TextureTest w;
    w.show();

    return a.exec();
}

//运行结果截图:





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值