easy_opengl/lib.rs
1//! # easy-opengl
2//!
3//! `easy-opengl` is a collection of utilities to make opengl app
4//! more quickly and easy without losing custumization and freedom
5//! # Example
6//! ``` rust
7//!extern crate gl;
8//!extern crate sdl2;
9//!
10//!use easy_opengl::buffers::*;
11//!use easy_opengl::shader::*;
12//!use easy_opengl::textures::*;
13//!
14//!use sdl2::event::Event;
15//!use sdl2::event::WindowEvent;
16//!
17//!const SCR_WIDTH: u32 = 800;
18//!const SCR_HEIGHT: u32 = 600;
19//!
20//!const VERTEX_SHADER_SOURCE: &str = r#"
21//! #version 330 core
22//! layout (location = 0) in vec3 aPos;
23//! layout (location = 1) in vec2 aTexCoord;
24//!
25//! out vec2 TexCoord;
26//!
27//! void main() {
28//! gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
29//! TexCoord = aTexCoord;
30//! }
31//!"#;
32//!
33//!const FRAGMENT_SHADER_SOURCE: &str = r#"
34//! #version 330 core
35//! out vec4 FragColor;
36//! in vec2 TexCoord;
37//!
38//! uniform sampler2D texture1;
39//! uniform vec4 color;
40//!
41//! void main() {
42//! FragColor = texture(texture1, TexCoord) * color;
43//! }
44//!"#;
45//!
46//!pub struct Window {
47//! pub sdl_context: sdl2::Sdl,
48//! pub sdl_window: sdl2::video::Window,
49//! pub gl_context: sdl2::video::GLContext,
50//! pub event_pump: sdl2::EventPump,
51//! pub name: String,
52//! pub width: u32,
53//! pub height: u32,
54//!}
55//!
56//!impl Window {
57//! pub fn new(width: u32, height: u32, name: String) -> Self {
58//! let sdl_context = sdl2::init().unwrap();
59//! let video_subsystem = sdl_context.video().unwrap();
60//!
61//! let window = video_subsystem
62//! .window("Titulo", width.clone(), height.clone())
63//! .opengl()
64//! .resizable()
65//! .build()
66//! .unwrap();
67//!
68//! let gl_context = window.gl_create_context().unwrap();
69//! gl::load_with(|name| video_subsystem.gl_get_proc_address(name) as *const std::ffi::c_void);
70//! let mut event_pump = sdl_context.event_pump().unwrap();
71//!
72//! Self {
73//! sdl_context,
74//! sdl_window: window,
75//! gl_context,
76//! event_pump,
77//! width,
78//! height,
79//! name,
80//! }
81//! }
82//!}
83//!
84//!pub fn main() {
85//! let mut window = Window::new(SCR_WIDTH, SCR_HEIGHT, "Test".to_string());
86//! let mut shader = Shader::new();
87//! shader.load_from_memory(VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE, None);
88//!
89//! let vertices = vec![
90//! 0.5, 0.5, 0.0, 1.0, 1.0, // top right
91//! 0.5, -0.5, 0.0, 1.0, 0.0, // bottom right
92//! -0.5, -0.5, 0.0, 0.0, 0.0, // bottom left
93//! -0.5, 0.5, 0.0, 0.0, 1.0, // top left
94//! ];
95//! let indices = vec![
96//! 0, 1, 3, // first Triangle
97//! 1, 2, 3, // second Triangle
98//! ];
99//!
100//! let vao = VertexArray::new();
101//! // Is important keep alive the variable, because when is out of scope it will destroy the buffer
102//! let _vbo = VertexBuffer::new(calc_bytes_size(&vertices) as isize, Some(&vertices));
103//!
104//! vao.bind();
105//!
106//! submit_vertex_attribs(&mut vec![
107//! VertexAttrib::new(VertexAttribType::Float3, false, "aPos".to_string()),
108//! VertexAttrib::new(VertexAttribType::Float2, false, "aTexCoord".to_string()),
109//! ]);
110//!
111//! // Is important keep alive the variable, because when is out of scope it will destroy the buffer
112//! let _ibo = IndexBuffer::new(calc_bytes_size(&indices) as isize, Some(&indices));
113//!
114//! let mut texture = Texture2D::new();
115//! texture.load_from_file("./a.png", TextureConfig::new());
116//!
117//! 'main: loop {
118//! window.sdl_window.gl_swap_window();
119//! for event in window.event_pump.poll_iter() {
120//! unsafe {
121//! gl::ClearColor(0.1, 0.1, 0.1, 1.0);
122//! gl::Clear(gl::COLOR_BUFFER_BIT);
123//! texture.bind();
124//! shader.bind();
125//! shader.set_uniform("color", UniformType::Fv4(1.0, 0.4, 0.1, 1.0));
126//! vao.bind();
127//! gl::DrawElements(
128//! gl::TRIANGLES,
129//! indices.len() as i32,
130//! gl::UNSIGNED_INT,
131//! std::ptr::null(),
132//! );
133//! }
134//! match event {
135//! Event::Quit { .. } => {
136//! break 'main;
137//! }
138//! Event::Window {
139//! timestamp,
140//! window_id,
141//! win_event,
142//! } => match win_event {
143//! WindowEvent::Resized(w, h) => unsafe {
144//! gl::Viewport(0, 0, w, h);
145//! },
146//! _ => {}
147//! },
148//! _ => {}
149//! }
150//! }
151//! // ::std::thread::sleep(::std::time::Duration::new(0, 1_000_000_000u32 / 60));
152//! }
153//!}
154//! ```
155
156#[allow(dead_code)]
157pub mod buffers;
158pub mod shader;
159pub mod textures;