Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(optin_builtin_traits)]
- pub extern crate gl;
- pub extern crate core;
- pub extern crate image;
- pub extern crate glutin;
- pub mod shader;
- use shader::*;
- use core::math::*;
- use core::graphics::Camera;
- use glutin::{EventsLoop, Event, GlWindow, WindowBuilder, GlContext, ContextBuilder, WindowEvent};
- use gl::types::*;
- use image::GenericImage;
- use std::mem;
- use std::ptr;
- use std::os::raw::c_void;
- fn main() {
- unsafe {
- // create window and event loop, load OpenGL
- let mut events_loop = EventsLoop::new();
- let window = WindowBuilder::new()
- .with_title("gl test")
- .with_dimensions(600, 600);
- let context = ContextBuilder::new()
- .with_vsync(true);
- let window = GlWindow::new(window, context, &events_loop)
- .expect("window creation failure");
- window.make_current().unwrap();
- gl::load_with(|sym| window.get_proc_address(sym) as *const _);
- let mut cam = Camera::default();
- println!("{:#?}", cam);
- gl::ClearColor(0.5294, 0.8078, 0.9803, 1.0);
- //gl::ClearColor(1.0, 1.0, 1.0, 1.0);
- gl::Enable(gl::DEPTH_TEST);
- let shader = Shader::new(NewShader::VertFrag {
- vert: include_str!("shaders/a.vert"),
- frag: include_str!("shaders/a.frag")
- }).unwrap();
- let u_model = shader.uniform("model").wrn();
- let u_view = shader.uniform("view").wrn();
- let u_proj = shader.uniform("projection").wrn();
- let u_tex_1 = shader.uniform("texture1").wrn();
- let u_tex_2 = shader.uniform("texture2").wrn();
- let vertices: [f32; 180] = [
- -0.5, -0.5, -0.5, 0.0, 0.0,
- 0.5, -0.5, -0.5, 1.0, 0.0,
- 0.5, 0.5, -0.5, 1.0, 1.0,
- 0.5, 0.5, -0.5, 1.0, 1.0,
- -0.5, 0.5, -0.5, 0.0, 1.0,
- -0.5, -0.5, -0.5, 0.0, 0.0,
- -0.5, -0.5, 0.5, 0.0, 0.0,
- 0.5, -0.5, 0.5, 1.0, 0.0,
- 0.5, 0.5, 0.5, 1.0, 1.0,
- 0.5, 0.5, 0.5, 1.0, 1.0,
- -0.5, 0.5, 0.5, 0.0, 1.0,
- -0.5, -0.5, 0.5, 0.0, 0.0,
- -0.5, 0.5, 0.5, 1.0, 0.0,
- -0.5, 0.5, -0.5, 1.0, 1.0,
- -0.5, -0.5, -0.5, 0.0, 1.0,
- -0.5, -0.5, -0.5, 0.0, 1.0,
- -0.5, -0.5, 0.5, 0.0, 0.0,
- -0.5, 0.5, 0.5, 1.0, 0.0,
- 0.5, 0.5, 0.5, 1.0, 0.0,
- 0.5, 0.5, -0.5, 1.0, 1.0,
- 0.5, -0.5, -0.5, 0.0, 1.0,
- 0.5, -0.5, -0.5, 0.0, 1.0,
- 0.5, -0.5, 0.5, 0.0, 0.0,
- 0.5, 0.5, 0.5, 1.0, 0.0,
- -0.5, -0.5, -0.5, 0.0, 1.0,
- 0.5, -0.5, -0.5, 1.0, 1.0,
- 0.5, -0.5, 0.5, 1.0, 0.0,
- 0.5, -0.5, 0.5, 1.0, 0.0,
- -0.5, -0.5, 0.5, 0.0, 0.0,
- -0.5, -0.5, -0.5, 0.0, 1.0,
- -0.5, 0.5, -0.5, 0.0, 1.0,
- 0.5, 0.5, -0.5, 1.0, 1.0,
- 0.5, 0.5, 0.5, 1.0, 0.0,
- 0.5, 0.5, 0.5, 1.0, 0.0,
- -0.5, 0.5, 0.5, 0.0, 0.0,
- -0.5, 0.5, -0.5, 0.0, 1.0
- ];
- let positions: [V3F; 10] = [
- V3F::new(0.0, 0.0, 0.0),
- V3F::new(2.0, 5.0, -15.0),
- V3F::new(-1.5, -2.2, -2.5),
- V3F::new(-3.8, -2.0, -12.3),
- V3F::new(2.4, -0.4, -3.5),
- V3F::new(-1.7, 3.0, -7.5),
- V3F::new(1.3, -2.0, -2.5),
- V3F::new(1.5, 2.0, -2.5),
- V3F::new(1.5, 0.2, -1.5),
- V3F::new(-1.3, 1.0, -1.5)
- ];
- let mut vbo = 0;
- let mut vao = 0;
- gl::GenVertexArrays(1, &mut vao);
- gl::GenBuffers(1, &mut vbo);
- gl::BindVertexArray(vao);
- gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
- gl::BufferData(
- gl::ARRAY_BUFFER,
- (vertices.len() * mem::size_of::<GLfloat>()) as GLsizeiptr,
- &vertices[0] as *const f32 as *const c_void,
- gl::STATIC_DRAW
- );
- let stride = 5 * mem::size_of::<GLfloat>() as GLsizei;
- //let stride = 0 as GLsizei;
- // pos attribute
- gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, stride, ptr::null());
- gl::EnableVertexAttribArray(0);
- // tex coord atrribute
- gl::VertexAttribPointer(1, 2, gl::FLOAT, gl::FALSE, stride, (3 * mem::size_of::<GLfloat>()) as *const c_void);
- gl::EnableVertexAttribArray(1);
- // load and create a texture
- let mut texture = 0;
- // texture 1
- gl::GenTextures(1, &mut texture);
- gl::BindTexture(gl::TEXTURE_2D, texture);
- // set texture wrapping parameters
- gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::REPEAT as i32);
- gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::REPEAT as i32);
- // set texture filtering parameters
- gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR as i32);
- gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::LINEAR as i32);
- // load image, create texture and generate mipmaps
- let img = image::load_from_memory(include_bytes!("textures/container.jpg"))
- .expect("texture load failure")
- .flipv();
- let img_data = img.raw_pixels();
- gl::TexImage2D(
- gl::TEXTURE_2D,
- 0,
- gl::RGB as i32,
- img.width() as i32,
- img.height() as i32,
- 0,
- gl::RGB,
- gl::UNSIGNED_BYTE,
- &img_data[0] as *const u8 as *const c_void
- );
- gl::GenerateMipmap(gl::TEXTURE_2D);
- // set uniforms
- shader.activate();
- u_tex_1.set_i32(texture as i32);
- u_tex_2.set_i32(texture as i32);
- // matrix test
- /*
- let mat = Matrix4::new(
- Vector4::new(1.0, 2.0, 3.0, 4.0),
- Vector4::new(5.0, 6.0, 7.0, 8.0),
- Vector4::new(9.0, 10.0, 11.0, 12.0),
- Vector4::new(13.0, 14.0, 15.0, 16.0)
- );
- //let p: *const f32 = std::mem::transmute(&mat);
- let aa = core::graphics::mat_to_arr(&mat);
- let p: *const f32 = &aa[0][0] as *const f32;
- let a: &[f32] = std::slice::from_raw_parts(p, 16);
- println!("mat={:#?}", a);
- */
- // main loop
- let mut running = true;
- while running {
- let cam_pos = cam.pos;
- cam.rotate_around(cam_pos, UP_3F, 3.0);
- cam.pos = cam.pos + (cam.dir * 0.016);
- // poll events
- events_loop.poll_events(|event| match event {
- Event::WindowEvent {
- event,
- ..
- } => match event {
- WindowEvent::Closed => running = false,
- WindowEvent::Resized(w, h) => window.resize(w, h),
- _ => ()
- },
- _ => ()
- });
- // render
- // clear
- gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
- // bind texture(s)
- gl::ActiveTexture(0);
- gl::BindTexture(gl::TEXTURE_2D, texture);
- // activate shader
- shader.activate();
- // set proj/view uniforms
- u_proj.set_m4f(&cam.proj(), false);
- u_view.set_m4f(&cam.view(), false);
- //println!("{:#?}", cam.view());
- // render boxes
- gl::BindVertexArray(vao);
- for &pos in positions.iter() {
- // set model uniforms
- let model = trans(&[
- TransOp::Translate(pos * 50.0)
- ]);
- u_model.set_m4f(&model, false);
- gl::DrawArrays(gl::TRIANGLES, 0, 36);
- }
- // finish
- window.swap_buffers().unwrap();
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement