tuto4.cpp 7.4 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 "tuto4.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
28

29
#include "Topology/generic/parameters.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
30
31
32
33
34
35
36
37
#include "Topology/map/map2.h"

#include "Geometry/matrix.h"
#include "Geometry/vector_gen.h"
#include "Algo/Import/import.h"
#include "Algo/Geometry/boundingbox.h"
#include "Topology/generic/cellmarker.h"

38
39
40
41
42
#include "Algo/Render/GL2/mapRender.h"
#include "Algo/Render/GL2/topoRender.h"
#include "Utils/shaderSimpleColor.h"
#include "Utils/shaderFlat.h"

Sylvain Thery's avatar
Sylvain Thery committed
43
#include "Utils/cgognStream.h"
44

Pierre Kraemer's avatar
Pierre Kraemer committed
45
46
using namespace CGoGN ;

47
48
struct PFP: public PFP_STANDARD
{
Pierre Kraemer's avatar
Pierre Kraemer committed
49
50
51
52
53
54
55
56
57
58
59
	// definition de la carte
	typedef Map2 MAP;
};

PFP::MAP myMap;
SelectorTrue allDarts ;

PFP::TVEC3 position ;
PFP::TVEC3 normal ;
AttributeHandler<Geom::Vec4f> color ;

60
void MyQT::cb_initGL()
Pierre Kraemer's avatar
Pierre Kraemer committed
61
{
62
	// choose to use GL version 2
63
	Utils::GLSLShader::setCurrentOGLVersion(2);
Pierre Kraemer's avatar
Pierre Kraemer committed
64

65
66
	// create the render
	m_render = new Algo::Render::GL2::MapRender();
Pierre Kraemer's avatar
Pierre Kraemer committed
67

68
	m_render_topo = new Algo::Render::GL2::TopoRenderMapD();
Pierre Kraemer's avatar
Pierre Kraemer committed
69

70
71
	// create VBO for position
	m_positionVBO = new Utils::VBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
72

73
74
75
76
	// using simple shader with color
	m_shader = new Utils::ShaderSimpleColor();
	m_shader->setAttributePosition(m_positionVBO);
	m_shader->setColor(Geom::Vec4f(0.,1.,0.,0.));
Pierre Kraemer's avatar
Pierre Kraemer committed
77

78
79
80
	m_shader2 = new Utils::ShaderFlat();
	m_shader2->setAttributePosition(m_positionVBO);

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
81
82
	registerShader(m_shader);
	registerShader(m_shader2);
Pierre Kraemer's avatar
Pierre Kraemer committed
83
84
}

85
void MyQT::cb_redraw()
Pierre Kraemer's avatar
Pierre Kraemer committed
86
{
87
88
89
90
	if(render_topo)
	{
		m_render_topo->drawTopo();
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
91
92
93

	if(render_obj)
	{
94
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
Pierre Kraemer's avatar
Pierre Kraemer committed
95
96
97

		glEnable( GL_POLYGON_OFFSET_FILL );
		glPolygonOffset( 1.0f, 1.0f );
98
99
		m_render->draw(m_shader2, Algo::Render::GL2::TRIANGLES);
		glDisable(GL_POLYGON_OFFSET_FILL);
Pierre Kraemer's avatar
Pierre Kraemer committed
100
	}
101
102
103
104
105
106
}


void MyQT::cb_keyPress(int code)
{
	switch(code)
Pierre Kraemer's avatar
Pierre Kraemer committed
107
	{
108
109
110
111
112
113
114
115
116
	case 'o':
		render_obj = !render_obj;
		updateGL();
		break;

	case 't':
		render_topo = !render_topo;
		updateGL();
		break;
Pierre Kraemer's avatar
Pierre Kraemer committed
117
	}
118
119
120
121
122
123
124
}

void MyQT::button_compile()
{
	QString st1 = dynamic_cast<Utils::QT::uiDockInterface*>(dockWidget())->vertexEdit->toPlainText();
	QString st2 = dynamic_cast<Utils::QT::uiDockInterface*>(dockWidget())->fragmentEdit->toPlainText();
	QString st3 = dynamic_cast<Utils::QT::uiDockInterface*>(dockWidget())->geometryEdit->toPlainText();
Pierre Kraemer's avatar
Pierre Kraemer committed
125

126
127
128
129
130
131
132
133
134
135
136
137
	m_shader2->reloadVertexShaderFromMemory(st1.toStdString().c_str());
	m_shader2->reloadFragmentShaderFromMemory(st2.toStdString().c_str());
	m_shader2->reloadGeometryShaderFromMemory(st3.toStdString().c_str());

	m_shader2->recompile();
	updateGLMatrices();
}

void MyQT::slider_explode(int x)
{
	m_shader2->setExplode(0.01*x);
	updateGL();
Pierre Kraemer's avatar
Pierre Kraemer committed
138
139
140
141
142
}

int main(int argc, char **argv)
{
	/// Utilisation des Marker
143
	position = myMap.addAttribute<Geom::Vec3f>(VERTEX, "position");
Pierre Kraemer's avatar
Pierre Kraemer committed
144

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
	Algo::Modelisation::Polyhedron<PFP> prim3(myMap, position);
	Dart d2 = prim3.tore_topo(16, 24);
	prim3.embedTore(1.0f,0.3f);

//	position[d2] = PFP::VEC3(0.0f, 0.0f, 0.0f);
//	d2 = myMap.phi1(d2);
//	position[d2] = PFP::VEC3(2.0f, 0.0f, 0.0f);
//	d2 = myMap.phi1(d2);
//	position[d2] = PFP::VEC3(1.0f, 3.0f, 0.0f);
//	d2 = myMap.phi1(d2);
//	d3 = myMap.phi<11>(d3);
//	position[d3] = PFP::VEC3(0.0f, -2.0f, 0.0f);
//	d3 = myMap.phi1(d3);
//	position[d3] = PFP::VEC3(2.0f, -2.0f, 0.0f);
//	d3 = myMap.phi1(d3);
Pierre Kraemer's avatar
Pierre Kraemer committed
160

161
162
//	unsigned int idAttV2 = myMap.addAttribute<float>(VERTEX,"reel");
//	PFP::AttributeHandler<float> tableReels(idAttV2,VERTEX,myMap);
Pierre Kraemer's avatar
Pierre Kraemer committed
163
164
165
166
167
168
//
//	tableReels[d2] = 3.5f;
//	tableReels[myMap.phi1(d2)] = 3.7f;
//
//	// Attention ici on cree un attribut de FACE donc les face sont maintenant plangees
//	// l'attribut cree se detruira a la fin de la portee de l'objet
169
//	PFP::MAP::AutoAttributeHandler<Geom::Vec3f> tableRGB(myMap, FACE, "RGB"); // "RGB" optionnel
Pierre Kraemer's avatar
Pierre Kraemer committed
170
171
172
173
174
175
//
//	tableRGB[d3] = Geom::Vec3f(1.0f,2.0f,3.0f);
//
//	// acces par les brins
//	for (Dart d = myMap.begin(); d!= myMap.end(); myMap.next(d))
//	{
176
//		CGoGNout << "Brin "<<d.label()<< " reel="<<tableReels[d]<< "  RGB="<<tableRGB[d]<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
177
178
179
180
181
//	}
//
//	//acces direct par balayge du tableau
//	for (unsigned int id = tableRGB.begin(); id != tableRGB.end(); tableRGB.next(id))
//	{
182
//		CGoGNout << "RGB["<<id<<"] = "<<tableRGB.at(id)<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
183
184
//	}

185
	CellMarkerStore cm(myMap, VERTEX);
Pierre Kraemer's avatar
Pierre Kraemer committed
186

187
	Dart d3 = myMap.phi1(d2);
Pierre Kraemer's avatar
Pierre Kraemer committed
188
189
190
191
	cm.mark(d2);
	cm.mark(d3);
	cm.unmarkAll();

192
193
194
195
196
197
198
	// interface:
	QApplication app(argc, argv);
	MyQT sqt;

	// interface de tuto5.ui
    Utils::QT::uiDockInterface dock;
    sqt.setDock(&dock);
Pierre Kraemer's avatar
Pierre Kraemer committed
199

200
201
202
203
	// message d'aide
	sqt.setHelpMsg("Tuto4");

    //  bounding box
Pierre Kraemer's avatar
Pierre Kraemer committed
204
    Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
    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
	sqt.setParamObject(lWidthObj,lPosObj.data());

	sqt.setCallBack( dock.compileButton, SIGNAL(clicked()), SLOT(button_compile()) );
	sqt.setCallBack( dock.explodeSlider, SIGNAL(valueChanged(int)), SLOT(slider_explode(int)) );

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

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

	// update des primitives du renderer
	SelectorTrue allDarts;
	sqt.m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::TRIANGLES);
	sqt.m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::LINES);
Pierre Kraemer's avatar
Pierre Kraemer committed
224

225
	sqt.m_render_topo->updateData<PFP>(myMap, position, 0.9f, 0.9f);
Pierre Kraemer's avatar
Pierre Kraemer committed
226

227
228
229
230
231
	dock.vertexEdit->setPlainText(QString(sqt.m_shader2->getVertexShaderSrc()));
	dock.fragmentEdit->setPlainText(QString(sqt.m_shader2->getFragmentShaderSrc()));
	dock.geometryEdit->setPlainText(QString(sqt.m_shader2->getGeometryShaderSrc()));
	// show final pour premier redraw
	sqt.show();
Pierre Kraemer's avatar
Pierre Kraemer committed
232

233
234
	CGoGNout.toConsole(&sqt);
	CGoGNerr.toConsole(&sqt);
235

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