#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QGLWidget>
class GLWidget : public QGLWidget
{
public:
GLWidget();
~GLWidget();
void initializeGL();
void paintGL();
void resizeGL(int w, int h);
};
#endif // GLWIDGET_H
glwidget.cpp
#include "glwidget.h"
GLWidget::GLWidget()
: QGLWidget()
{}
GLWidget::~GLWidget()
{}
void GLWidget::initializeGL()
{
qglClearColor( Qt::black );
}
void GLWidget::paintGL()
{
glClear( GL_COLOR_BUFFER_BIT );
glLoadIdentity();
qglColor( Qt::white );
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();
}
void GLWidget::resizeGL(int w, int h)
{
h = h < 1 ? 1 : h;
glViewport( 0, 0, (GLint)w, (GLint)h );
}
main.cpp
#include <QApplication>
#include "glwidget.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
GLWidget w;
w.resize(800, 600);
w.show();
return app.exec();
}
gl.pro
QT += opengl
TARGET = gl
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
glwidget.cpp
HEADERS += glwidget.h
沒有留言:
張貼留言