panel 使用QtOpenGL绘制
luyued 发布于 2011-03-25 20:44 浏览 N 次
#include "glwidget.h"
#define BUFFER_OFFSET(bytes) ((GLubyte*)NULL+(bytes))
#define VERTICES 0
#define INDICES 1
#define NUM_BUFFERS 2
GLuint buffers[NUM_BUFFERS];
GLfloat vertices[][3]={
{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},
{1.0,-1.0,1.0},{-1.0,-1.0,1.0},
{-1.0,1.0,-1.0},{1.0,1.0,-1.0},
{1.0,1.0,1.0},{-1.0,1.0,1.0}
};
GLubyte indices[][4]={
{0,1,2,3},{4,5,6,7},
{0,1,5,4},{3,2,6,7},
{1,2,6,5},{0,4,7,3}
};
GLWidget::GLWidget(QWidget *parent /* =0 */)
:QGLWidget(parent)
{
fLookValue=15.0;
}
void GLWidget::drawX(GLfloat z)
{
glBegin(GL_LINES);
glVertex3f(-20.0,0.0,z);
glVertex3f(20.0,0.0,z);
glEnd();
}
void GLWidget::drawY()
{
glBegin(GL_LINES);
glVertex3f(0.0,20.0,0.0);
glVertex3f(0.0,-20.0,0.0);
glEnd();
}
void GLWidget::drawZ(GLfloat x)
{
glBegin(GL_LINES);
glVertex3f(x,0.0,20.0);
glVertex3f(x,0.0,-20.0);
glEnd();
}
void GLWidget::draw()
{
//glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,BUFFER_OFFSET(0));
gluLookAt(fLookValue,fLookValue,fLookValue,0.0,0.0,0.0,0.0,1.0,0.0);
glColor3f(0.5,0.5,0.5);
for (int i=-20;i<=20;i++)
{
drawX(i);
drawZ(i);
}
glColor3f(1.0,0.0,0.0);
drawX(0.0);
glColor3f(0.0,1.0,0.0);
drawY();
glColor3f(0.0,0.0,1.0);
drawZ(0.0);
glColor3f(0.0,0.0,1.0);
glScalef(1.5,2.0,1.0);
glutWireCube(1.0);
glScalef(0.8,0.5,0.8);
glTranslatef(-6.0,-5.0,0.0);
glutWireDodecahedron();
glTranslatef(8.6,8.6,2.0);
glutWireTetrahedron();
glTranslatef(-3.0,-1.0,0.0);
glutWireOctahedron();
glScalef(0.8,0.8,1.0);
glTranslatef(4.3,-2.0,0.5);
glutWireIcosahedron();
glColor3f(0.0,0.0,0.0);
glTranslatef(5.0,5.0,5.0);
glutSolidTeapot(1.0);
glFlush();
}
void GLWidget::initializeGL()
{
glClearColor(1.0,1.0,1.0,0.0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//glGenBuffers(NUM_BUFFERS,buffers);
//glBindBuffer(GL_ARRAY_BUFFER,buffers[VERTICES]);
//glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
//glVertexPointer(3,GL_FLOAT,0,BUFFER_OFFSET(0));
//glEnableClientState(GL_VERTEX_ARRAY);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,buffers[INDICES]);
//glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW);
/*const GLubyte * name = glGetString(GL_VENDOR);
const GLubyte * biaoshifu = glGetString(GL_RENDERER);
const GLubyte * openglVersion = glGetString(GL_VERSION);
const GLubyte * gluVersion = glGetString(GLU_VERSION);
QString glName = QString("opengl 厂商的名字:%1").arg((const char *)name);
QString glBiaoshi=QString("渲染器标识符:%1").arg((const char *)biaoshifu);
QString glVersion = QString("opengl 版本:%1").arg((const char *)openglVersion);
QString gluV = QString("glu version:%1").arg((const char *)gluVersion);
QMessageBox::information(this,"",glName+glBiaoshi+glVersion+gluV);*/
}
void GLWidget::resizeGL(int w,int h)
{
if (h==0)
{
h=1;
}
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(GLfloat)w/h,0.1,1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(4.0,4.0,4.0,0.0,0.0,0.0,0.0,1.0,0.0);
draw();
}
void GLWidget::wheelEvent(QWheelEvent *ev)
{
int newDelta = ev->delta();
//QString str=QString("delta:%1 last delta: %2").arg(newDelta).arg(fLastDelta);
//QMessageBox::information(this,str,str);
if (newDelta>0)
{
fLookValue-=2.0;
}else if(newDelta<0)
{
fLookValue+=2.0;
}
update();
}
#define BUFFER_OFFSET(bytes) ((GLubyte*)NULL+(bytes))
#define VERTICES 0
#define INDICES 1
#define NUM_BUFFERS 2
GLuint buffers[NUM_BUFFERS];
GLfloat vertices[][3]={
{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},
{1.0,-1.0,1.0},{-1.0,-1.0,1.0},
{-1.0,1.0,-1.0},{1.0,1.0,-1.0},
{1.0,1.0,1.0},{-1.0,1.0,1.0}
};
GLubyte indices[][4]={
{0,1,2,3},{4,5,6,7},
{0,1,5,4},{3,2,6,7},
{1,2,6,5},{0,4,7,3}
};
GLWidget::GLWidget(QWidget *parent /* =0 */)
:QGLWidget(parent)
{
fLookValue=15.0;
}
void GLWidget::drawX(GLfloat z)
{
glBegin(GL_LINES);
glVertex3f(-20.0,0.0,z);
glVertex3f(20.0,0.0,z);
glEnd();
}
void GLWidget::drawY()
{
glBegin(GL_LINES);
glVertex3f(0.0,20.0,0.0);
glVertex3f(0.0,-20.0,0.0);
glEnd();
}
void GLWidget::drawZ(GLfloat x)
{
glBegin(GL_LINES);
glVertex3f(x,0.0,20.0);
glVertex3f(x,0.0,-20.0);
glEnd();
}
void GLWidget::draw()
{
//glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,BUFFER_OFFSET(0));
gluLookAt(fLookValue,fLookValue,fLookValue,0.0,0.0,0.0,0.0,1.0,0.0);
glColor3f(0.5,0.5,0.5);
for (int i=-20;i<=20;i++)
{
drawX(i);
drawZ(i);
}
glColor3f(1.0,0.0,0.0);
drawX(0.0);
glColor3f(0.0,1.0,0.0);
drawY();
glColor3f(0.0,0.0,1.0);
drawZ(0.0);
glColor3f(0.0,0.0,1.0);
glScalef(1.5,2.0,1.0);
glutWireCube(1.0);
glScalef(0.8,0.5,0.8);
glTranslatef(-6.0,-5.0,0.0);
glutWireDodecahedron();
glTranslatef(8.6,8.6,2.0);
glutWireTetrahedron();
glTranslatef(-3.0,-1.0,0.0);
glutWireOctahedron();
glScalef(0.8,0.8,1.0);
glTranslatef(4.3,-2.0,0.5);
glutWireIcosahedron();
glColor3f(0.0,0.0,0.0);
glTranslatef(5.0,5.0,5.0);
glutSolidTeapot(1.0);
glFlush();
}
void GLWidget::initializeGL()
{
glClearColor(1.0,1.0,1.0,0.0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//glGenBuffers(NUM_BUFFERS,buffers);
//glBindBuffer(GL_ARRAY_BUFFER,buffers[VERTICES]);
//glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
//glVertexPointer(3,GL_FLOAT,0,BUFFER_OFFSET(0));
//glEnableClientState(GL_VERTEX_ARRAY);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,buffers[INDICES]);
//glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW);
/*const GLubyte * name = glGetString(GL_VENDOR);
const GLubyte * biaoshifu = glGetString(GL_RENDERER);
const GLubyte * openglVersion = glGetString(GL_VERSION);
const GLubyte * gluVersion = glGetString(GLU_VERSION);
QString glName = QString("opengl 厂商的名字:%1").arg((const char *)name);
QString glBiaoshi=QString("渲染器标识符:%1").arg((const char *)biaoshifu);
QString glVersion = QString("opengl 版本:%1").arg((const char *)openglVersion);
QString gluV = QString("glu version:%1").arg((const char *)gluVersion);
QMessageBox::information(this,"",glName+glBiaoshi+glVersion+gluV);*/
}
void GLWidget::resizeGL(int w,int h)
{
if (h==0)
{
h=1;
}
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(GLfloat)w/h,0.1,1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(4.0,4.0,4.0,0.0,0.0,0.0,0.0,1.0,0.0);
draw();
}
void GLWidget::wheelEvent(QWheelEvent *ev)
{
int newDelta = ev->delta();
//QString str=QString("delta:%1 last delta: %2").arg(newDelta).arg(fLastDelta);
//QMessageBox::information(this,str,str);
if (newDelta>0)
{
fLookValue-=2.0;
}else if(newDelta<0)
{
fLookValue+=2.0;
}
update();
}
相关资讯
- 07-01· 禁教唐诗算术能还幼儿快
- 07-01· 2011年06月17日
- 07-01· 唐诗宋词英译:李商隐 筹
- 07-01· 仿评《唐诗1000首》第186首
- 07-01· 没事干的时候背背唐诗吧
- 07-01· [转载]唐诗中“斜”字该读
- 07-01· 湖南醴陵瓷业转型升级
- 07-01· 奇瑞风云2两厢黑色|2010款
- 07-01· 摩根士丹利华鑫摩根士丹
- 07-01· 摩根士丹利华鑫近期优选
图文资讯
最新资讯
- 07-01· 中金投行部大摩出售中金
- 07-01· 摩根士丹利招聘6月2日【实
- 07-01· 营养防病圣典
- 07-01· 《博伽梵歌原意》之第十
- 07-01· [不错]斑斓圣典---减肥中常
- 07-01· 武乐圣典《太极武当》:武
- 07-01· 铁血英雄-现阶段战功牌兑
- 07-01· 2011年06月10日【原创】南歌
- 07-01· 【淘宝网信息】- 2010年的
- 07-01· 深圳品牌女装有哪些?