<commit_before>/*
 * Copyright (C) 2010-2011 <NAME>
 *
 * This file is part of glosm.
 *
 * glosm is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * glosm 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with glosm.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef VERTEXBUFFER_HH
#define VERTEXBUFFER_HH

#include <glosm/NonCopyable.hh>
#include <glosm/Math.hh>

#include <glosm/util/gl.h>

#include <sys/types.h>

#include <vector>
#include <memory>
#include <stdexcept>
#include <cassert>

#define BUFFER_OFFSET(i) ((char *)NULL + (i))

/**
 * Vertex Buffer Object
 *
 * This class is mainly a wrapper around OpenGL VBO's. However,
 * since delayed VBO creation is common in glosm, this may also
 * be used as a vector of vertex data, which turns into VBO on
 * first Bind() or Freeze() call.
 */
template<class T>
class VertexBuffer : private NonCopyable {
public:
	typedef std::vector<T> DataVector;

protected:
	GLuint buffer_id_;
	size_t size_;
	GLenum type_;
	std::auto_ptr<DataVector> ram_data_;

protected:
	void CreateBufferObject(const T* data, int count) {
		glGenBuffers(1, &buffer_id_);
		glBindBuffer(type_, buffer_id_);
		glBufferData(type_, count * sizeof(T), data, GL_STATIC_DRAW);
		glBindBuffer(type_, 0);
		size_ = count;
	}

public:
	/**
	 * Constructs dynamic vertex buffer
	 *
	 * Vertex buffer constructed this way is ready to be filled
	 * with data and be generally used as vector of T's via
	 * Data() call. OpenGL buffers are not created until first
	 * Bind() or Freeze() calls
	 *
	 * @param type buffer type (GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER)
	 */
	VertexBuffer(GLenum type): buffer_id_(0), size_(0), type_(type), ram_data_(new DataVector) {
	}

	/**
	 * Constructs static vertex buffer
	 *
	 * Constructs OpenGL VBO directly from preexisting data.
	 *
	 * @param type buffer type (GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER)
	 * @param data pointer to array of data items
	 * @param count number of items
	 */
	VertexBuffer(GLenum type, const T* data, int count): type_(type) {
		CreateBufferObject(data, count);
	}

	/**
	 * Destructor
	 */
	~VertexBuffer() {
		if (buffer_id_)
			glDeleteBuffers(1, &buffer_id_);
	}
<commit_msg>Allow to specify mode of VertexBuffer<commit_after>/*
 * Copyright (C) 2010-2011 <NAME>
 *
 * This file is part of glosm.
 *
 * glosm is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * glosm 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with glosm.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef VERTEXBUFFER_HH
#define VERTEXBUFFER_HH

#include <glosm/NonCopyable.hh>
#include <glosm/Math.hh>

#include <glosm/util/gl.h>

#include <sys/types.h>

#include <vector>
#include <memory>
#include <stdexcept>
#include <cassert>

#define BUFFER_OFFSET(i) ((char *)NULL + (i))

/**
 * Vertex Buffer Object
 *
 * This class is mainly a wrapper around OpenGL VBO's. However,
 * since delayed VBO creation is common in glosm, this may also
 * be used as a vector of vertex data, which turns into VBO on
 * first Bind() or Freeze() call.
 */
template<class T>
class VertexBuffer : private NonCopyable {
public:
	typedef std::vector<T> DataVector;

protected:
	GLuint buffer_id_;
	size_t size_;
	GLenum type_;
	GLenum mode_;
	std::auto_ptr<DataVector> ram_data_;

protected:
	void CreateBufferObject(const T* data, int count) {
		glGenBuffers(1, &buffer_id_);
		glBindBuffer(type_, buffer_id_);
		glBufferData(type_, count * sizeof(T), data, mode_);
		glBindBuffer(type_, 0);
		size_ = count;
	}

public:
	/**
	 * Constructs dynamic vertex buffer
	 *
	 * Vertex buffer constructed this way is ready to be filled
	 * with data and be generally used as vector of T's via
	 * Data() call. OpenGL buffers are not created until first
	 * Bind() or Freeze() calls
	 *
	 * @param type buffer type (GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER)
	 */
	VertexBuffer(GLenum type = GL_ARRAY_BUFFER, GLenum mode = GL_STATIC_DRAW): buffer_id_(0), size_(0), type_(type), mode_(mode), ram_data_(new DataVector) {
	}

	/**
	 * Constructs static vertex buffer
	 *
	 * Constructs OpenGL VBO directly from preexisting data.
	 *
	 * @param type buffer type (GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER)
	 * @param data pointer to array of data items
	 * @param count number of items
	 */
	VertexBuffer(GLenum type, const T* data, int count): type_(type) {
		CreateBufferObject(data, count);
	}

	/**
	 * Destructor
	 */
	~VertexBuffer() {
		if (buffer_id_)
			glDeleteBuffers(1, &buffer_id_);
	}

{'fraction_non_alphanumeric': 0.09915966386554621, 'fraction_numerical': 0.00522875816993464, 'mean_word_length': 4.100952380952381, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5891290', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1679, 'n_words': 634}