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
// lib.rs -- Aldaron's System Interface / OpenGL
// Copyright (c) 2018  Jeron A. Lau <jeron.lau@plopgrizzly.com>
// Licensed under the MIT LICENSE

extern crate libc;

use std::{ mem, ptr };

mod loader;
mod types;

use types::*;

#[derive(Copy, Clone)]
pub struct Texture(libc::c_uint);
pub struct Attribute(pub GLint); // Pub is for testing,

/// The OpenGL builder.
pub struct OpenGLBuilder {
	lib: loader::Lib,
	display: loader::Display,
}

impl OpenGLBuilder {
	/// Begin the building.
	pub fn new() -> Option<(OpenGLBuilder, i32)> {
		if let Some(lib) = loader::Lib::new() {
			let (mut display, visual_id) = lib.init();

			Some((OpenGLBuilder {
				lib,
				display,
			}, visual_id))
		} else {
			None
		}
	}

	/// Complete the building
	pub fn to_opengl(mut self, window: EGLNativeWindowType) -> OpenGL {
		self.lib.init2(&mut self.display, window);

		OpenGL {
			// FFI OpenGL Functions.
			clear: self.lib.load(b"glClear\0"),
			clear_color: self.lib.load(b"glClearColor\0"),
			disable: self.lib.load(b"glDisable\0"),
			enable: self.lib.load(b"glEnable\0"),
			#[cfg(debug_assertions)]
			get_error: self.lib.load(b"glGetError\0"),
			blend_func_separate:
				self.lib.load(b"glBlendFuncSeparate\0"),
			create_shader: self.lib.load(b"glCreateShader\0"),
			shader_source: self.lib.load(b"glShaderSource\0"),
			compile_shader: self.lib.load(b"glCompileShader\0"),
			create_program: self.lib.load(b"glCreateProgram\0"),
			attach_shader: self.lib.load(b"glAttachShader\0"),
			link_program: self.lib.load(b"glLinkProgram\0"),
			uniform: self.lib.load(b"glGetUniformLocation\0"),
			gen_buffers: self.lib.load(b"glGenBuffers\0"),
			bind_buffer: self.lib.load(b"glBindBuffer\0"),
			buffer_data: self.lib.load(b"glBufferData\0"),
			attribute: self.lib.load(b"glGetAttribLocation\0"),
			get_shader: self.lib.load(b"glGetShaderiv\0"),
			info_log: self.lib.load(b"glGetShaderInfoLog\0"),
			draw_elements: self.lib.load(b"glDrawElements\0"),
			use_program: self.lib.load(b"glUseProgram\0"),
			uniform_mat4: self.lib.load(b"glUniformMatrix4fv\0"),
			uniform_int1: self.lib.load(b"glUniform1i\0"),
			uniform_vec1: self.lib.load(b"glUniform1f\0"),
			uniform_vec2: self.lib.load(b"glUniform2f\0"),
			uniform_vec3: self.lib.load(b"glUniform3f\0"),
			uniform_vec4: self.lib.load(b"glUniform4f\0"),
			bind_texture: self.lib.load(b"glBindTexture\0"),
			vertex_attrib: self.lib.load(b"glVertexAttribPointer\0"),
			gen_textures: self.lib.load(b"glGenTextures\0"),
			tex_params: self.lib.load(b"glTexParameteri\0"),
			tex_image: self.lib.load(b"glTexImage2D\0"),
			tex_subimage: self.lib.load(b"glTexSubImage2D\0"),
			enable_vattrib: self.lib.load(b"glEnableVertexAttribArray\0"),
			viewport: self.lib.load(b"glViewport\0"),
			// Other
			display: self.display,
		}
	}
}

/// The OpenGL context.
pub struct OpenGL {
	display: loader::Display,
	clear: unsafe extern "system" fn(GLbitfield) -> (),
	clear_color: unsafe extern "system" fn(GLfloat, GLfloat, GLfloat,
		GLfloat) -> (),
	disable: unsafe extern "system" fn(GLenum) -> (),
	enable: unsafe extern "system" fn(GLenum) -> (),
	#[cfg(debug_assertions)] get_error: unsafe extern "system" fn() -> GLenum,
	blend_func_separate: unsafe extern "system" fn(GLenum, GLenum, GLenum,
		GLenum) -> (),
	create_shader: unsafe extern "system" fn(GLenum) -> GLuint,
	shader_source: unsafe extern "system" fn(GLuint, GLsizei,
		*const *const GLchar, *const GLint) -> (),
	compile_shader: unsafe extern "system" fn(GLuint) -> (),
	create_program: unsafe extern "system" fn() -> GLuint,
	attach_shader: unsafe extern "system" fn(GLuint, GLuint) -> (),
	link_program: unsafe extern "system" fn(GLuint) -> (),
	uniform: unsafe extern "system" fn(GLuint, *const GLchar) -> GLint,
	gen_buffers: unsafe extern "system" fn(GLsizei, *mut GLuint) -> (),
	bind_buffer: unsafe extern "system" fn(GLenum, GLuint) -> (),
	buffer_data: unsafe extern "system" fn(GLenum, GLsizeiptr,
		*const libc::c_void, GLenum) -> (),
	attribute: unsafe extern "system" fn(GLuint, *const GLchar) -> GLint,
	get_shader: unsafe extern "system" fn(GLuint, GLenum, *mut GLint) -> (),
	info_log: unsafe extern "system" fn(GLuint, GLsizei, *mut GLsizei,
		*mut GLchar) -> (),
	draw_elements: unsafe extern "system" fn(GLenum, GLsizei, GLenum,
		*const libc::c_void) -> (),
	use_program: unsafe extern "system" fn(GLuint) -> (),
	uniform_mat4: unsafe extern "system" fn(GLint, GLsizei, GLboolean,
		*const GLfloat) -> (),
	uniform_int1: unsafe extern "system" fn(GLint, GLint) -> (),
	uniform_vec1: unsafe extern "system" fn(GLint, GLfloat) -> (),
	uniform_vec2: unsafe extern "system" fn(GLint, GLfloat, GLfloat) -> (),
	uniform_vec3: unsafe extern "system" fn(GLint, GLfloat, GLfloat, GLfloat)
		-> (),
	uniform_vec4: unsafe extern "system" fn(GLint, GLfloat, GLfloat, GLfloat,
		GLfloat) -> (),
	bind_texture: unsafe extern "system" fn(GLenum, GLuint) -> (),
	vertex_attrib: unsafe extern "system" fn(GLuint, GLint, GLenum,
		GLboolean, GLsizei, *const libc::c_void) -> (),
	gen_textures: unsafe extern "system" fn(GLsizei, *mut GLuint) -> (),
	tex_params: unsafe extern "system" fn(GLenum, GLenum, GLint) -> (),
	tex_image: unsafe extern "system" fn(GLenum, GLint, GLint, GLsizei,
		GLsizei, GLint, GLenum, GLenum, *const libc::c_void) -> (),
	tex_subimage: unsafe extern "system" fn(GLenum, GLint, GLint, GLint, GLsizei,
		GLsizei, GLenum, GLenum, *const libc::c_void) -> (),
	enable_vattrib: unsafe extern "system" fn(GLuint) -> (),
	viewport: unsafe extern "system" fn(GLint, GLint, GLsizei, GLsizei) -> (),
}

impl OpenGL {
	/// Clear the screen with a specific color.
	pub fn clear(&self) {
		// Clear Color & Depth
		unsafe {
			(self.clear)(0x00000100 | 0x00004000);
			self.error()
		}
	}

	/// Set the color for `clear`.
	pub fn color(&self, r: f32, g: f32, b: f32) {
		unsafe {
			(self.clear_color)(r, g, b, 1.0);
			self.error()
		}
	}

	/// Update the screen
	pub fn update(&self) {
		// Swap Display
		self.display.swap();
	}

	/// Enable something
	pub fn enable(&self, what: u32) {
		unsafe {
			(self.enable)(what);
			self.error()
		}
	}

	/// Disable something
	pub fn disable(&self, what: u32) {
		unsafe {
			(self.disable)(what);
			self.error()
		}
	}

	/// Configure blending
	pub fn blend(&self) {
		const GL_SRC_ALPHA : u32 = 0x0302;
		const GL_ONE_MINUS_SRC_ALPHA : u32 = 0x0303;
		const GL_DST_ALPHA : u32 = 0x0304;

		unsafe {
			(self.blend_func_separate)(
				GL_SRC_ALPHA,
				GL_ONE_MINUS_SRC_ALPHA,
				GL_SRC_ALPHA,
				GL_DST_ALPHA
			);
			self.error()
		}
	}

	/// Load a shader program
	pub fn shader(&self, vertex: &[u8], fragment: &[u8]) -> u32 {
		// Last character in slices needs to null for it to be safe.
		assert_eq!(vertex[vertex.len() -1], b'\0');
		assert_eq!(fragment[fragment.len() -1], b'\0');

		let program;

		unsafe {
			self.error();
			let v_shader = (self.create_shader)(0x8B31/*vertex*/);
			self.error();
			(self.shader_source)(v_shader, 1,
				[vertex.as_ptr() as *const _].as_ptr(), ptr::null());
			self.error();
			(self.compile_shader)(v_shader);
			self.error();
			//		if cfg!(debug) { // TODO
			// unsafe {
				let mut value = mem::uninitialized();

				(self.get_shader)(v_shader,
					0x8B81 /*GL_COMPILE_STATUS*/,
					&mut value);
				self.error();

				if value == 0 {
					let mut value = mem::uninitialized();
					(self.get_shader)(v_shader,
						0x8B84 /*GL_INFO_LOG_LENGTH*/,
						&mut value);

					self.error();
					let mut buffer : Vec<u8> =
						vec![mem::uninitialized();
							value as usize];
					(self.info_log)(v_shader,
						value as GLsizei,
						ptr::null_mut(),
						buffer.as_mut_ptr() as *mut _);
					self.error();

					panic!("Failed to compile: {}.",
						::std::str::from_utf8(
							buffer.as_slice())
							.unwrap());
				}
			// }
//		}

			let f_shader = (self.create_shader)(0x8B30/*fragment*/);
			self.error();
			(self.shader_source)(f_shader, 1,
				[fragment.as_ptr() as *const _].as_ptr(), ptr::null());
			self.error();
			(self.compile_shader)(f_shader);
			self.error();
			//		if cfg!(debug) { // TODO
			// unsafe {
				let mut value = mem::uninitialized();

				(self.get_shader)(f_shader,
					0x8B81 /*GL_COMPILE_STATUS*/,
					&mut value);
				self.error();

				if value == 0 {
					let mut value = mem::uninitialized();
					(self.get_shader)(f_shader,
						0x8B84 /*GL_INFO_LOG_LENGTH*/,
						&mut value);

					self.error();
					let mut buffer : Vec<u8> =
						vec![mem::uninitialized();
							value as usize];
					(self.info_log)(f_shader,
						value as GLsizei,
						ptr::null_mut(),
						buffer.as_mut_ptr() as *mut _);
				println!("E6");
					self.error();

					panic!("Failed to compile: {}.",
						::std::str::from_utf8(
							buffer.as_slice())
							.unwrap());
				}
			// }
//		}

			program = (self.create_program)();
			self.error();

			(self.attach_shader)(program, v_shader);
			self.error();
			(self.attach_shader)(program, f_shader);
			self.error();

			(self.link_program)(program);
			self.error();
		}

		program
	}

	/// Get uniform from a shader.
	pub fn uniform(&self, shader: u32, name: &[u8]) -> i32 {
		// Last character in slice needs to null for it to be safe.
		assert_eq!(name[name.len() -1], b'\0');

		unsafe {
			let r = (self.uniform)(shader, name.as_ptr() as *const _);
			if r == -1 {
				panic!("Error No Uniform: {:?}",
					::std::str::from_utf8(name).unwrap());
			}
			self.error();
			r
		}
	}

	/// Get the attribute
	pub fn attribute(&self, shader: u32, name: &[u8]) -> Attribute {
		// Last character in slice needs to null for it to be safe.
		assert_eq!(name[name.len() -1], b'\0');

		let attrib = unsafe {
			let a = (self.attribute)(shader, name.as_ptr() as *const _);
			if a == -1 {
				panic!("Error No Attribute: {:?}",
					::std::str::from_utf8(name).unwrap());
			}
			self.error();

			(self.enable_vattrib)(a as u32);
			self.error();

			a
		};

		Attribute(attrib)
	}

	/// Create some new buffers
	pub fn new_buffers(&self, n: usize) -> Vec<u32> {
		unsafe {
			let mut buffers = vec![mem::uninitialized(); n];

			(self.gen_buffers)(n as i32, buffers.as_mut_ptr());
			self.error();

			buffers
		}
	}

	/// Bind a buffer from `new_buffers()`
	pub fn bind_buffer(&self, is_index_buffer: bool, buffer: u32) {
		unsafe {
			(self.bind_buffer)(
				if is_index_buffer {
					GL_ELEMENT_ARRAY_BUFFER
				} else {
					GL_ARRAY_BUFFER
				}, buffer);
			self.error();
		}


	}

	/// Set the bound buffer's data
	pub fn set_buffer<T>(&self, is_index_buffer: bool, data: &[T]) {
		unsafe {
			(self.buffer_data)(
				if is_index_buffer {
					GL_ELEMENT_ARRAY_BUFFER
				} else {
					GL_ARRAY_BUFFER
				}, (data.len() * mem::size_of::<T>()) as isize,
				data.as_ptr() as *const _,
				GL_DYNAMIC_DRAW);
			self.error();
		}
	}

	// TODO: this actually unsafe because uniforms can only be accessed when
	// their program is in use.
	/// Use a program.
	pub fn use_program(&self, shader: u32) {
		unsafe {
			(self.use_program)(shader);
			self.error();
		}
	}

	/// Set a mat4 uniform
	pub fn set_mat4(&self, uniform: i32, mat4: &[f32; 16]) -> () {
		unsafe {
			(self.uniform_mat4)(uniform, 1, 0 /*bool: transpose*/,
				mat4.as_ptr());
			self.error();
		}
	}

	/// Set an int uniform 
	pub fn set_int1(&self, uniform: i32, int1: i32) -> () {
		unsafe {
			(self.uniform_int1)(uniform, int1);
			self.error();
		}
	}

	/// Set a float uniform
	pub fn set_vec1(&self, uniform: i32, vec1: f32) -> () {
		unsafe {
			(self.uniform_vec1)(uniform, vec1);
			self.error();
		}
	}

	/// Set a vec2 uniform
	pub fn set_vec2(&self, uniform: i32, vec: &[f32; 2]) -> () {
		unsafe {
			(self.uniform_vec2)(uniform, vec[0], vec[1]);
			self.error();
		}
	}

	/// Set a vec3 uniform
	pub fn set_vec3(&self, uniform: i32, vec: &[f32; 3]) -> () {
		unsafe {
			(self.uniform_vec3)(uniform, vec[0], vec[1], vec[2]);
			self.error();
		}
	}

	/// Set a vec4 uniform
	pub fn set_vec4(&self, uniform: i32, vec: &[f32; 4]) -> () {
		unsafe {
			(self.uniform_vec4)(uniform, vec[0], vec[1], vec[2],
				vec[3]);
			self.error();
		}
	}

	/// Draw the elements.
	pub fn draw_elements(&self, n_indices: u32) {
		unsafe {
			// draw
			(self.draw_elements)(0x0004 /*GL_TRIANGLES*/,
				n_indices as GLsizei,
				0x1405 /*GL_UNSIGNED_INT*/, ptr::null());
			self.error();
		}
	}

	/// Create a new texture.
	pub fn new_texture(&self) -> Texture {
		Texture(unsafe {
			let mut a = mem::uninitialized();

			(self.gen_textures)(1, &mut a);
			self.error();

			self.use_texture(&Texture(a));

			(self.tex_params)(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
				GL_NEAREST);
			self.error();
			(self.tex_params)(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
				GL_NEAREST);
			self.error();

			a
		})
	}

	/// Set the bound texture's pixels
	pub fn set_texture(&self, w: u32, h: u32, px: &[u32]) -> () {
		unsafe {
			(self.tex_image)(GL_TEXTURE_2D, 0, GL_RGBA as i32,
				w as i32, h as i32, 0, GL_RGBA,
				GL_UNSIGNED_BYTE, px.as_ptr() as *const _);
			self.error();
		}
	}

	/// Update the pixels of an already bound & set texture.
	pub fn texture_update(&self, w: u32, h: u32, px: &[u32]) -> () {
		unsafe {
			(self.tex_subimage)(GL_TEXTURE_2D, 0, 0, 0,
				w as i32, h as i32, GL_RGBA,
				GL_UNSIGNED_BYTE, px.as_ptr() as *const _);
			self.error();
		}
	}

	/// Use a texture.
	pub fn use_texture(&self, texture: &Texture) {
		unsafe {
			(self.bind_texture)(GL_TEXTURE_2D, texture.0);
			self.error();
		}
	}

	/// Set vertex attribute to current buffer.
	pub fn vertex_attrib(&self, attrib: &Attribute) {
		unsafe {
			(self.vertex_attrib)(attrib.0 as GLuint, 4, GL_FLOAT, 0,
				0, ptr::null());
			self.error();
		}
	}

	/// Update the viewport.
	pub fn viewport(&self, w: u32, h: u32) {
		unsafe {
			(self.viewport)(0, 0, w as GLsizei, h as GLsizei);
			self.error();
		}
	}

	#[cfg(not(debug_assertions))]
	unsafe fn error(&self) { /* Do nothing in release mode for speed. */ }

	#[cfg(debug_assertions)]
	unsafe fn error(&self) {
		match (self.get_error)() {
			0 => return, // NO_ERROR
			0x0500 => panic!("OpenGL Error: Invalid enum"),
			0x0501 => panic!("OpenGL Error: Invalid value"),
			0x0502 => panic!("OpenGL Error: Invalid operation"),
			0x0503 => panic!("OpenGL Error: Stack overflow"),
			0x0504 => panic!("OpenGL Error: Stack underflow"),
			0x0505 => panic!("OpenGL Error: Out of memory"),
			_ => panic!("OpenGL Error: Unknown"),
		}
	}
}