mapRender.cpp 4.98 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
{

40

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

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

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
59 60 61
		case POINTS:
			m_nbIndices[POINT_INDICES] = tableIndices.size();
			vbo_ind = m_indexBuffers[POINT_INDICES];
Sylvain Thery's avatar
Sylvain Thery committed
62 63
			break;
		case LINES:
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
64 65
			m_nbIndices[LINE_INDICES] = tableIndices.size();
			vbo_ind = m_indexBuffers[LINE_INDICES];
Sylvain Thery's avatar
Sylvain Thery committed
66
			break;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
67 68 69
		case TRIANGLES:
			m_nbIndices[TRIANGLE_INDICES] = tableIndices.size();
			vbo_ind = m_indexBuffers[TRIANGLE_INDICES];
Sylvain Thery's avatar
Sylvain Thery committed
70
			break;
71 72 73 74
		case BOUNDARY:
			m_nbIndices[BOUNDARY_INDICES] = tableIndices.size();
			vbo_ind = m_indexBuffers[BOUNDARY_INDICES];
			break;
Sylvain Thery's avatar
Sylvain Thery committed
75
		default:
76
			CGoGNerr << "problem initializing VBO indices" << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
77 78 79 80 81 82 83 84 85 86 87
			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)
{
88
	sh->enableVertexAttribs();
Sylvain Thery's avatar
Sylvain Thery committed
89 90 91

	switch(prim)
	{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
92 93 94
		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
95 96
			break;
		case LINES:
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
97 98
			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
99
			break;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
100 101 102
		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
103
			break;
104 105 106 107 108
		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
109 110 111 112
		default:
			break;
	}

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

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
150 151 152 153 154 155 156
} // namespace GL2

} // namespace Render

} // namespace Algo

} // namespace CGoGN