QT QGLWidget下opengl绘制的内容显示不了

lcysonya 2016-10-30 09:46:18
照着别人的例子做的,仍然不能显示,请大神们帮帮忙
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();
}




...全文
543 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
glTranslatef(-1.5f, 0.0f, 0.0f); 调整一下z的值。

16,201

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧