vbo_base.h 7.43 KB
Newer Older
Thery Sylvain's avatar
Thery Sylvain 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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, 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: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef __CGoGN_GLSL_VBO__
#define __CGoGN_GLSL_VBO__


#include <vector>
#include "Utils/gl_def.h"
#include "Container/convert.h"
32
#include "Topology/generic/attributeHandler.h"
Sylvain Thery's avatar
Sylvain Thery committed
33
#include <GL/glew.h>
Thery Sylvain's avatar
Thery Sylvain committed
34

Sylvain Thery's avatar
Sylvain Thery committed
35 36
#include "Utils/dll.h"

Thery Sylvain's avatar
Thery Sylvain committed
37 38 39 40 41 42 43 44 45 46 47
namespace CGoGN
{

namespace Utils
{

class GLSLShader;

/**
 * Encapsulation of OpenGL Vertex Buffer Object
 * Manage
48
 * - alloc / release of GL buffer
Thery Sylvain's avatar
Thery Sylvain committed
49 50 51
 * - ref by Shaders
 * - size of data (invidual cells)
 */
Sylvain Thery's avatar
Sylvain Thery committed
52
class CGoGN_UTILS_API VBO
Thery Sylvain's avatar
Thery Sylvain committed
53 54 55 56
{
protected:
	// VBO id
	CGoGNGLuint m_id;
57

Thery Sylvain's avatar
Thery Sylvain committed
58 59
	// size of data (in floats)
	unsigned int m_data_size;
60

Thery Sylvain's avatar
Thery Sylvain committed
61 62
	// shaders that ref this vbo
	std::vector<GLSLShader*> m_refs;
63

Thery Sylvain's avatar
Thery Sylvain committed
64 65 66
	unsigned int m_nbElts;
	mutable bool m_lock;

67
	/// name of the last attribute used to fill the VBO
68 69
	std::string m_name;

70
	/// type name of the last attribute used to fill the VBO
71 72
	std::string m_typeName;

Thery Sylvain's avatar
Thery Sylvain committed
73 74 75 76
public:
	/**
	 * constructor: allocate the OGL VBO
	 */
77
	VBO(const std::string& name = "");
Thery Sylvain's avatar
Thery Sylvain committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91

	/**
	 * copy constructor, new VBO copy content
	 */
	VBO(const VBO& vbo);

	/**
	 * destructor: release the OGL VBO and clean references between VBO/Shaders
	 */
	~VBO();

	/**
	 * get id of vbo
	 */
Sylvain Thery's avatar
Sylvain Thery committed
92
	inline GLuint id() const { return *m_id; }
Thery Sylvain's avatar
Thery Sylvain committed
93 94 95 96

	/**
	 * get dataSize
	 */
Sylvain Thery's avatar
Sylvain Thery committed
97
	inline unsigned int dataSize() const { return m_data_size; }
Thery Sylvain's avatar
Thery Sylvain committed
98

99 100 101
	/**
	 * get name
	 */
Sylvain Thery's avatar
Sylvain Thery committed
102
	inline const std::string& name() const { return m_name; }
103

104
	/**
105
	 * get type name
106
	 */
Sylvain Thery's avatar
Sylvain Thery committed
107
	inline const std::string& typeName() const { return m_typeName; }
108

Thery Sylvain's avatar
Thery Sylvain committed
109 110 111
	/**
	 * set the data size (in number of float)
	 */
Sylvain Thery's avatar
Sylvain Thery committed
112
	inline void setDataSize(unsigned int ds) { m_data_size = ds; }
Thery Sylvain's avatar
Thery Sylvain committed
113 114 115 116

	/**
	 * get nb element in vbo (vertices, colors ...)
	 */
Sylvain Thery's avatar
Sylvain Thery committed
117
	inline unsigned int nbElts() { return m_nbElts; }
Thery Sylvain's avatar
Thery Sylvain committed
118 119 120 121

	/**
	 * bind array vbo
	 */
Sylvain Thery's avatar
Sylvain Thery committed
122
	inline void bind() const  { glBindBuffer(GL_ARRAY_BUFFER, *m_id); }
Thery Sylvain's avatar
Thery Sylvain committed
123 124 125 126 127 128

	/**
	 * alloc buffer of same size than parameter
	 */
	void sameAllocSameBufferSize(const VBO& vbo);

129 130 131 132 133
	/**
	 * update data from attribute multivector to the vbo (automatic conversion if necessary and possible)
	 */
	void updateData(const AttributeMultiVectorGen* attrib);

Thery Sylvain's avatar
Thery Sylvain committed
134 135 136
	/**
	 * update data from attribute handler to the vbo
	 */
137 138 139 140 141
	inline void updateData(const AttributeHandlerGen& attrib)
	{
		updateData(attrib.getDataVectorGen()) ;
	}

Thery Sylvain's avatar
Thery Sylvain committed
142 143 144
	/**
	 * update data from given data vector
	 * @warning use only with include vbo.h (not vbo_base.h)
145 146
	 * @tparam VEC_DIM dim of vec (1,2,3 or 4)
	 * @param data a vector of float/VecXf or double/VecXd
Thery Sylvain's avatar
Thery Sylvain committed
147
	 */
148
	template <unsigned int VEC_DIM, typename T>
Thery Sylvain's avatar
Thery Sylvain committed
149 150 151 152 153 154 155 156 157 158 159
	void updateData(std::vector<T>& data);

	void* lockPtr();

	const void* lockPtr() const;

	void releasePtr() const;

	void copyData(void *ptr) const;

	void allocate(unsigned int nbElts);
160

161
	/**
162
	 * update the VBO from Attribute Handler of vectors with on the fly conversion
163 164 165 166
	 * template paramters:
	 * T_IN input type  attribute handler
	 * NB_COMPONENTS 3 for vbo of pos/normal, 2 for texture etc..
	 * @param attribHG the attribute handler source
167
	 * @param conv lambda or function/fonctor that take a const T_IN& and return a Vector<NB_COMPONENTS,float>
168 169
	 */
	template <typename T_IN, unsigned int NB_COMPONENTS, typename CONVFUNC>
170
	inline void updateDataConversion(const AttributeHandlerGen& attribHG, CONVFUNC conv)
171
	{
172 173 174
		const AttributeMultiVectorGen* attrib = attribHG.getDataVectorGen();
		updateDataConversion<T_IN,Geom::Vector<NB_COMPONENTS,float>,NB_COMPONENTS,CONVFUNC>(attrib,conv);
	}
175

176 177 178 179 180 181 182 183 184 185
	/**
	 * update the VBO from Attribute Handler of vectors with on the fly conversion
	 * template paramters:
	 * T_IN input type  attribute handler
	 * @param attribHG the attribute handler source
	 * @param conv lambda or function/fonctor that take a const T_IN& and return a Vector<NB_COMPONENTS,float>
	 */
	template <typename T_IN, typename CONVFUNC>
	inline void updateDataConversion(const AttributeHandlerGen& attribHG, CONVFUNC conv)
	{
186
		const AttributeMultiVectorGen* attrib = attribHG.getDataVectorGen();
187 188
		updateDataConversion<T_IN,float,1,CONVFUNC>(attrib,conv);
	}
189

190 191 192 193 194 195 196 197 198 199 200 201 202 203

protected:
	/**
	 * update the VBO from Attribute Handler with on the fly conversion
	 * template paramters:
	 * T_IN input type  attribute handler
	 * NB_COMPONENTS 3 for vbo of pos/normal, 2 for texture etc..
	 * @param attrib the attribute multivector source
	 * @param conv lambda function that take a const T_IN& and return a Vector<NB_COMPONENTS,float>
	 */
	template <typename T_IN, typename T_OUT, unsigned int NB_COMPONENTS, typename CONVFUNC>
	void updateDataConversion(const AttributeMultiVectorGen* attrib, CONVFUNC conv)
	{
		unsigned int old_nbb =  sizeof(float) * m_data_size * m_nbElts;
204 205
		m_name = attrib->getName();
		m_typeName = attrib->getTypeName();
206
		m_data_size = NB_COMPONENTS;
207 208 209 210 211 212 213 214

		// alloue la memoire pour le buffer et initialise le conv
		T_OUT* typedBuffer = new T_OUT[_BLOCKSIZE_];

		std::vector<void*> addr;
		unsigned int byteTableSize;
		unsigned int nbb = attrib->getBlocksPointers(addr, byteTableSize);

215
		m_nbElts = nbb * _BLOCKSIZE_/(sizeof(T_OUT));
216 217 218 219

		unsigned int offset = 0;
		unsigned int szb = _BLOCKSIZE_*sizeof(T_OUT);

220 221
		// bind buffer to update
		glBindBuffer(GL_ARRAY_BUFFER, *m_id);
222 223
		if (nbb!=old_nbb)
			glBufferData(GL_ARRAY_BUFFER, nbb * szb, 0, GL_STREAM_DRAW);
224

225 226 227 228 229 230
		for (unsigned int i = 0; i < nbb; ++i)
		{
			// convertit les donnees dans le buffer de conv
			const T_IN* typedIn = reinterpret_cast<const T_IN*>(addr[i]);
			T_OUT* typedOut = typedBuffer;
			// compute conversion
231 232
			for (unsigned int j = 0; j < _BLOCKSIZE_; ++j)
				*typedOut++ = conv(*typedIn++);
233 234

			// update sub-vbo
235
			glBufferSubData(GL_ARRAY_BUFFER, offset, szb, reinterpret_cast<void*>(typedBuffer));
236 237 238 239 240 241
			// block suivant
			offset += szb;
		}

		// libere la memoire de la conversion
		delete[] typedBuffer;
242

243 244
	}

Thery Sylvain's avatar
Thery Sylvain committed
245 246
};

247 248


Thery Sylvain's avatar
Thery Sylvain committed
249 250 251 252 253
} // namespace Utils

} // namespace CGoGN

#endif