QT QGLWidget下opengl绘制的内容显示不了
照着别人的例子做的,仍然不能显示,请大神们帮帮忙
main.cpp
#include "mainwindow.h"
//#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
mainWindow w;
w.show();
return a.exec();
}
////////////////////////////////////////////////////////////////////////////////////
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QKeyEvent>
#include "nehewidget.h"
#include <QtWidgets/QMainWindow>
#include "ui_mainwindow.h"
class mainWindow : public QMainWindow
{
Q_OBJECT
public:
mainWindow(QWidget *parent = 0);
~mainWindow();
protected:
bool fullscreen;
//处理键盘事件
void keyPressEvent(QKeyEvent *e);
private:
NeHeWidget *neheWidget;
Ui::mainWindowClass ui;
};
#endif // MAINWINDOW_H
////////////////////////////////////////////////////////////////////////////////
#include "mainwindow.h"
mainWindow::mainWindow(QWidget *parent)
: QMainWindow(parent)
{
//ui.setupUi(this);
neheWidget = new NeHeWidget();
fullscreen = true;
setGeometry(100, 100, 1000, 768);
setWindowTitle(tr("NeHe's OpenGL Framework"));
setCentralWidget(neheWidget);
}
mainWindow::~mainWindow()
{
}
void mainWindow::keyPressEvent(QKeyEvent *e)
{
switch (e->key())
{
case Qt::Key_F2:
fullscreen = !fullscreen;
if (fullscreen)
{
showFullScreen();
}
else
{
showNormal();
}
neheWidget->updateGL();
break;
case Qt::Key_Escape:
close();
break;
}
}
/////////////////////////////////////////////////////////////////////////////////
#ifndef NEHEWIDGET_H
#define NEHEWIDGET_H
#include <QtWidgets/QApplication>
#include <QGLWidget>
#include <QtGui>
#include <QtOpenGL>
class NeHeWidget : public QGLWidget
{
Q_OBJECT
public:
explicit NeHeWidget(QWidget *parent = 0);
~NeHeWidget();
protected:
//设置渲染环境
void initializeGL();
//绘制窗口
void paintGL();
//响应窗口的大小变化
void resizeGL(int width, int height);
};
#endif // NEHEWIDGET_H
///////////////////////////////////////////////////////////////////////////////////////////////////
#include "nehewidget.h"
#include <GL/glut.h>
NeHeWidget::NeHeWidget(QWidget *parent) :
QGLWidget(parent)
{
}
NeHeWidget::~NeHeWidget()
{}
void NeHeWidget::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 NeHeWidget::paintGL()
{
// 清除屏幕和深度缓存
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//坐标转移
glTranslatef(-1.5f, 0.0f, 0.0f);
//设置颜色
glColor3f(1.0, 1.0, 1.0);
//绘制一个正方形
glBegin(GL_QUADS);
glVertex3f(-1.0, 1.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);
glEnd();
}
// 重置OpenGL窗口大小
void NeHeWidget::resizeGL(int width, int height)
{
// 防止窗口大小变为0
if (height == 0)
{
height = 1;
}
// 重置当前的视口
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();
}
main.cpp
#include "mainwindow.h"
//#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
mainWindow w;
w.show();
return a.exec();
}
////////////////////////////////////////////////////////////////////////////////////
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QKeyEvent>
#include "nehewidget.h"
#include <QtWidgets/QMainWindow>
#include "ui_mainwindow.h"
class mainWindow : public QMainWindow
{
Q_OBJECT
public:
mainWindow(QWidget *parent = 0);
~mainWindow();
protected:
bool fullscreen;
//处理键盘事件
void keyPressEvent(QKeyEvent *e);
private:
NeHeWidget *neheWidget;
Ui::mainWindowClass ui;
};
#endif // MAINWINDOW_H
////////////////////////////////////////////////////////////////////////////////
#include "mainwindow.h"
mainWindow::mainWindow(QWidget *parent)
: QMainWindow(parent)
{
//ui.setupUi(this);
neheWidget = new NeHeWidget();
fullscreen = true;
setGeometry(100, 100, 1000, 768);
setWindowTitle(tr("NeHe's OpenGL Framework"));
setCentralWidget(neheWidget);
}
mainWindow::~mainWindow()
{
}
void mainWindow::keyPressEvent(QKeyEvent *e)
{
switch (e->key())
{
case Qt::Key_F2:
fullscreen = !fullscreen;
if (fullscreen)
{
showFullScreen();
}
else
{
showNormal();
}
neheWidget->updateGL();
break;
case Qt::Key_Escape:
close();
break;
}
}
/////////////////////////////////////////////////////////////////////////////////
#ifndef NEHEWIDGET_H
#define NEHEWIDGET_H
#include <QtWidgets/QApplication>
#include <QGLWidget>
#include <QtGui>
#include <QtOpenGL>
class NeHeWidget : public QGLWidget
{
Q_OBJECT
public:
explicit NeHeWidget(QWidget *parent = 0);
~NeHeWidget();
protected:
//设置渲染环境
void initializeGL();
//绘制窗口
void paintGL();
//响应窗口的大小变化
void resizeGL(int width, int height);
};
#endif // NEHEWIDGET_H
///////////////////////////////////////////////////////////////////////////////////////////////////
#include "nehewidget.h"
#include <GL/glut.h>
NeHeWidget::NeHeWidget(QWidget *parent) :
QGLWidget(parent)
{
}
NeHeWidget::~NeHeWidget()
{}
void NeHeWidget::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 NeHeWidget::paintGL()
{
// 清除屏幕和深度缓存
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//坐标转移
glTranslatef(-1.5f, 0.0f, 0.0f);
//设置颜色
glColor3f(1.0, 1.0, 1.0);
//绘制一个正方形
glBegin(GL_QUADS);
glVertex3f(-1.0, 1.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);
glEnd();
}
// 重置OpenGL窗口大小
void NeHeWidget::resizeGL(int width, int height)
{
// 防止窗口大小变为0
if (height == 0)
{
height = 1;
}
// 重置当前的视口
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();
}
...全文
相关推荐
QT下openGL三维图形绘制的光照模型实现
实现了QT下OpenGL的光源照射编程,模拟现实中的光源实现三维图形的绘制,在Vs2017+QT插件平台下运行
QT OpenGL绘制三维图形(立方体、圆柱体、圆锥、球体、圆环等等)
本文使用QGLWidget来绘制各种三维基本图形,包括立方体、圆柱体、圆锥、球体、圆环等等,涉及包括基本绘制以及上色、纹理、旋转等操作。 使用的软件版本:QT5.12 + QT Creater4.8.0 GLWidget.h 12345678910111213141516171819202122232425262728293031323334353637383940...
qt+opengl 实现多窗口三维图形变换
实现多窗口三维图形的变换。左侧窗口为立方体的变换,右侧窗口为三角锥的变换。
qt下的openGL纹理图像的绘制
利用QT下的OpenGL库,实现了三维立体图形的绘制,并利用纹理贴图美化了显示效果,软件在VS2017+Qt控件下运行
Qt OpenGL 教程
Qt 开发过程中,如何调用GPU加速界面响应,一直是开发难点,从这个点切入,详细介绍了Qt开发中如何使用OPENGL,混合开发,C++语言,易懂,推荐大家一起学习。


