tuto_ogl2.cpp 5.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*******************************************************************************
* 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                                        *
*                                                                              *
*******************************************************************************/

#include <iostream>

#include "Utils/glutwin.h"

#include "Topology/generic/parameters.h"
#include "Topology/map/map2.h"
#include "Topology/generic/embeddedMap2.h"


#include "Algo/Geometry/boundingbox.h"
#include "Algo/Render/vbo_MapRender.h"

#include "Algo/Modelisation/polyhedron.h"
#include "Geometry/vector_gen.h"
#include "Algo/Geometry/normal.h"

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
 */
struct PFP: public PFP_STANDARD
{
	// definition of the type of the map
	typedef EmbeddedMap2<Map2> MAP;
};


/**
 * A class for a little interface and rendering
 */
class MyGlutWin: public Utils::SimpleGlutWin
{
public:

	void myRedraw();

    PFP::REAL gWidthObj;
    PFP::VEC3 gPosObj;

    Algo::Render::VBO::MapRender_VBO* m_render;

    Utils::GLSLShader shaders[4];

    Geom::Vec4f colSpec;
	float shininess;

    int rt;

 	MyGlutWin(int* argc, char **argv, int winX, int winY) : SimpleGlutWin(argc, argv, winX, winY),rt(0)
    {
 		shaders[0].loadShaders("phong_gl2.vert","phong_gl2.frag");

 	    colSpec = Geom::Vec4f(0.9f, 0.9f, 0.9f, 1.0f) ;
 	    shininess = 80.0f ;
    }
 	~MyGlutWin()
 	{
 		delete m_render ;
 	}
};




void MyGlutWin::myRedraw()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();

	// center the scene
	float sc = 50./gWidthObj;
	glScalef(sc,sc,sc);
	glTranslatef(-gPosObj[0],-gPosObj[1],-gPosObj[2]);

	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	glEnable(GL_LIGHTING);
	glEnable(GL_COLOR_MATERIAL);

	// some materials
	glMaterialfv(GL_FRONT, GL_SPECULAR, colSpec.data()) ;
	glMaterialf(GL_FRONT, GL_SHININESS, shininess) ;

	// bin shader
	shaders[0].bind();

	// draw triangles
	m_render->draw(Algo::Render::VBO::TRIANGLES) ;

	// unbind (in case of other rendering wihtout shader)
	shaders[0].unbind();

	glPopMatrix();


}




int main(int argc, char **argv)
{

    // instanciation of the interface
	MyGlutWin mgw(&argc, argv, 800, 800);

	// declaration of the map
	PFP::MAP myMap;

	AttributeHandler<PFP::VEC3> position = myMap.addAttribute<PFP::VEC3>(VERTEX_ORBIT, "position");
	AttributeHandler<PFP::VEC3> normal = myMap.addAttribute<PFP::VEC3>(VERTEX_ORBIT, "normal");

	Algo::Modelisation::Polyhedron<PFP> prim3(myMap, position);
	prim3.tore_topo(16, 32);
	prim3.embedTore(30.0f,10.0f);

	// compute the normal of mesh
	Algo::Geometry::computeNormalVertices<PFP>(myMap,position,normal);

	mgw.init();

	glClearColor(0.1,0.1,0.1,0.0);
	glEnable(GL_DEPTH_TEST);

    // computation of the bounding box
    Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
    mgw.gWidthObj = std::max<PFP::REAL>(std::max<PFP::REAL>(bb.size(0), bb.size(1)), bb.size(2));
    mgw.gPosObj = (bb.min() +  bb.max()) / PFP::REAL(2);


    // instanciation of the vbo renderer
    mgw.m_render = new Algo::Render::VBO::MapRender_VBO();

    // vertex attribute declaration (max 12)
    mgw.m_render->useVertexAttributeName("ColorPerVertex",mgw.shaders[0]);

    // must relink the shader after binding attributes
    mgw.shaders[0].link();

    // copy data from map attributes to vbo for  vertex attribute
    mgw.m_render->updateVAData("ColorPerVertex", normal);

    // copy data from map attributes to vbo
    mgw.m_render->updateData(Algo::Render::VBO::POSITIONS, position);
    mgw.m_render->updateData(Algo::Render::VBO::NORMALS, normal);

    // update the renderer (primitives)
	SelectorTrue allDarts;
    mgw.m_render->initPrimitives<PFP>(myMap, allDarts,Algo::Render::VBO::TRIANGLES);
//    mgw.m_render->initPrimitives<PFP>(myMap, allDarts,Algo::Render::VBO::LINES);
//    mgw.m_render->initPrimitives<PFP>(myMap, allDarts,Algo::Render::VBO::POINTS);


    mgw.mainLoop();

    return 0;
}