tuto4.cpp 7.45 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 47

using namespace CGoGN ;

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

PFP::MAP myMap;
SelectorTrue allDarts ;

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

61 62

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

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

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

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

75 76 77 78
	// 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
79

80 81 82 83 84
	m_shader2 = new Utils::ShaderFlat();
	m_shader2->setAttributePosition(m_positionVBO);

	registerRunning(m_shader);
	registerRunning(m_shader2);
Pierre Kraemer's avatar
Pierre Kraemer committed
85 86 87 88

}


89
void MyQT::cb_redraw()
Pierre Kraemer's avatar
Pierre Kraemer committed
90 91
{

92 93 94 95
	if(render_topo)
	{
		m_render_topo->drawTopo();
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
96 97 98

	if(render_obj)
	{
99
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
Pierre Kraemer's avatar
Pierre Kraemer committed
100 101 102

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

107 108 109 110 111 112
}


void MyQT::cb_keyPress(int code)
{
	switch(code)
Pierre Kraemer's avatar
Pierre Kraemer committed
113
	{
114 115 116 117 118 119 120 121 122 123
	case 'o':
		render_obj = !render_obj;
		updateGL();
		break;

	case 't':
		render_topo = !render_topo;
		updateGL();
		break;

Pierre Kraemer's avatar
Pierre Kraemer committed
124
	}
125 126 127 128 129 130 131
}

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
132 133


134 135 136 137 138 139 140 141 142 143 144 145 146

	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
147 148 149 150 151 152 153 154 155 156
}



int main(int argc, char **argv)
{
	/// Utilisation des Marker

	position = myMap.addAttribute<Geom::Vec3f>(VERTEX_ORBIT, "position");

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	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
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

//	unsigned int idAttV2 = myMap.addAttribute<float>(VERTEX_ORBIT,"reel");
//	PFP::AttributeHandler<float> tableReels(idAttV2,VERTEX_ORBIT,myMap);
//
//	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
//	PFP::MAP::AutoAttributeHandler<Geom::Vec3f> tableRGB(myMap, FACE_ORBIT, "RGB"); // "RGB" optionnel
//
//	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))
//	{
//		std::cout << "Brin "<<d.label()<< " reel="<<tableReels[d]<< "  RGB="<<tableRGB[d]<<std::endl;
//	}
//
//	//acces direct par balayge du tableau
//	for (unsigned int id = tableRGB.begin(); id != tableRGB.end(); tableRGB.next(id))
//	{
//		std::cout << "RGB["<<id<<"] = "<<tableRGB.at(id)<<std::endl;
//	}

	CellMarkerStore cm(myMap, VERTEX_ORBIT);

199
	Dart d3 = myMap.phi1(d2);
Pierre Kraemer's avatar
Pierre Kraemer committed
200 201 202 203 204
	cm.mark(d2);
	cm.mark(d3);
	cm.unmarkAll();


205 206 207 208 209 210 211
	// 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
212

213 214 215 216
	// message d'aide
	sqt.setHelpMsg("Tuto4");

    //  bounding box
Pierre Kraemer's avatar
Pierre Kraemer committed
217
    Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    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
238

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


242 243 244 245 246
	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
247

248 249 250
	CGoGNout.out2Console(&sqt);
	CGoGNerr.out2Console(&sqt);

251 252
	// et on attend la fin.
	return app.exec();
Pierre Kraemer's avatar
Pierre Kraemer committed
253 254 255


}