opengl png图片 qt_QT+OpenGL 描绘简单图形

#include "myopengl.h"#include#includemyopenGL::myopenGL(QWindow*parent)

{

rTri= 0.0;

rQuad= 0.0;

xRot= yRot = zRot = 0.0;

}

myopenGL::~myopenGL()

{

}voidmyopenGL::loadGLTextures()

{

QImage tex, buf;if ( !buf.load( "./msbg_blue.png") )

{

qWarning("Could not read image file, using single-color instead.");

QImage dummy(128, 128, QImage::Format_RGB32 );

dummy.fill( Qt::green );

buf=dummy;

}

tex=QGLWidget::convertToGLFormat( buf );

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 );

}//初始化opengl窗口部件

voidmyopenGL::initializeGL()

{

initializeOpenGLFunctions();

glClearColor(0.0,0.0,0.0,1.0);

glShadeModel(GL_SMOOTH);

glClearDepth(1.0);

glEnable(GL_DEPTH_TEST);

glDepthFunc(GL_LEQUAL);

glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);

}//绘制opengl窗口

voidmyopenGL::paintGL()

{

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

glTranslatef(-1.5f,0.0f,-6.0f);

glRotatef( rTri,0.0, 1.0, 0.0);

glBegin(GL_TRIANGLES);

glColor3f(1.0, 0.0, 0.0);

glVertex3f(0.0, 1.0, 0.0);

glColor3f(0.0, 1.0, 0.0);

glVertex3f(-1.0, -1.0, 1.0);

glColor3f(0.0, 0.0, 1.0);

glVertex3f(1.0, -1.0, 1.0);

glColor3f(1.0, 0.0, 0.0);

glVertex3f(0.0, 1.0, 0.0);

glColor3f(0.0, 0.0, 1.0);

glVertex3f(1.0, -1.0, 1.0);

glColor3f(0.0, 1.0, 0.0);

glVertex3f(1.0, -1.0, -1.0);

glColor3f(1.0, 0.0, 0.0);

glVertex3f(0.0, 1.0, 0.0);

glColor3f(0.0, 1.0, 0.0);

glVertex3f(1.0, -1.0, -1.0);

glColor3f(0.0, 0.0, 1.0);

glVertex3f(-1.0, -1.0, -1.0);

glColor3f(1.0, 0.0, 0.0);

glVertex3f(0.0, 1.0, 0.0);

glColor3f(0.0, 0.0, 1.0);

glVertex3f(-1.0, -1.0, -1.0);

glColor3f(0.0, 1.0, 0.0);

glVertex3f(-1.0, -1.0, 1.0);

glEnd();

glTranslatef(3.0f,0.0f,-7.0f);

glRotatef( rQuad,0.0, 0.0, 1.0);

glRotatef( rTri,0.0, 1.0, 0.0);

glRotatef( rQuad,0.0, 0.0, 1.0);

glBegin(GL_QUADS);

glColor3f(0.0, 1.0, 0.0);

glVertex3f(1.0, 1.0, -1.0);

glVertex3f(-1.0, 1.0, -1.0);

glVertex3f(-1.0, 1.0, 1.0);

glVertex3f(1.0, 1.0, 1.0);

glColor3f(1.0, 0.5, 0.0);

glVertex3f(1.0, -1.0, 1.0);

glVertex3f(-1.0, -1.0, 1.0);

glVertex3f(-1.0, -1.0, -1.0);

glVertex3f(1.0, -1.0, -1.0);

glColor3f(1.0, 0.0, 0.0);

glVertex3f(1.0, 1.0, 1.0);

glVertex3f(-1.0, 1.0, 1.0);

glVertex3f(-1.0, -1.0, 1.0);

glVertex3f(1.0, -1.0, 1.0);

glColor3f(1.0, 1.0, 0.0);

glVertex3f(1.0, -1.0, -1.0);

glVertex3f(-1.0, -1.0, -1.0);

glVertex3f(-1.0, 1.0, -1.0);

glVertex3f(1.0, 1.0, -1.0);

glColor3f(0.0, 0.0, 1.0);

glVertex3f(-1.0, 1.0, 1.0);

glVertex3f(-1.0, 1.0, -1.0);

glVertex3f(-1.0, -1.0, -1.0);

glVertex3f(-1.0, -1.0, 1.0);

glColor3f(1.0, 0.0, 1.0);

glVertex3f(1.0, 1.0, -1.0);

glVertex3f(1.0, 1.0, 1.0);

glVertex3f(1.0, -1.0, 1.0);

glVertex3f(1.0, -1.0, -1.0);

glEnd();

rTri+= 0.2;

rQuad-= 0.15;

}//处理窗口大小

void myopenGL::resizeGL(int width, intheight)

{

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(); //重置模型观察矩阵

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值