opengl中最基础的应该就是绘制箱子了,这篇文章就当作记录用了。
直接看代码:
#ifndef MYGLWGT_H
#define MYGLWGT_H
#include <QWidget>
#include <QOpenGLWidget>
#include <QGLWidget>
#include <QKeyEvent>
class myglWgt : public QOpenGLWidget
{
Q_OBJECT
public:
explicit myglWgt(QWidget *parent = nullptr);
QImage CreateTextures(QString i_path);
protected:
void initializeGL();
void paintGL();
void resizeGL( int width, int height );
void loadGLTextures();
protected:
void keyPressEvent(QKeyEvent *event);
void wheelEvent(QWheelEvent *event);
protected slots:
void TimerOut();
signals:
private:
GLfloat f_rote=0.0;
GLfloat f_quad=0.0;
GLfloat xRot=0, yRot=0, zRot=0;
GLuint texture[3];
GLfloat lightAmbient[4] = { 0.5, 0.5, 0.5, 1.0 };
GLfloat lightDiffuse[4] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat lightPosition[4] = { 0.0, 0.0, 2.0, 1.0 };
float zoom=0.0;
int i_filter=0;
};
#endif // MYGLWGT_H
.cpp
#include "myglwgt.h"
#include "GL/GL.h"
#include <QTimer>
#include <qdebug.h>
#include <QKeyEvent>
#include <QImage>
#include <GL/GLU.h>
myglWgt::myglWgt(QWidget *parent) : QOpenGLWidget(parent)
{
f_rote=0.0;
f_quad=0.0;
QTimer *timer=new QTimer(this);
timer->start(100);
connect(timer,&QTimer::timeout,this,&myglWgt::TimerOut);
}
QImage myglWgt::CreateTextures(QString i_path)
{
QImage image;
if ( !image.load(i_path) )
{
//qWarning( "Could not read image file, using single-color instead." );
QImage dummy(128,128,QImage::Format_RGB32);
dummy.fill(Qt::green);
image = dummy;
}
return QGLWidget::convertToGLFormat(image);
}
void myglWgt::initializeGL()
{
loadGLTextures();
glEnable( GL_TEXTURE_2D );
glShadeModel( GL_SMOOTH );
glClearColor(0.0,0.0,0.0,1.0);
glClearDepth( 50.0 ); //设置深度缓存
glEnable( GL_DEPTH_TEST ); //启用深度测试
glDepthFunc( GL_LEQUAL ); //所作深度测试类型
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //透视修正
glBlendFunc( GL_SRC_ALPHA, GL_ONE );
glEnable( GL_BLEND );
}
void myglWgt::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //清除屏幕和深度缓存
/*
glLoadIdentity();
glTranslatef(-0.3,0.0,0); //沿着xyz进行移动
glRotated(f_rote,0.0,1.0,0.0);
//绘制三角形
glBegin(GL_TRIANGLES);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f(0.0,0.5,0.0);
glVertex3f(-0.5,-0.5,0.0);
glVertex3f(0.5,-0.5,0.0);
//第2面
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f(0.0,0.5,0.0);
glVertex3f(0.5,-0.5,0.0);
glVertex3f(0.0,0.0,-0.5);
//第三面
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f(0.0,0.5,0.0);
glVertex3f(0.0,0.0,-0.5);
glVertex3f(-0.5,-0.5,0.0);
//第四面
glColor3f( 0.0, 1.0, 1.0 );
glVertex3f(0.0,0.0,-0.5);
glVertex3f(-0.5,-0.5,0.0);
glVertex3f(0.5,-0.5,0.0);
glEnd();
//glRotatef(f_rote,0.0,1.0,0.0);
glLoadIdentity(); //重置观察模型
glTranslatef(0.7,0.0,0.0);
glRotatef(f_quad,1.0,0.0,0.0);
glBegin(GL_QUADS);
//顶面
glVertex3f( 0.3, 0.3, -0.3 );
glVertex3f( -0.3, 0.3, -0.3 );
glVertex3f( -0.3, 0.3, 0.3 );
glVertex3f( 0.3, 0.3, 0.3 );
//底面
glColor3f( 1.0, 0.5, 0.0 );
glVertex3f( 0.3, -0.3, 0.3 );
glVertex3f( -0.3, -0.3, 0.3 );
glVertex3f( -0.3, -0.3, -0.3 );
glVertex3f( 0.3, -0.3, -0.3 );
//正面
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f(0.3,0.3,0.3);
glVertex3f(-0.3,0.3,0.3);
glVertex3f(-0.3,-0.3,0.3);
glVertex3f(0.3,-0.3,0.3);
//背面
glColor3f( 1.0, 0.0, 1.0 );
glVertex3f(0.3,-0.3,-0.3);
glVertex3f(-0.3,-0.3,-0.3);
glVertex3f(-0.3,0.3,-0.3);
glVertex3f(0.3,0.3,-0.3);
//左侧面
glVertex3f(-0.3,0.3,0.3);
glVertex3f(-0.3,0.3,-0.3);
glVertex3f(-0.3,-0.3,-0.3);
glVertex3f(-0.3,-0.3,0.3);
//右侧面
glVertex3f(0.3,0.3,-0.3);
glVertex3f(0.3,0.3,0.3);
glVertex3f(0.3,-0.3,0.3);
glVertex3f(0.3,-0.3,-0.3);
glEnd();
*/
glLoadIdentity();
glTranslatef( 0.0, 0.0, zoom );
glRotatef( xRot, 1.0, 0.0, 0.0 );
glRotatef( yRot, 0.0, 1.0, 0.0 );
glRotatef( zRot, 0.0, 0.0, 1.0 );
glBindTexture( GL_TEXTURE_2D, texture[i_filter] ); //纹理
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 );
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 );
glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 );
glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 );
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( 1.0, 1.0 ); glVertex3f( -1.0, -1.0, -1.0 );
glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, -1.0, -1.0 );
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, 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 );
glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 );
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();
}
void myglWgt::resizeGL(int width, int height)
{
if(height==0)
{
height=1;
}
glViewport( 0, 0, (GLint)width, (GLint)height );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
void myglWgt::loadGLTextures()
{
QImage tex;
tex=CreateTextures("C:/Users/dujiajun/Desktop/3.png");
glGenTextures(1,&texture[0]);
glBindTexture(GL_TEXTURE_2D,texture[0]);
glTexImage2D(GL_TEXTURE_2D,0,3,tex.width(),tex.height(),
0,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);
QImage tex1;
tex1=CreateTextures("C:/Users/dujiajun/Desktop/NeHe.bmp");
glGenTextures(1,&texture[1]);
glBindTexture(GL_TEXTURE_2D,texture[1]);
glTexImage2D(GL_TEXTURE_2D,0,3,tex1.width(),tex1.height(),
0,GL_RGBA,GL_UNSIGNED_BYTE,tex1.bits());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
void myglWgt::keyPressEvent(QKeyEvent *event)
{
if(event->key()==Qt::Key_L)
{
glDisable( GL_LIGHTING );
}
if(event->key()==Qt::Key_K)
{
glEnable( GL_LIGHTING );
}
if(event->key()==Qt::Key_F)
{
i_filter=i_filter+1;
if(i_filter>=2)
{
i_filter=0;
}
}
if(event->key()==Qt::Key_B)
{
glEnable( GL_BLEND );
glDisable( GL_DEPTH_TEST );
}
glFlush();
update();
}
void myglWgt::wheelEvent(QWheelEvent *event)
{
if(event->delta()>0)
{
zoom+=0.5;
}
else
{
zoom-=0.5;
}
glFlush();
update();
}
void myglWgt::TimerOut()
{
f_rote+=10;
f_quad-=10;
xRot += 1;
yRot += 1;
zRot += 1;
glFlush();
update();
}
效果图:
这个是因为做了透明处理,若不需要透明的话,只需要将Init中的
glBlendFunc( GL_SRC_ALPHA, GL_ONE );
glEnable( GL_BLEND );
去掉
由定时函数控制着它进行旋转
最后说一下qt配置opengl的使用吧。在windows下使用opengl,要么你直接从网上下载opengl库。然后进行配置。或者是你下载visual studio。在windowskits中就包含opengl库。
配置步骤
1.在pro文件中加入:QT += opengl
2.在pro文件末尾加入win32:LIBS += -lOpengl32
-lglu32
3.在头文件中加入 #include “GL/GL.h” #include <GL/GLU.h>
这样就是可以进行使用了