mapRender.cpp 4.97 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2011, 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.u-strasbg.fr/                                         *
Sylvain Thery's avatar
Sylvain Thery committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Render/GL2/mapRender.h"
#include "Utils/GLSLShader.h"

namespace CGoGN
{

namespace Algo
{

namespace Render
{

namespace GL2
{

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
40
MapRender::MapRender()
Sylvain Thery's avatar
Sylvain Thery committed
41
{
42 43
	glGenBuffersARB(SIZE_BUFFER, m_indexBuffers) ;
	for(unsigned int i = 0; i < SIZE_BUFFER; ++i)
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
44
		m_nbIndices[i] = 0 ;
Sylvain Thery's avatar
Sylvain Thery committed
45 46 47 48
}

MapRender::~MapRender()
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
49
	glDeleteBuffersARB(4, m_indexBuffers);
Sylvain Thery's avatar
Sylvain Thery committed
50 51 52 53 54 55 56 57
}

void MapRender::initPrimitives(int prim, std::vector<GLuint>& tableIndices)
{
	// indice du VBO a utiliser
	int vbo_ind = 0;
	switch(prim)
	{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
58 59 60
		case POINTS:
			m_nbIndices[POINT_INDICES] = tableIndices.size();
			vbo_ind = m_indexBuffers[POINT_INDICES];
Sylvain Thery's avatar
Sylvain Thery committed
61 62
			break;
		case LINES:
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
63 64
			m_nbIndices[LINE_INDICES] = tableIndices.size();
			vbo_ind = m_indexBuffers[LINE_INDICES];
Sylvain Thery's avatar
Sylvain Thery committed
65
			break;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
66 67 68
		case TRIANGLES:
			m_nbIndices[TRIANGLE_INDICES] = tableIndices.size();
			vbo_ind = m_indexBuffers[TRIANGLE_INDICES];
Sylvain Thery's avatar
Sylvain Thery committed
69
			break;
70 71 72 73
		case BOUNDARY:
			m_nbIndices[BOUNDARY_INDICES] = tableIndices.size();
			vbo_ind = m_indexBuffers[BOUNDARY_INDICES];
			break;
Sylvain Thery's avatar
Sylvain Thery committed
74
		default:
75
			CGoGNerr << "problem initializing VBO indices" << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
76 77 78 79 80 81 82 83 84 85 86
			break;
	}
	int size = tableIndices.size();

	// setup du buffer d'indices
	glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, vbo_ind);
	glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER, size*sizeof(GLuint), &(tableIndices[0]), GL_STREAM_DRAW);
}

void MapRender::draw(Utils::GLSLShader* sh, int prim)
{
87
	sh->enableVertexAttribs();
Sylvain Thery's avatar
Sylvain Thery committed
88 89 90

	switch(prim)
	{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
91 92 93
		case POINTS:
			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffers[POINT_INDICES]);
			glDrawElements(GL_POINTS, m_nbIndices[POINT_INDICES], GL_UNSIGNED_INT, 0) ;
Sylvain Thery's avatar
Sylvain Thery committed
94 95
			break;
		case LINES:
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
96 97
			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffers[LINE_INDICES]);
			glDrawElements(GL_LINES, m_nbIndices[LINE_INDICES], GL_UNSIGNED_INT, 0);
Sylvain Thery's avatar
Sylvain Thery committed
98
			break;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
99 100 101
		case TRIANGLES:
			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffers[TRIANGLE_INDICES]);
			glDrawElements(GL_TRIANGLES, m_nbIndices[TRIANGLE_INDICES], GL_UNSIGNED_INT, 0);
Sylvain Thery's avatar
Sylvain Thery committed
102
			break;
103 104 105 106 107
		case BOUNDARY:
			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffers[BOUNDARY_INDICES]);
			glDrawElements(GL_LINES, m_nbIndices[BOUNDARY_INDICES], GL_UNSIGNED_INT, 0);
			break;

Sylvain Thery's avatar
Sylvain Thery committed
108 109 110 111
		default:
			break;
	}

112
	sh->disableVertexAttribs();
Sylvain Thery's avatar
Sylvain Thery committed
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

unsigned int MapRender::drawSub(Utils::GLSLShader* sh, int prim, unsigned int nb_elm)
{
	sh->enableVertexAttribs();
	switch(prim)
	{
		case POINTS:
			if (nb_elm > m_nbIndices[POINT_INDICES])
				nb_elm = m_nbIndices[POINT_INDICES];
			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffers[POINT_INDICES]);
			glDrawElements(GL_POINTS, nb_elm, GL_UNSIGNED_INT, 0) ;

			break;
		case LINES:
			if (2*nb_elm > m_nbIndices[LINE_INDICES])
				nb_elm = m_nbIndices[LINE_INDICES]/2;
			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffers[LINE_INDICES]);
			glDrawElements(GL_LINES, 2*nb_elm, GL_UNSIGNED_INT, 0);
			break;
		case TRIANGLES:
			if (3*nb_elm > m_nbIndices[TRIANGLE_INDICES])
				nb_elm = m_nbIndices[TRIANGLE_INDICES]/3;
			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffers[TRIANGLE_INDICES]);
			glDrawElements(GL_TRIANGLES, 3*nb_elm, GL_UNSIGNED_INT, 0);
			break;
		default:
			break;
	}

	sh->disableVertexAttribs();
	return nb_elm;
}


Sylvain Thery's avatar
Sylvain Thery committed
149 150 151 152 153 154 155
} // namespace GL2

} // namespace Render

} // namespace Algo

} // namespace CGoGN