1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
// Copyright Jeron A. Lau 2018.
// Dual-licensed under either the MIT License or the Boost Software License,
// Version 1.0.  (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)

//! OpenGL implementation for adi_gpu.

extern crate asi_opengl;
extern crate adi_gpu_base;

use std::mem;

pub use base::Shape;
pub use base::Gradient;
pub use base::Model;
pub use base::TexCoords;
pub use base::Texture;

use adi_gpu_base as base;
use asi_opengl::{
	OpenGL, OpenGLBuilder, VertexData, Program, Buffer, UniformData,
	Feature, Topology,
};
use adi_gpu_base::*;

const SHADER_SOLID_FRAG: &'static [u8] = include_bytes!("shaders/solid-frag.glsl");
const SHADER_SOLID_VERT: &'static [u8] = include_bytes!("shaders/solid-vert.glsl");
const SHADER_GRADIENT_FRAG: &'static [u8] = include_bytes!("shaders/gradient-frag.glsl");
const SHADER_GRADIENT_VERT: &'static [u8] = include_bytes!("shaders/gradient-vert.glsl");
const SHADER_TEX_FRAG: &'static [u8] = include_bytes!("shaders/texture-frag.glsl");
const SHADER_TEX_VERT: &'static [u8] = include_bytes!("shaders/texture-vert.glsl");
const SHADER_FADED_VERT: &'static [u8] = include_bytes!("shaders/faded-vert.glsl");
const SHADER_TINTED_FRAG: &'static [u8] = include_bytes!("shaders/tinted-frag.glsl");
const SHADER_COMPLEX_VERT: &'static [u8] = include_bytes!("shaders/complex-vert.glsl");
const SHADER_COMPLEX_FRAG: &'static [u8] = include_bytes!("shaders/complex-frag.glsl");

const STYLE_GRADIENT: usize = 0;
const STYLE_TEXTURE: usize = 1;
const STYLE_FADED: usize = 2;
const STYLE_TINTED: usize = 3;
const STYLE_SOLID: usize = 4;
const STYLE_COMPLEX: usize = 5;

struct Style {
	shader: Program,
	matrix_uniform: UniformData,
	has_camera: UniformData,
	camera_uniform: UniformData,
	has_fog: UniformData,
	fog: UniformData,
	range: UniformData,
	alpha: UniformData,
	color: UniformData,
	position: VertexData,
	texpos: VertexData,
	acolor: VertexData,
}

impl Style {
	// Create a new style.
	fn new(context: &OpenGL, vert: &[u8], frag: &[u8]) -> Style {
		let shader = Program::new(context, vert, frag);
		let matrix_uniform = shader.uniform(b"models_tfm\0");
		let has_camera = shader.uniform(b"has_camera\0");
		let camera_uniform = shader.uniform(b"matrix\0");
		let has_fog = shader.uniform(b"has_fog\0");
		let fog = shader.uniform(b"fog\0");
		let range = shader.uniform(b"range\0");
		let alpha = shader.uniform(b"alpha\0");
		let color = shader.uniform(b"color\0");
		let position = shader.vertex_data(b"position\0");
		let texpos = shader.vertex_data(b"texpos\0");
		let acolor = shader.vertex_data(b"acolor\0");

		Style {
			shader, matrix_uniform, has_camera, camera_uniform, fog,
			range, position, texpos, alpha, has_fog, color, acolor,
		}
	}
}

struct ShapeData {
	style: usize,
	buffers: [Option<Buffer>; 2],
	has_fog: bool,
	alpha: Option<f32>,
	color: Option<[f32; 4]>,
	transform: Transform, // Transformation matrix.
	texture: Option<asi_opengl::Texture>,
	vertex_buffer: Buffer,
	fans: Vec<(u32, u32)>,
}

impl ::adi_gpu_base::Point for ShapeData {
	fn point(&self) -> Vec3 {
		// Position vector at origin * object transform.
		(self.transform.0 * vec4!(0f32, 0f32, 0f32, 1f32)).xyz()
	}
}

struct ModelData {
	vertex_buffer: Buffer,
	// TODO alot could be in base as duplicate
	vertex_count: u32,
	fans: Vec<(u32, u32)>,
}

struct TexcoordsData {
	vertex_buffer: Buffer,
	vertex_count: u32,
}

struct GradientData {
	vertex_buffer: Buffer,
	vertex_count: u32,
}

struct TextureData {
	t: asi_opengl::Texture,
}

/// To render anything with adi_gpu, you have to make a `Display`
pub struct Display {
	window: adi_gpu_base::Window,
	context: OpenGL,
	color: (f32, f32, f32),
	opaque_ind: Vec<u32>,
	alpha_ind: Vec<u32>,
	opaque_vec: Vec<ShapeData>,
	alpha_vec: Vec<ShapeData>,
	gui_vec: Vec<ShapeData>,
	models: Vec<ModelData>,
	texcoords: Vec<TexcoordsData>,
	gradients: Vec<GradientData>,
	textures: Vec<TextureData>,
	styles: [Style; 6],
	xyz: Vec3,
	rotate_xyz: Vec3,
	ar: f32,
	projection: Transform,
}

pub fn new(title: &str, icon: &afi::Video) -> Result<Box<Display>, &'static str>
{
	if let Some(tuple) = OpenGLBuilder::new() {
		let (builder, v) = tuple;
		let window = adi_gpu_base::Window::new(title, icon, Some(v));

		let context = builder.to_opengl(match window.get_connection() {
			WindowConnection::Xcb(_, window) => // |
			//	WindowConnection::Windows(_, window) =>
			{
				unsafe {mem::transmute(window as usize)}
			},
			WindowConnection::Windows(_, window) => {
				window
			}
			WindowConnection::Wayland => return Err(
				"OpenGL support on Wayland is WIP"),
			WindowConnection::DirectFB => return Err(
				"OpenGL support on DirectFB is WIP"),
			WindowConnection::Android => return Err(
				"OpenGL support on Android is WIP"),
			WindowConnection::IOS => return Err(
				"OpenGL support on iOS is WIP"),
			WindowConnection::AldaronsOS => return Err(
				"AldaronsOS doesn't support OpenGL"),
			WindowConnection::Arduino => return Err(
				"Arduino doesn't support OpenGL"),
			WindowConnection::Switch => return Err(
				"Nintendo Switch doesn't support OpenGL"),
			WindowConnection::Web => return Err(
				"WebGL support is WIP"),
			WindowConnection::NoOS => return Err(
				"NoOS doesn't support OpenGL"),
		});

		// Set the settings.
		context.disable(Feature::Dither);
		context.enable(Feature::CullFace);
		context.enable(Feature::Blend);
		context.blend();

		// Load shaders
		let style_solid = Style::new(&context,
			SHADER_SOLID_VERT, SHADER_SOLID_FRAG);
		let style_gradient = Style::new(&context,
			SHADER_GRADIENT_VERT, SHADER_GRADIENT_FRAG);
		let style_texture = Style::new(&context,
			SHADER_TEX_VERT, SHADER_TEX_FRAG);
		let style_faded = Style::new(&context,
			SHADER_FADED_VERT, SHADER_TEX_FRAG);
		let style_tinted = Style::new(&context,
			SHADER_TEX_VERT, SHADER_TINTED_FRAG);
		let style_complex = Style::new(&context,
			SHADER_COMPLEX_VERT, SHADER_COMPLEX_FRAG);

		let wh = window.wh();
		let ar = wh.0 as f32 / wh.1 as f32;

		let projection = base::projection(ar, 0.5 * PI);

		// Adjust the viewport
		context.viewport(wh.0, wh.1);

		let mut display = ::Display {
			window,
			context,
			color: (0.0, 0.0, 0.0),
			alpha_ind: vec![],
			opaque_ind: vec![],
			alpha_vec: vec![],
			opaque_vec: vec![],
			gui_vec: vec![],
			models: vec![],
			texcoords: vec![],
			gradients: vec![],
			textures: vec![],
			styles: [
				style_gradient,
				style_texture,
				style_faded,
				style_tinted,
				style_solid,
				style_complex,
			],
			xyz: vec3!(0.0, 0.0, 0.0),
			rotate_xyz: vec3!(0.0, 0.0, 0.0),
			ar,
			projection,
		};

		use base::Display;
		display.camera(vec3!(0.0, 0.0, 0.0), vec3!(0.0, 0.0, 0.0));

		Ok(Box::new(display))
	} else {
		Err("Couldn't find OpenGL!")
	}
}

impl base::Display for Display {
	fn color(&mut self, color: (f32, f32, f32)) {
		self.color = color;
		self.context.color(color.0, color.1, color.2);
	}

	fn update(&mut self) -> Option<adi_gpu_base::Input> {
		if let Some(input) = self.window.update() {
			return Some(input);
		}

		// Update Window:
		// TODO: This is copied pretty much from adi_gpu_vulkan.  Move
		// to the base.

		// Opaque & Alpha Shapes need a camera.
		for i in (&self.styles).iter() {
			i.has_camera.set_int1(1);
		}

		// Enable for 3D depth testing
		self.context.enable(Feature::DepthTest);

		// sort nearest
		::adi_gpu_base::zsort(&mut self.opaque_ind, &self.opaque_vec,
			true, self.xyz);
		for shape in self.opaque_vec.iter() {
			draw_shape(&self.styles[shape.style], shape);
		}

		// sort farthest
		::adi_gpu_base::zsort(&mut self.alpha_ind, &self.alpha_vec,
			false, self.xyz);
		for shape in self.alpha_vec.iter() {
			draw_shape(&self.styles[shape.style], shape);
		}

		// Disable Depth Testing for GUI
		self.context.disable(Feature::DepthTest);

		// Gui Elements don't want a camera.
		for i in (&self.styles).iter() {
			i.has_camera.set_int1(0);
		}

		// No need to sort gui elements.
		for shape in self.gui_vec.iter() {
			draw_shape(&self.styles[shape.style], shape);
		}

		// end todo

		self.context.update();
		// Return None, there was no input, updated screen.
		None
	}

	fn camera(&mut self, xyz: Vec3, rotate_xyz: Vec3) {
		// Set Camera
		self.xyz = xyz;
		self.rotate_xyz = rotate_xyz;

		// Write To Camera Uniforms.  TODO: only before use (not here).
		// TODO this assignment copied from vulkan implementation.  Put
		// in the base library.
		let cam = Transform::IDENTITY
			.t(vec3!()-self.xyz) // Move camera - TODO: negation operator?
			.r(vec3!()-self.rotate_xyz) // Rotate camera - TODO: negation operator?
			.m(self.projection.0); // Apply projection to camera

		for i in (&self.styles).iter() {
			i.camera_uniform.set_mat4(cam.into());
		}
	}

	fn model(&mut self, vertices: &[f32], fans: Vec<(u32, u32)>) -> Model {
		// TODO most is duplicate from other implementation.
		let index = self.models.len();

		let buffer = Buffer::new(&self.context);

		let vertex_buffer = buffer;
		vertex_buffer.set(vertices);


		self.models.push(ModelData {
			vertex_buffer, vertex_count: vertices.len() as u32 / 4,
			fans
		});

		Model(index)
	}

	fn fog(&mut self, fog: Option<(f32, f32)>) -> () {
		let fogc = [self.color.0, self.color.1, self.color.2, 1.0];
		let fogr = if let Some(fog) = fog {
			[fog.0, fog.1]
		} else {
			[::std::f32::MAX, 0.0]
		};

		for i in (&self.styles).iter() {
			i.fog.set_vec4(&fogc);
			i.range.set_vec2(&fogr);
		}
	}

	fn texture(&mut self, wh: (u16,u16), graphic: &VFrame) -> Texture {
		let (w, h) = wh;
		let pixels = graphic.0.as_slice();

		let t = self.context.texture();

		t.set(w, h, pixels);

		let a = self.textures.len();

		self.textures.push(TextureData { t });

		Texture(a, w, h)
	}

	fn gradient(&mut self, colors: &[f32]) -> Gradient {
		// TODO: A lot of duplication here from adi_gpu_vulkan.  Put in
		// base.
		let vertex_buffer = Buffer::new(&self.context);
		vertex_buffer.set(colors);

		let a = self.gradients.len();

		self.gradients.push(GradientData {
			vertex_buffer,
			vertex_count: colors.len() as u32 / 4,
		});

		Gradient(a)
	}

	fn texcoords(&mut self, texcoords: &[f32]) -> TexCoords {
		// TODO: A lot of duplication here from adi_gpu_vulkan.  Put in
		// base.
		let vertex_buffer = Buffer::new(&self.context);
		vertex_buffer.set(texcoords);

		let a = self.texcoords.len();

		self.texcoords.push(TexcoordsData {
			vertex_buffer,
			vertex_count: texcoords.len() as u32 / 4,
		});

		TexCoords(a)
	}

	fn set_texture(&mut self, texture: &mut Texture, wh: (u16,u16),
		graphic: &VFrame)
	{
		self.textures[texture.0].t.set(wh.0, wh.1,
			graphic.0.as_slice());
	}

	#[inline(always)]
	fn shape_solid(&mut self, model: &Model, transform: Transform,
		color: [f32; 4], blending: bool, fog: bool, camera: bool)
		-> Shape
	{
		let shape = ShapeData {
			style: STYLE_SOLID,
			buffers: [None, None],
			has_fog: fog,
			alpha: None,
			color: Some(color),
			texture: None,
			vertex_buffer: self.models[model.0].vertex_buffer.clone(),
			transform, // Transformation matrix.
			fans: self.models[model.0].fans.clone(),
		};

		base::new_shape(if !camera && !fog {
			let index = self.gui_vec.len() as u32;
			self.gui_vec.push(shape);
			base::ShapeHandle::Gui(index)
		} else if blending {
			let index = self.alpha_vec.len() as u32;
			self.alpha_vec.push(shape);
			self.alpha_ind.push(index);
			base::ShapeHandle::Alpha(index)
		} else {
			let index = self.opaque_vec.len() as u32;
			self.opaque_vec.push(shape);
			self.opaque_ind.push(index);
			base::ShapeHandle::Opaque(index)
		})
	}

	#[inline(always)]
	fn shape_gradient(&mut self, model: &Model, transform: Transform,
		colors: Gradient, blending: bool, fog: bool, camera: bool)
		-> Shape
	{
		// TODO: is copied from adi_gpu_vulkan, move to base
		if self.models[model.0].vertex_count
			!= self.gradients[colors.0].vertex_count
		{
			panic!("TexCoord length doesn't match gradient length");
		}

		let shape = ShapeData {
			style: STYLE_GRADIENT,
			buffers: [
				Some(self.gradients[colors.0].vertex_buffer.clone()),
				None
			],
			has_fog: fog,
			alpha: None,
			color: None,
			texture: None,
			vertex_buffer: self.models[model.0].vertex_buffer.clone(),
			transform, // Transformation matrix.
			fans: self.models[model.0].fans.clone(),
		};

		base::new_shape(if !camera && !fog {
			let index = self.gui_vec.len() as u32;
			self.gui_vec.push(shape);
			base::ShapeHandle::Gui(index)
		} else if blending {
			let index = self.alpha_vec.len() as u32;
			self.alpha_vec.push(shape);
			self.alpha_ind.push(index);
			base::ShapeHandle::Alpha(index)
		} else {
			let index = self.opaque_vec.len() as u32;
			self.opaque_vec.push(shape);
			self.opaque_ind.push(index);
			base::ShapeHandle::Opaque(index)
		})
	}

	#[inline(always)]
	fn shape_texture(&mut self, model: &Model, transform: Transform,
		texture: &Texture, tc: TexCoords, blending: bool, fog: bool,
		camera: bool) -> Shape
	{
		// TODO: from adi_gpu_vulkan, move to the base
		if self.models[model.0].vertex_count
			!= self.texcoords[tc.0].vertex_count
		{
			panic!("TexCoord length doesn't match vertex length");
		}

		let shape = ShapeData {
			style: STYLE_TEXTURE,
			buffers: [
				Some(self.texcoords[tc.0].vertex_buffer.clone()),
				None
			],
			has_fog: fog,
			alpha: None,
			color: None,
			texture: Some(self.textures[texture.0].t.clone()),
			vertex_buffer: self.models[model.0].vertex_buffer.clone(),
			transform, // Transformation matrix.
			fans: self.models[model.0].fans.clone(),
		};

		base::new_shape(if !camera && !fog {
			let index = self.gui_vec.len() as u32;
			self.gui_vec.push(shape);
			base::ShapeHandle::Gui(index)
		} else if blending {
			let index = self.alpha_vec.len() as u32;
			self.alpha_vec.push(shape);
			self.alpha_ind.push(index);
			base::ShapeHandle::Alpha(index)
		} else {
			let index = self.opaque_vec.len() as u32;
			self.opaque_vec.push(shape);
			self.opaque_ind.push(index);
			base::ShapeHandle::Opaque(index)
		})
	}

	#[inline(always)]
	fn shape_faded(&mut self, model: &Model, transform: Transform,
		texture: &Texture, tc: TexCoords, alpha: f32, fog: bool,
		camera: bool) -> Shape
	{
		// TODO: from adi_gpu_vulkan, move to the base
		if self.models[model.0].vertex_count
			!= self.texcoords[tc.0].vertex_count
		{
			panic!("TexCoord length doesn't match vertex length");
		}

		let shape = ShapeData {
			style: STYLE_FADED,
			buffers: [
				Some(self.texcoords[tc.0].vertex_buffer.clone()),
				None
			],
			has_fog: fog,
			alpha: Some(alpha),
			color: None,
			texture: Some(self.textures[texture.0].t.clone()),
			vertex_buffer: self.models[model.0].vertex_buffer.clone(),
			transform, // Transformation matrix.
			fans: self.models[model.0].fans.clone(),
		};

		base::new_shape(if !camera && !fog {
			let index = self.gui_vec.len() as u32;
			self.gui_vec.push(shape);
			base::ShapeHandle::Gui(index)
		} else {
			let index = self.alpha_vec.len() as u32;
			self.alpha_vec.push(shape);
			self.alpha_ind.push(index);
			base::ShapeHandle::Alpha(index)
		})
	}

	#[inline(always)]
	fn shape_tinted(&mut self, model: &Model, transform: Transform,
		texture: &Texture, tc: TexCoords, tint: [f32; 4], blending: bool,
		fog: bool, camera: bool) -> Shape
	{
		// TODO: from adi_gpu_vulkan, move to the base
		if self.models[model.0].vertex_count
			!= self.texcoords[tc.0].vertex_count
		{
			panic!("TexCoord length doesn't match vertex length");
		}

		let shape = ShapeData {
			style: STYLE_TINTED,
			buffers: [
				Some(self.texcoords[tc.0].vertex_buffer.clone()),
				None,
			],
			has_fog: fog,
			alpha: None,
			color: Some(tint),
			texture: Some(self.textures[texture.0].t.clone()),
			vertex_buffer: self.models[model.0].vertex_buffer.clone(),
			transform, // Transformation matrix.
			fans: self.models[model.0].fans.clone(),
		};

		base::new_shape(if !camera && !fog {
			let index = self.gui_vec.len() as u32;
			self.gui_vec.push(shape);
			base::ShapeHandle::Gui(index)
		} else if blending {
			let index = self.alpha_vec.len() as u32;
			self.alpha_vec.push(shape);
			self.alpha_ind.push(index);
			base::ShapeHandle::Alpha(index)
		} else {
			let index = self.opaque_vec.len() as u32;
			self.opaque_vec.push(shape);
			self.opaque_ind.push(index);
			base::ShapeHandle::Opaque(index)
		})
	}

	#[inline(always)]
	fn shape_complex(&mut self, model: &Model, transform: Transform,
		texture: &Texture, tc: TexCoords, tints: Gradient,
		blending: bool, fog: bool, camera: bool) -> Shape
	{
		// TODO: from adi_gpu_vulkan, move to the base
		if self.models[model.0].vertex_count
			!= self.texcoords[tc.0].vertex_count
		{
			panic!("TexCoord length doesn't match vertex length");
		}

		// TODO: is copied from adi_gpu_vulkan, move to base
		if self.models[model.0].vertex_count
			!= self.gradients[tints.0].vertex_count
		{
			panic!("TexCoord length doesn't match gradient length");
		}

		let shape = ShapeData {
			style: STYLE_COMPLEX,
			buffers: [
				Some(self.texcoords[tc.0].vertex_buffer.clone()),
				Some(self.gradients[tints.0].vertex_buffer.clone()),
			],
			has_fog: fog,
			alpha: None,
			color: None,
			texture: Some(self.textures[texture.0].t.clone()),
			vertex_buffer: self.models[model.0].vertex_buffer.clone(),
			transform, // Transformation matrix.
			fans: self.models[model.0].fans.clone(),
		};

		base::new_shape(if !camera && !fog {
			let index = self.gui_vec.len() as u32;
			self.gui_vec.push(shape);
			base::ShapeHandle::Gui(index)
		} else if blending {
			let index = self.alpha_vec.len() as u32;
			self.alpha_vec.push(shape);
			self.alpha_ind.push(index);
			base::ShapeHandle::Alpha(index)
		} else {
			let index = self.opaque_vec.len() as u32;
			self.opaque_vec.push(shape);
			self.opaque_ind.push(index);
			base::ShapeHandle::Opaque(index)
		})
	}

	#[inline(always)]
	fn drop_shape(&mut self, shape: &Shape) {
		match get_shape(&shape) {
			ShapeHandle::Opaque(x) => {
				let index = self.opaque_ind.iter()
					.position(|y| *y == x).unwrap();
				self.opaque_ind.remove(index);
			},
			ShapeHandle::Alpha(x) => {
				let index = self.alpha_ind.iter()
					.position(|y| *y == x).unwrap();
				self.alpha_ind.remove(index);
			},
			ShapeHandle::Gui(_x) => {
				// TODO: make it obvious that there's only meant
				// to be 1 GUI object.
				self.gui_vec.clear();
			},
		}
	}

	fn transform(&mut self, shape: &Shape, transform: Transform) {
		// TODO: put in base, some is copy from vulkan implementation.
		match base::get_shape(shape) {
			ShapeHandle::Opaque(x) => {
				let x = x as usize; // for indexing
				self.opaque_vec[x].transform = transform;
			},
			ShapeHandle::Alpha(x) => {
				let x = x as usize; // for indexing
				self.alpha_vec[x].transform = transform;
			},
			ShapeHandle::Gui(x) => {
				let x = x as usize; // for indexing
				self.gui_vec[x].transform = transform;
			},
		}
	}

	fn resize(&mut self, wh: (u16, u16)) -> () {
		let xyz = self.xyz;
		let rotate_xyz = self.rotate_xyz;

		self.ar = wh.0 as f32 / wh.1 as f32;
		self.context.viewport(wh.0, wh.1);

		self.projection = ::base::projection(self.ar, 0.5 * PI);
		self.camera(xyz, rotate_xyz);
	}

	fn wh(&self) -> (u16, u16) {
		self.window.wh()
	}
}

fn draw_shape(style: &Style, shape: &ShapeData) {
	style.matrix_uniform.set_mat4(shape.transform.into());

	if !style.texpos.is_none() {
		// Set texpos for the program from the texpos buffer.
		style.texpos.set(shape.buffers[0].as_ref().unwrap());
		// Bind the texture
		shape.texture.as_ref().unwrap().bind();
	}

	if !style.acolor.is_none() {
		// Set colors for the program from the color buffer.
		// TODO: probably shouldn't be same buffer as texpos.
		style.acolor.set(shape.buffers[0].as_ref().unwrap());
	}

	if !style.alpha.is_none() {
		style.alpha.set_vec1(shape.alpha.unwrap());
	}

	if !style.color.is_none() {
		style.color.set_vec4(&shape.color.unwrap());
	}

	if shape.has_fog {
		style.has_fog.set_int1(1);
	} else {
		style.has_fog.set_int1(0);
	}

	// Set vertices for the program from the vertex buffer.
	style.position.set(&shape.vertex_buffer);
	for i in shape.fans.iter() {
		style.shader.draw_arrays(Topology::TriangleFan, i.0..i.1);
	}
}