Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

105 řádky
2.9 KiB

  1. #![allow(dead_code)]
  2. use std::mem::size_of;
  3. use glc::{
  4. buffer::{Buffer, Usage},
  5. misc::Bindable,
  6. shader::{Program, Type, Uniform},
  7. vector::{Vector2f, Vector4f},
  8. vertex_array::{DataType, VertexArray},
  9. };
  10. use space_crush_common::misc::LogResult;
  11. use specs::World;
  12. use crate::{constants::UNIFORM_BUFFER_INDEX_CAMERA, misc::WorldHelper, Error};
  13. pub struct Geometry {
  14. vertex_array_quad: VertexArray,
  15. vertex_array_line: VertexArray,
  16. program_line: Program,
  17. location_line_color: gl::GLint,
  18. }
  19. impl Geometry {
  20. pub fn new(world: &mut World) -> Result<Self, Error> {
  21. let vertex_array_quad = create_array_quad()?;
  22. let vertex_array_line = create_array_line()?;
  23. let program_line = world.load_program(vec![
  24. (Type::Vertex, "resources/shader/line/vert.glsl"),
  25. (Type::Fragment, "resources/shader/line/frag.glsl"),
  26. ])?;
  27. program_line.uniform_block_binding("Camera", UNIFORM_BUFFER_INDEX_CAMERA)?;
  28. let location_line_color = program_line.uniform_location("uColor")?;
  29. Ok(Self {
  30. vertex_array_quad,
  31. vertex_array_line,
  32. program_line,
  33. location_line_color,
  34. })
  35. }
  36. pub fn render_quad(&self) {
  37. self.vertex_array_quad.bind();
  38. gl::draw_arrays(gl::TRIANGLE_FAN, 0, 4);
  39. self.vertex_array_quad.unbind();
  40. }
  41. pub fn render_lines(&mut self, c: Vector4f, points: &[Vector2f]) {
  42. self.vertex_array_line.buffers_mut()[0]
  43. .buffer_data(Usage::StaticDraw, points)
  44. .unwrap();
  45. self.vertex_array_line.bind();
  46. self.program_line.bind();
  47. self.program_line
  48. .uniform(self.location_line_color, Uniform::Vector4f(&c))
  49. .warn("Unable to set line color");
  50. gl::draw_arrays(gl::LINE_STRIP, 0, points.len() as _);
  51. self.program_line.unbind();
  52. self.vertex_array_line.unbind();
  53. }
  54. }
  55. fn create_array_quad() -> Result<VertexArray, Error> {
  56. let vertices = &[
  57. Vector2f::new(-0.5, -0.5),
  58. Vector2f::new(0.5, -0.5),
  59. Vector2f::new(0.5, 0.5),
  60. Vector2f::new(-0.5, 0.5),
  61. ];
  62. const STRIDE_POS: gl::GLsizei = size_of::<Vector2f>() as gl::GLsizei;
  63. const OFFSET_POS: gl::GLsizei = 0;
  64. let mut buffer = Buffer::new()?;
  65. buffer.buffer_data(Usage::StaticDraw, vertices)?;
  66. let vertex_array = VertexArray::builder()
  67. .bind_buffer(buffer)
  68. .vertex_attrib_pointer(0, 2, DataType::Float, false, STRIDE_POS, OFFSET_POS)?
  69. .build()?;
  70. Ok(vertex_array)
  71. }
  72. fn create_array_line() -> Result<VertexArray, Error> {
  73. const STRIDE_POS: gl::GLsizei = size_of::<Vector2f>() as gl::GLsizei;
  74. const OFFSET_POS: gl::GLsizei = 0;
  75. let buffer = Buffer::new()?;
  76. let vertex_array = VertexArray::builder()
  77. .bind_buffer(buffer)
  78. .vertex_attrib_pointer(0, 2, DataType::Float, false, STRIDE_POS, OFFSET_POS)?
  79. .build()?;
  80. Ok(vertex_array)
  81. }