Files
qglengine/core/glbuffer.cpp
2022-10-13 08:57:27 +03:00

95 lines
2.3 KiB
C++

/*
QGL Buffer
Ivan Pelipenko peri4ko@yandex.ru
This program 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 3 of the License, or
(at your option) any later version.
This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*/
#define GL_GLEXT_PROTOTYPES
#include <QOpenGLExtraFunctions>
#include "glbuffer.h"
Buffer::Buffer(GLenum target, GLenum _usage) {
target_ = target;
usage_ = _usage;
buffer_ = 0;
prev_size = 0;
}
Buffer::~Buffer() {
}
void Buffer::init(QOpenGLExtraFunctions * f) {
if (!isInit()) {
f->glGenBuffers(1, &buffer_);
}
}
void Buffer::destroy(QOpenGLExtraFunctions * f) {
if (buffer_ != 0) {
f->glDeleteBuffers(1, &buffer_);
}
buffer_ = 0;
}
void Buffer::reinit() {
buffer_ = 0;
prev_size = 0;
}
void Buffer::bind(QOpenGLExtraFunctions * f) {
//qDebug() << "bind" << target_ << buffer_;
f->glBindBuffer(target_, buffer_);
}
void Buffer::release(QOpenGLExtraFunctions * f) {
f->glBindBuffer(target_, 0);
}
void * Buffer::map(QOpenGLExtraFunctions * f, GLbitfield mode, int size) {
if (size < 0) size = prev_size;
return f->glMapBufferRange(target_, 0, size, mode);
}
void Buffer::unmap(QOpenGLExtraFunctions * f) {
f->glUnmapBuffer(target_);
}
bool Buffer::resize(QOpenGLExtraFunctions * f, int new_size) {
if (new_size <= 0) return false;
//qDebug() << "check resize buffer" << buffer_ << "bytes" << new_size << ", old =" << prev_size;
if (new_size <= prev_size) return false;
prev_size = new_size;
//qDebug() << "resize buffer " << buffer_ << target_ << "for" << new_size << "bytes";
f->glBufferData(target_, new_size, 0, usage_);
return true;
}
void Buffer::load(QOpenGLExtraFunctions * f, const void * data, int size, int offset) {
if (!data || size <= 0) return;
//qDebug() << "load buffer" << buffer_ << "bytes" << size;
f->glBufferSubData(target_, offset, size, data);
}