scene_graph.cpp 6.65 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
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
/*******************************************************************************
* 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 "Utils/scene_graph.h"

namespace CGoGN
{
namespace Utils
{
namespace SceneGraph
{




// NODE METHODS

Node::Node()
: m_label("SceneGraph::Node"),
  m_nbRefs(0)
{}


Node::~Node()
{}

Node::Node(const std::string& lab)
: m_label(lab),
  m_nbRefs(0)
{}

void Node::ref()
{
	m_nbRefs++;
}

bool Node::unref()
{
	m_nbRefs--;
	return m_nbRefs==0;
}


const std::string& Node::getLabel() const
{
	return m_label;
}

void Node::setLabel(const std::string& lab)
{
	m_label = lab ;
}



// GROUP_NODE METHODS

Group_Node::Group_Node()
:Node("SceneGraph::Group_Node"),
m_matrix_transfo(NULL),
m_material(NULL)
{}

Group_Node::Group_Node(const std::string& label)
: Node(label)
{}

Group_Node::~Group_Node()
{
	if (m_matrix_transfo != NULL)
		delete m_matrix_transfo;

	if (m_material != NULL)
		if (m_material->unref())
					delete m_material;


	for (std::list<Node*>::const_iterator it = m_children.begin(); it !=m_children.end(); ++it)
		if ( (*it)->unref())
			delete *it;
}

const std::list<Node*>& Group_Node::getChildren() const
{
	return m_children;
}

void Group_Node::addChild(Node* child)
{
	child->ref();
	m_children.push_back(child);
}

void Group_Node::removeChild(Node* child)
{
	child->unref();
	m_children.remove( child);
}

void Group_Node::setMatrixTransfo(const Geom::Matrix44f& mat)
{
	if (m_matrix_transfo == NULL)
		m_matrix_transfo = new Geom::Matrix44f(mat);
	*m_matrix_transfo = mat.transposed();
}

Sylvain Thery's avatar
Sylvain Thery committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140

Geom::Matrix44f Group_Node::getMatrixTransfo() const
{
	if (m_matrix_transfo == NULL)
	{
		Geom::Matrix44f m;
		m.identity();
		return m;
	}

	return m_matrix_transfo->transposed();

}

Sylvain Thery's avatar
Sylvain Thery committed
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
void Group_Node::setMaterial(Material_Node* mat)
{
	mat->ref();
	m_material = mat;
}

void Group_Node::render()
{
	if (m_material != NULL)
		m_material->render();

	if (m_matrix_transfo != NULL)
		glMultMatrixf(&((*m_matrix_transfo)(0,0)));

	for (std::list<Node*>::const_iterator it = m_children.begin(); it !=m_children.end(); ++it)
		(*it)->render();
}




VBO_Node::VBO_Node()
: Node("SceneGraph::VBO_Node"), m_vbo(NULL), m_primitives(0)
{}


VBO_Node::VBO_Node(Algo::Render::VBO::MapRender_VBO* vbo)
Sylvain Thery's avatar
Sylvain Thery committed
168
: m_vbo(vbo), m_primitives(0)
Sylvain Thery's avatar
Sylvain Thery committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
{}


void VBO_Node::VBO_Node::setVBO(Algo::Render::VBO::MapRender_VBO* vbo)
{
	m_vbo = vbo;
}


void VBO_Node::setPrimitives(unsigned int p)
{
	m_primitives |= p ;
}


void VBO_Node::unsetPrimitives(unsigned int p)
{
	m_primitives &= ~p ;
}


void VBO_Node::render()
{
	if (m_vbo != NULL)
	{
		if (m_primitives & Algo::Render::VBO::TRIANGLES)
		{
			glEnable(GL_POLYGON_OFFSET_FILL);
			glPolygonOffset(1.0f, 1.0f);
			glEnable(GL_LIGHTING);
Sylvain Thery's avatar
Sylvain Thery committed
199
200
			glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) ;
			glShadeModel(GL_SMOOTH);
Sylvain Thery's avatar
Sylvain Thery committed
201
202
203
204
205
			m_vbo->draw(Algo::Render::VBO::TRIANGLES);
		}

		if (m_primitives & Algo::Render::VBO::LINES)
		{
Sylvain Thery's avatar
Sylvain Thery committed
206
207
208
			GLint prg;
			glGetIntegerv(GL_CURRENT_PROGRAM,&prg);
			glUseProgram(0);
Sylvain Thery's avatar
Sylvain Thery committed
209
210
211
			glDisable(GL_POLYGON_OFFSET_FILL);
			glDisable(GL_LIGHTING);
			m_vbo->draw(Algo::Render::VBO::LINES);
Sylvain Thery's avatar
Sylvain Thery committed
212
			glUseProgram(prg);
Sylvain Thery's avatar
Sylvain Thery committed
213
214
215
216
		}

		if (m_primitives & Algo::Render::VBO::POINTS)
		{
Sylvain Thery's avatar
Sylvain Thery committed
217
218
219
			GLint prg;
			glGetIntegerv(GL_CURRENT_PROGRAM,&prg);
			glUseProgram(0);
Sylvain Thery's avatar
Sylvain Thery committed
220
221
			glDisable(GL_POLYGON_OFFSET_FILL);
			glDisable(GL_LIGHTING);
Sylvain Thery's avatar
Sylvain Thery committed
222
			glPointSize(3.0f);
Sylvain Thery's avatar
Sylvain Thery committed
223
			m_vbo->draw(Algo::Render::VBO::POINTS);
Sylvain Thery's avatar
Sylvain Thery committed
224
			glUseProgram(prg);
Sylvain Thery's avatar
Sylvain Thery committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
		}
		if (m_primitives & Algo::Render::VBO::FLAT_TRIANGLES)
		{
			glEnable(GL_POLYGON_OFFSET_FILL);
			glPolygonOffset(1.0f, 1.0f);
			glEnable(GL_LIGHTING);
			m_vbo->draw(Algo::Render::VBO::FLAT_TRIANGLES);
		}
	}
}



// MATERIAL METHODS

Material_Node::Material_Node()
:Node("SceneGraph::Material_Node"),
 m_has_diffuse(false),
 m_has_specular(false),
 m_has_ambient(false),
 m_has_shininess(false),
Sylvain Thery's avatar
Sylvain Thery committed
246
247
 m_has_color(false),
m_shader(NULL)
Sylvain Thery's avatar
Sylvain Thery committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
{}

void Material_Node::setDiffuse(const Geom::Vec4f& v)
{
	m_diffuse = v;
	m_has_diffuse=true;
}


void Material_Node::setSpecular(const Geom::Vec4f& v)
{
	m_specular = v;
	m_has_specular=true;
}


void Material_Node::setAmbient(const Geom::Vec4f& v)
{
	m_ambient = v;
	m_has_ambient=true;
}


void Material_Node::setShininess(float v)
{
	m_shininess = v;
	m_has_shininess=true;
}


void Material_Node::setColor(const Geom::Vec4f& v)
{
	m_color = v;
	m_has_color=true;
}

Sylvain Thery's avatar
Sylvain Thery committed
284
285
286
287
288
289
290
291
292
293
void Material_Node::setShader(Utils::GLSLShader *shader)
{
	m_shader = shader;
}

void Material_Node::setNoShader()
{
	m_disable_shader=true;
}

Sylvain Thery's avatar
Sylvain Thery committed
294
295
296
297
298
299
300
301
302
303
304
305
306

void Material_Node::render()
{
	if (m_has_diffuse)
		glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,m_diffuse.data());
	if (m_has_specular)
		glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,m_specular.data());
	if (m_has_ambient)
		glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,m_ambient.data());
	if (m_has_shininess)
		glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,&m_shininess);
	if (m_has_color)
		glColor3fv(m_color.data());
Sylvain Thery's avatar
Sylvain Thery committed
307
308
309
310
311
312

	if (m_shader!=NULL)
		m_shader->bind();
	else
		if (m_disable_shader)
			glUseProgramObjectARB(0);
Sylvain Thery's avatar
Sylvain Thery committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
}




void renderGraph(Node* node)
{
	node->render();
}

void eraseGraph(Node* node)
{
	delete node;
}


}
}
}