tuto_ogl3.cpp 6.04 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1
/*******************************************************************************
Sylvain Thery's avatar
Sylvain Thery committed
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           *
Sylvain Thery's avatar
Sylvain Thery 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/                                           *
Sylvain Thery's avatar
Sylvain Thery committed
21 22 23
 * Contact information: cgogn@unistra.fr                                        *
 *                                                                              *
 *******************************************************************************/
Sylvain Thery's avatar
Sylvain Thery committed
24 25 26

#include <iostream>

Sylvain Thery's avatar
Sylvain Thery committed
27
#include "tuto_ogl3.h"
28
#include "Utils/vbo.h"
Sylvain Thery's avatar
Sylvain Thery committed
29 30 31

using namespace CGoGN ;

32 33 34 35 36
// declarations globales (plus simple)
PFP::MAP myMap;
AttributeHandler<PFP::VEC3> position;
AttributeHandler<PFP::VEC3> normal;

Sylvain Thery's avatar
Sylvain Thery committed
37
void MyQT::sliderFocale_cb(int x)
Sylvain Thery's avatar
Sylvain Thery committed
38
{
Sylvain Thery's avatar
Sylvain Thery committed
39 40 41
	m_glWidget->setFocal(0.01f*x);
	updateGL();
}
Sylvain Thery's avatar
Sylvain Thery committed
42

Sylvain Thery's avatar
Sylvain Thery committed
43
void MyQT::color_cb()
Sylvain Thery's avatar
Sylvain Thery committed
44
{
Sylvain Thery's avatar
Sylvain Thery committed
45 46 47
	QColor color = QColorDialog::getColor ();
	if (color.isValid())
	{
48
		CGoGNout <<"Color " << color.red()<<","<< color.green()<<","<<color.blue()<< CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
49 50 51 52 53
		shader1->setDiffuse(Geom::Vec4f(float(color.red())/255.0f,float(color.green())/255.0f,float(color.blue())/255.0f,0.0f));
		updateGL();
	}
	else
	{
54
		CGoGNout << "Cancel"<< CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
55
	}
Sylvain Thery's avatar
Sylvain Thery committed
56 57
}

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
void MyQT::pervertex_cb(bool val)
{
	if (val)
	{
		shader1->setAttributeColor(colorVBO);
		CGoGNout << "color per vertex"<< CGoGNendl;
	}
	else
	{
		shader1->unsetAttributeColor();
		CGoGNout << "global color"<< CGoGNendl;
	}
	updateGLMatrices();
}

Sylvain Thery's avatar
Sylvain Thery committed
73
MyQT::MyQT(): m_render(NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
74
{}
Sylvain Thery's avatar
Sylvain Thery committed
75

Sylvain Thery's avatar
Sylvain Thery committed
76
MyQT::~MyQT()
Pierre Kraemer's avatar
Pierre Kraemer committed
77
{}
Sylvain Thery's avatar
Sylvain Thery committed
78

Sylvain Thery's avatar
Sylvain Thery committed
79
void MyQT::cb_initGL()
Sylvain Thery's avatar
Sylvain Thery committed
80
{
Sylvain Thery's avatar
Sylvain Thery committed
81 82 83
	Utils::GLSLShader::setCurrentOGLVersion(3);
	// instanciation of the renderer (here using VBOs)
	m_render = new Algo::Render::GL2::MapRender();
Sylvain Thery's avatar
Sylvain Thery committed
84

Sylvain Thery's avatar
Sylvain Thery committed
85 86
	positionVBO = new Utils::VBO();
	normalVBO = new Utils::VBO();
Sylvain Thery's avatar
Sylvain Thery committed
87

88 89
	positionVBO->updateData(position);
	normalVBO->updateData(normal);
Sylvain Thery's avatar
Sylvain Thery committed
90

91 92 93 94 95 96 97 98 99 100 101
	// create colors from normal (remap [-1,1] to [0,1]
	colorVBO = new Utils::VBO(*normalVBO);
	Geom::Vec3f* colors= reinterpret_cast<Geom::Vec3f*>(colorVBO->lockPtr());
	for (unsigned int i =0 ; i<colorVBO->nbElts();++i)
	{
		*colors = (Geom::Vec3f(1.,1.,1.)+ *colors)/2.0f;
		colors++;
	}
	colorVBO->releasePtr();

	// update the renderer (primitives and geometry)
Pierre Kraemer's avatar
Pierre Kraemer committed
102 103 104
	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);
105 106

	shader1 = new Utils::ShaderPhong();
107 108
	shader1->setAttributePosition(positionVBO);
	shader1->setAttributeNormal(normalVBO);
109

Sylvain Thery's avatar
Sylvain Thery committed
110
	currentShader = shader1;
Sylvain Thery's avatar
Sylvain Thery committed
111 112
}

Sylvain Thery's avatar
Sylvain Thery committed
113
void MyQT::cb_redraw()
Sylvain Thery's avatar
Sylvain Thery committed
114
{
Sylvain Thery's avatar
Sylvain Thery committed
115 116 117
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	glEnable(GL_LIGHTING);
	if (currentShader)
Sylvain Thery's avatar
Sylvain Thery committed
118
	{
Sylvain Thery's avatar
Sylvain Thery committed
119
		m_render->draw(currentShader, Algo::Render::GL2::TRIANGLES);
Sylvain Thery's avatar
Sylvain Thery committed
120 121 122
	}
}

Sylvain Thery's avatar
Sylvain Thery committed
123 124
int main(int argc, char **argv)
{
125 126
	position = myMap.addAttribute<PFP::VEC3>(VERTEX, "position");
	normal = myMap.addAttribute<PFP::VEC3>(VERTEX, "normal");
Sylvain Thery's avatar
Sylvain Thery committed
127 128

	Algo::Modelisation::Polyhedron<PFP> prim3(myMap, position);
Sylvain Thery's avatar
Sylvain Thery committed
129 130
	prim3.tore_topo(32, 64);
	prim3.embedTore(1.0f,0.3f);
Sylvain Thery's avatar
Sylvain Thery committed
131 132 133

	Algo::Geometry::computeNormalVertices<PFP>(myMap,position,normal);

Sylvain Thery's avatar
Sylvain Thery committed
134 135
	QApplication app(argc, argv);
	MyQT sqt;
Sylvain Thery's avatar
Sylvain Thery committed
136

Sylvain Thery's avatar
Sylvain Thery committed
137
	// 	Qt "a la main"
Sylvain Thery's avatar
Sylvain Thery committed
138

Sylvain Thery's avatar
Sylvain Thery committed
139
	QDockWidget* dock = sqt.addEmptyDock();
Sylvain Thery's avatar
Sylvain Thery committed
140

Sylvain Thery's avatar
Sylvain Thery committed
141 142 143
	// group principal
	QGroupBox *interface_gb = new QGroupBox();
	QVBoxLayout *layout = new QVBoxLayout;
Sylvain Thery's avatar
Sylvain Thery committed
144

Sylvain Thery's avatar
Sylvain Thery committed
145
	layout->addWidget(new QLabel("Focale"));
Sylvain Thery's avatar
Sylvain Thery committed
146

Sylvain Thery's avatar
Sylvain Thery committed
147 148 149 150
	QSlider *slider_foc = new QSlider(Qt::Horizontal);
	slider_foc->setRange(50, 300);
	slider_foc->setSliderPosition(200);
	layout->addWidget(slider_foc);
Sylvain Thery's avatar
Sylvain Thery committed
151

Sylvain Thery's avatar
Sylvain Thery committed
152 153
	QPushButton* buttonColor = new QPushButton("Color");
	layout->addWidget(buttonColor);
Sylvain Thery's avatar
Sylvain Thery committed
154

155 156 157
	QCheckBox* perVertexColor = new QCheckBox("PerVertex");
	layout->addWidget(perVertexColor);

Sylvain Thery's avatar
Sylvain Thery committed
158 159 160 161 162 163 164 165 166
	// on tasse  vers le haut
	layout->addItem(new QSpacerItem(0,0, QSizePolicy::Expanding, QSizePolicy::Expanding));

	// et met tout ça dans le dock
	interface_gb->setLayout(layout);
	dock->setWidget(interface_gb);

	sqt.windowTitle("OGL3 QT");
	sqt.dockTitle("interface");
Sylvain Thery's avatar
Sylvain Thery committed
167

168 169
	sqt.setDock(dock) ;

Sylvain Thery's avatar
Sylvain Thery committed
170 171 172
	// connection widget callback
	sqt.setCallBack( slider_foc, SIGNAL(valueChanged(int)), SLOT(sliderFocale_cb(int)) );
	sqt.setCallBack( buttonColor, SIGNAL(clicked()), SLOT(color_cb()) );
173
	sqt.setCallBack( perVertexColor, SIGNAL(toggled(bool)), SLOT(pervertex_cb(bool)) );
Sylvain Thery's avatar
Sylvain Thery committed
174 175 176 177 178 179 180 181 182 183 184

	// computation of the bounding box
	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);

	sqt.setParamObject(lWidthObj,lPosObj.data());

	// first show to init GL Context before initializing VBO
	sqt.show();

185 186
	CGoGNStream::allToConsole(&sqt);
	CGoGNStream::allToStd(false);
Sylvain Thery's avatar
Sylvain Thery committed
187 188 189

	// second show to have a good initial redraw
	sqt.show();
Sylvain Thery's avatar
Sylvain Thery committed
190

Sylvain Thery's avatar
Sylvain Thery committed
191 192
	return app.exec();
	//    return 0;
Sylvain Thery's avatar
Sylvain Thery committed
193
}