tuto2.cpp 6.74 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 30
using namespace CGoGN ;


Sylvain Thery's avatar
Sylvain Thery committed
31
int main(int argc, char **argv)
Pierre Kraemer's avatar
Pierre Kraemer committed
32
{
Sylvain Thery's avatar
Sylvain Thery committed
33 34 35 36 37
	//	// 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
38

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

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



Sylvain Thery's avatar
Sylvain Thery committed
52
void MyQT::createMap()
Sylvain Thery's avatar
Sylvain Thery committed
53
{
Pierre Kraemer's avatar
Pierre Kraemer committed
54

Sylvain Thery's avatar
Sylvain Thery committed
55 56 57 58 59 60 61 62 63 64
	// 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);
	PFP::TVEC3 position = myMap.addAttribute<PFP::VEC3>(VERTEX, "position");
	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
65

Pierre Kraemer's avatar
Pierre Kraemer committed
66

Sylvain Thery's avatar
Sylvain Thery committed
67 68
	// create another attribute on vertices (for faces drawing)
	AttributeHandler<Geom::Vec3f> colorF = myMap.addAttribute<PFP::VEC3>(VERTEX, "colorF");
Sylvain Thery's avatar
Sylvain Thery committed
69

Sylvain Thery's avatar
Sylvain Thery committed
70 71 72 73 74
	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
75

Pierre Kraemer's avatar
Pierre Kraemer committed
76

Sylvain Thery's avatar
Sylvain Thery committed
77 78
	// create another attribute on vertices (for edges drawing)
	AttributeHandler<Geom::Vec3f> colorE = myMap.addAttribute<PFP::VEC3>(VERTEX, "colorE");
Pierre Kraemer's avatar
Pierre Kraemer committed
79

Sylvain Thery's avatar
Sylvain Thery committed
80 81 82 83 84
	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
85 86


Sylvain Thery's avatar
Sylvain Thery committed
87 88 89 90 91
	// example of attribute on face
	// here for example we store the number of edges of faces at construction
	AttributeHandler<int> side  = myMap.addAttribute<int>(FACE, "nb_sides");
	side[d1]=3;
	side[d2]=4;
Pierre Kraemer's avatar
Pierre Kraemer committed
92 93


Sylvain Thery's avatar
Sylvain Thery committed
94 95 96 97
    //  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
Pierre Kraemer committed
98

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

Sylvain Thery's avatar
Sylvain Thery committed
102 103
	// first show for be sure that GL context is binded
	show();
Pierre Kraemer's avatar
Pierre Kraemer committed
104 105


Sylvain Thery's avatar
Sylvain Thery committed
106 107 108 109
	// 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
110

Sylvain Thery's avatar
Sylvain Thery committed
111 112 113 114
	// construct rendering primities
	m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::TRIANGLES);
	m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::LINES);
	m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::POINTS);	// special primitive for boundary edges
Pierre Kraemer's avatar
Pierre Kraemer committed
115 116


Sylvain Thery's avatar
Sylvain Thery committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	// traverse of all dart of the map:
	// and write informations
	for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
	{
		CGoGNout << "Dart "<< d;
		if (myMap.isBoundaryMarked(d))
			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
132 133


Sylvain Thery's avatar
Sylvain Thery committed
134
}
Pierre Kraemer's avatar
Pierre Kraemer committed
135 136 137



Sylvain Thery's avatar
Sylvain Thery committed
138 139 140 141 142
// initialization GL callback
void MyQT::cb_initGL()
{
	// choose to use GL version 2
	Utils::GLSLShader::setCurrentOGLVersion(2);
Pierre Kraemer's avatar
Pierre Kraemer committed
143

Sylvain Thery's avatar
Sylvain Thery committed
144 145
	// create the render
	m_render = new Algo::Render::GL2::MapRender();
Pierre Kraemer's avatar
Pierre Kraemer committed
146

Sylvain Thery's avatar
Sylvain Thery committed
147 148 149 150 151
	// 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
152 153


Sylvain Thery's avatar
Sylvain Thery committed
154 155 156 157 158 159
	// 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
160

Sylvain Thery's avatar
Sylvain Thery committed
161 162 163 164 165
	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
166

Pierre Kraemer's avatar
Pierre Kraemer committed
167

Sylvain Thery's avatar
Sylvain Thery committed
168
}
Sylvain Thery's avatar
Sylvain Thery committed
169

Sylvain Thery's avatar
Sylvain Thery committed
170 171 172 173 174
// 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
175

Sylvain Thery's avatar
Sylvain Thery committed
176 177 178 179
	// 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
180

Sylvain Thery's avatar
Sylvain Thery committed
181 182 183 184
	// 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
185

Sylvain Thery's avatar
Sylvain Thery committed
186 187 188
	// use offset for nice drawing
	glEnable(GL_POLYGON_OFFSET_FILL);
	glPolygonOffset(1.0f, 1.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
189

Sylvain Thery's avatar
Sylvain Thery committed
190 191 192
	// 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
193

Sylvain Thery's avatar
Sylvain Thery committed
194 195
	glDisable(GL_POLYGON_OFFSET_FILL);
}
Pierre Kraemer's avatar
Pierre Kraemer committed
196 197