<commit_before>// See LICENSE file for copyright and license details.

use core::types::MInt;
use visualizer::mgl::{Vbo, Vao, Triangles};
use visualizer::types::{Color3, VertexCoord, TextureCoord};
use visualizer::shader::Shader;
use visualizer::texture::Texture;

pub struct MeshId{pub id: MInt}

pub struct Mesh {
    vertex_coords_vbo: Vbo,
    color_vbo: Option<Vbo>,
    texture_coords_vbo: Option<Vbo>,
    texture: Option<Texture>,
    length: MInt,
    vao: Vao,
}

impl Mesh {
    pub fn new(data: &[VertexCoord]) -> Mesh {
        let length = data.len() as MInt;
        Mesh {
            vertex_coords_vbo: Vbo::from_data(data),
            color_vbo: None,
            texture_coords_vbo: None,
            texture: None,
            length: length,
            vao: Vao::new(),
        }
    }

    pub fn set_color(&mut self, data: &[Color3]) {
        assert_eq!(self.length, data.len() as MInt);
        self.color_vbo = Some(Vbo::from_data(data));
    }

    pub fn set_texture(&mut self, texture: Texture, data: &[TextureCoord]) {
        assert_eq!(self.length, data.len() as MInt);
        self.texture_coords_vbo = Some(Vbo::from_data(data));
        self.texture = Some(texture);
    }

    pub fn prepare(&mut self, shader: &Shader) {
        self.vao.bind();
        shader.activate();
        if !self.texture_coords_vbo.is_none() {
            self.texture_coords_vbo.get_ref().bind();
            shader.enable_attr("in_texture_coordinates", 2);
        }
        if !self.color_vbo.is_none() {
            self.color_vbo.get_ref().bind();
            shader.enable_attr("color", 3);
        }
        self.vertex_coords_vbo.bind();
        shader.enable_attr("in_vertex_coordinates", 3);
        self.vao.unbind();
    }

    pub fn draw(&self, shader: &Shader) {
        self.vao.bind();
        if !self.texture.is_none() {
            self.texture.unwrap().enable(shader);
        }
        self.vao.draw_array(Triangles, self.length);
        self.vao.unbind();
    }
}

// vim: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab:<commit_msg>Replaced few 'unwrap' and 'is_none' with 'match'<commit_after>// See LICENSE file for copyright and license details.

use core::types::MInt;
use visualizer::mgl::{Vbo, Vao, Triangles};
use visualizer::types::{Color3, VertexCoord, TextureCoord};
use visualizer::shader::Shader;
use visualizer::texture::Texture;

pub struct MeshId{pub id: MInt}

pub struct Mesh {
    vertex_coords_vbo: Vbo,
    color_vbo: Option<Vbo>,
    texture_coords_vbo: Option<Vbo>,
    texture: Option<Texture>,
    length: MInt,
    vao: Vao,
}

impl Mesh {
    pub fn new(data: &[VertexCoord]) -> Mesh {
        let length = data.len() as MInt;
        Mesh {
            vertex_coords_vbo: Vbo::from_data(data),
            color_vbo: None,
            texture_coords_vbo: None,
            texture: None,
            length: length,
            vao: Vao::new(),
        }
    }

    pub fn set_color(&mut self, data: &[Color3]) {
        assert_eq!(self.length, data.len() as MInt);
        self.color_vbo = Some(Vbo::from_data(data));
    }

    pub fn set_texture(&mut self, texture: Texture, data: &[TextureCoord]) {
        assert_eq!(self.length, data.len() as MInt);
        self.texture_coords_vbo = Some(Vbo::from_data(data));
        self.texture = Some(texture);
    }

    pub fn prepare(&mut self, shader: &Shader) {
        self.vao.bind();
        shader.activate();
        match self.texture_coords_vbo {
            Some(ref vbo) => {
                vbo.bind();
                shader.enable_attr("in_texture_coordinates", 2);
            },
            None => {},
        }
        match self.color_vbo {
            Some(ref vbo) => {
                vbo.bind();
                shader.enable_attr("color", 3);
            },
            None => {},
        }
        self.vertex_coords_vbo.bind();
        shader.enable_attr("in_vertex_coordinates", 3);
        self.vao.unbind();
    }

    pub fn draw(&self, shader: &Shader) {
        self.vao.bind();
        match self.texture {
            Some(ref texture) => texture.enable(shader),
            None => {},
        }
        self.vao.draw_array(Triangles, self.length);
        self.vao.unbind();
    }
}

// vim: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab:
{'fraction_non_alphanumeric': 0.13793905559432426, 'fraction_numerical': 0.0037217957664573155, 'mean_word_length': 2.3076923076923075, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '2449841', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1433, 'n_words': 286}