#include "glwidget.h"
#include "../../shared/math3d.h"
#include "../../shared/gltools.h"
#include <QPainter>
#include <QPaintEvent>
#include <GL/glu.h>
#include <math.h>
GLWidget
::GLWidget(QWidget *parent
){
animationTimer.setSingleShot(false);
animationTimer.setInterval(33);
connect(&animationTimer, SIGNAL(timeout()), this, SLOT(update()));
animationTimer.start();
qDebug() << pixmap.load("../Transformgl/graphicsview-pixmapitem.png");
qDebug() << pixmap.width();
qDebug() << pixmap.height();
setAutoFillBackground(false);
setMinimumSize(800, 600);
setWindowTitle("Transformgl with PaintEvent");
}
QSize GLWidget
::sizeHint() const {
}
void GLWidget::initializeGL()
{
// Bluish background
glClearColor(0.0f, 0.0f, .50f, 1.0f );
// Draw everything as wire frame
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
void GLWidget::resizeGL(int w, int h)
{
// GLfloat fAspect;
// // Prevent a divide by zero, when window is too short
// // (you cant make a window of zero width).
// if(h == 0)
// h = 1;
// glViewport(0, 0, w, h);
// fAspect = (GLfloat)w / (GLfloat)h;
// // Reset the coordinate system before modifying
// glMatrixMode(GL_PROJECTION);
// glLoadIdentity();
// // Set the clipping volume
// gluPerspective(35.0f, fAspect, 1.0f, 50.0f);
// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
setupViewport(w, h);
}
//void GLWidget::paintGL()
//{
// M3DMatrix44f rotationMatrix, translationMatrix, transformationMatrix;
// static GLfloat yRot = 0.0f; // Rotation angle for animation
// yRot += 0.5f;
// // Clear the window with current clearing color
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// // Build a rotation matrix
// m3dRotationMatrix44(transformationMatrix, m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f);
// transformationMatrix[12] = 0.0f;
// transformationMatrix[13] = 0.0f;
// transformationMatrix[14] = -2.5f;
// glLoadMatrixf(transformationMatrix);
// gltDrawTorus(0.35f, 0.15f, 40, 20);
// // Do the buffer Swap
// swapBuffers();
//}
{
makeCurrent();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// Bluish background
glClearColor(0.0f, 0.0f, .50f, 1.0f );
// Draw everything as wire frame
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
M3DMatrix44f rotationMatrix, translationMatrix, transformationMatrix;
static GLfloat yRot = 0.0f; // Rotation angle for animation
yRot += 0.5f;
setupViewport(width(), height());
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// Build a rotation matrix
m3dRotationMatrix44(transformationMatrix, m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f);
transformationMatrix[12] = 0.0f;
transformationMatrix[13] = 0.0f;
transformationMatrix[14] = -2.5f;
glLoadMatrixf(transformationMatrix);
gltDrawTorus(0.35f, 0.15f, 40, 20);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
painter.setBrush(Qt::cyan);
painter.drawLine(0, 0, width(), height());
// QString text = tr("Click and drag with the left mouse button "
// "to rotate the Qt logo.");
// QFontMetrics metrics = QFontMetrics(font());
// int border = qMax(4, metrics.leading());
// QRect rect = metrics.boundingRect(0, 0, width() - 2*border, int(height()*0.125),
// Qt::AlignCenter | Qt::TextWordWrap, text);
// painter.setRenderHint(QPainter::TextAntialiasing);
// painter.fillRect(QRect(0, 0, width(), rect.height() + 2*border),
// QColor(0, 0, 0, 127));
// painter.setPen(Qt::white);
// painter.fillRect(QRect(0, 0, width(), rect.height() + 2*border),
// QColor(0, 0, 0, 127));
// painter.drawText((width() - rect.width())/2, border,
// rect.width(), rect.height(),
// Qt::AlignCenter | Qt::TextWordWrap, text);
painter.end();
}
void GLWidget::setupViewport(int w, int h)
{
GLfloat fAspect;
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;
glViewport(0, 0, w, h);
fAspect = (GLfloat)w / (GLfloat)h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the clipping volume
gluPerspective(35.0f, fAspect, 1.0f, 50.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
#include "glwidget.h"
#include "../../shared/math3d.h"
#include "../../shared/gltools.h"
#include <QPainter>
#include <QPaintEvent>
#include <GL/glu.h>
#include <math.h>
GLWidget::GLWidget(QWidget *parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
animationTimer.setSingleShot(false);
animationTimer.setInterval(33);
connect(&animationTimer, SIGNAL(timeout()), this, SLOT(update()));
animationTimer.start();
qDebug() << pixmap.load("../Transformgl/graphicsview-pixmapitem.png");
qDebug() << pixmap.width();
qDebug() << pixmap.height();
setAutoFillBackground(false);
setMinimumSize(800, 600);
setWindowTitle("Transformgl with PaintEvent");
}
QSize GLWidget::sizeHint() const
{
return QSize(800, 600);
}
void GLWidget::initializeGL()
{
// Bluish background
glClearColor(0.0f, 0.0f, .50f, 1.0f );
// Draw everything as wire frame
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
void GLWidget::resizeGL(int w, int h)
{
// GLfloat fAspect;
// // Prevent a divide by zero, when window is too short
// // (you cant make a window of zero width).
// if(h == 0)
// h = 1;
// glViewport(0, 0, w, h);
// fAspect = (GLfloat)w / (GLfloat)h;
// // Reset the coordinate system before modifying
// glMatrixMode(GL_PROJECTION);
// glLoadIdentity();
// // Set the clipping volume
// gluPerspective(35.0f, fAspect, 1.0f, 50.0f);
// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
setupViewport(w, h);
}
//void GLWidget::paintGL()
//{
// M3DMatrix44f rotationMatrix, translationMatrix, transformationMatrix;
// static GLfloat yRot = 0.0f; // Rotation angle for animation
// yRot += 0.5f;
// // Clear the window with current clearing color
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// // Build a rotation matrix
// m3dRotationMatrix44(transformationMatrix, m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f);
// transformationMatrix[12] = 0.0f;
// transformationMatrix[13] = 0.0f;
// transformationMatrix[14] = -2.5f;
// glLoadMatrixf(transformationMatrix);
// gltDrawTorus(0.35f, 0.15f, 40, 20);
// // Do the buffer Swap
// swapBuffers();
//}
void GLWidget::paintEvent(QPaintEvent *event)
{
makeCurrent();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// Bluish background
glClearColor(0.0f, 0.0f, .50f, 1.0f );
// Draw everything as wire frame
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
M3DMatrix44f rotationMatrix, translationMatrix, transformationMatrix;
static GLfloat yRot = 0.0f; // Rotation angle for animation
yRot += 0.5f;
setupViewport(width(), height());
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// Build a rotation matrix
m3dRotationMatrix44(transformationMatrix, m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f);
transformationMatrix[12] = 0.0f;
transformationMatrix[13] = 0.0f;
transformationMatrix[14] = -2.5f;
glLoadMatrixf(transformationMatrix);
gltDrawTorus(0.35f, 0.15f, 40, 20);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
QPainter painter(this);
painter.setBrush(Qt::cyan);
painter.drawLine(0, 0, width(), height());
// QString text = tr("Click and drag with the left mouse button "
// "to rotate the Qt logo.");
// QFontMetrics metrics = QFontMetrics(font());
// int border = qMax(4, metrics.leading());
// QRect rect = metrics.boundingRect(0, 0, width() - 2*border, int(height()*0.125),
// Qt::AlignCenter | Qt::TextWordWrap, text);
// painter.setRenderHint(QPainter::TextAntialiasing);
// painter.fillRect(QRect(0, 0, width(), rect.height() + 2*border),
// QColor(0, 0, 0, 127));
// painter.setPen(Qt::white);
// painter.fillRect(QRect(0, 0, width(), rect.height() + 2*border),
// QColor(0, 0, 0, 127));
// painter.drawText((width() - rect.width())/2, border,
// rect.width(), rect.height(),
// Qt::AlignCenter | Qt::TextWordWrap, text);
painter.end();
}
void GLWidget::setupViewport(int w, int h)
{
GLfloat fAspect;
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;
glViewport(0, 0, w, h);
fAspect = (GLfloat)w / (GLfloat)h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the clipping volume
gluPerspective(35.0f, fAspect, 1.0f, 50.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
To copy to clipboard, switch view to plain text mode
Bookmarks