tuto2.cpp 6.72 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
}

Sylvain Thery's avatar
Sylvain Thery committed
50
void MyQT::createMap()
Sylvain Thery's avatar
Sylvain Thery committed
51
{
Sylvain Thery's avatar
Sylvain Thery committed
52 53 54 55
	// 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);
56
	VertexAttribute<VEC3> position = myMap.addAttribute<PFP::VEC3, VERTEX>("position");
Sylvain Thery's avatar
Sylvain Thery committed
57 58 59 60 61
	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
62

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

Sylvain Thery's avatar
Sylvain Thery committed
66 67 68 69 70
	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
71

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

Sylvain Thery's avatar
Sylvain Thery committed
75 76 77 78 79
	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
80

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

Sylvain Thery's avatar
Sylvain Thery committed
87 88 89 90
    //  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
91

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

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

Sylvain Thery's avatar
Sylvain Thery committed
98 99 100 101
	// 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
102

Sylvain Thery's avatar
Sylvain Thery committed
103 104 105 106
	// 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
107

Sylvain Thery's avatar
Sylvain Thery committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	// 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
124

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

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

Sylvain Thery's avatar
Sylvain Thery committed
134 135 136 137 138
	// 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
139

Sylvain Thery's avatar
Sylvain Thery committed
140 141 142 143 144 145
	// 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
146

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

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

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

Sylvain Thery's avatar
Sylvain Thery committed
165 166 167 168
	// 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
169

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

Sylvain Thery's avatar
Sylvain Thery committed
174 175 176
	// 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
177

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