<commit_before>#include "GL.h"
#include "VBO.h"

#include <cassert>
#include <iostream>

using namespace std;

static GLenum getGLDrawType(VBO::DrawType type) {
  switch (type) {
  case VBO::NONE: break;
  case VBO::POINTS: return GL_POINTS;
  case VBO::LINES: return GL_LINES;
  case VBO::TRIANGLES: return GL_TRIANGLES;
  case VBO::TRIANGLE_STRIP: return GL_TRIANGLE_STRIP;
  case VBO::TRIANGLE_FAN: return GL_TRIANGLE_FAN;
  }
  assert(0);
  return 0;
}

VBO::~VBO() {
  clear();
}

void
VBO::clear() {
  if (vao) {
    glDeleteVertexArrays(1, &vao);
    vao = 0;
  }
  if (vbo) {
    glDeleteBuffers(1, &vbo);
    vbo = 0;
  }
  if (indexVbo) {
    glDeleteBuffers(1, &indexVbo);
    indexVbo = 0;    
  }
  num_indices = num_elements = 0;
}

void
VBO::upload(DataType type, const void * ptr, size_t size) {
  // assert(size > 0);
  
  data_type = type;
  switch (type) {
  case VBO::T2F_N3F_V3F: stride = 8 * sizeof(float); break;
  case VBO::NODE_BILLBOARDS: stride = sizeof(node_billboard_vbo_s); break; // ?
  case VBO::BILLBOARDS: stride = sizeof(billboard_data_s); break;
  case VBO::EDGES: stride = sizeof(line_data_s); break;
  case VBO::ARCS_2D: stride = sizeof(arc_data_2d_s); break;
  case VBO::ARCS_3D: stride = sizeof(arc_data_3d_s); break;
  }
  
  num_elements = size / stride;
  if (!vao) glGenVertexArrays(1, &vao);
  if (!vbo) glGenBuffers(1, &vbo);
  glBindVertexArray(vao);
  glBindBuffer(GL_ARRAY_BUFFER, vbo);
  glBufferData(GL_ARRAY_BUFFER, size, ptr, GL_DYNAMIC_DRAW);
  
  switch (getDataType()) {
  case T2F_N3F_V3F:
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, getStride(), (void *)(0 * sizeof(float)));
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, getStride(), (void *)(5 * sizeof(float)));
    break;
  case NODE_BILLBOARDS:
    glVertexAttribPointer(0, 4, GL_UNSIGNED_BYTE, GL_TRUE, getStride(), (void *)(0)); // color
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, getStride(), (void *)(4)); // center position
    glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, getStride(), (void *)(16)); // age
    glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, getStride(), (void *)(20)); // size
    glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, getStride(), (void *)(24)); // scaling
    glVertexAttribPointer(5, 2, GL_UNSIGNED_SHORT, GL_FALSE, getStride(), (void *)(28)); // texture / flags
    break;
  case BILLBOARDS:
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, getStride(), (void *)(0 * sizeof(float)));
    glVertexAttribPointer(1, 2, GL_HALF_FLOAT, GL_FALSE, getStride(), (void *)(3 * sizeof(float)));
    glVertexAttribPointer(2, 2, GL_HALF_FLOAT, GL_FALSE, getStride(), (void *)(4 * sizeof(float)));
    glVertexAttribPointer(3, 2, GL_HALF_FLOAT, GL_FALSE, getStride(), (void *)(5 * sizeof(float)));
    glVertexAttribPointer(4, 4, GL_UNSIGNED_BYTE, GL_TRUE, getStride(), (void *)(6 * sizeof(float)));
    glVertexAttribPointer(5, 4, GL_UNSIGNED_BYTE, GL_TRUE, getStride(), (void *)(7 * sizeof(float)));
    break;
  case EDGES:
    glVertexAttribPointer(0, 4, GL_UNSIGNED_BYTE, GL_TRUE, getStride(), (void *)(0 * sizeof(float)));
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, getStride(), (void *)(1 * sizeof(float)));
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, getStride(), (void *)(4 * sizeof(float)));
    glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, getStride(), (void *)(7 * sizeof(float)));
    glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, getStride(), (void *)(8 * sizeof(float)));
    glVertexAttribPointer(5, 1, GL_FLOAT, GL_FALSE, getStride(), (void *)(9 * sizeof(float)));
    break;
  case ARCS_2D:
    glVertexAttribPointer(0, 4, GL_UNSIGNED_BYTE, GL_TRUE, getStride(), (void *)(0 * sizeof(float)));
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, getStride(), (void *)(1 * sizeof(float)));
    break;
  case ARCS_3D:
    glVertexAttribPointer(0, 4, GL_UNSIGNED_BYTE, GL_TRUE, getStride(), (void *)(0 * sizeof(float)));
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, getStride(), (void *)(1 * sizeof(float)));
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, getStride(), (void *)(4 * sizeof(float)));
    break;
  }  
  // glBindVertexArray(0);
}

void
VBO::uploadIndices(const void * ptr, size_t size) {
  assert(size > 0);

  if (!vao) glGenVertexArrays(1, &vao);
  if (!indexVbo) glGenBuffers(1, &indexVbo);
  num_indices = size / 4;
  glBindVertexArray(vao);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVbo);
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, ptr, GL_DYNAMIC_DRAW);
  // glBindVertexArray(0);
}

void
VBO::draw(DrawType type, const vector<int> & _indices) {
  if (vao) {
    glBindVertexArray(vao);
    const int & first_index = _indices.front();
    glDrawElements(getGLDrawType(type), _indices.size(), GL_UNSIGNED_INT, &first_index);
    // glBindVertexArray(0);
  }
}

void
VBO::draw(DrawType type) {
  if (vao) {
    glBindVertexArray(vao);
    if (!type) type = default_draw_type;
    if (!num_indices) {
      glDrawArrays(getGLDrawType(type), 0, num_elements);
    } else {
      glDrawElements(getGLDrawType(type), num_indices, GL_UNSIGNED_INT, 0);
    }
    // glBindVertexArray(0);
  }
}

void
VBO::quad2d(float x1, float y1,
	    float x2, float y2,
	    float x3, float y3,
	    float x4, float y4) {
  default_draw_type = TRIANGLE_FAN;
  vbo_data_s * data = new vbo_data_s[4];
  unsigned int * indices = new unsigned int[4];
  data[0] = { glm::vec2(0, 0), glm::vec3(0, 0, 1), glm::vec3(x1, y1, 0) };<commit_msg>remove binding, refactor, set GL_DYNAMIC_DRAW for dynamic VBOs and GL_STATIC_DRAW for others<commit_after>#include "GL.h"
#include "VBO.h"

#include <cassert>
#include <iostream>

using namespace std;

static GLenum getGLDrawType(VBO::DrawType type) {
  switch (type) {
  case VBO::NONE: break;
  case VBO::POINTS: return GL_POINTS;
  case VBO::LINES: return GL_LINES;
  case VBO::TRIANGLES: return GL_TRIANGLES;
  case VBO::TRIANGLE_STRIP: return GL_TRIANGLE_STRIP;
  case VBO::TRIANGLE_FAN: return GL_TRIANGLE_FAN;
  }
  assert(0);
  return 0;
}

VBO::~VBO() {
  clear();
}

void
VBO::clear() {
  if (vao) {
    glDeleteVertexArrays(1, &vao);
    vao = 0;
  }
  if (vbo) {
    glDeleteBuffers(1, &vbo);
    vbo = 0;
  }
  if (indexVbo) {
    glDeleteBuffers(1, &indexVbo);
    indexVbo = 0;    
  }
  num_indices = num_elements = 0;
}

void
VBO::upload(DataType type, const void * ptr, size_t size) {
  // assert(size > 0);
  
  data_type = type;
  switch (type) {
  case VBO::T2F_N3F_V3F: stride = 8 * sizeof(float); break;
  case VBO::NODE_BILLBOARDS: stride = sizeof(node_billboard_vbo_s); break; // ?
  case VBO::BILLBOARDS: stride = sizeof(billboard_data_s); break;
  case VBO::EDGES: stride = sizeof(line_data_s); break;
  case VBO::ARCS_2D: stride = sizeof(arc_data_2d_s); break;
  case VBO::ARCS_3D: stride = sizeof(arc_data_3d_s); break;
  }
  
  num_elements = size / stride;
  if (!vao) {
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
  } else {
    glBindVertexArray(vao);
  }
  if (!vbo) glGenBuffers(1, &vbo);
  glBindBuffer(GL_ARRAY_BUFFER, vbo);
  glBufferData(GL_ARRAY_BUFFER, size, ptr, is_dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);  
    switch (getDataType()) {
    case T2F_N3F_V3F:
      glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, getStride(), (void *)(0 * sizeof(float)));
      glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, getStride(), (void *)(5 * sizeof(float)));
      break;
    case NODE_BILLBOARDS:
      glVertexAttribPointer(0, 4, GL_UNSIGNED_BYTE, GL_TRUE, getStride(), (void *)(0)); // color
      glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, getStride(), (void *)(4)); // center position
      glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, getStride(), (void *)(16)); // age
      glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, getStride(), (void *)(20)); // size
      glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, getStride(), (void *)(24)); // scaling
      glVertexAttribPointer(5, 2, GL_UNSIGNED_SHORT, GL_FALSE, getStride(), (void *)(28)); // texture / flags
      break;
    case BILLBOARDS:
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, getStride(), (void *)(0 * sizeof(float)));
      glVertexAttribPointer(1, 2, GL_HALF_FLOAT, GL_FALSE, getStride(), (void *)(3 * sizeof(float)));
      glVertexAttribPointer(2, 2, GL_HALF_FLOAT, GL_FALSE, getStride(), (void *)(4 * sizeof(float)));
      glVertexAttribPointer(3, 2, GL_HALF_FLOAT, GL_FALSE, getStride(), (void *)(5 * sizeof(float)));
      glVertexAttribPointer(4, 4, GL_UNSIGNED_BYTE, GL_TRUE, getStride(), (void *)(6 * sizeof(float)));
      glVertexAttribPointer(5, 4, GL_UNSIGNED_BYTE, GL_TRUE, getStride(), (void *)(7 * sizeof(float)));
      break;
    case EDGES:
      glVertexAttribPointer(0, 4, GL_UNSIGNED_BYTE, GL_TRUE, getStride(), (void *)(0 * sizeof(float)));
      glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, getStride(), (void *)(1 * sizeof(float)));
      glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, getStride(), (void *)(4 * sizeof(float)));
      glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, getStride(), (void *)(7 * sizeof(float)));
      glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, getStride(), (void *)(8 * sizeof(float)));
      glVertexAttribPointer(5, 1, GL_FLOAT, GL_FALSE, getStride(), (void *)(9 * sizeof(float)));
      break;
    case ARCS_2D:
      glVertexAttribPointer(0, 4, GL_UNSIGNED_BYTE, GL_TRUE, getStride(), (void *)(0 * sizeof(float)));
      glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, getStride(), (void *)(1 * sizeof(float)));
      break;
    case ARCS_3D:
      glVertexAttribPointer(0, 4, GL_UNSIGNED_BYTE, GL_TRUE, getStride(), (void *)(0 * sizeof(float)));
      glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, getStride(), (void *)(1 * sizeof(float)));
      glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, getStride(), (void *)(4 * sizeof(float)));
      break;
    }
  // glBindVertexArray(0);
}

void
VBO::uploadIndices(const void * ptr, size_t size) {
  assert(size > 0);

  if (!vao) glGenVertexArrays(1, &vao);
  if (!indexVbo) glGenBuffers(1, &indexVbo);
  num_indices = size / 4;
  glBindVertexArray(vao);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVbo);
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, ptr, is_dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  // glBindVertexArray(0);
}

void
VBO::draw(DrawType type, const vector<int> & _indices) {
  const int & first_index = _indices.front();
  glDrawElements(getGLDrawType(type), (GLsizei)_indices.size(), GL_UNSIGNED_INT, &first_index);
  // glBindVertexArray(0);
}

void
VBO::draw(DrawType type) {
  if (!type) type = default_draw_type;
  if (!num_indices) {
    // cerr << "drawing A: " << num_elements << endl;
    glDrawArrays(getGLDrawType(type), 0, num_elements);
  } else {
    // cerr << "drawing B: " << num_indices << endl;
    glDrawElements(getGLDrawType(type), num_indices, GL_UNSIGNED_INT, 0);
  }
  // glBindVertexArray(0);  
}

void
VBO::quad2d(float x1, float y1,
	    float x2, float y2,
	    float x3, float y3,
	    float x4, float y4) {
  default_draw_type = TRIANGLE_FAN;
  vbo_data_s * data = new vbo_data_s[4];
  unsigned int * indices = new unsigned int[4];
  data[0] = { glm::vec2(0, 0), glm::vec3(0, 0, 1), glm::vec3(x1, y1, 0) };
{'fraction_non_alphanumeric': 0.15681187941676358, 'fraction_numerical': 0.024510242418821004, 'mean_word_length': 4.592796398199099, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5139268', 'n_tokens_mistral': 4425, 'n_tokens_neox': 4382, 'n_words': 937}