#ifdef WIN32
#define NOMINMAX
#endif
#include <QtOpenGL>
#include "glwidget.h"
using namespace libmv;
GLWidget::GLWidget(QWidget *parent)
:QGLWidget(QGLFormat(QGL::SampleBuffers), parent) {
viewer_position_ << 0, 0, -10;
viewer_orientation_ << 0, 0, 0;
point_size_ = 0.03;
}
GLWidget::~GLWidget(){
foreach(GLuint s, structures_list_) {
glDeleteLists(s, 1);
}
}
void GLWidget::initializeGL()
{
QGLWidget::initializeGL();
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
static GLfloat light_position[4] = { 0, 0, 50.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
void GLWidget::resizeGL(int w, int h)
{
QGLWidget::resizeGL(w, h);
}
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-5, 5, -5, 5, 1, 1000);
glTranslatef(viewer_position_[0], viewer_position_[1], viewer_position_[2]);
glRotatef(viewer_orientation_[0], 1.0, 0.0, 0.0);
glRotatef(viewer_orientation_[1], 0.0, 1.0, 0.0);
glRotatef(viewer_orientation_[2], 0.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, width(), height());
glDisable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
foreach(GLuint s, structures_list_) {
glCallList(s);
}
}
void GLWidget::mousePressEvent(QMouseEvent *event) {
last_mouse_position_ = event->pos();
}
static void NormalizeAngle(float &angle) {
while(angle < 0)
angle += 360;
while(angle > 360)
angle -= 360;
}
void GLWidget::mouseMoveEvent(QMouseEvent *event) {
int dx = event->x() - last_mouse_position_.x();
int dy = event->y() - last_mouse_position_.y();
if (event->buttons() & Qt::LeftButton) {
viewer_orientation_[0] += 0.5 * dy;
viewer_orientation_[1] += 0.5 * dx;
NormalizeAngle(viewer_orientation_[0]);
NormalizeAngle(viewer_orientation_[1]);
} else if (event->buttons() & Qt::RightButton) {
viewer_orientation_[0] += 0.5 * dy;
viewer_orientation_[2] += 0.5 * dx;
NormalizeAngle(viewer_orientation_[0]);
NormalizeAngle(viewer_orientation_[2]);
}
last_mouse_position_ = event->pos();
updateGL();
}
void GLWidget::wheelEvent(QWheelEvent *event) {
float scale = 1.0f;
if (event->delta() < 0)
scale = 1.2;
else
scale = 0.8;
if (event->orientation() == Qt::Horizontal) {
viewer_position_[0] *= scale;
} else {
viewer_position_[2] *= scale;
}
updateGL();
}
inline void GLWidget::DrawPointStructure(libmv::Vec3 &p) {
glVertex3f(p[0]-point_size_, p[1]-point_size_, p[2]+point_size_);
glVertex3f(p[0]+point_size_, p[1]-point_size_, p[2]+point_size_);
glVertex3f(p[0]+point_size_, p[1]+point_size_, p[2]+point_size_);
glVertex3f(p[0]-point_size_, p[1]+point_size_, p[2]+point_size_);
}
void GLWidget::AddNewStructure(vector<Vec3> &struct_coords) {
GLuint s_new = glGenLists(1);
structures_list_ << s_new;
glNewList(s_new, GL_COMPILE);
glBegin(GL_QUADS);
for (size_t s = 0; s < struct_coords.size(); ++s) {
DrawPointStructure(struct_coords[s]);
}
glEnd();
glEndList();
updateGL();
}