参考 http://blog.csdn.net/myths_0/article/details/24431597
用glut绘制一个茶壶
一句话,继承QGLWidget,实现下面三个函数,用子类定义窗口就行了。
三个函数:
void initializeGL(); void paintGL(); void resizeGL(int width, int height);
下面是我的QGLWidget的子类的源码。
GLWidget.h
#pragma once
#include "qgl.h"
#include <gl/GLU.h>
#include <OpenGL/GLUT.H>
class GLWidget : public QGLWidget{
Q_OBJECT
public:
GLWidget(QWidget *parent = nullptr) :QGLWidget(parent){};
~GLWidget();
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
void keyPressEvent(QKeyEvent *e);
};
GLWidget.cpp
#include "GLWidget.h"
GLWidget::~GLWidget(){}
void GLWidget::initializeGL(){
glShadeModel(GL_SMOOTH);
glClearColor(0.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 GLWidget::paintGL(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glScalef(0.1,0.1,0.1);
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
glutWireTeapot(2);
}
void GLWidget::resizeGL(int width, int height){
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 GLWidget::keyPressEvent(QKeyEvent *e){
}