#pragma once
#include "OpenGL.h"
#include <QtGui/QWindow>
#include <QtGui/QOpenGLFunctions>
QT_BEGIN_NAMESPACE
class QPainter;
class QOpenGLContext;
class QOpenGLPaintDevice;
QT_END_NAMESPACE
//! [1]
class OpenGLWindow : public QWindow, protected QOpenGLFunctions
{
Q_OBJECT
public:
explicit OpenGLWindow(QWindow *parent = 0);
~OpenGLWindow();
virtual void render(QPainter *painter);
virtual void render();
virtual void initialize();
void setAnimating(bool animating);
int showFlag = 0;
static COpenGL openglData;
//static int aa;
public slots:
void renderLater();
void renderNow();
protected:
bool event(QEvent *event) override;
void exposeEvent(QExposeEvent *event) override;
private:
bool m_animating;
QOpenGLContext *m_context;
QOpenGLPaintDevice *m_device;
};
//! [1]
#include <qdebug.h>
#include "openglwindow.h"
#include <QtCore/QCoreApplication>
#include <QtGui/QOpenGLContext>
#include <QtGui/QOpenGLPaintDevice>
#include <QtGui/QPainter>
COpenGL OpenGLWindow::openglData;
OpenGLWindow::OpenGLWindow(QWindow *parent)
: QWindow(parent)
, m_animating(false)
, m_context(0)
, m_device(0) {
setSurfaceType(QWindow::OpenGLSurface);
}
OpenGLWindow::~OpenGLWindow() {
delete m_device;
}
void OpenGLWindow::render(QPainter *painter) {
Q_UNUSED(painter);
}
void OpenGLWindow::initialize() {
}
void OpenGLWindow::render() {
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
// light blue color
float r = 187/255.0f;
float g = 228/255.0f;
float b = 254/255.0f;
glClearColor(r,g,b, 1.0f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glLoadIdentity(); // Reset The Current Modelview Matrix
//glTranslatef(0.0f, 0.0f, -6.0f); // Move Left 1.5 Units And Into The Screen 6.0
if (showFlag == 1) {
glViewport(0, 0, width() , height() );
openglData.Plot3DView();
}
else if(showFlag == 2){
glViewport(0, 0, width(), height());
openglData.PlotViewRadius();
}
else if (showFlag == 3) {
glViewport(0, 0, width(), height());
openglData.PlotViewHeigth();
}
glFlush();
}
void OpenGLWindow::renderLater() {
requestUpdate();
}
bool OpenGLWindow::event(QEvent *event) {
switch (event->type()) {
case QEvent::UpdateRequest:
renderNow();
return true;
default:
return QWindow::event(event);
}
}
void OpenGLWindow::exposeEvent(QExposeEvent *event) {
Q_UNUSED(event);
if (isExposed())
renderNow();
}
//! [3]
//! [4]
void OpenGLWindow::renderNow() {
if (!isExposed())
return;
bool needsInitialize = false;
if (!m_context) {
m_context = new QOpenGLContext(this);
m_context->setFormat(requestedFormat());
m_context->create();
needsInitialize = true;
}
m_context->makeCurrent(this);
if (needsInitialize) {
initializeOpenGLFunctions();
initialize();
}
render();
m_context->swapBuffers(this);
if (m_animating)
renderLater();
}
void OpenGLWindow::setAnimating(bool animating) {
m_animating = animating;
if (animating)
renderLater();
}