<commit_before>/*
    Copyright 2013 Jesse 'Jeaye' Wilkerson
    See licensing in LICENSE file, or at:
        http://www.opensource.org/licenses/BSD-3-Clause

    File: obj/voxel/map.rs
    Author: Jesse 'Jeaye' Wilkerson
    Description:
      A voxelization of arbitrary triangles
      into OpenGL-ready cubes.
*/

use math::{ Vec3f, Vec3i, Vec3u8 };
use primitive::Vertex_PC;
use primitive::Triangle;
use primitive::{ Cube };
use super::{ Vertex, Behavior, Default };

#[path = "../../gl/mod.rs"]
mod gl;
#[path = "../../gl/util.rs"]
mod util;
#[macro_escape]
#[path = "../../gl/check.rs"]
mod check;

/* TODO: Voxels should have indexed verts or should be drawn as a fan. */
struct Map
{
  resolution: u32,
  voxel_size: f32,

  vao: gl::GLuint,
  vbo: gl::GLuint,
  ibo: gl::GLuint,

  voxels: ~[Behavior],
  indices: ~[Vertex],
}

impl Map
{
  pub fn new(tris: &[Triangle], res: u32) -> Map
  {
    let mut map = Map
    {
      resolution: res,
      voxel_size: 0.0,
      vao: 0,
      vbo: 0,
      ibo: 0,
      voxels: ~[],
      indices: ~[],
    };

    map.voxelize(tris);

    /* Single voxel that will be instance-rendered. */
    let mut voxel = ~[];
    voxel.push(Cube::new(map.voxel_size, Vec3f::zero()));

    map.vao = check!(gl::gen_vertex_arrays(1))[0]; /* TODO: Check these. */
    map.vbo = check!(gl::gen_buffers(1))[0];
    map.ibo = check!(gl::gen_buffers(1))[0];
    check!(gl::bind_vertex_array(map.vao));
    check!(gl::bind_buffer(gl::ARRAY_BUFFER, map.vbo));
    check!(gl::buffer_data(gl::ARRAY_BUFFER, voxel, gl::STATIC_DRAW));

    check!(gl::bind_buffer(gl::ARRAY_BUFFER, map.ibo));
    check!(gl::buffer_data(gl::ARRAY_BUFFER, map.indices, gl::STATIC_DRAW));

    map
  }

  pub fn draw(&self)
  {
    check!(gl::bind_vertex_array(self.vao));

    check!(gl::bind_buffer(gl::ARRAY_BUFFER, self.vbo));
    check!(gl::vertex_attrib_pointer_f32(0, 3, false, (sys::size_of::<Vertex_PC>()) as i32, 0));
    check!(gl::enable_vertex_attrib_array(0));

    check!(gl::bind_buffer(gl::ARRAY_BUFFER, self.ibo));

    check!(gl::vertex_attrib_pointer_i32(1, 3, false, (sys::size_of::<Vertex>()) as i32, 0));
    check!(gl::enable_vertex_attrib_array(1));
    check!(gl::vertex_attrib_divisor(1, 1));

    check!(gl::vertex_attrib_pointer_u8(2, 3, true, (sys::size_of::<Vertex>()) as i32, 
                                        (sys::size_of::<Vec3i>()) as u32));
    check!(gl::enable_vertex_attrib_array(2));
    check!(gl::vertex_attrib_divisor(2, 1));

    //check!(gl::polygon_mode(gl::FRONT_AND_BACK, gl::LINE));
    check!(gl::draw_arrays_instanced(gl::TRIANGLES, 0, 36, self.indices.len() as i32));
    //check!(gl::polygon_mode(gl::FRONT_AND_BACK, gl::FILL));

    check!(gl::disable_vertex_attrib_array(0));
    check!(gl::disable_vertex_attrib_array(1));
    check!(gl::disable_vertex_attrib_array(2));
    check!(gl::bind_vertex_array(0));
    check!(gl::bind_buffer(gl::ARRAY_BUFFER, 0));
  }

  priv fn voxelize(&mut self, tris: &[Triangle])
  {
    /* Require at least one triangle. */
    assert!(tris.len() >= 1);
    debug!("VOXEL: Incoming triangles: %?", tris.len());

    /* Bounding box of vert dimensions. */
    let mut min = Vec3f::new( tris[0].verts[0].position.x,
                              tris[0].verts[0].position.y, 
                              tris[0].verts[0].position.z);
    let mut max = Vec3f::new( tris[0].verts[0].position.x,
                              tris[0].verts[0].position.y,
                              tris[0].verts[0].position.z);
    for tris.each |curr|
    {<commit_msg> #12 First try at triangle strip voxel rendering<commit_after>/*
    Copyright 2013 Jesse 'Jeaye' Wilkerson
    See licensing in LICENSE file, or at:
        http://www.opensource.org/licenses/BSD-3-Clause

    File: obj/voxel/map.rs
    Author: Jesse 'Jeaye' Wilkerson
    Description:
      A voxelization of arbitrary triangles
      into OpenGL-ready cubes.
*/

use math::{ Vec3f, Vec3i, Vec3u8 };
use primitive::Vertex_PC;
use primitive::Triangle;
use primitive::{ Cube };
use super::{ Vertex, Behavior, Default };

#[path = "../../gl/mod.rs"]
mod gl;
#[path = "../../gl/util.rs"]
mod util;
#[macro_escape]
#[path = "../../gl/check.rs"]
mod check;

/* TODO: Voxels should have indexed verts or should be drawn as a fan. */
struct Map
{
  resolution: u32,
  voxel_size: f32,

  vao: gl::GLuint,
  vbo: gl::GLuint,
  ibo: gl::GLuint,

  voxels: ~[Behavior],
  indices: ~[Vertex],
}

impl Map
{
  pub fn new(tris: &[Triangle], res: u32) -> Map
  {
    let mut map = Map
    {
      resolution: res,
      voxel_size: 0.0,
      vao: 0,
      vbo: 0,
      ibo: 0,
      voxels: ~[],
      indices: ~[],
    };

    map.voxelize(tris);

    /* Single voxel that will be instance-rendered. */
    //let mut voxel = ~[];
    //voxel.push(Cube::new(map.voxel_size, Vec3f::zero()));
    let h: f32 = map.voxel_size / 2.0;
    let voxel: ~[f32] =
    ~[
      -h,-h,h,  h,-h,h,   
      -h,h,h,   h,h,h,    

      h,-h,h,   h,-h,-h,  
      h,h,h,    h,h,-h,   

      h,-h,-h,  -h,-h,-h, 
      h,h,-h,   -h,h,-h,  

      -h,-h,-h, -h,-h,h,  
      -h,h,-h,  -h,h,h,   

      -h,-h,-h, h,-h,-h,  
      -h,-h,h,  h,-h,h,   

      -h,h,h,   h,h,h,    
      -h,h,-h,  h,h,-h,   
    ];

    map.vao = check!(gl::gen_vertex_arrays(1))[0]; /* TODO: Check these. */
    map.vbo = check!(gl::gen_buffers(1))[0];
    map.ibo = check!(gl::gen_buffers(1))[0];
    check!(gl::bind_vertex_array(map.vao));
    check!(gl::bind_buffer(gl::ARRAY_BUFFER, map.vbo));
    check!(gl::buffer_data(gl::ARRAY_BUFFER, voxel, gl::STATIC_DRAW));

    check!(gl::bind_buffer(gl::ARRAY_BUFFER, map.ibo));
    check!(gl::buffer_data(gl::ARRAY_BUFFER, map.indices, gl::STATIC_DRAW));

    map
  }

  pub fn draw(&self)
  {
    check!(gl::bind_vertex_array(self.vao));

    check!(gl::bind_buffer(gl::ARRAY_BUFFER, self.vbo));
    //check!(gl::vertex_attrib_pointer_f32(0, 3, false, (sys::size_of::<Vertex_PC>()) as i32, 0));
    check!(gl::vertex_attrib_pointer_f32(0, 3, false, 0, 0));
    check!(gl::enable_vertex_attrib_array(0));

    check!(gl::bind_buffer(gl::ARRAY_BUFFER, self.ibo));

    check!(gl::vertex_attrib_pointer_i32(1, 3, false, (sys::size_of::<Vertex>()) as i32, 0));
    check!(gl::enable_vertex_attrib_array(1));
    check!(gl::vertex_attrib_divisor(1, 1));

    check!(gl::vertex_attrib_pointer_u8(2, 3, true, (sys::size_of::<Vertex>()) as i32, 
                                        (sys::size_of::<Vec3i>()) as u32));
    check!(gl::enable_vertex_attrib_array(2));
    check!(gl::vertex_attrib_divisor(2, 1));

    //check!(gl::polygon_mode(gl::FRONT_AND_BACK, gl::LINE));
    check!(gl::draw_arrays_instanced(gl::TRIANGLE_STRIP, 0, 24, self.indices.len() as i32));
    //check!(gl::polygon_mode(gl::FRONT_AND_BACK, gl::FILL));

    check!(gl::disable_vertex_attrib_array(0));
    check!(gl::disable_vertex_attrib_array(1));
    check!(gl::disable_vertex_attrib_array(2));
    check!(gl::bind_vertex_array(0));
    check!(gl::bind_buffer(gl::ARRAY_BUFFER, 0));
  }

  priv fn voxelize(&mut self, tris: &[Triangle])
  {
    /* Require at least one triangle. */
    assert!(tris.len() >= 1);
    debug!("VOXEL: Incoming triangles: %?", tris.len());

    /* Bounding box of vert dimensions. */
    let mut min = Vec3f::new( tris[0].verts[0].position.x,
                              tris[0].verts[0].position.y, 
                              tris[0].verts[0].position.z);
    let mut max = Vec3f::new( tris[0].verts[0].position.x,
                              tris[0].verts[0].position.y,
                              tris[0].verts[0].position.z);
    for tris.each |curr|
    {
{'fraction_non_alphanumeric': 0.19078775189740904, 'fraction_numerical': 0.023030620256477362, 'mean_word_length': 2.9662688116242864, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6303798', 'n_tokens_mistral': 3050, 'n_tokens_neox': 2905, 'n_words': 523}