tuto1.cpp 6.16 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
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.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
24

25
#include "tuto1.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
26
27
28

#include <iostream>

29
#include "Topology/generic/parameters.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
30
#include "Topology/map/embeddedMap2.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
31
32
#include "Geometry/vector_gen.h"

33

Pierre Kraemer's avatar
Pierre Kraemer committed
34
35
36
#include "Algo/Import/import.h"
#include "Algo/Geometry/boundingbox.h"

Sylvain Thery's avatar
Sylvain Thery committed
37
#include "Algo/Render/GL2/mapRender.h"
38
#include "Utils/Shaders/shaderSimpleColor.h"
Sylvain Thery's avatar
Sylvain Thery committed
39

40
41
#include <glm/gtc/type_ptr.hpp>

42
43
44
#include "Algo/Render/SVG/mapSVGRender.h"


Pierre Kraemer's avatar
Pierre Kraemer committed
45
46
47
48
49
50
using namespace CGoGN ;

/**
 * Struct that contains some informations about the types of the manipulated objects
 * Mainly here to be used by the algorithms that are parameterized by it
 */
51
struct PFP: public PFP_STANDARD
Pierre Kraemer's avatar
Pierre Kraemer committed
52
53
{
	// definition of the type of the map
Pierre Kraemer's avatar
Pierre Kraemer committed
54
	typedef EmbeddedMap2 MAP;
Pierre Kraemer's avatar
Pierre Kraemer committed
55
56
};

57
58
59
60
61
// declaration of the map
PFP::MAP myMap;
// and attribute of position
AttributeHandler<PFP::VEC3> position;

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

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

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

Sylvain Thery's avatar
Sylvain Thery committed
73
74
	// using simple shader with color
	m_shader = new Utils::ShaderSimpleColor();
75
	m_shader->setAttributePosition(m_positionVBO);
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
76
77
	m_shader->setColor(Geom::Vec4f(0.0f, 1.0f, 0.0f, 0.0f));
	registerShader(m_shader);
Sylvain Thery's avatar
Sylvain Thery committed
78
79
80
81
}

void MyQT::cb_redraw()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
82
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
83
	glEnable(GL_CULL_FACE);
Sylvain Thery's avatar
Sylvain Thery committed
84
85
86
87
88
89
90
	glEnable(GL_LIGHTING);
	if (m_shader)
	{
		glLineWidth(2.0f);
		m_shader->setColor(Geom::Vec4f(1.,1.,0.,0.));
		m_render->draw(m_shader, Algo::Render::GL2::LINES);

91
92
93
94
95
		m_shader->setColor(Geom::Vec4f(0.,1.,1.,0.));
		m_render->draw(m_shader, Algo::Render::GL2::BOUNDARY);



Sylvain Thery's avatar
Sylvain Thery committed
96
97
98
99
100
		glEnable(GL_POLYGON_OFFSET_FILL);
		glPolygonOffset(1.0f, 1.0f);

		m_shader->setColor(Geom::Vec4f(0.,1.,0.,0.));
		m_render->draw(m_shader, Algo::Render::GL2::TRIANGLES);
Pierre Kraemer's avatar
Pierre Kraemer committed
101

Sylvain Thery's avatar
Sylvain Thery committed
102
103
104
105
106
107
108
		glDisable(GL_POLYGON_OFFSET_FILL);
	}
}

void MyQT::cb_keyPress(int code)
{
	if ((code >65) && (code< 123 ))
109
		CGoGNout << " key char " << char(code) << "pressed"<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
110

Sylvain Thery's avatar
Sylvain Thery committed
111
	if ((code >'0') && (code<='9'))
112
		CGoGNout << " key num " << code-'0' << "pressed"<< CGoGNendl;
113
114
115
116
117
118
119
120
121
122
123

	if (code  == 's')
	{
		std::string filename = selectFileSave("Export SVG file ");
		CGoGNout << "Exporting "<<filename<<CGoGNendl;
		Algo::Render::SVG::SVGOut svg(filename,modelViewMatrix(),projectionMatrix());
		svg.renderLinesToSVG<PFP>(myMap,position);
		svg.setColor(Geom::Vec3f(0.7f,0.0f,0.4f));
		svg.renderFacesToSVG<PFP>(myMap,position,0.8f);
		//svg destruction close the file
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
}

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

	// creation of 2 new faces: 1 triangle and 1 square
	Dart d1 = myMap.newFace(3);
	Dart d2 = myMap.newFace(4);

	// sew these faces along one of their edge
	myMap.sewFaces(d1, d2);

	// creation of a new attribute on vertices of type 3D vector
	// a handler to this attribute is returned
138
	position = myMap.addAttribute<PFP::VEC3>(VERTEX, "position");
Pierre Kraemer's avatar
Pierre Kraemer committed
139
140
141
142
143
144
145
146
147

	// affect a position to the vertices of the mesh
	position[d1] = PFP::VEC3(0, 0, 0);
	position[myMap.phi1(d1)] = PFP::VEC3(2, 0, 0);
	position[myMap.phi_1(d1)] = PFP::VEC3(1, 2, 0);

	position[myMap.phi<11>(d2)] = PFP::VEC3(0, -2, 0);
	position[myMap.phi_1(d2)] = PFP::VEC3(2, -2, 0);

Sylvain Thery's avatar
Sylvain Thery committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
	// interface:
	QApplication app(argc, argv);
	MyQT sqt;

	// ajout entree dans le menu application
	sqt.add_menu_entry("entree1", SLOT(menu_slot1()));

	// message d'aide
	sqt.setHelpMsg("First Tuto:\n"
			"create 2 faces\n"
			"and sew them \n"
			"simple interface in Qt");

    //  bounding box
Pierre Kraemer's avatar
Pierre Kraemer committed
162
    Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
Sylvain Thery's avatar
Sylvain Thery committed
163
164
165
166
    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
167
	sqt.setParamObject(lWidthObj, lPosObj.data());
Pierre Kraemer's avatar
Pierre Kraemer committed
168

Sylvain Thery's avatar
Sylvain Thery committed
169
170
171
172
173
174
175
	// show 1 pour GL context
	sqt.show();

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

	// update des primitives du renderer
Sylvain Thery's avatar
Sylvain Thery committed
176
	SelectorTrue allDarts;
177
	SelectorEdgeNoBoundary<PFP::MAP> insideEdges(myMap);// just to draw only inside edges
Sylvain Thery's avatar
Sylvain Thery committed
178
	sqt.m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::TRIANGLES);
179
180
	sqt.m_render->initPrimitives<PFP>(myMap, insideEdges, Algo::Render::GL2::LINES);
	sqt.m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::BOUNDARY);	// special primitive for boundary edges
Pierre Kraemer's avatar
Pierre Kraemer committed
181

Sylvain Thery's avatar
Sylvain Thery committed
182
183
	// show final pour premier redraw
	sqt.show();
Pierre Kraemer's avatar
Pierre Kraemer committed
184

Sylvain Thery's avatar
Sylvain Thery committed
185
186
	// et on attend la fin.
	return app.exec();
Pierre Kraemer's avatar
Pierre Kraemer committed
187
}