| 2 | 2 |
pub use gl::types::*;
|
| 3 | 3 |
pub use gl::*;
|
| 4 | 4 |
}
|
| 5 | |
use std::{convert,ffi,ptr,string};
|
| 6 | |
|
| 7 | |
pub enum ShaderError {
|
| 5 |
use glutin;
|
| 6 |
use std::{convert,ffi,mem,ptr,string};
|
| 7 |
|
| 8 |
#[derive(Debug)]
|
| 9 |
pub enum GraphicsError {
|
| 8 | 10 |
BadCString,
|
| 9 | 11 |
InvalidShaderLog,
|
| 10 | 12 |
CompileError(String),
|
| 11 | |
}
|
| 12 | |
|
| 13 | |
impl convert::From<ffi::NulError> for ShaderError {
|
| 14 | |
fn from(_: ffi::NulError) -> ShaderError {
|
| 15 | |
ShaderError::BadCString
|
| 16 | |
}
|
| 17 | |
}
|
| 18 | |
|
| 19 | |
impl convert::From<string::FromUtf8Error> for ShaderError {
|
| 20 | |
fn from(_: string::FromUtf8Error) -> ShaderError {
|
| 21 | |
ShaderError::InvalidShaderLog
|
| 13 |
LinkError(String),
|
| 14 |
GlutinCreation(glutin::CreationError),
|
| 15 |
GlutinContext(glutin::ContextError),
|
| 16 |
}
|
| 17 |
|
| 18 |
impl convert::From<glutin::CreationError> for GraphicsError {
|
| 19 |
fn from(err: glutin::CreationError) -> GraphicsError {
|
| 20 |
GraphicsError::GlutinCreation(err)
|
| 21 |
}
|
| 22 |
}
|
| 23 |
|
| 24 |
impl convert::From<glutin::ContextError> for GraphicsError {
|
| 25 |
fn from(err: glutin::ContextError) -> GraphicsError {
|
| 26 |
GraphicsError::GlutinContext(err)
|
| 27 |
}
|
| 28 |
}
|
| 29 |
|
| 30 |
impl convert::From<ffi::NulError> for GraphicsError {
|
| 31 |
fn from(_: ffi::NulError) -> GraphicsError {
|
| 32 |
GraphicsError::BadCString
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
| 36 |
impl convert::From<string::FromUtf8Error> for GraphicsError {
|
| 37 |
fn from(_: string::FromUtf8Error) -> GraphicsError {
|
| 38 |
GraphicsError::InvalidShaderLog
|
| 39 |
}
|
| 40 |
}
|
| 41 |
|
| 42 |
//
|
| 43 |
|
| 44 |
pub struct Window {
|
| 45 |
events: glutin::EventsLoop,
|
| 46 |
gl_window: glutin::GlWindow,
|
| 47 |
}
|
| 48 |
|
| 49 |
impl Window {
|
| 50 |
pub fn create() -> Result<Window, GraphicsError> {
|
| 51 |
use glutin::GlContext;
|
| 52 |
let events = glutin::EventsLoop::new();
|
| 53 |
let window = glutin::WindowBuilder::new();
|
| 54 |
let context = glutin::ContextBuilder::new();
|
| 55 |
let gl_window = glutin::GlWindow::new(window, context, &events)?;
|
| 56 |
|
| 57 |
unsafe { gl_window.make_current() }?;
|
| 58 |
|
| 59 |
gl::load_with(|symbol| gl_window.get_proc_address(symbol) as *const _);
|
| 60 |
|
| 61 |
Ok(Window { events, gl_window })
|
| 62 |
}
|
| 63 |
|
| 64 |
pub fn run<F>(self, mut cb: F) -> Result<(), GraphicsError>
|
| 65 |
where F: FnMut(glutin::Event) -> glutin::ControlFlow
|
| 66 |
{
|
| 67 |
use glutin::GlContext;
|
| 68 |
let Window { mut events, gl_window } = self;
|
| 69 |
let mut result = Ok(());
|
| 70 |
|
| 71 |
events.run_forever(|ev| {
|
| 72 |
let r = cb(ev);
|
| 73 |
|
| 74 |
match gl_window.swap_buffers() {
|
| 75 |
Ok(()) => r,
|
| 76 |
Err(ctx) => {
|
| 77 |
result = Err(GraphicsError::GlutinContext(ctx));
|
| 78 |
glutin::ControlFlow::Break
|
| 79 |
}
|
| 80 |
}
|
| 81 |
});
|
| 82 |
|
| 83 |
result
|
| 22 | 84 |
}
|
| 23 | 85 |
}
|
| 24 | 86 |
|
|
| 28 | 90 |
|
| 29 | 91 |
impl Drop for Shader {
|
| 30 | 92 |
fn drop(&mut self) {
|
| 31 | |
unsafe {
|
| 32 | |
gl::DeleteShader(self.n);
|
| 33 | |
}
|
| 34 | |
}
|
| 35 | |
}
|
| 36 | |
|
| 37 | |
pub fn compile_shader(src: &str, ty: gl::GLenum) -> Result<Shader, ShaderError> {
|
| 38 | |
let shader;
|
| 39 | |
unsafe {
|
| 40 | |
shader = gl::CreateShader(ty);
|
| 41 | |
let c_str = ffi::CString::new(src.as_bytes())?;
|
| 42 | |
gl::ShaderSource(shader, 1, &c_str.as_ptr(), ptr::null());
|
| 43 | |
gl::CompileShader(shader);
|
| 44 | |
|
| 45 | |
let mut status = gl::FALSE as gl::GLint;
|
| 46 | |
gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut status);
|
| 47 | |
|
| 48 | |
if status != (gl::TRUE as gl::GLint) {
|
| 49 | |
let mut len = 0;
|
| 50 | |
gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut len);
|
| 51 | |
let mut buf = Vec::with_capacity(len as usize);
|
| 52 | |
buf.set_len((len as usize) - 1);
|
| 53 | |
gl::GetShaderInfoLog(
|
| 54 | |
shader,
|
| 55 | |
len,
|
| 56 | |
ptr::null_mut(),
|
| 57 | |
buf.as_mut_ptr() as *mut gl::GLchar
|
| 93 |
unsafe { gl::DeleteShader(self.n); }
|
| 94 |
}
|
| 95 |
}
|
| 96 |
|
| 97 |
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
|
| 98 |
pub enum ShaderType {
|
| 99 |
Fragment,
|
| 100 |
Vertex,
|
| 101 |
}
|
| 102 |
|
| 103 |
impl ShaderType {
|
| 104 |
fn to_glenum(&self) -> gl::GLenum {
|
| 105 |
match *self {
|
| 106 |
ShaderType::Fragment => gl::FRAGMENT_SHADER,
|
| 107 |
ShaderType::Vertex => gl::VERTEX_SHADER,
|
| 108 |
}
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
| 112 |
impl Shader {
|
| 113 |
pub fn compile(ty: ShaderType, src: &str) -> Result<Shader, GraphicsError> {
|
| 114 |
let shader;
|
| 115 |
unsafe {
|
| 116 |
shader = gl::CreateShader(ty.to_glenum());
|
| 117 |
let c_str = ffi::CString::new(src.as_bytes())?;
|
| 118 |
gl::ShaderSource(shader, 1, &c_str.as_ptr(), ptr::null());
|
| 119 |
gl::CompileShader(shader);
|
| 120 |
|
| 121 |
let mut status = gl::FALSE as gl::GLint;
|
| 122 |
gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut status);
|
| 123 |
|
| 124 |
if status != (gl::TRUE as gl::GLint) {
|
| 125 |
let mut len = 0;
|
| 126 |
gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut len);
|
| 127 |
let mut buf = Vec::with_capacity(len as usize);
|
| 128 |
buf.set_len((len as usize) - 1);
|
| 129 |
gl::GetShaderInfoLog(
|
| 130 |
shader,
|
| 131 |
len,
|
| 132 |
ptr::null_mut(),
|
| 133 |
buf.as_mut_ptr() as *mut gl::GLchar
|
| 134 |
);
|
| 135 |
return Err(GraphicsError::CompileError(String::from_utf8(buf)?));
|
| 136 |
}
|
| 137 |
}
|
| 138 |
Ok(Shader { n: shader })
|
| 139 |
}
|
| 140 |
}
|
| 141 |
|
| 142 |
|
| 143 |
//
|
| 144 |
|
| 145 |
pub struct Program {
|
| 146 |
pub p: gl::GLuint,
|
| 147 |
pub shaders: Vec<Shader>,
|
| 148 |
}
|
| 149 |
|
| 150 |
impl Drop for Program {
|
| 151 |
fn drop(&mut self) {
|
| 152 |
self.shaders = vec![];
|
| 153 |
unsafe {
|
| 154 |
gl::DeleteProgram(self.p)
|
| 155 |
}
|
| 156 |
}
|
| 157 |
}
|
| 158 |
|
| 159 |
impl Program {
|
| 160 |
pub fn link(shaders: Vec<Shader>) -> Result<Program, GraphicsError> {
|
| 161 |
unsafe {
|
| 162 |
let program = gl::CreateProgram();
|
| 163 |
for s in shaders.iter() {
|
| 164 |
gl::AttachShader(program, s.n);
|
| 165 |
}
|
| 166 |
gl::LinkProgram(program);
|
| 167 |
|
| 168 |
let mut status = gl::FALSE as gl::GLint;
|
| 169 |
gl::GetProgramiv(program, gl::LINK_STATUS, &mut status);
|
| 170 |
|
| 171 |
// Fail on error
|
| 172 |
if status != (gl::TRUE as gl::GLint) {
|
| 173 |
let mut len: gl::GLint = 0;
|
| 174 |
gl::GetProgramiv(program, gl::INFO_LOG_LENGTH, &mut len);
|
| 175 |
let mut buf = Vec::with_capacity(len as usize);
|
| 176 |
buf.set_len((len as usize) - 1);
|
| 177 |
gl::GetProgramInfoLog(
|
| 178 |
program,
|
| 179 |
len,
|
| 180 |
ptr::null_mut(),
|
| 181 |
buf.as_mut_ptr() as *mut gl::GLchar,
|
| 182 |
);
|
| 183 |
return Err(GraphicsError::LinkError(String::from_utf8(buf)?));
|
| 184 |
}
|
| 185 |
|
| 186 |
Ok(Program {
|
| 187 |
p: program,
|
| 188 |
shaders: shaders,
|
| 189 |
})
|
| 190 |
}
|
| 191 |
}
|
| 192 |
|
| 193 |
pub fn use_program(&self) {
|
| 194 |
unsafe { gl::UseProgram(self.p); }
|
| 195 |
}
|
| 196 |
|
| 197 |
pub fn bind_frag_data_location(&self, idx: u32, s: &str) -> Result<(), GraphicsError> {
|
| 198 |
unsafe {
|
| 199 |
gl::BindFragDataLocation(
|
| 200 |
self.p,
|
| 201 |
idx,
|
| 202 |
ffi::CString::new(s)?.as_ptr()
|
| 58 | 203 |
);
|
| 59 | |
return Err(ShaderError::CompileError(String::from_utf8(buf)?));
|
| 60 | |
}
|
| 61 | |
}
|
| 62 | |
Ok(Shader { n: shader })
|
| 63 | |
}
|
| 204 |
}
|
| 205 |
Ok(())
|
| 206 |
}
|
| 207 |
|
| 208 |
pub fn get_attrib_location(&self, s: &str) -> Result<i32, GraphicsError> {
|
| 209 |
Ok(unsafe {
|
| 210 |
gl::GetAttribLocation(
|
| 211 |
self.p,
|
| 212 |
ffi::CString::new(s)?.as_ptr()
|
| 213 |
)
|
| 214 |
})
|
| 215 |
}
|
| 216 |
}
|
| 217 |
|
| 218 |
//
|
| 219 |
|
| 220 |
pub struct VertexArray {
|
| 221 |
idx: u32,
|
| 222 |
}
|
| 223 |
|
| 224 |
impl Drop for VertexArray {
|
| 225 |
fn drop(&mut self) {
|
| 226 |
unsafe { gl::DeleteVertexArrays(1, &self.idx); }
|
| 227 |
}
|
| 228 |
}
|
| 229 |
|
| 230 |
pub struct VertexBuffer {
|
| 231 |
idx: u32,
|
| 232 |
}
|
| 233 |
|
| 234 |
impl Drop for VertexBuffer {
|
| 235 |
fn drop(&mut self) {
|
| 236 |
unsafe { gl::DeleteBuffers(1, &self.idx); }
|
| 237 |
}
|
| 238 |
}
|
| 239 |
|
| 240 |
impl VertexArray {
|
| 241 |
pub fn new() -> VertexArray {
|
| 242 |
let mut idx = 0;
|
| 243 |
unsafe {
|
| 244 |
gl::GenVertexArrays(1, &mut idx);
|
| 245 |
gl::BindVertexArray(idx);
|
| 246 |
}
|
| 247 |
|
| 248 |
VertexArray { idx }
|
| 249 |
}
|
| 250 |
}
|
| 251 |
|
| 252 |
impl VertexBuffer {
|
| 253 |
pub fn new_array_buffer(vertex_data: &[gl::GLfloat]) -> VertexBuffer {
|
| 254 |
let mut idx = 0;
|
| 255 |
unsafe {
|
| 256 |
gl::GenBuffers(1, &mut idx);
|
| 257 |
gl::BindBuffer(gl::ARRAY_BUFFER, idx);
|
| 258 |
gl::BufferData(
|
| 259 |
gl::ARRAY_BUFFER,
|
| 260 |
(vertex_data.len() * mem::size_of::<gl::GLfloat>()) as gl::GLsizeiptr,
|
| 261 |
mem::transmute(&vertex_data[0]),
|
| 262 |
gl::STATIC_DRAW,
|
| 263 |
);
|
| 264 |
}
|
| 265 |
|
| 266 |
VertexBuffer { idx }
|
| 267 |
}
|
| 268 |
}
|