QT筆記 -- (6) opengl
阿新 • • 發佈:2017-06-17
ini ref func demo correct ptr csdn entity eap
參考 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){
}
QT筆記 -- (6) opengl