tuto1.cpp 5.89 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
/*******************************************************************************
* 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                                        *
*                                                                              *
*******************************************************************************/
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
31
32
33
34
35
36
#include "Topology/map/map2.h"
#include "Topology/generic/embeddedMap2.h"
#include "Geometry/vector_gen.h"

#include "Algo/Import/import.h"
#include "Algo/Geometry/boundingbox.h"

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

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
54
55
56
{
	// definition of the type of the map
	typedef EmbeddedMap2<Map2> MAP;
};

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
91
92
93
94
95
	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);

		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
96

Sylvain Thery's avatar
Sylvain Thery committed
97
98
99
100
101
102
103
		glDisable(GL_POLYGON_OFFSET_FILL);
	}
}

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

Sylvain Thery's avatar
Sylvain Thery committed
106
	if ((code >'0') && (code<='9'))
107
		CGoGNout << " key num " << code-'0' << "pressed"<< CGoGNendl;
108
109
110
111
112
113
114
115
116
117
118

	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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
}

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
133
	position = myMap.addAttribute<PFP::VEC3>(VERTEX_ORBIT, "position");
Pierre Kraemer's avatar
Pierre Kraemer committed
134
135
136
137
138
139
140
141
142

	// 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
	// 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
157
    Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
Sylvain Thery's avatar
Sylvain Thery committed
158
159
160
161
    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
162
	sqt.setParamObject(lWidthObj, lPosObj.data());
Pierre Kraemer's avatar
Pierre Kraemer committed
163

Sylvain Thery's avatar
Sylvain Thery committed
164
165
166
167
168
169
170
171
172
173
	// 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
174

Sylvain Thery's avatar
Sylvain Thery committed
175
176
	// show final pour premier redraw
	sqt.show();
Pierre Kraemer's avatar
Pierre Kraemer committed
177

Sylvain Thery's avatar
Sylvain Thery committed
178
179
	// et on attend la fin.
	return app.exec();
Pierre Kraemer's avatar
Pierre Kraemer committed
180
}