vbo.cpp 5.68 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer 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           *
Pierre Kraemer's avatar
Pierre Kraemer 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/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

Thery Sylvain's avatar
Thery Sylvain committed
25
#include "Utils/vbo_base.h"
Sylvain Thery's avatar
Sylvain Thery committed
26
#include "Utils/GLSLShader.h"
27 28
#include <stdio.h>
#include <string.h>
Sylvain Thery's avatar
Sylvain Thery committed
29

Pierre Kraemer's avatar
Pierre Kraemer committed
30 31
namespace CGoGN
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
32

Sylvain Thery's avatar
Sylvain Thery committed
33
namespace Utils
Pierre Kraemer's avatar
Pierre Kraemer committed
34 35
{

36
VBO::VBO(const std::string& name) : m_nbElts(0), m_lock(false), m_name(name)
Pierre Kraemer's avatar
Pierre Kraemer committed
37
{
38
	glGenBuffers(1, &(*m_id));
Sylvain Thery's avatar
Sylvain Thery committed
39 40
	m_refs.reserve(4);
}
Pierre Kraemer's avatar
Pierre Kraemer committed
41

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
42 43 44 45
VBO::VBO(const VBO& vbo) :
	m_data_size(vbo.m_data_size),
	m_nbElts(vbo.m_nbElts),
	m_lock(false)
46
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
47
	unsigned int nbbytes =  sizeof(float) * m_data_size * m_nbElts;
48

49
	glGenBuffers(1, &(*m_id));
50 51 52 53 54 55 56 57 58 59 60

	vbo.bind();
	void* src = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);

	bind();
	glBufferData(GL_ARRAY_BUFFER, nbbytes, src, GL_STREAM_DRAW);

	vbo.bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
}

Sylvain Thery's avatar
Sylvain Thery committed
61
VBO::~VBO()
Pierre Kraemer's avatar
Pierre Kraemer committed
62
{
63 64
	if (m_lock)
		releasePtr();
65
	glDeleteBuffers(1, &(*m_id));
Pierre Kraemer's avatar
Pierre Kraemer committed
66 67
}

68 69 70 71 72 73 74 75 76
void VBO::sameAllocSameBufferSize(const VBO& vbo)
{
	m_data_size = vbo.m_data_size;
	m_nbElts = vbo.m_nbElts;
	unsigned int nbbytes =  sizeof(float) * m_data_size * m_nbElts;
	bind();
	glBufferData(GL_ARRAY_BUFFER, nbbytes, NULL, GL_STREAM_DRAW);
}

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
void VBO::updateData(const AttributeHandlerGen& attrib)
{
	updateData(attrib.getDataVectorGen()) ;
}

void VBO::updateData(const AttributeMultiVectorGen* attrib)
{
	if (m_lock)
	{
		CGoGNerr << "Error locked VBO" << CGoGNendl;
		return;
	}

	m_name = attrib->getName();
	m_typeName = attrib->getTypeName();

	m_data_size = attrib->getSizeOfType() / sizeof(float);

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

	glBindBuffer(GL_ARRAY_BUFFER, *m_id);
	glBufferData(GL_ARRAY_BUFFER, nbb * byteTableSize, 0, GL_STREAM_DRAW);

	m_nbElts = nbb * byteTableSize / attrib->getSizeOfType();

	unsigned int offset = 0;

	for (unsigned int i = 0; i < nbb; ++i)
	{
		glBufferSubDataARB(GL_ARRAY_BUFFER, offset, byteTableSize, addr[i]);
		offset += byteTableSize;
	}
}

void VBO::updateData(const AttributeHandlerGen& attrib, ConvertAttrib* conv)
{
	updateData(attrib.getDataVectorGen(), conv) ;
}

void VBO::updateData(const AttributeMultiVectorGen* attrib, ConvertAttrib* conv)
{
	if (m_lock)
	{
		CGoGNerr << "Error locked VBO" << CGoGNendl;
		return;
	}

	m_name = attrib->getName();
	m_typeName = attrib->getTypeName();

	m_data_size = conv->sizeElt();

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

	// alloue la memoire pour le buffer et initialise le conv
	conv->reserve(attrib->getBlockSize());

	// bind buffer to update
	glBindBuffer(GL_ARRAY_BUFFER, *m_id);
	glBufferData(GL_ARRAY_BUFFER, nbb * conv->sizeBuffer(), 0, GL_STREAM_DRAW);

	m_nbElts = nbb * conv->nbElt();

	unsigned int offset = 0;

	for (unsigned int i = 0; i < nbb; ++i)
	{
		// convertit les donnees dans le buffer de conv
		conv->convert(addr[i]);
		// update sub-vbo
		glBufferSubDataARB(GL_ARRAY_BUFFER, offset, conv->sizeBuffer(), conv->buffer());
		// block suivant
		offset += conv->sizeBuffer();
	}

	// libere la memoire de la conversion
	conv->release();
}

160 161 162 163
void* VBO::lockPtr()
{
	if (m_lock)
	{
164
		CGoGNerr << "Error already locked VBO" << CGoGNendl;
165 166 167
		return NULL;
	}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
168
	m_lock = true;
169
	glBindBuffer(GL_ARRAY_BUFFER, *m_id);
170 171 172
	return glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
}

173 174 175 176
const void* VBO::lockPtr() const
{
	if (m_lock)
	{
177
		CGoGNerr << "Error already locked VBO" << CGoGNendl;
178 179 180 181
		return NULL;
	}

	m_lock = true;
182
	glBindBuffer(GL_ARRAY_BUFFER, *m_id);
183 184 185 186
	return glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
}

void VBO::releasePtr() const
187
{
188
	glBindBuffer(GL_ARRAY_BUFFER, *m_id);
189
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
190
	m_lock = false;
191 192
}

193 194
void VBO::copyData(void *ptr) const
{
195
	glBindBuffer(GL_ARRAY_BUFFER, *m_id);
196
	glGetBufferSubData(GL_ARRAY_BUFFER, 0, m_nbElts * m_data_size * sizeof(float), ptr);
197 198 199 200 201
}

void VBO::allocate(unsigned int nbElts)
{
	m_nbElts = nbElts;
202
	glBindBuffer(GL_ARRAY_BUFFER, *m_id);
203
	glBufferData(GL_ARRAY_BUFFER, nbElts * m_data_size * sizeof(float), 0, GL_STREAM_DRAW);
204 205
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
206
} // namespace Utils
207

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
208
} // namespace CGoGN