frame_manip.cpp 10.9 KB
Newer Older
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* 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.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
21 22 23 24 25 26 27 28 29
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <iostream>

#include "frame_manip.h"

#include "Topology/generic/parameters.h"
30
#include "Topology/map/embeddedMap2.h"
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

#include "Geometry/vector_gen.h"
#include "Geometry/matrix.h"
#include "Geometry/transfo.h"
#include "Algo/Geometry/boundingbox.h"

#include "Algo/Modelisation/polyhedron.h"
#include "Topology/generic/parameters.h"

#include "Algo/Render/GL2/mapRender.h"
#include "Utils/Shaders/shaderFlat.h"
#include "Utils/Shaders/shaderSimpleColor.h"

#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_precision.hpp"
#include "glm/gtc/type_ptr.hpp"

using namespace CGoGN;

struct PFP: public PFP_STANDARD
{
	// definition de la carte
53
	typedef EmbeddedMap2 MAP;
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 135 136
};

PFP::MAP myMap;

void MyQT::cb_initGL()
{
	m_frame = new Utils::FrameManipulator();

	// frame is rendered twice the size of normal
	m_frame->setSize(2.f);

	// none axis is picked
	m_pickedAxis=0;

	// subdivision of 7 for each drawable
	m_precDraw=9;

	// create the Drawables (one of each)
	Utils::LineDrawable* ldp = new Utils::Grid(m_precDraw);
	ldp->setColor(Geom::Vec4f(0.0f,0.0f,0.8f,1.0f));
	m_ld.push_back(ldp);

	ldp = new Utils::Sphere(m_precDraw,m_precDraw);
	ldp->setColor(Geom::Vec4f(0.0f,0.8f,0.0f,1.0f));
	m_ld.push_back(ldp);

	ldp = new Utils::Cube(m_precDraw);
	ldp->setColor(Geom::Vec4f(0.8f,0.0f,0.0f,1.0f));
	m_ld.push_back(ldp);

	ldp = new Utils::Cone(m_precDraw,m_precDraw);
	ldp->setColor(Geom::Vec4f(0.8f,0.0f,0.8f,1.0f));
	m_ld.push_back(ldp);


	ldp= new Utils::Cylinder(m_precDraw,m_precDraw);
	ldp->setColor(Geom::Vec4f(0.0f,0.8f,0.8f,1.0f));
	m_ld.push_back(ldp);

	ldp= new Utils::IcoSphere(m_precDraw);
	ldp->setColor(Geom::Vec4f(0.8f,0.3f,0.1f,1.0f));
	m_ld.push_back(ldp);


	// create a grid of pickables
	Geom::Vec3f center(float(NBP-1)*2.0f, float(NBP-1)*2.0f, float(NBP-1)*2.0f);
	for (unsigned int i=0; i< NBP; ++i)
	{
		for (unsigned int j=0; j< NBP; ++j)
		{
			for (unsigned int k=0; k< NBP; ++k)
			{
				unsigned int idx = i*NBP*NBP + j*NBP + k;

				Utils::Pickable* pick = new Utils::Pickable(m_ld[idx%m_ld.size()],idx);
				m_pickables.push_back(pick);

				// attention transfo appliques en ordre inverse !
				Geom::Vec3f position( 4.0f * Geom::Vec3f(i,j,k) - center);
				pick->translate(position);
				pick->randomOrientation();
				pick->randomUniformScale(0.8f,1.6f);
			}
		}
	}


	m_lastPickedObject = m_pickables.front();
	m_frame->setTransformation(m_lastPickedObject->transfo());

}


void MyQT::cb_redraw()
{
	// affichage du frame
	if (m_lastPickedObject)
		m_frame->draw();

	// affichage des objets
	for (std::vector<Utils::Pickable*>::iterator it=m_pickables.begin(); it != m_pickables.end(); ++it)
	{
		if (*it == m_lastPickedObject)
Sylvain Thery's avatar
Sylvain Thery committed
137
			(*it)->setLineWidth(3.0);
138
		else
Sylvain Thery's avatar
Sylvain Thery committed
139
			(*it)->setLineWidth(1.0);
140 141 142
		(*it)->draw();
	}

Sylvain Thery's avatar
test  
Sylvain Thery committed
143 144 145 146 147 148 149
	m_nbFrames++;
	if (m_nbFrames >=40)
	{
		std::cout << 40000.0/m_frame_ch.elapsed()<< " fps"<<std::endl;
		m_nbFrames = 0;
		m_frame_ch.start();
	}
150 151
}

152
void  MyQT::cb_mousePress(int /*button*/, int x, int y)
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
{
	if (!Shift())
		return;

	m_begX = x;
	m_begY = y;

	// get ray of selection
	Geom::Vec3f rayA,rayB;
	float dist = getOrthoScreenRay(x,y,rayA,rayB);
	Geom::Vec3f AB = rayB-rayA;


	unsigned int fr_picked =0;
	if (m_lastPickedObject)	// an object is already picked ? => frame is drawn
	{
		// picking the frame -> axis
		fr_picked = m_frame->pick(rayA,AB,dist);
		// same already picked -> unpick
		if (fr_picked == m_pickedAxis)
			m_pickedAxis = 0;
		else
			m_pickedAxis=fr_picked;
	}


	// highlighting
	m_frame->highlight(m_pickedAxis);

	if (fr_picked == 0)	// frame not picked -> pick the pickable objects
	{
		m_lastPickedObject = Utils::Pickable::pick(m_pickables,rayA,AB);
		// set FrameManipulator on picked object
		if (m_lastPickedObject)
		{
			m_frame->setTransformation(m_lastPickedObject->transfo());
			std::cout << "Picked "<<m_lastPickedObject->shape() << " "<< m_lastPickedObject->id()<< std::endl;
		}
	}

Sylvain Thery's avatar
Sylvain Thery committed
193 194 195
	// store origin & selected axis on on screen projection for easy manipulation.
	m_frame->storeProjection(m_pickedAxis);

196 197 198 199
	updateGL();

}

Sylvain Thery's avatar
Sylvain Thery committed
200
void  MyQT::cb_mouseMove(int buttons, int x, int y)
201 202 203 204
{
	if (!Shift())
		return;

Sylvain Thery's avatar
Sylvain Thery committed
205
	// rotation selected ?
206 207
	if (Utils::FrameManipulator::rotationAxis(m_pickedAxis))
	{
Sylvain Thery's avatar
Sylvain Thery committed
208 209 210 211 212 213 214 215
		if (buttons&1)
		{
			float angle = m_frame->angleFromMouse(x,y,x-m_begX, y-m_begY);
			m_frame->rotate(m_pickedAxis, angle);
		}
		else if (buttons&2)
			m_frame->rotateInScreen(x-m_begX, y-m_begY);

216 217
		m_lastPickedObject->transfo() = m_frame->transfo();
	}
Sylvain Thery's avatar
Sylvain Thery committed
218
	// translation selected
219 220
	else if (Utils::FrameManipulator::translationAxis(m_pickedAxis))
	{
Sylvain Thery's avatar
Sylvain Thery committed
221 222 223 224 225 226 227 228
		if (buttons&1)
		{
			float dist =  m_frame->distanceFromMouse(x-m_begX, y-m_begY);
			m_frame->translate(m_pickedAxis, dist);
		}
		else if (buttons&2)
			m_frame->translateInScreen(x-m_begX, y-m_begY);

229 230
		m_lastPickedObject->transfo() = m_frame->transfo();
	}
Sylvain Thery's avatar
Sylvain Thery committed
231
	// scale selected
232 233 234 235 236 237
	else if (Utils::FrameManipulator::scaleAxis(m_pickedAxis) )
	{
		float scale = m_frame->scaleFromMouse(x-m_begX, y-m_begY);
		m_frame->scale(m_pickedAxis, scale );
		m_lastPickedObject->transfo() = m_frame->transfo();
	}
Sylvain Thery's avatar
Sylvain Thery committed
238 239 240 241 242 243 244 245 246
//	// nothing selected: using screen translation/rotation (button left/right)
//	else if (m_lastPickedObject)
//	{
//		if (buttons&1)
//			m_frame->translateInScreen(x-m_begX, y-m_begY);
//		else if (buttons&2)
//			m_frame->rotateInScreen(x-m_begX, y-m_begY);
//		m_lastPickedObject->transfo() = m_frame->transfo();
//	}
247 248 249 250 251 252 253 254 255

	m_begX = x;
	m_begY = y;
	updateGL();
	return;

}


256
void  MyQT::cb_wheelEvent(int delta, int /*x*/, int /*y*/)
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
{
	if (!Shift())
		return;

	if (!m_lastPickedObject)
		return;

	if (Utils::FrameManipulator::rotationAxis(m_pickedAxis))
		m_frame->rotate(m_pickedAxis, float(delta)/60.0f);
	else if (Utils::FrameManipulator::translationAxis(m_pickedAxis))
		m_frame->translate(m_pickedAxis, float(-delta)/1200.0f);
	else if (Utils::FrameManipulator::scaleAxis(m_pickedAxis) )
		m_frame->scale(m_pickedAxis, 1.0f+ float(-delta)/1200.0f);
	else
		m_frame->addSize(float(-delta)/480.0f);

	m_lastPickedObject->transfo() = m_frame->transfo();
	updateGL();
}

void  MyQT::cb_keyPress(int code)
{
	switch(code)
	{
	case '+':
		if (m_precDraw < 128)
			m_precDraw++;
		for (std::vector<Utils::LineDrawable*>::iterator it=m_ld.begin(); it != m_ld.end(); ++it)
			(*it)->updatePrecisionDrawing(m_precDraw);
		break;

	case '-':
		if (m_precDraw > 3)
			m_precDraw--;
		for (std::vector<Utils::LineDrawable*>::iterator it=m_ld.begin(); it != m_ld.end(); ++it)
			(*it)->updatePrecisionDrawing(m_precDraw);
		break;

	case 'r':
		for (unsigned int i=0; i< NBP*NBP*NBP; ++i)
			m_pickables[i]->randomOrientation();
		if (m_lastPickedObject)
			m_frame->setTransformation(m_lastPickedObject->transfo());
		break;
301

302
	case 'x':
Sylvain Thery's avatar
Sylvain Thery committed
303 304 305 306
		if (m_frame->locked(Utils::FrameManipulator::Xt))
			m_frame->unlock(Utils::FrameManipulator::Xt);
		else
			m_frame->lock(Utils::FrameManipulator::Xt);
307
		break;
308 309 310 311 312 313 314

	case 'w':
		if (m_frame->locked(Utils::FrameManipulator::Xt))
			m_frame->unlock(Utils::FrameManipulator::Translations);
		else
			m_frame->lock(Utils::FrameManipulator::Translations);
		break;
315
	case 'y':
Sylvain Thery's avatar
Sylvain Thery committed
316 317 318 319
		if (m_frame->locked(Utils::FrameManipulator::Yt))
			m_frame->unlock(Utils::FrameManipulator::Yt);
		else
			m_frame->lock(Utils::FrameManipulator::Yt);
320 321
		break;
	case 'z':
Sylvain Thery's avatar
Sylvain Thery committed
322 323 324 325
		if (m_frame->locked(Utils::FrameManipulator::Zt))
			m_frame->unlock(Utils::FrameManipulator::Zt);
		else
			m_frame->lock(Utils::FrameManipulator::Zt);
326 327
		break;
	case 'a':
Sylvain Thery's avatar
Sylvain Thery committed
328 329 330 331
		if (m_frame->locked(Utils::FrameManipulator::Xr))
			m_frame->unlock(Utils::FrameManipulator::Xr);
		else
			m_frame->lock(Utils::FrameManipulator::Xr);
332 333
		break;
	case 'b':
Sylvain Thery's avatar
Sylvain Thery committed
334 335 336 337
		if (m_frame->locked(Utils::FrameManipulator::Yr))
			m_frame->unlock(Utils::FrameManipulator::Yr);
		else
			m_frame->lock(Utils::FrameManipulator::Yr);
338 339
		break;
	case 'c':
Sylvain Thery's avatar
Sylvain Thery committed
340 341 342 343
		if (m_frame->locked(Utils::FrameManipulator::Zr))
			m_frame->unlock(Utils::FrameManipulator::Zr);
		else
			m_frame->lock(Utils::FrameManipulator::Zr);
344
		break;
Sylvain Thery's avatar
Sylvain Thery committed
345 346 347 348 349 350

	case 's':
		if (m_frame->locked(Utils::FrameManipulator::Xs))
			m_frame->unlock(Utils::FrameManipulator::Xs);
		else
			m_frame->lock(Utils::FrameManipulator::Xs);
351
		break;
Sylvain Thery's avatar
Sylvain Thery committed
352 353 354 355 356
	case 't':
		if (m_frame->locked(Utils::FrameManipulator::Ys))
			m_frame->unlock(Utils::FrameManipulator::Ys);
		else
			m_frame->lock(Utils::FrameManipulator::Ys);
357
		break;
Sylvain Thery's avatar
Sylvain Thery committed
358 359 360 361 362
	case 'u':
		if (m_frame->locked(Utils::FrameManipulator::Zs))
			m_frame->unlock(Utils::FrameManipulator::Zs);
		else
			m_frame->lock(Utils::FrameManipulator::Zs);
363
		break;
Sylvain Thery's avatar
Sylvain Thery committed
364

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
	}
	updateGL();
}


int main(int argc, char **argv)
{


	// interface:
	QApplication app(argc, argv);
	MyQT sqt;

	if (argc>1)
		sqt.NBP = atoi(argv[1]);

Sylvain Thery's avatar
Sylvain Thery committed
381

382
	sqt.setHelpMsg("Param :size of grid (number of objects)\nMpuse (with shift):\n"
Sylvain Thery's avatar
Sylvain Thery committed
383 384 385
			" -click on object: selection\n -left click on frame: constraint 3d Rotation/Translation/Scale\n"
			" -right click on frame :free 2D Rotation/Translation\nKeys:\n  x/y/z lock/unlock translation\n  a/b/c lock/unlock rotation\n   s/t/u lock/unlock scaling");

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
    //  bounding box
    Geom::Vec3f lPosObj = Geom::Vec3f(0.0f,0.0f,0.0f);
    float lWidthObj = sqt.NBP*4.0f;

    // envoit info BB a l'interface
	sqt.setParamObject(lWidthObj,lPosObj.data());

	// show final pour premier redraw
	sqt.show();

	// et on attend la fin.
	return app.exec();

	return 0;
}