winit-sonoma-fix/examples/support/mod.rs

110 lines
3.1 KiB
Rust
Raw Normal View History

2015-04-03 07:04:17 +11:00
use std::ffi::CStr;
2015-08-06 06:31:34 +10:00
use std::mem;
use std::ptr;
2014-09-21 19:33:09 +10:00
use glutin;
mod gl {
2015-08-06 06:31:34 +10:00
pub use self::Gles2 as Gl;
2014-12-24 18:09:16 +11:00
include!(concat!(env!("OUT_DIR"), "/test_gl_bindings.rs"));
2014-09-12 18:20:15 +10:00
}
pub struct Context {
gl: gl::Gl
}
2014-09-21 19:33:09 +10:00
pub fn load(window: &glutin::Window) -> Context {
2014-11-06 01:22:21 +11:00
let gl = gl::Gl::load(window);
2015-01-08 19:28:22 +11:00
let version = unsafe {
let data = CStr::from_ptr(gl.GetString(gl::VERSION) as *const _).to_bytes().to_vec();
2015-04-03 07:04:17 +11:00
String::from_utf8(data).unwrap()
};
2015-01-08 19:28:22 +11:00
println!("OpenGL version {}", version);
2015-08-06 06:31:34 +10:00
unsafe {
let vs = gl.CreateShader(gl::VERTEX_SHADER);
gl.ShaderSource(vs, 1, [VS_SRC.as_ptr() as *const i8].as_ptr(), ptr::null());
gl.CompileShader(vs);
let fs = gl.CreateShader(gl::FRAGMENT_SHADER);
gl.ShaderSource(fs, 1, [FS_SRC.as_ptr() as *const i8].as_ptr(), ptr::null());
gl.CompileShader(fs);
let program = gl.CreateProgram();
gl.AttachShader(program, vs);
gl.AttachShader(program, fs);
gl.LinkProgram(program);
gl.UseProgram(program);
let mut vb = mem::uninitialized();
gl.GenBuffers(1, &mut vb);
gl.BindBuffer(gl::ARRAY_BUFFER, vb);
gl.BufferData(gl::ARRAY_BUFFER,
(VERTEX_DATA.len() * mem::size_of::<f32>()) as gl::types::GLsizeiptr,
VERTEX_DATA.as_ptr() as *const _, gl::STATIC_DRAW);
2015-08-31 02:48:26 +10:00
if gl.BindVertexArray.is_loaded() {
let mut vao = mem::uninitialized();
gl.GenVertexArrays(1, &mut vao);
gl.BindVertexArray(vao);
}
2015-08-06 06:31:34 +10:00
let pos_attrib = gl.GetAttribLocation(program, b"position\0".as_ptr() as *const _);
let color_attrib = gl.GetAttribLocation(program, b"color\0".as_ptr() as *const _);
gl.VertexAttribPointer(pos_attrib as gl::types::GLuint, 2, gl::FLOAT, 0,
5 * mem::size_of::<f32>() as gl::types::GLsizei,
ptr::null());
gl.VertexAttribPointer(color_attrib as gl::types::GLuint, 3, gl::FLOAT, 0,
5 * mem::size_of::<f32>() as gl::types::GLsizei,
(2 * mem::size_of::<f32>()) as *const () as *const _);
gl.EnableVertexAttribArray(pos_attrib as gl::types::GLuint);
gl.EnableVertexAttribArray(color_attrib as gl::types::GLuint);
}
Context { gl: gl }
}
impl Context {
2014-09-12 18:20:15 +10:00
pub fn draw_frame(&self, color: (f32, f32, f32, f32)) {
unsafe {
2014-11-06 01:22:21 +11:00
self.gl.ClearColor(color.0, color.1, color.2, color.3);
self.gl.Clear(gl::COLOR_BUFFER_BIT);
self.gl.DrawArrays(gl::TRIANGLES, 0, 3);
2014-09-12 18:20:15 +10:00
}
}
}
2014-10-03 06:16:47 +10:00
2015-01-04 09:11:59 +11:00
static VERTEX_DATA: [f32; 15] = [
2014-10-03 06:16:47 +10:00
-0.5, -0.5, 1.0, 0.0, 0.0,
0.0, 0.5, 0.0, 1.0, 0.0,
0.5, -0.5, 0.0, 0.0, 1.0
];
2015-08-06 06:31:34 +10:00
const VS_SRC: &'static [u8] = b"
#version 100
2015-08-31 02:48:26 +10:00
precision mediump float;
2015-08-06 06:31:34 +10:00
2015-08-31 02:48:26 +10:00
attribute vec2 position;
attribute vec3 color;
2015-08-06 06:31:34 +10:00
2015-08-31 02:48:26 +10:00
varying vec3 v_color;
2015-08-06 06:31:34 +10:00
void main() {
gl_Position = vec4(position, 0.0, 1.0);
v_color = color;
}
\0";
const FS_SRC: &'static [u8] = b"
#version 100
2015-08-31 02:48:26 +10:00
precision mediump float;
2015-08-06 06:31:34 +10:00
2015-08-31 02:48:26 +10:00
varying vec3 v_color;
2015-08-06 06:31:34 +10:00
void main() {
gl_FragColor = vec4(v_color, 1.0);
}
\0";