vbo.h 3.84 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-2012, 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.unistra.fr/                                           *
Sylvain Thery's avatar
Sylvain Thery committed
21 22 23 24 25 26 27
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef __CGoGN_GLSL_VBO__
#define __CGoGN_GLSL_VBO__

28

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

namespace CGoGN
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
36

Sylvain Thery's avatar
Sylvain Thery committed
37 38 39 40
namespace Utils
{

class GLSLShader;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
41

Sylvain Thery's avatar
Sylvain Thery committed
42
/**
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
43
 * Encapsulation of OpenGL Vertex Buffer Object
Sylvain Thery's avatar
Sylvain Thery committed
44 45 46 47 48 49 50 51 52
 * Manage
 * - alloc /release of GL buffer
 * - ref by Shaders
 * - size of data (invidual cells)
 */
class VBO
{
protected:
	// VBO id
53
	CGoGNGLuint m_id;
Sylvain Thery's avatar
Sylvain Thery committed
54 55 56 57
	// size of data (in floats)
	unsigned int m_data_size;
	// shaders that ref this vbo
	std::vector<GLSLShader*> m_refs;
58
	unsigned int m_nbElts;
59
	mutable bool m_lock;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
60

Sylvain Thery's avatar
Sylvain Thery committed
61 62 63 64 65 66
public:
	/**
	 * constructor: allocate the OGL VBO
	 */
	VBO();

67 68 69 70 71
	/**
	 * copy constructor, new VBO copy content
	 */
	VBO(const VBO& vbo);

Sylvain Thery's avatar
Sylvain Thery committed
72 73 74 75 76 77 78 79
	/**
	 * destructor: release the OGL VBO and clean references between VBO/Shaders
	 */
	~VBO();

	/**
	 * get id of vbo
	 */
80
	GLuint id() const { return *m_id; }
Sylvain Thery's avatar
Sylvain Thery committed
81 82 83 84

	/**
	 * get dataSize
	 */
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
85
	unsigned int dataSize() const { return m_data_size; }
Sylvain Thery's avatar
Sylvain Thery committed
86 87 88 89

	/**
	 * set the data size (in number of float)
	 */
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
90
	void setDataSize(unsigned int ds) { m_data_size = ds; }
Sylvain Thery's avatar
Sylvain Thery committed
91

92 93 94
	/**
	 * get nb element in vbo (vertices, colors ...)
	 */
95
	unsigned int nbElts() { return m_nbElts; }
96

97 98 99
	/**
	 * bind array vbo
	 */
100
	void bind() const  { glBindBuffer(GL_ARRAY_BUFFER, *m_id); }
101

Sylvain Thery's avatar
Sylvain Thery committed
102
	/**
103
	 * alloc buffer of same size than parameter
Sylvain Thery's avatar
Sylvain Thery committed
104
	 */
105 106
	void sameAllocSameBufferSize(const VBO& vbo);

Sylvain Thery's avatar
Sylvain Thery committed
107 108 109 110 111 112 113 114 115 116 117
	/**
	 * update data from attribute handler to the vbo
	 */
	template <typename ATTR_HANDLER>
	void updateData(const ATTR_HANDLER& attrib);

	/**
	 * update data from attribute handler to the vbo, with conversion
	 */
	template <typename ATTR_HANDLER>
	void updateData(const ATTR_HANDLER& attrib, ConvertAttrib* conv);
118

119 120 121 122 123 124
	/**
	 * update data from given data vector
	 */
	template <typename T>
	void updateData(std::vector<T>& data);

125 126
	void* lockPtr();

127 128 129
	const void* lockPtr() const;

	void releasePtr() const;
130

131 132 133
	void copyData(void *ptr) const;

	void allocate(unsigned int nbElts);
Sylvain Thery's avatar
Sylvain Thery committed
134 135
};

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
136 137 138
} // namespace Utils

} // namespace CGoGN
Sylvain Thery's avatar
Sylvain Thery committed
139 140 141 142

#include "Utils/vbo.hpp"

#endif