mapRender.cpp 4.65 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
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
42 43 44
	glGenBuffersARB(4, m_indexBuffers) ;
	for(unsigned int i = 0; i < 4; ++i)
		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 70
			break;
		default:
71
			CGoGNerr << "problem initializing VBO indices" << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
72 73 74 75 76 77 78 79 80 81 82
			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)
{
83
	sh->enableVertexAttribs();
Sylvain Thery's avatar
Sylvain Thery committed
84 85 86

	switch(prim)
	{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
87 88 89
		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
90 91
			break;
		case LINES:
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
92 93
			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
94
			break;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
95 96 97
		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
98 99 100 101 102
			break;
		default:
			break;
	}

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

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
140 141 142 143 144 145 146
} // namespace GL2

} // namespace Render

} // namespace Algo

} // namespace CGoGN