//mainwindow.h
#ifndef NEHEWIDGET_H
#define NEHEWIDGET_H
#include <QtOpenGL>
class NeHeWidget : public QGLWidget
{
Q_OBJECT
public:
NeHeWidget( QWidget* parent = 0, const char* name = 0, bool fs = false );
~NeHeWidget();
protected:
void initializeGL();
void paintGL();
void resizeGL( int width, int height );
void keyPressEvent(QKeyEvent * event);
protected:
bool fullscreen;
private:
GLuint list;
};
#endif//NEHEWIDGET_H
//main.cpp
#include <qapplication.h>
#include <qmessagebox.h>
#include "mainwindow.h"
int main( int argc, char **argv )
{
bool fs = false;
QApplication a(argc,argv);
NeHeWidget w( 0, 0, fs );
//a.setMainWidget( &w );
w.show();
return a.exec();
}
//mainwindow.h
#include "mainwindow.h"
NeHeWidget::NeHeWidget( QWidget* parent, const char* name, bool fs )
: QGLWidget( QGLFormat(QGL::SampleBuffers), parent )
{
showMaximized();
}
void NeHeWidget::initializeGL()
{
glShadeModel( GL_SMOOTH );
// 这一行启用smooth shading(阴影平滑)。阴影平滑通过多边形精细的混合色彩,并对外部光进行平滑。我将在另一个教程中更详细的解释阴影平滑。
glClearColor(0.0,0.0,0.0,0);
glClearDepth( 1.0 );
//设置深度缓存。
glEnable( GL_DEPTH_TEST );
//启用深度测试。
glDepthFunc( GL_LEQUAL );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
}
void NeHeWidget::paintGL()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
//清楚屏幕和深度缓存。
glLoadIdentity();
glBegin( GL_QUADS);
//开始绘制四边形。
//glColor3f(1.0,0.0,0.0);
glVertex3f( -1.0, 1.0, 0.0 );
//左上顶点。
glVertex3f( 1.0, 1.0, 0.0 );
//右上顶点。
//glColor3f(0.0,0.0,1.0);
glVertex3f( 1.0,-1.0, 0.0 );
//右下顶点。
glVertex3f( -1.0, -1.0, 0.0 );
//左下顶点。
glEnd();
glBegin(GL_LINES);
// glColor3f(0,1,0);
glVertex3f(0,0,0);
glVertex3f(2,2,0);
glEnd();
}
void NeHeWidget::resizeGL( int width, int height )
{
// updateGL();
glViewport(0,0,width,height);
}
void NeHeWidget::keyPressEvent( QKeyEvent *event )
{
if(event->key()==Qt::Key_Escape)
{
this->close();
}
}
NeHeWidget::~NeHeWidget()
{
}