Created
February 18, 2016 22:16
-
-
Save bvssvni/ca0377fae4413da89584 to your computer and use it in GitHub Desktop.
Cube example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use gl; | |
use gl::types::*; | |
use image; | |
use helper; | |
use color::Color; | |
use mat4::Mat4; | |
pub struct Cube { | |
program_id: GLuint, | |
vertex_array_id: GLuint, | |
texture_id: GLuint, | |
// Parameters. | |
vertex: GLuint, | |
uv: GLuint, | |
// Locations. | |
vertex_location: GLint, | |
uv_location: GLint, | |
color_location: GLint, | |
mvp_location: GLint, | |
} | |
impl Drop for Cube { | |
fn drop(&mut self) { | |
unsafe { | |
gl::DeleteProgram(self.program_id); | |
gl::DeleteTextures(1, &self.texture_id); | |
gl::DeleteBuffers(1, &self.vertex); | |
gl::DeleteBuffers(1, &self.uv); | |
gl::DeleteVertexArrays(1, &self.vertex_array_id); | |
} | |
} | |
} | |
impl Cube { | |
pub fn new() -> Cube { | |
let vertex_shader = include_str!("../../assets/solid.vs"); | |
let fragment_shader = include_str!("../../assets/solid.fs"); | |
let program_id = helper::create_program(vertex_shader, fragment_shader) | |
.unwrap(); | |
let vertex_data: Vec<GLfloat> = vec![ | |
-1.0,-1.0,-1.0, | |
-1.0,-1.0, 1.0, | |
-1.0, 1.0, 1.0, | |
1.0, 1.0,-1.0, | |
-1.0,-1.0,-1.0, | |
-1.0, 1.0,-1.0, | |
1.0,-1.0, 1.0, | |
-1.0,-1.0,-1.0, | |
1.0,-1.0,-1.0, | |
1.0, 1.0,-1.0, | |
1.0,-1.0,-1.0, | |
-1.0,-1.0,-1.0, | |
-1.0,-1.0,-1.0, | |
-1.0, 1.0, 1.0, | |
-1.0, 1.0,-1.0, | |
1.0,-1.0, 1.0, | |
-1.0,-1.0, 1.0, | |
-1.0,-1.0,-1.0, | |
-1.0, 1.0, 1.0, | |
-1.0,-1.0, 1.0, | |
1.0,-1.0, 1.0, | |
1.0, 1.0, 1.0, | |
1.0,-1.0,-1.0, | |
1.0, 1.0,-1.0, | |
1.0,-1.0,-1.0, | |
1.0, 1.0, 1.0, | |
1.0,-1.0, 1.0, | |
1.0, 1.0, 1.0, | |
1.0, 1.0,-1.0, | |
-1.0, 1.0,-1.0, | |
1.0, 1.0, 1.0, | |
-1.0, 1.0,-1.0, | |
-1.0, 1.0, 1.0, | |
1.0, 1.0, 1.0, | |
-1.0, 1.0, 1.0, | |
1.0,-1.0, 1.0 | |
]; | |
let uv_data: Vec<GLfloat> = vec![ | |
0.000059, 0.000004, | |
0.000103, 0.336048, | |
0.335973, 0.335903, | |
1.000023, 0.000013, | |
0.667979, 0.335851, | |
0.999958, 0.336064, | |
0.667979, 0.335851, | |
0.336024, 0.671877, | |
0.667969, 0.671889, | |
1.000023, 0.000013, | |
0.668104, 0.000013, | |
0.667979, 0.335851, | |
0.000059, 0.000004, | |
0.335973, 0.335903, | |
0.336098, 0.000071, | |
0.667979, 0.335851, | |
0.335973, 0.335903, | |
0.336024, 0.671877, | |
1.000004, 0.671847, | |
0.999958, 0.336064, | |
0.667979, 0.335851, | |
0.668104, 0.000013, | |
0.335973, 0.335903, | |
0.667979, 0.335851, | |
0.335973, 0.335903, | |
0.668104, 0.000013, | |
0.336098, 0.000071, | |
0.000103, 0.336048, | |
0.000004, 0.671870, | |
0.336024, 0.671877, | |
0.000103, 0.336048, | |
0.336024, 0.671877, | |
0.335973, 0.335903, | |
0.667969, 0.671889, | |
1.000004, 0.671847, | |
0.667979, 0.335851 | |
]; | |
let mut vertex_array_id: GLuint = 0; | |
unsafe { | |
gl::GenVertexArrays(1, &mut vertex_array_id); | |
gl::BindVertexArray(vertex_array_id); | |
} | |
let vertex = helper::create_static_buffer(&vertex_data); | |
let uv = helper::create_static_buffer(&uv_data); | |
let vertex_location = helper::get_attrib_location(program_id, "vertex"); | |
let uv_location = helper::get_attrib_location(program_id, "uv"); | |
let color_location = helper::get_uniform_location(program_id, "color"); | |
let mvp_location = helper::get_uniform_location(program_id, "mvp"); | |
let img = image::open("assets/bridge.png").unwrap().to_rgba(); | |
let mut texture_id: GLuint = 0; | |
unsafe { | |
gl::GenTextures(1, &mut texture_id); | |
gl::BindTexture(gl::TEXTURE_2D, texture_id); | |
let (width, height) = img.dimensions(); | |
gl::TexImage2D(gl::TEXTURE_2D, 0, gl::RGBA as i32, | |
width as i32, height as i32, 0, | |
gl::RGBA, gl::UNSIGNED_BYTE, img.as_ptr() as *const _); | |
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, | |
gl::NEAREST as i32); | |
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, | |
gl::NEAREST as i32); | |
} | |
Cube { | |
program_id: program_id, | |
vertex_array_id: vertex_array_id, | |
texture_id: texture_id, | |
vertex: vertex, | |
uv: uv, | |
vertex_location: vertex_location, | |
uv_location: uv_location, | |
color_location: color_location, | |
mvp_location: mvp_location, | |
} | |
} | |
pub fn draw(&self, color: Color, mvp: Mat4) { | |
unsafe { | |
helper::enable_depth_less(); | |
gl::UseProgram(self.program_id); | |
gl::BindVertexArray(self.vertex_array_id); | |
gl::UniformMatrix4fv(self.mvp_location as i32, 1, gl::FALSE, | |
mvp.as_ptr() as *const _); | |
gl::Uniform4f(self.color_location, color[0], color[1], color[2], | |
color[3]); | |
gl::EnableVertexAttribArray(0); | |
gl::BindBuffer(gl::ARRAY_BUFFER, self.vertex); | |
gl::VertexAttribPointer( | |
self.vertex_location as GLuint, | |
3, | |
gl::FLOAT, | |
gl::FALSE, | |
0, | |
0 as *const _ | |
); | |
gl::BindTexture(gl::TEXTURE_2D, self.texture_id); | |
gl::EnableVertexAttribArray(1); | |
gl::BindBuffer(gl::ARRAY_BUFFER, self.uv); | |
gl::VertexAttribPointer( | |
self.uv_location as GLuint, | |
2, | |
gl::FLOAT, | |
gl::FALSE, | |
0, | |
0 as *const _ | |
); | |
gl::DrawArrays(gl::TRIANGLES, 0, 12 * 3); | |
gl::DisableVertexAttribArray(1); | |
gl::DisableVertexAttribArray(0); | |
gl::BindVertexArray(0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment