pickables.cpp 26.2 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
 * Contact information: cgogn@unistra.fr                                        *
 *                                                                              *
 *******************************************************************************/

#include "Utils/pickables.h"
#include "glm/gtc/matrix_transform.hpp"
27
#include "glm/gtx/norm.hpp"
28 29 30
#include "Geometry/distances.h"
#include "Geometry/intersection.h"
#include <algorithm>
thery's avatar
thery committed
31 32
#define _USE_MATH_DEFINES
#include <math.h>
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

namespace CGoGN
{

namespace Utils
{

LineDrawable::LineDrawable()
{
	m_vboPos = new VBO();
	m_vboPos->setDataSize(3);

	m_shader = new ShaderSimpleColor();

	m_shader->setAttributePosition(m_vboPos);
	m_shader->setColor(Geom::Vec4f(1.,1.,0.,0.));
	GLSLShader::registerShader(NULL, m_shader);

	glGenBuffers(1, &m_ind);
}

LineDrawable::~LineDrawable()
{
	delete m_vboPos;
	GLSLShader::unregisterShader(NULL, m_shader);
	delete m_shader;

	glDeleteBuffers(1, &m_ind);
}


void LineDrawable::setColor(const Geom::Vec4f& col)
{
66
	m_color=col;
67 68 69
	m_shader->setColor(col);
}

70 71 72 73 74
const Geom::Vec4f&  LineDrawable::getColor()
{
	return m_color;
}

75 76 77 78 79 80 81
void LineDrawable::draw()
{
	m_shader->enableVertexAttribs();
	glDrawArrays(GL_LINES, 0, m_nb);
	m_shader->disableVertexAttribs();
}

82 83 84 85 86
void LineDrawable::getPrecisionDrawing(unsigned int& sub, unsigned int& sub2)
{
	sub = m_sub1;
	sub2 = m_sub2;
}
87 88 89


Pickable::Pickable(LineDrawable* ld, unsigned int id):
Sylvain Thery's avatar
Sylvain Thery committed
90
	m_drawable(ld),m_transfo(1.0f), m_id(id), m_allocated(false)
91 92 93
{
}

Sylvain Thery's avatar
Sylvain Thery committed
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
Pickable::Pickable(int object, unsigned int id):
	m_transfo(1.0f), m_id(id),  m_allocated(true)
{
	switch (object)
	{
		case GRID:
			m_drawable = new Grid();
			break;
		case SPHERE:
			m_drawable = new Sphere();
			break;
		case CONE:
			m_drawable = new Sphere();
			break;
		case CYLINDER:
			m_drawable = new Cylinder();
			break;
		case CUBE:
			m_drawable = new Cube();
			break;
		case ICOSPHERE:
			m_drawable = new IcoSphere();
			break;
		default:
			break;
	}
}

Pickable::~Pickable()
{
	if (m_allocated)
		delete m_drawable;
}


129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
void Pickable::invertPV(const Geom::Vec3f& P, const Geom::Vec3f& V, const glm::mat4& transfo, Geom::Vec3f& PP, Geom::Vec3f& VV)
{
	glm::mat4 invtr = glm::inverse(transfo);
	glm::vec4 xP(P[0],P[1],P[2],1.0f);
	glm::vec4 xQ(P[0]+V[0],P[1]+V[1],P[2]+V[2],1.0f);

	glm::vec4 tP = invtr*xP;
	glm::vec4 tQ = invtr*xQ;

	PP = Geom::Vec3f(tP[0]/tP[3], tP[1]/tP[3], tP[2]/tP[3]);
	VV = Geom::Vec3f(tQ[0]/tQ[3] - PP[0], tQ[1]/tQ[3] - PP[1], tQ[2]/tQ[3]- PP[2]);
}




145
bool Pickable::pick(const Geom::Vec3f& P, const Geom::Vec3f& V,  Geom::Vec3f& I, float epsilon)
146 147 148 149 150
{
	Geom::Vec3f PP;
	Geom::Vec3f VV;
	invertPV(P,V,m_transfo,PP,VV);

151
	return m_drawable->pick(PP,VV,I,epsilon) != 0;
152 153 154 155 156 157 158 159 160 161 162

}


void Pickable::draw()
{
	glm::mat4 store = Utils::GLSLShader::currentTransfo();
	Utils::GLSLShader::currentTransfo() *= m_transfo;
	Utils::GLSLShader::updateCurrentMatrices();
	m_drawable->draw();
	Utils::GLSLShader::currentTransfo() = store;
163
	Utils::GLSLShader::updateCurrentMatrices();
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 193 194 195 196 197 198 199 200 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
}


glm::mat4&  Pickable::transfo()
{
	return m_transfo;
}

void Pickable::rotate(float angle, const Geom::Vec3f& Axis)
{
	m_transfo = glm::rotate(m_transfo, angle, glm::vec3(Axis[0],Axis[1],Axis[2]));
}
//void Pickable::rotate(float angle, const Geom::Vec3f& Axis)
//{
//	glm::mat4 tr = glm::rotate(glm::mat4(1.0f), angle, glm::vec3(Axis[0],Axis[1],Axis[2]));
//	m_transfo = tr*m_transfo;
//}

void Pickable::translate(const Geom::Vec3f& P)
{
	m_transfo = glm::translate(m_transfo, glm::vec3(P[0],P[1],P[2]));
}
//void Pickable::translate(const Geom::Vec3f& P)
//{
//	glm::mat4 tr = glm::translate(glm::mat4(1.0f), glm::vec3(P[0],P[1],P[2]));
//	m_transfo = tr*m_transfo;
//}

void Pickable::scale(const Geom::Vec3f& S)
{
	m_transfo = glm::scale(m_transfo, glm::vec3(S[0],S[1],S[2]));
}
//void Pickable::scale(const Geom::Vec3f& S)
//{
//	glm::mat4 tr = glm::scale(glm::mat4(1.0f), glm::vec3(S[0],S[1],S[2]));
//	m_transfo = tr*m_transfo;
//}


// TODO check why BUG
void Pickable::randomOrientation()
{
	Geom::Vec3f V1(float(rand() - RAND_MAX/2), float(rand() - RAND_MAX/2), float(rand() - RAND_MAX/2));
	V1.normalize();
	float angle = float(rand()%360);
	rotate(angle,V1);
}

void Pickable::randomScale(float min, float max)
{
	const unsigned int MAX_NB=10000;
	float amp = (max - min)/MAX_NB;
	float sx = float((rand()%MAX_NB))*amp + min;
	float sy = float((rand()%MAX_NB))*amp + min;
	float sz = float((rand()%MAX_NB))*amp + min;
	scale(Geom::Vec3f(sx,sy,sz));
}

void Pickable::randomUniformScale(float min, float max)
{
	const unsigned int MAX_NB=10000;
	float amp = (max - min)/MAX_NB;
	float sc = float((rand()%MAX_NB))*amp + min;

	scale(Geom::Vec3f(sc,sc,sc));
}



float Pickable::distancefrom(const Geom::Vec3f& P)
{
	Geom::Vec3f origin(m_transfo[3][0],m_transfo[3][1],m_transfo[3][2]);
	origin -= P;
	return float(origin.norm());
}


Pickable* Pickable::pick(const std::vector<Pickable*>& picks,const Geom::Vec3f& P, const Geom::Vec3f& V)
{
	float mdist = std::numeric_limits<float>::max();
	Pickable* res=NULL;
245
	Geom::Vec3f I;
246 247 248

	for (std::vector<Utils::Pickable*>::const_iterator it=picks.begin(); it != picks.end(); ++it)
	{
249
		if ((*it)->pick(P,V,I))
250
		{
251 252 253 254 255 256 257 258 259
			std::cout << "I="<<I <<std::endl;

//			float dist = (*it)->distancefrom(P);
			glm::mat4 tr = (*it)->transfo();
			glm::vec4 ii(I[0],I[1],I[2],1.0f);
			glm::vec4 IWglm = tr*ii;
			Geom::Vec3f IW(IWglm[0]/IWglm[3],IWglm[1]/IWglm[3],IWglm[2]/IWglm[3]);
			IW -= P;
			float dist = IW.norm();
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
			if (dist < mdist)
			{
				res = *it;
				mdist = dist;
			}
		}
	}
	return res;
}


bool Pickable::distOrder(const std::pair<float, Pickable*>& e1, const std::pair<float, Pickable*>& e2)
{
	return (e1.first < e2.first);
}

std::vector<Pickable*> Pickable::sortedPick(std::vector<Pickable*>& picks, const Geom::Vec3f& P, const Geom::Vec3f& V)
{
278
	Geom::Vec3f I;
279 280 281 282 283
	std::vector< std::pair<float, Pickable*> > sorted;
	sorted.reserve(picks.size());

	for (std::vector<Utils::Pickable*>::const_iterator it=picks.begin(); it != picks.end(); ++it)
	{
284
		if ((*it)->pick(P,V,I))
285
		{
286 287 288 289 290 291 292
//			float dist = (*it)->distancefrom(P);
			glm::mat4 tr = (*it)->transfo();
			glm::vec4 ii(I[0],I[1],I[2],1.0f);
			glm::vec4 IWglm = tr*ii;
			Geom::Vec3f IW(IWglm[0]/IWglm[3],IWglm[1]/IWglm[3],IWglm[2]/IWglm[3]);
			IW -= P;
			float dist = IW.norm();
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
			sorted.push_back(std::pair<float, Pickable*>(dist,*it));
		}
	}

	std::sort(sorted.begin(), sorted.end(),distOrder);

	std::vector<Pickable*> res;
	res.reserve(sorted.size());
	for (unsigned int i=0; i<sorted.size(); ++i)
	{
		res.push_back(sorted[i].second);
	}

	return res;
}


310 311 312 313
Geom::Vec3f Pickable::getPosition()
{
	return Geom::Vec3f(m_transfo[3][0],m_transfo[3][1],m_transfo[3][2]);
}
314

315 316 317 318 319 320
Geom::Vec3f Pickable::getAxisScale(unsigned int ax, float& scale)
{
	Geom::Vec3f tempo(m_transfo[ax][0],m_transfo[ax][1],m_transfo[ax][2]);
	scale = tempo.normalize();
	return tempo;
}
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353




Grid::Grid(unsigned int sub)
{
	changeTopo(sub);
}

void Grid::changeTopo(unsigned int sub)
{
	std::vector<Geom::Vec3f> points;
	points.resize((sub+1)*2*2);

	m_nb=0;

	for (unsigned int i=0; i<=sub; ++i)
	{
		float a = -1.0f + (2.0f/sub)*i;

		points[4*i] = Geom::Vec3f(a,-1.0f,0.0f);
		points[4*i+1] = Geom::Vec3f(a,1.0f,0.0f);
		points[4*i+2] = Geom::Vec3f(-1.0f,a,0.0f);
		points[4*i+3] = Geom::Vec3f(1.0f,a,0.0f);
		m_nb+=4;
	}

	m_vboPos->bind();
	glBufferData(GL_ARRAY_BUFFER, m_nb * sizeof(Geom::Vec3f), &(points[0]), GL_STREAM_DRAW);
}

void Grid::updatePrecisionDrawing(unsigned int sub, unsigned int sub2)
{
354 355
	m_sub1 = sub;
	m_sub2 = sub2;
356 357 358 359
	changeTopo(sub);
}


360
unsigned int Grid::pick(const Geom::Vec3f& P, const Geom::Vec3f& V, Geom::Vec3f& I, float epsilon)
361
{
362
	if (fabs(V[2])>=0.0000001f)
363 364
	{
		float a = -1.0f*P[2]/V[2];
365
		I = Geom::Vec3f(P+a*V);	// intersection with plane z=0
366

367
		if ( (fabs(I[0])<=1.0f) && (fabs(I[1])<=1.0f) )
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
			return 1;
	}

	return 0;
}






Sphere::Sphere(unsigned int par, unsigned int mer)
{
	changeTopo(par,mer);
}


void Sphere::changeTopo(unsigned int parp, unsigned int mer)
{
387 388 389 390
	if (parp<2)
		parp=2;
	if (mer<2)
		mer=2;
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
	// to obtain right number of slice
	unsigned int par = parp-1;

	unsigned int merfactor=1;
	unsigned int parfactor=1;

	if (mer<8)
		merfactor = 8;
	else if (mer<16)
		merfactor = 4;
	else if (mer<32)
		merfactor = 2;

	if (par<8)
		parfactor = 8;
	else if (par<16)
		parfactor = 4;
	else if (par<32)
		parfactor = 2;

	unsigned int merAll = merfactor * mer;
412
	unsigned int parAll = parfactor* (par+1);
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448

	std::vector<Geom::Vec3f> points;
	points.reserve(parAll*merAll+2);

	for (unsigned int i=0; i<parAll; ++i)
	{
		float beta = float(i+1)*M_PI/float(parAll+1);
		float z = -cos(beta);

		float radius = sin(beta);

		for (unsigned int j=0; j<merAll; ++j)
		{
			float alpha = 2.0f*float(j)*M_PI/float(merAll);
			float x = radius*cos(alpha);
			float y = radius*sin(alpha);
			points.push_back(Geom::Vec3f(x,y,z));
		}
	}
	//poles
	unsigned int north = points.size();
	points.push_back(Geom::Vec3f(0.0f,0.0f,-1.0f));
	unsigned int south = points.size();
	points.push_back(Geom::Vec3f(0.0f,0.0f,1.0f));

	m_vboPos->bind();
	glBufferData(GL_ARRAY_BUFFER, points.size() * sizeof(Geom::Vec3f), &(points[0]), GL_STREAM_DRAW);


	// indices
	std::vector<unsigned int> tableIndices;
	tableIndices.reserve(2*(mer*parAll + par*merAll + 8));


	for (unsigned int i=0; i<par; ++i)
	{
449
		unsigned int k = i*parfactor*merAll + parfactor*merAll;
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
		for (unsigned int j=0; j<merAll; ++j)
		{
			tableIndices.push_back(k+j);
			tableIndices.push_back(k+(j+1)%merAll);
		}
	}


	for (unsigned int i=0; i<mer; ++i)
	{
		tableIndices.push_back(north);
		unsigned int k=i*merfactor;
		tableIndices.push_back(k);
		for (unsigned int j=1; j<parAll; ++j)
		{
			tableIndices.push_back(k);
			k += merAll;
			tableIndices.push_back(k);
		}
		tableIndices.push_back(k);
		tableIndices.push_back(south);
	}

	m_nb=tableIndices.size();
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ind);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_nb*sizeof(GLuint), &(tableIndices[0]), GL_STREAM_DRAW);
}

void Sphere::updatePrecisionDrawing(unsigned int sub, unsigned int sub2)
{
480 481 482
	m_sub1 = sub;
	m_sub2 = sub2;

483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
	if (sub2)
		changeTopo(sub,sub2);
	else
		changeTopo(sub,sub);
}



void Sphere::draw()
{
	m_shader->enableVertexAttribs();
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ind);
	glDrawElements(GL_LINES, m_nb, GL_UNSIGNED_INT, 0);
	m_shader->disableVertexAttribs();
}


500
unsigned int Sphere::pick(const Geom::Vec3f& P, const Geom::Vec3f& V, Geom::Vec3f& I, float epsilon)
501 502 503
{
	float dist = Geom::squaredDistanceLine2Point<Geom::Vec3f>(P,V,V*V, Geom::Vec3f(0.0f,0.0f,0.0f));

504 505
	if (dist > 1.0f)
		return 0;
506

507 508

	I=P;
509
	I.normalize();			// grossiere approximation TODO ameliorer approxim ?
510 511

	return 1;
512 513 514 515 516 517 518 519 520 521 522 523 524 525
}





Cone::Cone(unsigned int par, unsigned int mer)
{
	changeTopo(par,mer);
}


void Cone::changeTopo(unsigned int par, unsigned int mer)
{
526 527 528 529 530
	if (par<2)
		par=2;
	if (mer<2)
		mer=2;

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
	unsigned int merfactor=1;

	if (mer<8)
		merfactor = 8;
	else if (mer<16)
		merfactor = 4;
	else if (mer<32)
		merfactor = 2;

	unsigned int merAll = merfactor * mer;

	std::vector<Geom::Vec3f> points;
	points.reserve(par*merAll+2);

	for (unsigned int i=0; i<par; ++i)
	{
		float radius = 1.0f / float(par) * float(i+1);
		float z = 1.0f - 2.0f*radius;

		for (unsigned int j=0; j<merAll; ++j)
		{
			float alpha = 2.0f*float(j)*M_PI/float(merAll);
			float x = radius*cos(alpha);
			float y = radius*sin(alpha);
			points.push_back(Geom::Vec3f(x,y,z));
		}
	}
	//poles
	points.push_back(Geom::Vec3f(0.0f,0.0f,1.0f));
	points.push_back(Geom::Vec3f(0.0f,0.0f,-1.0f));

	m_vboPos->bind();
	glBufferData(GL_ARRAY_BUFFER, points.size() * sizeof(Geom::Vec3f), &(points[0]), GL_STREAM_DRAW);

	// indices
	std::vector<unsigned int> tableIndices;
	tableIndices.reserve(4*par*mer+4*mer);


	for (unsigned int i=0; i<par; ++i)
	{
		unsigned int k = i*merAll;
		for (unsigned int j=0; j<merAll; ++j)
		{
			tableIndices.push_back(k+j);
			tableIndices.push_back(k+(j+1)%merAll);
		}
	}

	for (unsigned int j=0; j<mer; ++j)
	{
		tableIndices.push_back(par*merAll);
		tableIndices.push_back(j*merfactor + (par-1)*merAll);
		tableIndices.push_back(j*merfactor + (par-1)*merAll);
		tableIndices.push_back(par*merAll+1);

	}


	m_nb=tableIndices.size();
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ind);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_nb*sizeof(GLuint), &(tableIndices[0]), GL_STREAM_DRAW);
}

void Cone::updatePrecisionDrawing(unsigned int sub, unsigned int sub2)
{
597 598 599
	m_sub1 = sub;
	m_sub2 = sub2;

600 601 602 603 604 605 606 607 608 609 610 611
	if (sub2)
		changeTopo(sub,sub2);
	else
		changeTopo(sub,sub);
}







612
unsigned int Cone::pick(const Geom::Vec3f& P, const Geom::Vec3f& V, Geom::Vec3f& I, float epsilon)
613 614 615 616 617 618 619 620 621
{
	Geom::Vec3f Z,Q;
	if (Geom::lineLineClosestPoints<Geom::Vec3f>(P, V, Geom::Vec3f(0.0f,0.0f,0.0f), Geom::Vec3f(0.0f,0.0f,1.0f), Q, Z))
	{
		if ((Q[2]>=-1.0f)&&(Q[2]<=1.0f))
		{
			float dist = Q[0]*Q[0] + Q[1]*Q[1];
			float cdist = (1.0f - Q[2])/2.0f;
			if (dist <= cdist*cdist) // squared !!
622 623
			{
				I = Q; 					// WARNING VERY BAD APPROXIMATON : TODO better approxim.
624
				return 1;
625 626
			}

627 628 629 630
		}
		// else check inter with base
		// Z=-1
		float a = (-1.0f - P[2]) / V[2];
631 632 633 634 635
		I = Geom::Vec3f(P+a*V);
		float dist = I[0]*I[0] + I[1]*I[1];
		if (dist > 1.0f)
			return 0;
		return 1;
636 637 638 639
	}

	// ray in Z direction
	float dist = P[0]*P[0] + P[1]*P[1];
640 641 642 643 644
	if (dist > 1.0f)
		return 0;
	I=P;
	I[2]=-1.0f;
	return 1;
645 646 647 648 649 650 651 652 653 654 655 656 657
}




Cylinder::Cylinder(unsigned int par, unsigned int mer)
{
	changeTopo(par,mer);
}


void Cylinder::changeTopo(unsigned int parp, unsigned int mer)
{
658 659 660 661 662
	if (parp<2)
		parp=2;
	if (mer<2)
		mer=2;

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
	// to obtain right number of slice (with almost same code as sphere)
	unsigned int par = parp+1;

	unsigned int merfactor=1;

	if (mer<8)
		merfactor = 8;
	else if (mer<16)
		merfactor = 4;
	else if (mer<32)
		merfactor = 2;

	unsigned int merAll = merfactor * mer;

	std::vector<Geom::Vec3f> points;
	points.reserve(par*merAll+2);

	for (unsigned int i=0; i<par; ++i)
	{
		float z = -1.0f + 2.0f/float(par-1) * float(i);

		for (unsigned int j=0; j<merAll; ++j)
		{
			float alpha = 2.0f*float(j)*M_PI/float(merAll);
			float x = cos(alpha);
			float y = sin(alpha);
			points.push_back(Geom::Vec3f(x,y,z));
		}
	}
	//poles
	points.push_back(Geom::Vec3f(0.0f,0.0f,1.0f));
	points.push_back(Geom::Vec3f(0.0f,0.0f,-1.0f));

	m_vboPos->bind();
	glBufferData(GL_ARRAY_BUFFER, points.size() * sizeof(Geom::Vec3f), &(points[0]), GL_STREAM_DRAW);

	// indices
	std::vector<unsigned int> tableIndices;
	tableIndices.reserve(4*par*mer+4*mer);


	for (unsigned int i=0; i<par; ++i)
	{
		unsigned int k = i*merAll;
		for (unsigned int j=0; j<merAll; ++j)
		{
			tableIndices.push_back(k+j);
			tableIndices.push_back(k+(j+1)%merAll);
		}
	}

	for (unsigned int j=0; j<mer; ++j)
	{
		tableIndices.push_back(par*merAll);
		tableIndices.push_back(j*merfactor + (par-1)*merAll);
		tableIndices.push_back(j*merfactor + (par-1)*merAll);
		tableIndices.push_back(j*merfactor );
		tableIndices.push_back(j*merfactor );
		tableIndices.push_back(par*merAll+1);

	}


	m_nb=tableIndices.size();
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ind);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_nb*sizeof(GLuint), &(tableIndices[0]), GL_STREAM_DRAW);

}

void Cylinder::updatePrecisionDrawing(unsigned int sub, unsigned int sub2)
{
734 735 736
	m_sub1 = sub;
	m_sub2 = sub2;

737 738 739 740 741 742 743 744
	if (sub2)
		changeTopo(sub,sub2);
	else
		changeTopo(sub,sub);
}



745
unsigned int Cylinder::pick(const Geom::Vec3f& P, const Geom::Vec3f& V,  Geom::Vec3f& I, float epsilon)
746 747 748 749 750 751 752 753
{
	Geom::Vec3f Z,Q;
	if (Geom::lineLineClosestPoints<Geom::Vec3f>(P, V, Geom::Vec3f(0.0f,0.0f,0.0f), Geom::Vec3f(0.0f,0.0f,1.0f), Q, Z))
	{
		if ((Q[2]>=-1.0f)&&(Q[2]<=1.0f))
		{
			float dist = Q[0]*Q[0] + Q[1]*Q[1];
			if (dist < 1.0f)
754 755
			{
				I = Q; 					// WARNING VERY BAD APPROXIMATON : TODO better approxim.
756
				return 1;
757 758
			}

759 760 761 762
		}
		// else check inter with bases
		// Z=1
		float a = (1.0f - P[2]) / V[2];
763 764
		I = Geom::Vec3f(P+a*V);
		float dist = I[0]*I[0] + I[1]*I[1];
765 766 767 768
		if (dist < 1.0f)
			return 1;
		// Z=-1
		a = (-1.0f - P[2]) / V[2];
769 770
		I = Geom::Vec3f(P+a*V);
		dist = I[0]*I[0] + I[1]*I[1];
771 772 773 774 775 776 777 778
		if (dist < 1.0f)
			return 1;
		//else no inter
		return 0;
	}

	// ray in Z direction
	float dist = P[0]*P[0] + P[1]*P[1];
779 780 781 782 783 784 785 786
	if (dist > 1.0f)
		return 0;
	I=P;
	if (V[2]<0.0f)
		I[2]=-1.0f;
	else
		I[2]=1.0f;
	return 1;
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
}







Cube::Cube(unsigned int sub)
{
	changeTopo(sub);
}

void Cube::changeTopo(unsigned int sub)
{
	// subdiv = number of internal points on each edge
	unsigned int subdiv = sub-1;
	std::vector<Geom::Vec3f> points;
	points.reserve(8+12*subdiv);

	points.push_back(Geom::Vec3f(-1.0f,-1.0f,-1.0f));
	points.push_back(Geom::Vec3f( 1.0f,-1.0f,-1.0f));
	points.push_back(Geom::Vec3f( 1.0f, 1.0f,-1.0f));
	points.push_back(Geom::Vec3f(-1.0f, 1.0f,-1.0f));
	points.push_back(Geom::Vec3f(-1.0f,-1.0f, 1.0f));
	points.push_back(Geom::Vec3f( 1.0f,-1.0f, 1.0f));
	points.push_back(Geom::Vec3f( 1.0f, 1.0f, 1.0f));
	points.push_back(Geom::Vec3f(-1.0f, 1.0f, 1.0f));

	for (unsigned int i=0; i< subdiv; ++i)
	{
		float v = -1.0f + float(2*i+2)/float(subdiv+1);

		points.push_back(Geom::Vec3f(-1.0f,-1.0f, v));
		points.push_back(Geom::Vec3f(-1.0f, 1.0f, v));
		points.push_back(Geom::Vec3f( 1.0f, 1.0f, v));
		points.push_back(Geom::Vec3f( 1.0f,-1.0f, v));

		points.push_back(Geom::Vec3f(-1.0f, v,-1.0f));
		points.push_back(Geom::Vec3f(-1.0f, v, 1.0f));
		points.push_back(Geom::Vec3f( 1.0f, v, 1.0f));
		points.push_back(Geom::Vec3f( 1.0f, v,-1.0f));

		points.push_back(Geom::Vec3f(v,-1.0f,-1.0f));
		points.push_back(Geom::Vec3f(v,-1.0f, 1.0f));
		points.push_back(Geom::Vec3f(v, 1.0f, 1.0f));
		points.push_back(Geom::Vec3f(v, 1.0f,-1.0f));
	}

	m_vboPos->bind();
	glBufferData(GL_ARRAY_BUFFER, points.size() * sizeof(Geom::Vec3f), &(points[0]), GL_STREAM_DRAW);

	// indices
	std::vector<unsigned int> tableIndices;
	tableIndices.reserve(24+24*subdiv);

	for (unsigned int i=0; i<4; ++i)
	{
		tableIndices.push_back(i);
		tableIndices.push_back((i+1)%4);
		tableIndices.push_back(4 + i);
		tableIndices.push_back(4 + (i+1)%4);
		tableIndices.push_back(i);
		tableIndices.push_back(4 + i);
	}
	for (unsigned int i=0; i< subdiv; ++i)
	{
		for (unsigned int j=0; j< 3; ++j) // direction X Y Z (or edge)
		{
			for (unsigned int k=0; k< 4; ++k)	// turn around cube
			{
				tableIndices.push_back(8 + i*12 + (j*4) + k);
				tableIndices.push_back(8 + i*12 + (j*4) + (k+1)%4);
			}
		}
	}

	m_nb=tableIndices.size();
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ind);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_nb*sizeof(GLuint), &(tableIndices[0]), GL_STREAM_DRAW);
}


void Cube::updatePrecisionDrawing(unsigned int sub, unsigned int sub2)
{
872 873 874
	m_sub1 = sub;
	m_sub2 = sub2;

875 876 877 878 879 880 881 882 883 884 885 886 887 888
	changeTopo(sub);
}



void Cube::draw()
{
	m_shader->enableVertexAttribs();
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ind);
	glDrawElements(GL_LINES, m_nb, GL_UNSIGNED_INT, 0);
	m_shader->disableVertexAttribs();
}


889
unsigned int Cube::pick(const Geom::Vec3f& P, const Geom::Vec3f& V, Geom::Vec3f& I, float epsilon)
890 891 892 893 894 895 896
{

//	// firs quick picking with bounding sphere
	float dist2 = Geom::squaredDistanceLine2Point<Geom::Vec3f>(P,V,V*V, Geom::Vec3f(0.0f,0.0f,0.0f));
	if (dist2 > 3.0f)
		return 0;

897 898 899 900 901
	I=P;
	I.normalize();			// grossiere approximation TODO amelioerer approxim ?

	std::vector<Geom::Vec3f> intersections;

902 903
	for (unsigned int i=0; i<3; ++i)
	{
904
		if (fabs(V[i])>=0.0000001f)
905 906 907
		{
			float a = (-1.0f-P[i])/V[i];
			Geom::Vec3f Q = Geom::Vec3f(P+a*V);	// intersection with plane z=0
908
			if ( (fabs(Q[(i+1)%3])<=1.0f) && (fabs(Q[(i+2)%3])<=1.0f) )
909 910 911
				return 1;
			a = (1.0f-P[i])/V[i];
			Q = Geom::Vec3f(P+a*V);	// intersection with plane z=0
912
			if ( (fabs(Q[(i+1)%3])<=1.0f) && (fabs(Q[(i+2)%3])<=1.0f) )
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
				return 1;
		}
	}

	return 0;
}




IcoSphere::IcoSphere(unsigned int sub):
		m_sub(0xffffffff)
{
	changeTopo(sub);
}

unsigned int IcoSphere::insertPoint(std::vector<Geom::Vec3f>& points, const Geom::Vec3f& P)
{
	for (unsigned int i=0; i< points.size();++i)
		if (((P-points[i]).norm2())< 0.00000001f)
			return i;
	points.push_back(P);
	return points.size()-1;
}

void IcoSphere::subdivide(std::vector<unsigned int>& triangles, std::vector<Geom::Vec3f>& points)
{
	std::vector<unsigned int> newTriangles;
	newTriangles.reserve(triangles.size()*4);

	unsigned int nbtris = triangles.size()/3;

	for(unsigned int t=0; t<nbtris;++t)
	{
		const Geom::Vec3f& Pa =points[triangles[3*t]];
		const Geom::Vec3f& Pb =points[triangles[3*t+1]];
		const Geom::Vec3f& Pc =points[triangles[3*t+2]];

		Geom::Vec3f Pab =(Pa+Pb)/2.0f;
		Pab.normalize();
		Geom::Vec3f Pac =(Pa+Pc)/2.0f;
		Pac.normalize();
		Geom::Vec3f Pbc =(Pb+Pc)/2.0f;
		Pbc.normalize();

		unsigned int iAB = insertPoint(points,Pab);
		unsigned int iAC = insertPoint(points,Pac);
		unsigned int iBC = insertPoint(points,Pbc);

		newTriangles.push_back(triangles[3*t]); //Pa
		newTriangles.push_back(iAB); //Pab
		newTriangles.push_back(iAC); //Pac

		newTriangles.push_back(triangles[3*t+1]); //Pb
		newTriangles.push_back(iBC); //Pbc
		newTriangles.push_back(iAB); //Pab

		newTriangles.push_back(triangles[3*t+2]); //Pc
		newTriangles.push_back(iAC); //Pac
		newTriangles.push_back(iBC); //Pbc

		newTriangles.push_back(iAB); //Pab
		newTriangles.push_back(iBC); //Pbc
		newTriangles.push_back(iAC); //Pac
	}

	triangles.swap(newTriangles);
}

void IcoSphere::changeTopo(unsigned int sub)
{
984 985
	if (sub<2)
		sub=2;
thery's avatar
thery committed
986
	int subd = int(log(0.5*sub)/log(2.0))-1;
987 988 989 990 991 992 993 994 995 996 997 998 999
	if (subd<0)
		subd=0;

	changeTopoSubdivision(subd);
}

void IcoSphere::changeTopoSubdivision(unsigned int sub)
{
	if (m_sub == sub)
		return;

	m_sub = sub;

thery's avatar
thery committed
1000
	unsigned int subEdge = (unsigned int)(powf(2.0f,4.0f-sub));
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139

	std::vector<Geom::Vec3f> points;
	points.reserve(10000);

	unsigned int uitriangles[60]={0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11,
			1, 5, 9, 5, 11, 4, 11, 10, 2, 10, 7, 6, 7, 1, 8,
			3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9,
			4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7, 9, 8, 1 };

	std::vector<unsigned int> triangles;
	triangles.reserve(60);
	for (unsigned int i=0; i<60; ++i)
		triangles.push_back(uitriangles[i]);


	// create 12 vertices of an icosahedron
	float t = (1.0f + sqrtf(5.0f)) / 2.0f;

	points.push_back(Geom::Vec3f(-1,  t,  0));
	points.push_back(Geom::Vec3f( 1,  t,  0));
	points.push_back(Geom::Vec3f(-1, -t,  0));
	points.push_back(Geom::Vec3f( 1, -t,  0));
	points.push_back(Geom::Vec3f( 0, -1,  t));
	points.push_back(Geom::Vec3f( 0,  1,  t));
	points.push_back(Geom::Vec3f( 0, -1, -t));
	points.push_back(Geom::Vec3f( 0,  1, -t));
	points.push_back(Geom::Vec3f( t,  0, -1));
	points.push_back(Geom::Vec3f( t,  0,  1));
	points.push_back(Geom::Vec3f(-t,  0, -1));
	points.push_back(Geom::Vec3f(-t,  0,  1));

	for (std::vector<Geom::Vec3f>::iterator pt=points.begin(); pt!=points.end(); ++pt)
		pt->normalize();

	// subdivide
	for (unsigned int i=0; i<sub; ++i)
		subdivide(triangles, points);


	// add some vertices on edges for nice round edges
	unsigned int idxNewPoints=points.size();

	for (unsigned int i=0; i<triangles.size()/3; ++i)
	{
		unsigned int a = triangles[3*i];
		unsigned int b = triangles[3*i+1];
		unsigned int c = triangles[3*i+2];

		if (a < b)
		{
			for(unsigned int i=0; i < subEdge; ++i)
			{
				float x = float(i+1)/float(subEdge+1);
				Geom::Vec3f P(x*points[b] + (1.0f-x)*points[a]);
				P.normalize();
				points.push_back(P);
			}
		}
		if (b < c)
		{
			for(unsigned int i=0; i < subEdge; ++i)
			{
				float x = float(i+1)/float(subEdge+1);
				Geom::Vec3f P(x*points[c] + (1.0f-x)*points[b]);
				P.normalize();
				points.push_back(P);
			}

		}
		if (c < a)
		{
			for(unsigned int i=0; i < subEdge; ++i)
			{
				float x = float(i+1)/float(subEdge+1);
				Geom::Vec3f P(x*points[a] + (1.0f-x)*points[c]);
				P.normalize();
				points.push_back(P);
			}
		}
	}

	// send buffer
	m_vboPos->bind();
	glBufferData(GL_ARRAY_BUFFER, points.size() * sizeof(Geom::Vec3f), &(points[0]), GL_STREAM_DRAW);

	// indices
	std::vector<unsigned int> tableIndices;
	tableIndices.reserve(triangles.size()/2 + 1000);

	unsigned int k=0;
	for (unsigned int i=0; i<triangles.size()/3; ++i)
	{
		unsigned int a = triangles[3*i];
		unsigned int b = triangles[3*i+1];
		unsigned int c = triangles[3*i+2];

		if (a < b)
		{
			tableIndices.push_back(a);
			for(unsigned int j=0; j < subEdge; ++j)
			{
				tableIndices.push_back(idxNewPoints+subEdge*k+j);
				tableIndices.push_back(idxNewPoints+subEdge*k+j);
			}
			tableIndices.push_back(b);
			k++;
		}
		if (b < c)
		{
			tableIndices.push_back(b);
			for(unsigned int j=0; j < subEdge; ++j)
			{
				tableIndices.push_back(idxNewPoints+subEdge*k+j);
				tableIndices.push_back(idxNewPoints+subEdge*k+j);
			}
			tableIndices.push_back(c);
			k++;
		}
		if (c < a)
		{
			tableIndices.push_back(c);
			for(unsigned int j=0; j < subEdge; ++j)
			{
				tableIndices.push_back(idxNewPoints+subEdge*k+j);
				tableIndices.push_back(idxNewPoints+subEdge*k+j);
			}
			tableIndices.push_back(a);
			k++;
		}
	}

	m_nb=tableIndices.size();
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ind);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_nb*sizeof(GLuint), &(tableIndices[0]), GL_STREAM_DRAW);
}


void IcoSphere::updatePrecisionDrawing(unsigned int sub, unsigned int sub2)
{
1140 1141
	m_sub1 = sub;
	m_sub2 = sub2;
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
	changeTopo(sub);
}



}
}