vbo_base.h 7.29 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"
Thery Sylvain's avatar
Thery Sylvain committed
33

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

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

namespace Utils
{

class GLSLShader;

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

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

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

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

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

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

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

	/**
	 * 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
91
	inline GLuint id() const { return *m_id; }
Thery Sylvain's avatar
Thery Sylvain committed
92 93 94 95

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

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

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

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

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

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

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

128 129 130 131 132
	/**
	 * 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
133 134 135
	/**
	 * update data from attribute handler to the vbo
	 */
136 137 138 139 140
	inline void updateData(const AttributeHandlerGen& attrib)
	{
		updateData(attrib.getDataVectorGen()) ;
	}

Thery Sylvain's avatar
Thery Sylvain committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	/**
	 * update data from given data vector
	 * @warning use only with include vbo.h (not vbo_base.h)
	 */
	template <typename T>
	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);
Sylvain Thery's avatar
Sylvain Thery committed
157

158
	/**
159
	 * update the VBO from Attribute Handler of vectors with on the fly conversion
160 161 162 163
	 * 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
164
	 * @param conv lambda or function/fonctor that take a const T_IN& and return a Vector<NB_COMPONENTS,float>
165 166
	 */
	template <typename T_IN, unsigned int NB_COMPONENTS, typename CONVFUNC>
167
	inline void updateDataConversion(const AttributeHandlerGen& attribHG, CONVFUNC conv)
Sylvain Thery's avatar
Sylvain Thery committed
168
	{
169 170 171
		const AttributeMultiVectorGen* attrib = attribHG.getDataVectorGen();
		updateDataConversion<T_IN,Geom::Vector<NB_COMPONENTS,float>,NB_COMPONENTS,CONVFUNC>(attrib,conv);
	}
172

173 174 175 176 177 178 179 180 181 182
	/**
	 * 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)
	{
Sylvain Thery's avatar
Sylvain Thery committed
183
		const AttributeMultiVectorGen* attrib = attribHG.getDataVectorGen();
184 185
		updateDataConversion<T_IN,float,1,CONVFUNC>(attrib,conv);
	}
Sylvain Thery's avatar
Sylvain Thery committed
186

187 188 189 190 191 192 193 194 195 196 197 198 199 200

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;
Sylvain Thery's avatar
Sylvain Thery committed
201 202
		m_name = attrib->getName();
		m_typeName = attrib->getTypeName();
203
		m_data_size = NB_COMPONENTS;
Sylvain Thery's avatar
Sylvain Thery committed
204 205 206 207 208 209 210 211

		// 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);

212
		m_nbElts = nbb * _BLOCKSIZE_/(sizeof(T_OUT));
Sylvain Thery's avatar
Sylvain Thery committed
213 214 215 216

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

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

Sylvain Thery's avatar
Sylvain Thery committed
222 223 224 225 226 227
		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
228 229
			for (unsigned int j = 0; j < _BLOCKSIZE_; ++j)
				*typedOut++ = conv(*typedIn++);
Sylvain Thery's avatar
Sylvain Thery committed
230 231

			// update sub-vbo
232
			glBufferSubData(GL_ARRAY_BUFFER, offset, szb, reinterpret_cast<void*>(typedBuffer));
Sylvain Thery's avatar
Sylvain Thery committed
233 234 235 236 237 238
			// block suivant
			offset += szb;
		}

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

Sylvain Thery's avatar
Sylvain Thery committed
240 241
	}

Thery Sylvain's avatar
Thery Sylvain committed
242 243
};

Sylvain Thery's avatar
Sylvain Thery committed
244 245


Thery Sylvain's avatar
Thery Sylvain committed
246 247 248 249 250
} // namespace Utils

} // namespace CGoGN

#endif