qtQGLV_glw.cpp 7.03 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
* Web site: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Utils/GLSLShader.h"
#include "Utils/Qt/qtQGLV.h"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_precision.hpp"
#include "glm/gtc/type_ptr.hpp"

#include <QtGui/QTextEdit>
#include <QImage>

namespace CGoGN
{

namespace Utils
{

namespace QT
{


44 45
QGLView::QGLView(SimpleQGLV* ptr, QGLFormat& format, QWidget *parent):
	QGLViewer(format,parent),
Sylvain Thery's avatar
Sylvain Thery committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59
	m_sqgl(ptr),m_state_modifier(0)
{
}

QGLView::~QGLView()
{
}

void QGLView::setParamObject(float width, float* pos)
{
	qglviewer::Vec bbMin(pos[0]-width/2.0f, pos[1]-width/2.0f, pos[2]-width/2.0f);
	qglviewer::Vec bbMax(pos[0]+width/2.0f, pos[1]+width/2.0f, pos[2]+width/2.0f);
	camera()->setSceneBoundingBox(bbMin, bbMax);
	camera()->showEntireScene();
60
}
Sylvain Thery's avatar
Sylvain Thery committed
61

62 63 64 65 66 67
void QGLView::setObjectBB(float* bbmin, float* bbmax)
{
	qglviewer::Vec bbMin(bbmin[0], bbmin[1], bbmin[2]);
	qglviewer::Vec bbMax(bbmax[0], bbmax[1], bbmax[2]);
	camera()->setSceneBoundingBox(bbMin, bbMax);
	camera()->showEntireScene();
Sylvain Thery's avatar
Sylvain Thery committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81
}

QSize QGLView::minimumSizeHint() const
{
	return QSize(200, 200);
}

QSize QGLView::sizeHint() const
{
	return QSize(500, 500);
}

void QGLView::init()
{
82
	glewExperimental = GL_TRUE;
Sylvain Thery's avatar
Sylvain Thery committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	glewInit();
	if (m_sqgl)
		m_sqgl->cb_initGL();
}

void QGLView::preDraw()
{
	GLdouble gl_mvm[16];
	camera()->getModelViewMatrix(gl_mvm);

	glm::mat4& mvm = m_sqgl->modelViewMatrix();
	for(unsigned int i = 0; i < 4; ++i)
	{
		for(unsigned int j = 0; j < 4; ++j)
			mvm[i][j] = (float)gl_mvm[i*4+j];
	}

	GLdouble gl_pm[16];
	camera()->getProjectionMatrix(gl_pm);
	glm::mat4& pm = m_sqgl->projectionMatrix();
	for(unsigned int i = 0; i < 4; ++i)
	{
		for(unsigned int j = 0; j < 4; ++j)
			pm[i][j] = (float)gl_pm[i*4+j];
	}

	m_sqgl->cb_updateMatrix();
}


void QGLView::draw()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	if (m_sqgl)
	{
		Utils::GLSLShader::s_current_matrices = m_sqgl->matricesPtr();
		m_sqgl->cb_redraw();
	}
}


void QGLView::resizeGL(int w, int h)
{
	W = w;
	H = h;
	QGLViewer::resizeGL(w,h);
}


void QGLView::keyPressEvent(QKeyEvent* event)
{
	QGLViewer::keyPressEvent(event);
135

Sylvain Thery's avatar
Sylvain Thery committed
136 137
	if (m_sqgl)
	{
138
		m_state_modifier = event->modifiers();
Sylvain Thery's avatar
Sylvain Thery committed
139
		int k = event->key();
140
		if ( (k >= 65) && (k <= 91) && !(m_state_modifier & Qt::ShiftModifier) )
Sylvain Thery's avatar
Sylvain Thery committed
141 142 143 144 145 146 147 148 149 150
			k += 32;
		m_sqgl->cb_keyPress(k);
	}
}

void QGLView::keyReleaseEvent(QKeyEvent* event)
{
	QGLViewer::keyReleaseEvent(event);
	if (m_sqgl)
	{
151
		m_state_modifier = event->modifiers();
Sylvain Thery's avatar
Sylvain Thery committed
152
		int k = event->key();
153
		if ( (k >= 65) && (k <= 91) && !(m_state_modifier & Qt::ShiftModifier) )
Sylvain Thery's avatar
Sylvain Thery committed
154 155 156 157 158 159 160 161 162 163 164
			k += 32;
		m_sqgl->cb_keyRelease(k);
	}
}


void QGLView::mousePressEvent(QMouseEvent* event)
{
//	beginx = event->x();
//	beginy = event->y();
//	clickPoint = event->pos();
Sylvain Thery's avatar
Sylvain Thery committed
165
	m_current_button = event->button();
Sylvain Thery's avatar
Sylvain Thery committed
166 167 168 169 170 171 172 173 174 175
	if (m_sqgl)
		m_sqgl->cb_mousePress(event->button(), event->x(), getHeight() - event->y());

	QGLViewer::mousePressEvent(event);
}

void QGLView::mouseReleaseEvent(QMouseEvent* event)
{
	if (m_sqgl)
		m_sqgl->cb_mouseRelease(event->button(), event->x(), getHeight() - event->y());
Sylvain Thery's avatar
Sylvain Thery committed
176
	m_current_button = 0;
Sylvain Thery's avatar
Sylvain Thery committed
177 178 179 180 181 182 183 184 185
	QGLViewer::mouseReleaseEvent(event);
}

void QGLView::mouseClickEvent(QMouseEvent* event)
{
	if (m_sqgl)
		m_sqgl->cb_mouseClick(event->button(), event->x(), getHeight() - event->y());
}

Sylvain Thery's avatar
Sylvain Thery committed
186 187 188 189 190 191 192 193
void QGLView::mouseMoveEvent(QMouseEvent* event)
{
	if (m_sqgl)
		m_sqgl->cb_mouseMove(m_current_button, event->x(), getHeight() - event->y());

	QGLViewer::mouseMoveEvent(event);
}

Sylvain Thery's avatar
Sylvain Thery committed
194 195 196 197 198 199 200
void QGLView::glMousePosition(int& x, int& y)
{
	QPoint xy = mapFromGlobal(QCursor::pos());
	x = xy.x();
	y = getHeight() - xy.y();
}

Sylvain Thery's avatar
Sylvain Thery committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
GLfloat QGLView::getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB, int radius)
{
	// get Z from depth buffer
	int yy = y;
	GLfloat depth_t[25];
	glReadPixels(x-2, yy-2, 5, 5, GL_DEPTH_COMPONENT, GL_FLOAT, depth_t);

	GLfloat depth=0.0f;
	unsigned int nb=0;
	for (unsigned int i=0; i< 25; ++i)
	{
		if (depth_t[i] != 1.0f)
		{
			depth += depth_t[i];
			nb++;
		}
	}
	if (nb>0)
		depth /= float(nb);
	else
		depth = 0.5f;

	glm::i32vec4 viewport;
	glGetIntegerv(GL_VIEWPORT, &(viewport[0]));

	glm::vec3 win(x, yy, 0.0f);

	glm::vec3 P = glm::unProject(win, m_sqgl->modelViewMatrix(), m_sqgl->projectionMatrix(), viewport);

	rayA[0] = P[0];
	rayA[1] = P[1];
	rayA[2] = P[2];

	win[2] = depth;

	P = glm::unProject(win, m_sqgl->modelViewMatrix(), m_sqgl->projectionMatrix(), viewport);
	rayB[0] = P[0];
	rayB[1] = P[1];
	rayB[2] = P[2];

	if (depth == 1.0f)	// depth vary in [0-1]
		win[2] = 0.5f;

	win[0] += radius;
	P = glm::unProject(win, m_sqgl->modelViewMatrix(), m_sqgl->projectionMatrix(), viewport);
	Geom::Vec3f Q;
	Q[0] = P[0];
	Q[1] = P[1];
	Q[2] = P[2];

	// compute & return distance
	Q -= rayB;
	return float(Q.norm());
}


float QGLView::getWidthInWorld(unsigned int pixel_width, const Geom::Vec3f& center)
{

	glm::i32vec4 viewport;
	glGetIntegerv(GL_VIEWPORT, &(viewport[0]));

	glm::vec3 win = glm::project(glm::vec3(center[0],center[1],center[2]), m_sqgl->modelViewMatrix(), m_sqgl->projectionMatrix(), viewport);

	win[0]-= pixel_width/2;

	glm::vec3 P = glm::unProject(win, m_sqgl->modelViewMatrix(), m_sqgl->projectionMatrix(), viewport);

	win[0] += pixel_width;

	glm::vec3 Q = glm::unProject(win, m_sqgl->modelViewMatrix(), m_sqgl->projectionMatrix(), viewport);

	return glm::distance(P,Q);
}
Sylvain Thery's avatar
Sylvain Thery committed
275 276 277 278 279 280

} // namespace QT

} // namespace Utils

} // namespace CGoGN