您的位置:首页 > 服装鞋帽 > 女装 > panel 使用QtOpenGL绘制

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();
}
图文资讯
广告赞助商