summaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
authorKjetil Orbekk <kjetil.orbekk@gmail.com>2016-06-10 23:45:06 -0400
committerKjetil Orbekk <kjetil.orbekk@gmail.com>2016-06-10 23:45:06 -0400
commit695f4cec3b8216577b9ce7e0b017e23b2eb19212 (patch)
treefb6d5c4bf94216714f7ff5c833c86712348db148 /rust
parent7374f65411d76dfe855d345f87dddb2fcda7ce5e (diff)
OpenGL example using glium.
Diffstat (limited to 'rust')
-rw-r--r--rust/opengl/Cargo.toml8
-rw-r--r--rust/opengl/default.nix10
-rw-r--r--rust/opengl/src/main.rs64
3 files changed, 82 insertions, 0 deletions
diff --git a/rust/opengl/Cargo.toml b/rust/opengl/Cargo.toml
new file mode 100644
index 0000000..6407830
--- /dev/null
+++ b/rust/opengl/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "opengl"
+version = "0.1.0"
+authors = ["Kjetil Orbekk <kjetil.orbekk@gmail.com>"]
+
+[dependencies]
+glium = "0.14.0"
+
diff --git a/rust/opengl/default.nix b/rust/opengl/default.nix
new file mode 100644
index 0000000..522e4d0
--- /dev/null
+++ b/rust/opengl/default.nix
@@ -0,0 +1,10 @@
+with import <nixpkgs> {};
+with xorg;
+{
+ allowUnfree = true;
+ games = stdenv.mkDerivation rec {
+ name = "opengl";
+ buildInputs = [ libXi libX11 xorg.libXext xorg.libXcursor xorg.libXrandr xorg.libXxf86vm mesa openal ];
+ LD_LIBRARY_PATH = lib.makeLibraryPath buildInputs;
+ };
+}
diff --git a/rust/opengl/src/main.rs b/rust/opengl/src/main.rs
new file mode 100644
index 0000000..667339b
--- /dev/null
+++ b/rust/opengl/src/main.rs
@@ -0,0 +1,64 @@
+#[macro_use]
+extern crate glium;
+
+fn main() {
+ use glium::DisplayBuild;
+ use glium::Surface;
+
+ let display = glium::glutin::WindowBuilder::new()
+ .with_dimensions(1024, 768)
+ .with_title(format!("Hello world"))
+ .build_glium()
+ .unwrap();
+
+ // let display = glium::glutin::WindowBuilder::new().build_glium().unwrap();
+
+ #[derive(Copy, Clone)]
+ struct Vertex {
+ position: [f32; 2],
+ }
+
+ implement_vertex!(Vertex, position);
+
+ let vertex1 = Vertex { position: [-0.5, -0.5] };
+ let vertex2 = Vertex { position: [ 0.0, 0.5] };
+ let vertex3 = Vertex { position: [ 0.5, -0.25] };
+ let shape = vec![vertex1, vertex2, vertex3];
+
+ let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();
+ let indices = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList);
+
+ let vertex_shader_src = r#"
+ #version 140
+ in vec2 position;
+ void main() {
+ gl_Position = vec4(position, 0.0, 1.0);
+ }
+ "#;
+
+ let fragment_shader_src = r#"
+ #version 140
+ out vec4 color;
+ void main() {
+ color = vec4(1.0, 0.0, 0.0, 1.0);
+ }
+ "#;
+
+ let program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap();
+
+ loop {
+ let mut target = display.draw();
+ target.clear_color(0.0, 0.0, 1.0, 1.0);
+ target.draw(&vertex_buffer, &indices, &program, &glium::uniforms::EmptyUniforms,
+ &Default::default()).unwrap();
+ target.finish().unwrap();
+
+ for ev in display.poll_events() {
+ match ev {
+ glium::glutin::Event::Closed => return,
+ _ => ()
+ }
+ }
+ }
+
+}