2014-10-05 04:17:02 +11:00
|
|
|
#![cfg(feature = "window")]
|
|
|
|
|
2015-04-03 07:04:17 +11:00
|
|
|
use std::ffi::CStr;
|
2014-09-21 19:33:09 +10:00
|
|
|
use glutin;
|
2014-09-12 16:50:54 +10:00
|
|
|
|
2014-09-12 18:20:15 +10:00
|
|
|
#[cfg(not(target_os = "android"))]
|
2014-09-12 16:50:54 +10:00
|
|
|
mod 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
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
mod gl {
|
|
|
|
pub use self::Gles1 as Gl;
|
2014-12-24 18:09:16 +11:00
|
|
|
include!(concat!(env!("OUT_DIR"), "/test_gles1_bindings.rs"));
|
2014-09-12 16:50:54 +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);
|
2014-09-12 16:50:54 +10:00
|
|
|
|
2015-01-08 19:28:22 +11:00
|
|
|
let version = unsafe {
|
2015-07-23 07:18:50 +10:00
|
|
|
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()
|
2014-09-12 16:50:54 +10:00
|
|
|
};
|
|
|
|
|
2015-01-08 19:28:22 +11:00
|
|
|
println!("OpenGL version {}", version);
|
2014-09-12 16:50:54 +10:00
|
|
|
|
|
|
|
Context { gl: gl }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Context {
|
2014-09-12 18:20:15 +10:00
|
|
|
#[cfg(not(target_os = "android"))]
|
2014-09-12 16:50:54 +10:00
|
|
|
pub fn draw_frame(&self, color: (f32, f32, f32, f32)) {
|
2014-11-06 01:22:21 +11:00
|
|
|
unsafe {
|
|
|
|
self.gl.ClearColor(color.0, color.1, color.2, color.3);
|
|
|
|
self.gl.Clear(gl::COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
self.gl.Begin(gl::TRIANGLES);
|
|
|
|
self.gl.Color3f(1.0, 0.0, 0.0);
|
|
|
|
self.gl.Vertex2f(-0.5, -0.5);
|
|
|
|
self.gl.Color3f(0.0, 1.0, 0.0);
|
|
|
|
self.gl.Vertex2f(0.0, 0.5);
|
|
|
|
self.gl.Color3f(0.0, 0.0, 1.0);
|
|
|
|
self.gl.Vertex2f(0.5, -0.5);
|
|
|
|
self.gl.End();
|
|
|
|
|
|
|
|
self.gl.Flush();
|
|
|
|
}
|
2014-09-12 18:20:15 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
pub fn draw_frame(&self, color: (f32, f32, f32, f32)) {
|
2015-05-01 07:04:07 +10:00
|
|
|
use std::mem;
|
|
|
|
|
2014-09-12 18:20:15 +10:00
|
|
|
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.EnableClientState(gl::VERTEX_ARRAY);
|
|
|
|
self.gl.EnableClientState(gl::COLOR_ARRAY);
|
|
|
|
|
2015-05-01 07:04:07 +10:00
|
|
|
self.gl.VertexPointer(2, gl::FLOAT, (mem::size_of::<f32>() * 5) as i32,
|
|
|
|
mem::transmute(VERTEX_DATA.as_ptr()));
|
|
|
|
self.gl.ColorPointer(3, gl::FLOAT, (mem::size_of::<f32>() * 5) as i32,
|
|
|
|
mem::transmute(VERTEX_DATA.as_ptr().offset(2)));
|
2014-11-06 01:22:21 +11:00
|
|
|
|
|
|
|
self.gl.DrawArrays(gl::TRIANGLES, 0, 3);
|
|
|
|
self.gl.DisableClientState(gl::VERTEX_ARRAY);
|
|
|
|
self.gl.DisableClientState(gl::COLOR_ARRAY);
|
|
|
|
|
|
|
|
self.gl.Flush();
|
2014-09-12 18:20:15 +10:00
|
|
|
}
|
2014-09-12 16:50:54 +10:00
|
|
|
}
|
|
|
|
}
|
2014-10-03 06:16:47 +10:00
|
|
|
|
|
|
|
#[cfg(target_os = "android")]
|
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
|
|
|
|
];
|