环境:OpenGL(QT5.6.0)+VS2015
参考 http://blog.csdn.net/WAmani/article/details/52701211
问题:
1. 把生成opengl图像的程序单独拎出来封装后,如何添加进已有的QGLWidget窗口中 :
在引用的教程中给了如下代码:
openGL OPENGL;//实例化
//将opengl的窗口放到QT界面中
//通过容器将opengl渲染的图像添加到ui界面中
QHBoxLayout container;
//container.SetFixedSize;
container.addWidget(&OPENGL);
ui.openGLWidget->setLayout(&container);
但我改的这个封装的类无法在主线程中实例化【求教】,所以直接在QT Design界面中,将组件提升到已经写好的类中。右键选择提升为,输入类名,提升。再运行就可以看到写在opengl.cpp中的图像已经显示在窗口中了。
代码:
#include "demo.h"
#include "opengl.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DEMO w;
//openGL OPENGL;//实例化
w.show();
return a.exec();
}
#ifndef DEMO_H
#define DEMO_H
#include
#include "ui_demo.h"
#include "opengl.h"
class DEMO : public QWidget
{
Q_OBJECT
public:
DEMO(QWidget *parent = 0);
~DEMO();
private slots:
void setParams();
private:
Ui::DEMOClass ui;
};
#endif // DEMO_H
#include "demo.h"
int xAngle; int yAngle; int zAngle;
DEMO::DEMO(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
//openGL OPENGL;//实例化
将opengl的窗口放到QT界面中
通过容器将opengl渲染的图像添加到ui界面中
//QHBoxLayout container;
//container.addWidget(&OPENGL);
//ui.openGLWidget->setLayout(&container);
}
DEMO::~DEMO()
{
}
void DEMO::setParams()
{
xAngle = ui.horizontalSlider->value();
yAngle = ui.horizontalSlider_2->value();
zAngle = ui.horizontalSlider_3->value();
}#ifndef OPENGL_H
#define OPENGL_H
#include
#include
#include
class openGL : public QGLWidget { Q_OBJECT public: openGL(QWidget *parent); ~openGL(); QTimer clk; float m_x, m_y, m_z; GLuint textur; protected: void initializeGL(); void initWidget(); void paintGL(); void resizeGL(int width, int height); void loadGLTextures(); private slots: void updateWindow(); private: }; #endif // OPENGL_H #include "opengl.h" extern int xAngle; extern int yAngle; extern int zAngle; openGL::openGL(QWidget *parent) : QGLWidget(parent) { initWidget(); initializeGL(); clk.start(30); QObject::connect(&clk, SIGNAL(timeout()), this, SLOT(updateWindow())); } openGL::~openGL() { } void openGL::initializeGL() { loadGLTextures(); glEnable(GL_TEXTURE_2D); 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 openGL::initWidget() { setGeometry(0, 200, 640, 480); setWindowTitle(tr("opengl demo")); } void openGL::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0.0, 0.0, -6.0);//沿坐标轴移动 glRotatef(m_x, 1.0, 0.0, 0.0);//旋转 glRotatef(m_y, 0.0, 1.0, 0.0); glRotatef(m_z, 0.0, 0.0, 1.0); //绑定纹理特性 glBindTexture(GL_TEXTURE_2D, textur); glBegin(GL_QUADS); glNormal3f(0.0, 0.0, 1.0); glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0); glTexCoord2f(1.0, 0.0); glVertex3f(1.0, -1.0, 1.0); glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 1.0); glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0); glNormal3f(0.0, 0.0, -1.0); glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0); glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, -1.0); glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, -1.0); glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, -1.0); glNormal3f(0.0, 1.0, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0); glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 1.0, 1.0); glTexCoord2f(1.0, 0.0); glVertex3f(1.0, 1.0, 1.0); glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, -1.0); glNormal3f(0.0, -1.0, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0); glTexCoord2f(0.0, 1.0); glVertex3f(1.0, -1.0, -1.0); glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 1.0); glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0); glNormal3f(1.0, 0.0, 0.0); glTexCoord2f(1.0, 0.0); glVertex3f(1.0, -1.0, -1.0); glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, -1.0); glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 1.0); glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 1.0); glNormal3f(-1.0, 0.0, 0.0); glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0); glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0); glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, 1.0); glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0); glEnd(); } void openGL::resizeGL(int width, int height) { if (0 == height) { height = 1; } glViewport(0, 0, (GLint)width, (GLint)height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //gluPerspective(45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0); GLdouble aspectRatio = (GLfloat)width / (GLfloat)height; GLdouble zNear = 0.1; GLdouble zFar = 100.0; GLdouble rFov = 50.0 * 3.14159265 / 180.0; glFrustum(-zNear * tan(rFov / 2.0) * aspectRatio, zNear * tan(rFov / 2.0) * aspectRatio, -zNear * tan(rFov / 2.0), zNear * tan(rFov / 2.0), zNear, zFar); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void openGL::updateWindow() { //m_x += 1; //m_y += 2; //m_z += 3; //updateGL();//刷新界面 //将ui控制信号赋值给转动角度 m_x = xAngle; m_y = yAngle; m_z = zAngle; //paintGL(); updateGL();//刷新界面 } void openGL::loadGLTextures() { QImage tex; QImage buf; if (!buf.load("F://AR//VS//demo//leimu.jpg")) { qWarning("load image failed!"); QImage dummy(128, 128, QImage::Format_RGB32); dummy.fill(Qt::white); buf = dummy; } tex = QGLWidget::convertToGLFormat(buf); glGenTextures(1, &textur); glBindTexture(GL_TEXTURE_2D, textur); glTexImage2D(GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits()); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); }