Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.91 KB | None | 0 0
  1. #![feature(optin_builtin_traits)]
  2.  
  3. pub extern crate gl;
  4. pub extern crate core;
  5. pub extern crate image;
  6. pub extern crate glutin;
  7.  
  8. pub mod shader;
  9. use shader::*;
  10.  
  11. use core::math::*;
  12. use core::graphics::Camera;
  13.  
  14. use glutin::{EventsLoop, Event, GlWindow, WindowBuilder, GlContext, ContextBuilder, WindowEvent};
  15. use gl::types::*;
  16. use image::GenericImage;
  17.  
  18. use std::mem;
  19. use std::ptr;
  20. use std::os::raw::c_void;
  21.  
  22. fn main() {
  23. unsafe {
  24. // create window and event loop, load OpenGL
  25. let mut events_loop = EventsLoop::new();
  26. let window = WindowBuilder::new()
  27. .with_title("gl test")
  28. .with_dimensions(600, 600);
  29. let context = ContextBuilder::new()
  30. .with_vsync(true);
  31. let window = GlWindow::new(window, context, &events_loop)
  32. .expect("window creation failure");
  33. window.make_current().unwrap();
  34. gl::load_with(|sym| window.get_proc_address(sym) as *const _);
  35.  
  36. let mut cam = Camera::default();
  37. println!("{:#?}", cam);
  38.  
  39. gl::ClearColor(0.5294, 0.8078, 0.9803, 1.0);
  40. //gl::ClearColor(1.0, 1.0, 1.0, 1.0);
  41. gl::Enable(gl::DEPTH_TEST);
  42.  
  43. let shader = Shader::new(NewShader::VertFrag {
  44. vert: include_str!("shaders/a.vert"),
  45. frag: include_str!("shaders/a.frag")
  46. }).unwrap();
  47. let u_model = shader.uniform("model").wrn();
  48. let u_view = shader.uniform("view").wrn();
  49. let u_proj = shader.uniform("projection").wrn();
  50. let u_tex_1 = shader.uniform("texture1").wrn();
  51. let u_tex_2 = shader.uniform("texture2").wrn();
  52.  
  53. let vertices: [f32; 180] = [
  54. -0.5, -0.5, -0.5, 0.0, 0.0,
  55. 0.5, -0.5, -0.5, 1.0, 0.0,
  56. 0.5, 0.5, -0.5, 1.0, 1.0,
  57. 0.5, 0.5, -0.5, 1.0, 1.0,
  58. -0.5, 0.5, -0.5, 0.0, 1.0,
  59. -0.5, -0.5, -0.5, 0.0, 0.0,
  60.  
  61. -0.5, -0.5, 0.5, 0.0, 0.0,
  62. 0.5, -0.5, 0.5, 1.0, 0.0,
  63. 0.5, 0.5, 0.5, 1.0, 1.0,
  64. 0.5, 0.5, 0.5, 1.0, 1.0,
  65. -0.5, 0.5, 0.5, 0.0, 1.0,
  66. -0.5, -0.5, 0.5, 0.0, 0.0,
  67.  
  68. -0.5, 0.5, 0.5, 1.0, 0.0,
  69. -0.5, 0.5, -0.5, 1.0, 1.0,
  70. -0.5, -0.5, -0.5, 0.0, 1.0,
  71. -0.5, -0.5, -0.5, 0.0, 1.0,
  72. -0.5, -0.5, 0.5, 0.0, 0.0,
  73. -0.5, 0.5, 0.5, 1.0, 0.0,
  74.  
  75. 0.5, 0.5, 0.5, 1.0, 0.0,
  76. 0.5, 0.5, -0.5, 1.0, 1.0,
  77. 0.5, -0.5, -0.5, 0.0, 1.0,
  78. 0.5, -0.5, -0.5, 0.0, 1.0,
  79. 0.5, -0.5, 0.5, 0.0, 0.0,
  80. 0.5, 0.5, 0.5, 1.0, 0.0,
  81.  
  82. -0.5, -0.5, -0.5, 0.0, 1.0,
  83. 0.5, -0.5, -0.5, 1.0, 1.0,
  84. 0.5, -0.5, 0.5, 1.0, 0.0,
  85. 0.5, -0.5, 0.5, 1.0, 0.0,
  86. -0.5, -0.5, 0.5, 0.0, 0.0,
  87. -0.5, -0.5, -0.5, 0.0, 1.0,
  88.  
  89. -0.5, 0.5, -0.5, 0.0, 1.0,
  90. 0.5, 0.5, -0.5, 1.0, 1.0,
  91. 0.5, 0.5, 0.5, 1.0, 0.0,
  92. 0.5, 0.5, 0.5, 1.0, 0.0,
  93. -0.5, 0.5, 0.5, 0.0, 0.0,
  94. -0.5, 0.5, -0.5, 0.0, 1.0
  95. ];
  96.  
  97. let positions: [V3F; 10] = [
  98. V3F::new(0.0, 0.0, 0.0),
  99. V3F::new(2.0, 5.0, -15.0),
  100. V3F::new(-1.5, -2.2, -2.5),
  101. V3F::new(-3.8, -2.0, -12.3),
  102. V3F::new(2.4, -0.4, -3.5),
  103. V3F::new(-1.7, 3.0, -7.5),
  104. V3F::new(1.3, -2.0, -2.5),
  105. V3F::new(1.5, 2.0, -2.5),
  106. V3F::new(1.5, 0.2, -1.5),
  107. V3F::new(-1.3, 1.0, -1.5)
  108. ];
  109.  
  110. let mut vbo = 0;
  111. let mut vao = 0;
  112. gl::GenVertexArrays(1, &mut vao);
  113. gl::GenBuffers(1, &mut vbo);
  114.  
  115. gl::BindVertexArray(vao);
  116. gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
  117. gl::BufferData(
  118. gl::ARRAY_BUFFER,
  119. (vertices.len() * mem::size_of::<GLfloat>()) as GLsizeiptr,
  120. &vertices[0] as *const f32 as *const c_void,
  121. gl::STATIC_DRAW
  122. );
  123. let stride = 5 * mem::size_of::<GLfloat>() as GLsizei;
  124. //let stride = 0 as GLsizei;
  125. // pos attribute
  126. gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, stride, ptr::null());
  127. gl::EnableVertexAttribArray(0);
  128. // tex coord atrribute
  129. gl::VertexAttribPointer(1, 2, gl::FLOAT, gl::FALSE, stride, (3 * mem::size_of::<GLfloat>()) as *const c_void);
  130. gl::EnableVertexAttribArray(1);
  131.  
  132. // load and create a texture
  133. let mut texture = 0;
  134. // texture 1
  135. gl::GenTextures(1, &mut texture);
  136. gl::BindTexture(gl::TEXTURE_2D, texture);
  137. // set texture wrapping parameters
  138. gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::REPEAT as i32);
  139. gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::REPEAT as i32);
  140. // set texture filtering parameters
  141. gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR as i32);
  142. gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::LINEAR as i32);
  143. // load image, create texture and generate mipmaps
  144. let img = image::load_from_memory(include_bytes!("textures/container.jpg"))
  145. .expect("texture load failure")
  146. .flipv();
  147. let img_data = img.raw_pixels();
  148.  
  149. gl::TexImage2D(
  150. gl::TEXTURE_2D,
  151. 0,
  152. gl::RGB as i32,
  153. img.width() as i32,
  154. img.height() as i32,
  155. 0,
  156. gl::RGB,
  157. gl::UNSIGNED_BYTE,
  158. &img_data[0] as *const u8 as *const c_void
  159. );
  160. gl::GenerateMipmap(gl::TEXTURE_2D);
  161.  
  162. // set uniforms
  163. shader.activate();
  164. u_tex_1.set_i32(texture as i32);
  165. u_tex_2.set_i32(texture as i32);
  166.  
  167. // matrix test
  168. /*
  169. let mat = Matrix4::new(
  170. Vector4::new(1.0, 2.0, 3.0, 4.0),
  171. Vector4::new(5.0, 6.0, 7.0, 8.0),
  172. Vector4::new(9.0, 10.0, 11.0, 12.0),
  173. Vector4::new(13.0, 14.0, 15.0, 16.0)
  174. );
  175. //let p: *const f32 = std::mem::transmute(&mat);
  176. let aa = core::graphics::mat_to_arr(&mat);
  177. let p: *const f32 = &aa[0][0] as *const f32;
  178. let a: &[f32] = std::slice::from_raw_parts(p, 16);
  179. println!("mat={:#?}", a);
  180. */
  181.  
  182.  
  183. // main loop
  184. let mut running = true;
  185. while running {
  186. let cam_pos = cam.pos;
  187. cam.rotate_around(cam_pos, UP_3F, 3.0);
  188. cam.pos = cam.pos + (cam.dir * 0.016);
  189.  
  190. // poll events
  191. events_loop.poll_events(|event| match event {
  192. Event::WindowEvent {
  193. event,
  194. ..
  195. } => match event {
  196. WindowEvent::Closed => running = false,
  197. WindowEvent::Resized(w, h) => window.resize(w, h),
  198. _ => ()
  199. },
  200. _ => ()
  201. });
  202.  
  203. // render
  204.  
  205. // clear
  206. gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
  207.  
  208. // bind texture(s)
  209. gl::ActiveTexture(0);
  210. gl::BindTexture(gl::TEXTURE_2D, texture);
  211.  
  212. // activate shader
  213. shader.activate();
  214.  
  215. // set proj/view uniforms
  216. u_proj.set_m4f(&cam.proj(), false);
  217. u_view.set_m4f(&cam.view(), false);
  218. //println!("{:#?}", cam.view());
  219.  
  220. // render boxes
  221. gl::BindVertexArray(vao);
  222. for &pos in positions.iter() {
  223. // set model uniforms
  224. let model = trans(&[
  225. TransOp::Translate(pos * 50.0)
  226. ]);
  227. u_model.set_m4f(&model, false);
  228.  
  229. gl::DrawArrays(gl::TRIANGLES, 0, 36);
  230. }
  231.  
  232. // finish
  233. window.swap_buffers().unwrap();
  234. }
  235.  
  236. };
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement