tuto3.cpp 7.28 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer 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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, 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: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <iostream>

27
#include "Topology/generic/parameters.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
28 29 30 31 32
#include "Topology/map/map2.h"
#include "Topology/generic/cellmarker.h"

#include "Geometry/vector_gen.h"
#include "Algo/Geometry/boundingbox.h"
33 34 35 36 37
#include "Algo/Render/GL2/mapRender.h"
#include "Utils/shaderSimpleColor.h"
#include "Utils/shaderColorPerVertex.h"

#include "tuto3.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
38

39 40
MyQT* sqt1_ptr;
MyQT* sqt2_ptr;
Pierre Kraemer's avatar
Pierre Kraemer committed
41 42 43

using namespace CGoGN ;

44 45
struct PFP: public PFP_STANDARD
{
Pierre Kraemer's avatar
Pierre Kraemer committed
46 47 48 49
	// definition de la carte
	typedef Map2 MAP;
};

50
void MyQT::cb_initGL()
Pierre Kraemer's avatar
Pierre Kraemer committed
51
{
52 53
	// choose to use GL version 2
	Utils::GLSLShader::setCurrentOGLVersion(2);
Pierre Kraemer's avatar
Pierre Kraemer committed
54

55 56
	// create the render
	m_render = new Algo::Render::GL2::MapRender();
Pierre Kraemer's avatar
Pierre Kraemer committed
57

58 59 60
	// create VBO for position
	m_positionVBO = new Utils::VBO();
	m_colorVBO = new Utils::VBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
61

62 63 64 65 66 67 68 69 70
	// using simple shader with color
	m_shader = new Utils::ShaderSimpleColor();
	m_shader->setAttributePosition(m_positionVBO);


	m_shader2 = new Utils::ShaderColorPerVertex();
	m_shader2->setAttributePosition(m_positionVBO);
	m_shader2->setAttributeColor(m_colorVBO);

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
71 72
	registerShader(m_shader);
	registerShader(m_shader2);
73
}
Pierre Kraemer's avatar
Pierre Kraemer committed
74

75
void MyQT::cb_redraw()
Pierre Kraemer's avatar
Pierre Kraemer committed
76
{
77
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
Pierre Kraemer's avatar
Pierre Kraemer committed
78

79 80 81
	glLineWidth(2.0f);
	m_shader->setColor(Geom::Vec4f(1.,1.,0.,0.));
	m_render->draw(m_shader, Algo::Render::GL2::LINES);
Pierre Kraemer's avatar
Pierre Kraemer committed
82

83 84 85 86 87
	glEnable(GL_POLYGON_OFFSET_FILL);
	glPolygonOffset(1.0f, 1.0f);
	m_render->draw(m_shader2, Algo::Render::GL2::TRIANGLES);
	glDisable(GL_POLYGON_OFFSET_FILL);
}
Pierre Kraemer's avatar
Pierre Kraemer committed
88

89 90 91 92 93 94 95 96 97 98 99
void MyQT::cb_keyPress(int code)
{
	switch(code)
	{
	case 's':
		if (this == sqt2_ptr)
			sqt1_ptr->synchronize(sqt2_ptr);
		if (this == sqt1_ptr)
			sqt2_ptr->synchronize(sqt1_ptr);
		break;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
100 101 102 103
}

int main(int argc, char **argv)
{
104 105 106 107 108 109
	PFP::MAP myMap;
	SelectorTrue allDarts;

	PFP::TVEC3 position ;
	PFP::TVEC3 normal ;
	AttributeHandler<Geom::Vec3f> color ;
Pierre Kraemer's avatar
Pierre Kraemer committed
110 111 112 113 114

	Dart d2 = myMap.newOrientedFace(3);
	Dart d3 = myMap.newOrientedFace(4);
	myMap.sewFaces(d2,d3);

115 116
	position = myMap.addAttribute<PFP::VEC3>(VERTEX, "position");
	color = myMap.addAttribute<PFP::VEC3>(VERTEX, "couleur");
Pierre Kraemer's avatar
Pierre Kraemer committed
117 118

	position[d2] = PFP::VEC3(0.0f, 0.0f, 0.0f);
119
	color[d2] = PFP::VEC3(1.0f, 0.0f, 0.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
120 121
	d2 = myMap.phi1(d2);
	position[d2] = PFP::VEC3(2.0f, 0.0f, 0.0f);
122
	color[d2] = PFP::VEC3(0.0f, 1.0f, 0.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
123 124
	d2 = myMap.phi1(d2);
	position[d2] = PFP::VEC3(1.0f, 3.0f, 0.0f);
125
	color[d2] = PFP::VEC3(0.0f, 0.0f, 1.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
126 127 128
	d2 = myMap.phi1(d2);
	d3 = myMap.phi<11>(d3);
	position[d3] = PFP::VEC3(0.0f, -2.0f, 0.0f);
129
	color[d3] = PFP::VEC3(1.0f, 0.0f, 1.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
130 131
	d3 = myMap.phi1(d3);
	position[d3] = PFP::VEC3(2.0f, -2.0f, 0.0f);
132
	color[d3] = PFP::VEC3(0.0f, 1.0f, 1.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
133 134 135 136 137 138 139 140 141
	d3 = myMap.phi1(d3);

	//MARK !!!

	// on reserve des marqueur de brins
	DartMarkerStore mf(myMap);
	DartMarkerStore me(myMap);

	// avec lesquels on peut marquer des orbits
142 143
	mf.markOrbit(FACE, d2);
	me.markOrbit(EDGE, d2);
Pierre Kraemer's avatar
Pierre Kraemer committed
144 145

	// les sommet sont plonges, on peut utiliser un marqueur de cellule sommet
146
	CellMarker cm(myMap, VERTEX);
Pierre Kraemer's avatar
Pierre Kraemer committed
147 148 149 150 151
	cm.mark(d3);

	for (Dart d = myMap.begin(); d!= myMap.end(); myMap.next(d))
	{
		if (me.isMarked(d))
152
			CGoGNout << "Dart "<< d.label() << " marque par me"<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
153
		if (mf.isMarked(d))
154
			CGoGNout << "Dart "<< d.label() << " marque par mf"<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
155
		if (cm.isMarked(d))
156
			CGoGNout << "Sommet de dart "<< d.label() << " marque par mcv"<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
157 158 159 160 161
	}

	//nettoyage
	cm.unmarkAll();

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
162
	// interface:
163 164 165
	QApplication app(argc, argv);

	MyQT sqt;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
166
	sqt1_ptr = &sqt;
167 168

	MyQT sqt2;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
169
	sqt2_ptr = &sqt2;
170 171 172 173 174 175 176

	// message d'aide
	sqt.setHelpMsg("Tuto3:\n"
			"marker tuto\n"
			"using two VBO & two shader");

    //  bounding box
Pierre Kraemer's avatar
Pierre Kraemer committed
177
    Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
178 179 180 181
    float lWidthObj = std::max<PFP::REAL>(std::max<PFP::REAL>(bb.size(0), bb.size(1)), bb.size(2));
    Geom::Vec3f lPosObj = (bb.min() +  bb.max()) / PFP::REAL(2);

    // envoit info BB a l'interface
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
182
	sqt.setParamObject(lWidthObj, lPosObj.data());
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

	// show 1 pour GL context
	sqt.show();

	// update du VBO position (context GL necessaire)
	sqt.m_positionVBO->updateData(position);
	// update du VBO color
	sqt.m_colorVBO->updateData(color);

	// update des primitives du renderer
	sqt.m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::TRIANGLES);
	sqt.m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::LINES);

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

	// et pour le fun une deuxieme carte et une deuxieme interface:

	PFP::MAP myMap2;
	PFP::TVEC3 position2 ;
	AttributeHandler<Geom::Vec3f> color2 ;

	Dart dx = myMap2.newOrientedFace(4);

207 208
	position2 = myMap2.addAttribute<PFP::VEC3>(VERTEX, "position");
	color2 = myMap2.addAttribute<PFP::VEC3>(VERTEX, "couleur");
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

	position2[dx] = PFP::VEC3(0.0f, 0.0f, 0.0f);
	color2[dx] = PFP::VEC3(1.0f, 1.0f, 0.0f);
	dx = myMap.phi1(dx);
	position2[dx] = PFP::VEC3(2.0f, 0.0f, 0.0f);
	color2[dx] = PFP::VEC3(0.0f, 1.0f, 0.0f);
	dx = myMap.phi1(dx);
	position2[dx] = PFP::VEC3(2.0f, 2.0f, 0.0f);
	color2[dx] = PFP::VEC3(1.0f, 0.0f, 1.0f);
	dx = myMap.phi1(dx);
	position2[dx] = PFP::VEC3(0.0f, 2.0f, 0.0f);
	color2[dx] = PFP::VEC3(0.0f, 1.0f, 1.0f);

	sqt2.setHelpMsg("Fenetre 2!!");

	sqt2.setParamObject(lWidthObj,lPosObj.data());

	// show 1 pour GL context
	sqt2.show();

	// update du VBO position (context GL necessaire)
	sqt2.m_positionVBO->updateData(position2);
	// update du VBO color
	sqt2.m_colorVBO->updateData(color2);
Pierre Kraemer's avatar
Pierre Kraemer committed
233

234 235 236
	// update des primitives du renderer
	sqt2.m_render->initPrimitives<PFP>(myMap2, allDarts, Algo::Render::GL2::TRIANGLES);
	sqt2.m_render->initPrimitives<PFP>(myMap2, allDarts, Algo::Render::GL2::LINES);
Pierre Kraemer's avatar
Pierre Kraemer committed
237

238 239
	// et on attend la fin.
	return app.exec();
Pierre Kraemer's avatar
Pierre Kraemer committed
240
}