Last active
May 15, 2019 21:54
-
-
Save MadcapJake/b7aa09af1937797dc851 to your computer and use it in GitHub Desktop.
GLFW/OpenGL with Perl 6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6; | |
use NativeCall; | |
sub lib { '/usr/lib/x86_64-linux-gnu/libglfw.so.3.1' } | |
=head1 GLFW/OpenGL in Perl 6 | |
=head2 Bindings | |
class Window is repr('CPointer') {} | |
class Monitor is repr('CPointer') {} | |
=head3 GLFW | |
sub glfwInit(--> int32) is native(&lib) {*} | |
sub glfwCreateWindow(int32, int32, Str, Monitor, Window --> Window) is native(&lib) {*} | |
sub glfwTerminate() is native(&lib) {*} | |
sub glfwMakeContextCurrent(Window) is native(&lib) {*} | |
sub glfwSetWindowShouldClose(Window, int32(Bool)) is native(&lib) {*} | |
sub glfwWindowShouldClose(Window --> int32) is native(&lib) {*} | |
sub glfwSwapBuffers(Window) is native(&lib) {*} | |
sub glfwSwapInterval(int32) is native(&lib) {*} | |
sub glfwPollEvents() is native(&lib) {*} | |
sub glfwSetErrorCallback(&cb (int32, Str)) is native(&lib) {*} | |
sub glfwSetKeyCallback(Window, &cb (Window, int32, int32, int32, int32)) is native(&lib) {*} | |
sub glfwGetFramebufferSize(Window, int32 is rw, int32 is rw) is native(&lib) {*} | |
=head3 OpenGL | |
enum PrimitiveMode( | |
GL_POINTS => 0x0000, | |
GL_LINES => 0x0001, | |
GL_LINE_LOOP => 0x0002, | |
GL_LINE_STRIP => 0x0003, | |
GL_TRIANGLES => 0x0004, | |
GL_TRIANGLE_STRIP => 0x0005, | |
GL_TRIANGLE_FAN => 0x0006, | |
GL_QUADS => 0x0007, | |
GL_QUAD_STRIP => 0x0008, | |
GL_POLYGON => 0x0009 | |
); | |
enum MatrixMode( | |
GL_MATRIX_MODE => 0x0BA0, | |
GL_MODELVIEW => 0x1700, | |
GL_PROJECTION => 0x1701, | |
GL_TEXTURE => 0x1702 | |
); | |
sub glViewport(int32, int32, int32, int32) is native(&lib) {*} | |
sub glClear(int32) is native(&lib) {*} | |
sub glMatrixMode(int32) is native(&lib) {*} | |
sub glLoadIdentity() is native(&lib) {*} | |
sub glOrtho(num64, num64, num64, num64, num64, num64) is native(&lib) {*} | |
sub glRotatef(num32, num32, num32, num32) is native(&lib) {*} | |
sub glBegin(int32) is native(&lib) {*} | |
sub glColor3f(num32, num32, num32) is native(&lib) {*} | |
sub glVertex3f(num32, num32, num32) is native(&lib) {*} | |
sub glEnd() is native(&lib) {*} | |
constant GL_COLOR_BUFFER_BIT = 0x00004000; | |
=head2 Example Run | |
sub error-cb(int32 $i, Str $desc) { note $desc } | |
glfwSetErrorCallback(&error-cb); | |
die 'Failed to initialize GLFW' unless glfwInit().so; | |
my $w = glfwCreateWindow(640, 480, "Hello World", Nil, Nil); | |
without $w { glfwTerminate(); die 'Failed to create window' } | |
glfwMakeContextCurrent($w); | |
glfwSwapInterval(1); | |
sub key-cb(Window $w, int32 $key, int32 $scancode, int32 $action, int32 $mods) { | |
glfwSetWindowShouldClose($w, True) if $key == 256 and $action == 1; | |
} | |
glfwSetKeyCallback($w, &key-cb); | |
while not glfwWindowShouldClose($w) { | |
my num32 $ratio; | |
my int32 $width; | |
my int32 $height; | |
glfwGetFramebufferSize($w, $width, $height); | |
$ratio = ($width / $height).Num; | |
glViewport(0, 0, $width, $height); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glOrtho(-$ratio, $ratio, -1e0, 1e0, 1e0, -1e0); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
glRotatef((now * 50e0) % 360, 0e0, 0e0, 1e0); | |
glBegin(GL_TRIANGLES); | |
glColor3f(1e0, 0e0, 0e0); | |
glVertex3f(-0.6e0, -0.4e0, 0e0); | |
glColor3f(0e0, 1e0, 0e0); | |
glVertex3f( 0.6e0, -0.4e0, 0e0); | |
glColor3f(0e0, 0e0, 1e0); | |
glVertex3f( 0e0, 0.6e0, 0e0); | |
glEnd(); | |
glfwSwapBuffers($w); | |
glfwPollEvents(); | |
} | |
glfwTerminate(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment