Qt_OpenGL:3D贴不同纹理小测
2014-12-10 00:24
288 查看
Qt_OpenGL:3D贴不同纹理小测
//.h
//.cpp
//main.cpp
//运行结果截图:
//.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(); }
//运行结果截图:
相关文章推荐
- OpenGL_Qt学习笔记之_05(纹理映射)
- qt5.6.0 opengl —— 纹理贴图
- OpenGL学习 将不同纹理映射到不同面上
- Qt_OpenGL:光照纹理滤波色彩混合小测
- OpenGL_Qt学习笔记之_06(纹理滤波、光照和色彩融合)
- 【Qt OpenGL】Qt Creator中的3D绘图及动画教程
- Qt OpenGL----纹理贴图
- OpenGL3D图形、旋转、纹理、键盘移动、光照、滤波、透明(完整)
- Qt OpenGL:学习现代3D图形编程之四,透视投影
- OpenGL3D图形、旋转、纹理、键盘移动、光照、滤波、透明(完整) 转自http://www.cnblogs.com/tiandsp/archive/2012/01/23/2329049.html
- Qt_OpenGL:3D旋转自定义换色
- 现代3D图形编程学习--opengl使用不同的缓存对象(译者添加)
- Qt下的OpenGL 编程(9)3D视图变换、旋转的地球
- Qt-OpenGL中的多纹理贴图
- Qt下的OpenGL 编程(9)3D视图变换、旋转的地球
- Qt openGL 使用QImage 生成二维纹理
- NeHe_OpenGL_Qt4 圆柱纹理映射
- QT与OpenGL之3D图形绘制
- Qt下的OpenGL 编程(9)3D视图变换、旋转的地球
- OpenGL+Qt界面(四) 纹理贴图