<commit_before>import { mat4 } from 'gl-matrix';

const canvas = document.createElement('canvas');
const gl = canvas.getContext('webgl');

function create_float_buffer(data) {
  const buffer = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW);

  return buffer;
}

function create_index_buffer(data) {
  const buffer = gl.createBuffer();
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
  gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(data), gl.STATIC_DRAW);
  return buffer;
}

function create_shader_object(shader_type, source) {
  const shader = gl.createShader(shader_type);
  gl.shaderSource(shader, source);
  gl.compileShader(shader);
  if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
    throw gl.getShaderInfoLog(shader);
  }

  return shader;
}

function create_program_object(vs, fs) {
  const program = gl.createProgram();
  gl.attachShader(program, vs);
  gl.attachShader(program, fs);
  gl.linkProgram(program);
  if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
    throw gl.getProgramInfoLog(program);
  }

  return program;
}

function set_projection(program, fov, aspect, near, far) {
  const projection_matrix = mat4.create();
  mat4.perspective(
    projection_matrix,
    fov,
    aspect,
    near,
    far
  );

  gl.uniformMatrix4fv(
    gl.getUniformLocation(program, "uProjection"),
    false,
    projection_matrix
  );

  return projection_matrix;
}

function set_normal_matrix(program, mv) {
  // use this instead if model-view has been scaled
  /*
  const normal_matrix = mat4.toInverseMat3(mv);
  mat3.transpose(normal_matrix);
  */

  const normal_matrix = mat4.toMat3(mv);
  gl.uniformMatrix3fv(
    gl.getUniformLocation(program, "uNormalMatrix"),
    false,
    normal_matrix
  );

  return normal_matrix;
}

export {
  gl,
  canvas,
  create_float_buffer,
  create_index_buffer,
  create_shader_object,<commit_msg>:herb: Calculate normals for models<commit_after>import { mat4, mat3 } from 'gl-matrix';

const canvas = document.createElement('canvas');
const gl = canvas.getContext('webgl');

function create_float_buffer(data) {
  const buffer = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW);

  return buffer;
}

function create_index_buffer(data) {
  const buffer = gl.createBuffer();
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
  gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(data), gl.STATIC_DRAW);
  return buffer;
}

function create_shader_object(shader_type, source) {
  const shader = gl.createShader(shader_type);
  gl.shaderSource(shader, source);
  gl.compileShader(shader);
  if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
    console.log(shader_type, gl.VERTEX_SHADER, gl.FRAGMENT_SHADER);
    throw gl.getShaderInfoLog(shader);
  }

  return shader;
}

function create_program_object(vs, fs) {
  const program = gl.createProgram();
  gl.attachShader(program, vs);
  gl.attachShader(program, fs);
  gl.linkProgram(program);
  if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
    throw gl.getProgramInfoLog(program);
  }

  return program;
}

function set_projection(program, fov, aspect, near, far) {
  const projection_matrix = mat4.create();
  mat4.perspective(
    projection_matrix,
    fov,
    aspect,
    near,
    far
  );

  gl.uniformMatrix4fv(
    gl.getUniformLocation(program, "uProjection"),
    false,
    projection_matrix
  );

  return projection_matrix;
}

function set_normal_matrix(program, mv) {
  // // use this instead if model-view has been scaled
  // let normal_matrix = mat3.create();
  // normal_matrix = mat3.fromMat4([], mv);
  // normal_matrix = mat3.transpose([], normal_matrix);
  // normal_matrix = mat3.invert([], normal_matrix);
  const normal_matrix = mat3.normalFromMat4([], mv);


  // const normal_matrix = mat4.toMat3(mv);
  // const normal_matrix = mat3.fromMat4(mat3.create(), mv);
  gl.uniformMatrix3fv(
    gl.getUniformLocation(program, "uNormalMatrix"),
    false,
    normal_matrix
    // mv
  );

  return normal_matrix;
}

export {
  gl,
  canvas,
  create_float_buffer,
  create_index_buffer,
  create_shader_object,
{'fraction_non_alphanumeric': 0.12102660701671768, 'fraction_numerical': 0.008476571697668942, 'mean_word_length': 4.545691906005222, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2315385', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1446, 'n_words': 270}