tuto2.cpp 6.75 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-2012, 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.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
24
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

25
#include "tuto2.h"
Sylvain Thery's avatar
Sylvain Thery committed
26
#include "Algo/Geometry/boundingbox.h"
Sylvain Thery's avatar
Sylvain Thery committed
27

Pierre Kraemer's avatar
Pierre Kraemer committed
28
29
using namespace CGoGN ;

Sylvain Thery's avatar
Sylvain Thery committed
30
int main(int argc, char **argv)
Pierre Kraemer's avatar
Pierre Kraemer committed
31
{
Sylvain Thery's avatar
Sylvain Thery committed
32
33
34
35
36
	//	// interface
	QApplication app(argc, argv);
	MyQT sqt;
	// copy output tout Qt console of application (shift enter)
	CGoGNout.toConsole(&sqt);
Sylvain Thery's avatar
Sylvain Thery committed
37

Sylvain Thery's avatar
Sylvain Thery committed
38
39
40
41
	// example code itself
	sqt.createMap();
	// set help message in menu
	sqt.setHelpMsg("Tuto 2: \nCreate and use multiple attributes\nrender with multiple shaders");
42

Sylvain Thery's avatar
Sylvain Thery committed
43
44
45
46
	// final show for redraw
	sqt.show();
	// and wait for the end
	return app.exec();
Sylvain Thery's avatar
Sylvain Thery committed
47
48
}

Sylvain Thery's avatar
Sylvain Thery committed
49
void MyQT::createMap()
Sylvain Thery's avatar
Sylvain Thery committed
50
{
Sylvain Thery's avatar
Sylvain Thery committed
51
52
53
54
	// creation of 2 new faces: 1 triangle and 1 square, sew and embed (see tuto1 for details)
	Dart d1 = myMap.newFace(3);
	Dart d2 = myMap.newFace(4);
	myMap.sewFaces(d1, d2);
Pierre Kraemer's avatar
Pierre Kraemer committed
55
	VertexAttribute<VEC3, MAP_IMPL> position = myMap.addAttribute<PFP::VEC3, VERTEX>("position");
Sylvain Thery's avatar
Sylvain Thery committed
56
57
58
59
60
	position[d1] = PFP::VEC3(0, 0, 0);
	position[PHI1(d1)] = PFP::VEC3(2, 0, 0);
	position[PHI_1(d1)] = PFP::VEC3(1, 2, 0);
	position[PHI<11>(d2)] = PFP::VEC3(0, -2, 0);
	position[PHI_1(d2)] = PFP::VEC3(2, -2, 0);
Sylvain Thery's avatar
Sylvain Thery committed
61

Sylvain Thery's avatar
Sylvain Thery committed
62
	// create another attribute on vertices (for faces drawing)
Pierre Kraemer's avatar
Pierre Kraemer committed
63
	VertexAttribute<VEC3, MAP_IMPL> colorF = myMap.addAttribute<PFP::VEC3, VERTEX>("colorF");
Sylvain Thery's avatar
Sylvain Thery committed
64

Sylvain Thery's avatar
Sylvain Thery committed
65
66
67
68
69
	colorF[d1] = Geom::Vec3f(1.0f,0.0f,0.0f);
	colorF[PHI1(d1)] = Geom::Vec3f(0.0f,1.0f,0.0f);
	colorF[PHI_1(d1)] = Geom::Vec3f(0.0f,0.0f,1.0f);
	colorF[PHI<11>(d2)] = Geom::Vec3f(1.0f,0.0f,1.0f);
	colorF[PHI_1(d2)] = Geom::Vec3f(0.0f,1.0f,1.0f);
Sylvain Thery's avatar
Sylvain Thery committed
70

Sylvain Thery's avatar
Sylvain Thery committed
71
	// create another attribute on vertices (for edges drawing)
Pierre Kraemer's avatar
Pierre Kraemer committed
72
	VertexAttribute<VEC3, MAP_IMPL> colorE = myMap.addAttribute<PFP::VEC3, VERTEX>("colorE");
Pierre Kraemer's avatar
Pierre Kraemer committed
73

Sylvain Thery's avatar
Sylvain Thery committed
74
75
76
77
78
	colorE[d1] = Geom::Vec3f(0.0f,0.5f,0.5f);
	colorE[PHI1(d1)] = Geom::Vec3f(0.5f,0.0f,0.5f);
	colorE[PHI_1(d1)] = Geom::Vec3f(0.5f,0.5f,0.0f);
	colorE[PHI<11>(d2)] = Geom::Vec3f(0.0f,0.5f,0.0f);
	colorE[PHI_1(d2)] = Geom::Vec3f(0.5f,0.0f,0.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
79

Sylvain Thery's avatar
Sylvain Thery committed
80
81
	// example of attribute on face
	// here for example we store the number of edges of faces at construction
Pierre Kraemer's avatar
Pierre Kraemer committed
82
	FaceAttribute<int, MAP_IMPL> side = myMap.addAttribute<int, FACE>("nb_sides");
Pierre Kraemer's avatar
Pierre Kraemer committed
83
84
	side[d1] = 3;
	side[d2] = 4;
Pierre Kraemer's avatar
Pierre Kraemer committed
85

Sylvain Thery's avatar
Sylvain Thery committed
86
87
88
89
    //  bounding box of scene
    Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
    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);
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
90

Sylvain Thery's avatar
Sylvain Thery committed
91
92
    // send BB info to interface for centering on GL screen
	setParamObject(lWidthObj, lPosObj.data());
Pierre Kraemer's avatar
Pierre Kraemer committed
93

Sylvain Thery's avatar
Sylvain Thery committed
94
95
	// first show for be sure that GL context is binded
	show();
Pierre Kraemer's avatar
Pierre Kraemer committed
96

Sylvain Thery's avatar
Sylvain Thery committed
97
98
99
100
	// update of position VBO (context GL necessary)
	m_positionVBO->updateData(position);
	m_colorVBO1->updateData(colorF);
	m_colorVBO2->updateData(colorE);
Pierre Kraemer's avatar
Pierre Kraemer committed
101

Sylvain Thery's avatar
Sylvain Thery committed
102
	// construct rendering primities
103
104
105
	m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::TRIANGLES);
	m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::LINES);
	m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::POINTS);	// special primitive for boundary edges
Pierre Kraemer's avatar
Pierre Kraemer committed
106

Sylvain Thery's avatar
Sylvain Thery committed
107
108
109
110
111
	// traverse of all dart of the map:
	// and write informations
	for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
	{
		CGoGNout << "Dart "<< d;
112
		if (myMap.isBoundaryMarked<PFP::MAP::DIMENSION>(d))
Sylvain Thery's avatar
Sylvain Thery committed
113
114
115
116
117
118
119
120
121
122
			CGoGNout << " is a boundary dart (implicitly added)"<< CGoGNendl;
		else
		{
			CGoGNout <<":  position= "<< position[d];
			CGoGNout <<" / color1= "<< colorF[d];
			CGoGNout <<" / color2= "<< colorE[d];
			CGoGNout << " / numer of side of face "<< side[d] << CGoGNendl;
		}
	}
}
Pierre Kraemer's avatar
Pierre Kraemer committed
123

Sylvain Thery's avatar
Sylvain Thery committed
124
125
126
127
128
// initialization GL callback
void MyQT::cb_initGL()
{
	// choose to use GL version 2
	Utils::GLSLShader::setCurrentOGLVersion(2);
Pierre Kraemer's avatar
Pierre Kraemer committed
129

Sylvain Thery's avatar
Sylvain Thery committed
130
131
	// create the render
	m_render = new Algo::Render::GL2::MapRender();
Pierre Kraemer's avatar
Pierre Kraemer committed
132

Sylvain Thery's avatar
Sylvain Thery committed
133
134
135
136
137
	// create VBO for position
	m_positionVBO = new Utils::VBO();
	// and color
	m_colorVBO1 = new Utils::VBO();
	m_colorVBO2 = new Utils::VBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
138

Sylvain Thery's avatar
Sylvain Thery committed
139
140
141
142
143
144
	// using simple shader with color
	m_shader = new Utils::ShaderSimpleColor();
	m_shader->setAttributePosition(m_positionVBO);
	m_shader->setColor(Geom::Vec4f(0.0f, 1.0f, 0.0f, 0.0f));
	// each shader must be registred to allow Qt interface to update matrices uniforms
	registerShader(m_shader);
Sylvain Thery's avatar
Sylvain Thery committed
145

Sylvain Thery's avatar
Sylvain Thery committed
146
147
148
149
150
151
	m_shader2 = new Utils::ShaderColorPerVertex();
	m_shader2->setAttributePosition(m_positionVBO);
//	m_shader2->setAttributeColor(m_colorVBO1);
	// each shader must be registred to allow Qt interface to update matrices uniforms
	registerShader(m_shader2);
}
Sylvain Thery's avatar
Sylvain Thery committed
152

Sylvain Thery's avatar
Sylvain Thery committed
153
154
155
156
157
// redraw GL callback (clear and swap already done)
void MyQT::cb_redraw()
{
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	glEnable(GL_CULL_FACE);
Sylvain Thery's avatar
Sylvain Thery committed
158

Sylvain Thery's avatar
Sylvain Thery committed
159
160
161
162
	// draw yellow points
	glLineWidth(2.0f);
	m_shader2->setAttributeColor(m_colorVBO1);
	m_render->draw(m_shader2, Algo::Render::GL2::LINES);
Sylvain Thery's avatar
Sylvain Thery committed
163

Sylvain Thery's avatar
Sylvain Thery committed
164
165
166
167
	// draw white points
	glPointSize(7.0f);
	m_shader->setColor(Geom::Vec4f(1.,1.,1.,0.));
	m_render->draw(m_shader, Algo::Render::GL2::POINTS);
Sylvain Thery's avatar
Sylvain Thery committed
168

Sylvain Thery's avatar
Sylvain Thery committed
169
170
171
	// use offset for nice drawing
	glEnable(GL_POLYGON_OFFSET_FILL);
	glPolygonOffset(1.0f, 1.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
172

Sylvain Thery's avatar
Sylvain Thery committed
173
174
175
	// draw faces with pervertex color rendering
	m_shader2->setAttributeColor(m_colorVBO2);
	m_render->draw(m_shader2, Algo::Render::GL2::TRIANGLES);
Pierre Kraemer's avatar
Pierre Kraemer committed
176

Sylvain Thery's avatar
Sylvain Thery committed
177
178
	glDisable(GL_POLYGON_OFFSET_FILL);
}