-
-
Save humbertodias/ab8ac79767b8baeb0e04d7d9e9f6bfa4 to your computer and use it in GitHub Desktop.
GLFW for C#
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
using System; | |
using System.Runtime.InteropServices; | |
// https://stackoverflow.com/questions/10394994/where-can-i-find-custom-opengl-datatypes-in-sharpgl | |
using GLbitfield = System.UInt32; | |
using GLboolean = System.Boolean; | |
using GLbyte = System.SByte; | |
using GLclampf = System.Single; | |
using GLdouble = System.Double; | |
using GLenum = System.UInt32; | |
using GLfloat = System.Single; | |
using GLint = System.Int32; | |
using GLshort = System.Int16; | |
using GLsizei = System.Int32; | |
using GLubyte = System.Byte; | |
using GLuint = System.UInt32; | |
using GLushort = System.UInt16; | |
using GLvoid = System.IntPtr; | |
public class GLFW | |
{ | |
// MAC | |
// brew install glfw3 | |
public const string GLFW_LIB = "libglfw.dylib"; | |
// Windows | |
// public const string GLFW_LIB = "glfw3"; | |
// Linux | |
// sudo apt install libglfw3-dev | |
//public const string GLFW_LIB = "libglfw"; | |
public static int WINDOW = 0x00010001; | |
public static int OPENED = 0x00020001; | |
public static int KEY_SPACE = 32; | |
public static int KEY_SPECIAL = 256; | |
public static int KEY_ESC = (KEY_SPECIAL + 1); | |
public static int KEY_F1 = (KEY_SPECIAL + 2); | |
public static int KEY_F2 = (KEY_SPECIAL + 3); | |
public static int KEY_F3 = (KEY_SPECIAL + 4); | |
public static int KEY_F4 = (KEY_SPECIAL + 5); | |
public static int KEY_F5 = (KEY_SPECIAL + 6); | |
public static int KEY_F6 = (KEY_SPECIAL + 7); | |
public static int KEY_F7 = (KEY_SPECIAL + 8); | |
public static int KEY_F8 = (KEY_SPECIAL + 9); | |
public static int KEY_F9 = (KEY_SPECIAL + 10); | |
public static int KEY_F10 = (KEY_SPECIAL + 11); | |
public static int KEY_F11 = (KEY_SPECIAL + 12); | |
public static int KEY_F12 = (KEY_SPECIAL + 13); | |
public static int KEY_F13 = (KEY_SPECIAL + 14); | |
public static int KEY_F14 = (KEY_SPECIAL + 15); | |
public static int KEY_F15 = (KEY_SPECIAL + 16); | |
public static int KEY_F16 = (KEY_SPECIAL + 17); | |
public static int KEY_F17 = (KEY_SPECIAL + 18); | |
public static int KEY_F18 = (KEY_SPECIAL + 19); | |
public static int KEY_F19 = (KEY_SPECIAL + 20); | |
public static int KEY_F20 = (KEY_SPECIAL + 21); | |
public static int KEY_F21 = (KEY_SPECIAL + 22); | |
public static int KEY_F22 = (KEY_SPECIAL + 23); | |
public static int KEY_F23 = (KEY_SPECIAL + 24); | |
public static int KEY_F24 = (KEY_SPECIAL + 25); | |
public static int KEY_F25 = (KEY_SPECIAL + 26); | |
public static int KEY_UP = (KEY_SPECIAL + 27); | |
public static int KEY_DOWN = (KEY_SPECIAL + 28); | |
public static int KEY_LEFT = (KEY_SPECIAL + 29); | |
public static int KEY_RIGHT = (KEY_SPECIAL + 30); | |
public static int KEY_LSHIFT = (KEY_SPECIAL + 31); | |
public static int KEY_RSHIFT = (KEY_SPECIAL + 32); | |
public static int KEY_LCTRL = (KEY_SPECIAL + 33); | |
public static int KEY_RCTRL = (KEY_SPECIAL + 34); | |
public static int KEY_LALT = (KEY_SPECIAL + 35); | |
public static int KEY_RALT = (KEY_SPECIAL + 36); | |
public static int KEY_TAB = (KEY_SPECIAL + 37); | |
public static int KEY_ENTER = (KEY_SPECIAL + 38); | |
public static int KEY_BACKSPACE = (KEY_SPECIAL + 39); | |
public static int KEY_INSERT = (KEY_SPECIAL + 40); | |
public static int KEY_DEL = (KEY_SPECIAL + 41); | |
public static int KEY_PAGEUP = (KEY_SPECIAL + 42); | |
public static int KEY_PAGEDOWN = (KEY_SPECIAL + 43); | |
public static int KEY_HOME = (KEY_SPECIAL + 44); | |
public static int KEY_END = (KEY_SPECIAL + 45); | |
public static int KEY_KP_0 = (KEY_SPECIAL + 46); | |
public static int KEY_KP_1 = (KEY_SPECIAL + 47); | |
public static int KEY_KP_2 = (KEY_SPECIAL + 48); | |
public static int KEY_KP_3 = (KEY_SPECIAL + 49); | |
public static int KEY_KP_4 = (KEY_SPECIAL + 50); | |
public static int KEY_KP_5 = (KEY_SPECIAL + 51); | |
public static int KEY_KP_6 = (KEY_SPECIAL + 52); | |
public static int KEY_KP_7 = (KEY_SPECIAL + 53); | |
public static int KEY_KP_8 = (KEY_SPECIAL + 54); | |
public static int KEY_KP_9 = (KEY_SPECIAL + 55); | |
public static int KEY_KP_DIVIDE = (KEY_SPECIAL + 56); | |
public static int KEY_KP_MULTIPLY = (KEY_SPECIAL + 57); | |
public static int KEY_KP_SUBTRACT = (KEY_SPECIAL + 58); | |
public static int KEY_KP_ADD = (KEY_SPECIAL + 59); | |
public static int KEY_KP_DECIMAL = (KEY_SPECIAL + 60); | |
public static int KEY_KP_EQUAL = (KEY_SPECIAL + 61); | |
public static int KEY_KP_ENTER = (KEY_SPECIAL + 62); | |
public static int KEY_KP_NUM_LOCK = (KEY_SPECIAL + 63); | |
public static int KEY_CAPS_LOCK = (KEY_SPECIAL + 64); | |
public static int KEY_SCROLL_LOCK = (KEY_SPECIAL + 65); | |
public static int KEY_PAUSE = (KEY_SPECIAL + 66); | |
public static int KEY_LSUPER = (KEY_SPECIAL + 67); | |
public static int KEY_RSUPER = (KEY_SPECIAL + 68); | |
public static int KEY_MENU = (KEY_SPECIAL + 69); | |
public static int KEY_LAST = KEY_MENU; | |
public static int MOUSE_BUTTON_LEFT = 0; | |
public static int MOUSE_BUTTON_RIGHT = 1; | |
public static int MOUSE_BUTTON_MIDDLE = 2; | |
// GL | |
const uint GL_FRAMEBUFFER = 0x8D40; | |
const uint GL_TEXTURE_2D = 0x0DE1; | |
const uint GL_RGBA8 = 0x8058; | |
const uint GL_COLOR_ATTACHMENT0 = 0x8CE0; | |
const uint GL_RENDERBUFFER = 0x8D41; | |
const uint GL_DEPTH_COMPONENT16 = 0x81A5; | |
const uint GL_DEPTH_ATTACHMENT = 0x8D00; | |
const uint GL_FRAMEBUFFER_COMPLETE = 0x8CD5; | |
const uint GL_FRAMEBUFFER_UNDEFINED = 0x8219; | |
const uint GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT = 0x8CD6; | |
const uint GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT = 0x8CD7; | |
const uint GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER = 0x8CDB; | |
const uint GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER = 0x8CDC; | |
const uint GL_FRAMEBUFFER_UNSUPPORTED = 0x8CDD; | |
const uint GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE = 0x8D56; | |
const uint GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS = 0x8DA8; | |
const uint GL_COLOR_BUFFER_BIT = 0x00004000; | |
const uint GL_DEPTH_BUFFER_BIT = 0x00000100; | |
const uint GL_TRIANGLES = 0x0004; | |
const uint GL_MODELVIEW = 0x1700; | |
const uint GL_PROJECTION = 0x1701; | |
[DllImport(GLFW_LIB)] static extern IntPtr glfwGetProcAddress(string procname); | |
[DllImport(GLFW_LIB)] static extern bool glfwInit(); | |
[DllImport(GLFW_LIB)] public static extern int glfwTerminate(); | |
[DllImport(GLFW_LIB)] static extern void glfwSwapBuffers(IntPtr window); | |
[DllImport(GLFW_LIB)] public static extern void glfwSleep(double t); | |
[DllImport(GLFW_LIB)] public static extern double glfwGetTime(); | |
[DllImport(GLFW_LIB)] public static extern void glfwSwapInterval(int interval); | |
// Windowing stuff | |
[DllImport(GLFW_LIB)] public static extern void glfwOpenWindow(int width, int height, int rbits, int gbits, int bbits, int alpha, int depth, int stencil, int mode); | |
[DllImport(GLFW_LIB)] static extern IntPtr glfwCreateWindow(int width, int height, string title, IntPtr monitor, IntPtr share); | |
[DllImport(GLFW_LIB)] static extern void glfwDestroyWindow(IntPtr window); | |
[DllImport(GLFW_LIB)] public static extern int glfwGetWindowParam(int param); | |
[DllImport(GLFW_LIB)] public unsafe static extern void glfwGetWindowSize(int* width, int* height); | |
[DllImport(GLFW_LIB)] public static extern void glfwSetWindowTitle(char[] title); | |
[DllImport(GLFW_LIB)] static extern void glfwMakeContextCurrent(IntPtr window); | |
[DllImport(GLFW_LIB)] static extern bool glfwWindowShouldClose(IntPtr window); | |
[DllImport(GLFW_LIB)] static extern void glfwPollEvents(); | |
// OpenGL | |
[DllImport(GLFW_LIB)] public unsafe static extern void glfwGetFramebufferSize (IntPtr window, int* width, int* height); | |
[DllImport(GLFW_LIB)] public static extern void glMatrixMode(GLenum glenum); | |
[DllImport(GLFW_LIB)] public static extern void glViewport( GLint x,GLint y,GLsizei width,GLsizei height ); | |
[DllImport(GLFW_LIB)] public static extern void glLoadIdentity(); | |
public struct GLFWvidmode | |
{ | |
public int Width, Height, RedBits, GreenBits, BlueBits; | |
} | |
[DllImport(GLFW_LIB)] public unsafe static extern void glfwGetDesktopMode(GLFWvidmode* mode); | |
// Input stuff. | |
[DllImport(GLFW_LIB)] public unsafe static extern void glfwGetMousePos(int* mx, int* my); | |
public static bool glfwGetKey(char key) | |
{ | |
return glfwGetKey((int)key) == 1; | |
} | |
[DllImport(GLFW_LIB)] public static extern int glfwGetKey(int key); | |
[DllImport(GLFW_LIB)] public static extern int glfwGetMouseButton(int button); | |
[DllImport(GLFW_LIB)] public static extern int glfwGetMouseWheel(); | |
[DllImport(GLFW_LIB, CallingConvention = CallingConvention.Cdecl)] | |
public static extern void glfwSetKeyCallback(MulticastDelegate callback); | |
[DllImport(GLFW_LIB, CallingConvention = CallingConvention.Cdecl)] | |
public static extern void glfwSetMouseButtonCallback(MulticastDelegate callback); | |
public delegate void MouseEvent(int mouseButton, int clicky); | |
public static MouseEvent mouseCallback; | |
unsafe delegate void glGenFramebuffers_f(int n, uint* ids); | |
delegate void glBindFramebuffer_f(uint target, uint framebuffer); | |
unsafe delegate void glGenTextures_f(uint n, uint* textures); | |
delegate void glBindTexture_f(uint target, uint texture); | |
delegate void glTexStorage2D_f(uint target, int levels, uint internalformat, int width, int height); | |
delegate void glFramebufferTexture2D_f(uint target, uint attachment, uint textarget, uint texture, int level); | |
unsafe delegate void glGenRenderbuffers_f(int n, uint* renderbuffers); | |
delegate void glBindRenderbuffer_f(uint target, uint renderbuffer); | |
delegate void glRenderbufferStorage_f(uint target, uint internalformat, int width, int height); | |
delegate void glFramebufferRenderbuffer_f(uint target, uint attachment, uint renderbuffertarget, uint renderbuffer); | |
delegate uint glCheckFramebufferStatus_f(uint target); | |
delegate void glClear_f(uint flags); | |
delegate uint glVertex2f_f(float x, float y); | |
delegate uint glBegin_f(uint flags); | |
delegate uint glEnd_f(); | |
delegate void glOrtho_f(float left, float right, float bottom, float top, float zNear, float zFar); | |
delegate void glRotatef_f (float angle, float x, float y, float z); | |
delegate void glColor3f_f (GLfloat red,GLfloat green,GLfloat blue); | |
delegate void glVertex3f_f(GLfloat x, GLfloat y, GLfloat z); | |
static glGenFramebuffers_f glGenFramebuffers; | |
static glBindFramebuffer_f glBindFramebuffer; | |
static glGenTextures_f glGenTextures; | |
static glBindTexture_f glBindTexture; | |
static glTexStorage2D_f glTexStorage2D; | |
static glFramebufferTexture2D_f glFramebufferTexture2D; | |
static glGenRenderbuffers_f glGenRenderbuffers; | |
static glBindRenderbuffer_f glBindRenderbuffer; | |
static glRenderbufferStorage_f glRenderbufferStorage; | |
static glFramebufferRenderbuffer_f glFramebufferRenderbuffer; | |
static glCheckFramebufferStatus_f glCheckFramebufferStatus; | |
static glClear_f glClear; | |
static glVertex2f_f glVertex2f; | |
static glBegin_f glBegin; | |
static glEnd_f glEnd; | |
static glOrtho_f glOrtho; | |
static glRotatef_f glRotatef; | |
static glColor3f_f glColor3f; | |
static glVertex3f_f glVertex3f; | |
// csc -out:GLFW.exe GLFW.cs /unsafe | |
// mono GLFW.exe | |
static void Main(string[] args) | |
{ | |
IntPtr _windowHandle; | |
if (!glfwInit()) | |
{ | |
Console.WriteLine("GLFW init error"); | |
return; | |
} | |
Console.WriteLine("GLFW init OK"); | |
_windowHandle = glfwCreateWindow(640, 480, "Testing", IntPtr.Zero, IntPtr.Zero); | |
if (_windowHandle == IntPtr.Zero) | |
{ | |
glfwTerminate(); | |
return; | |
} | |
glfwMakeContextCurrent(_windowHandle); | |
glGenFramebuffers = Marshal.GetDelegateForFunctionPointer<glGenFramebuffers_f>(glfwGetProcAddress("glGenFramebuffers")); | |
glBindFramebuffer = Marshal.GetDelegateForFunctionPointer<glBindFramebuffer_f>(glfwGetProcAddress("glBindFramebuffer")); | |
glGenTextures = Marshal.GetDelegateForFunctionPointer<glGenTextures_f>(glfwGetProcAddress("glGenTextures")); | |
glBindTexture = Marshal.GetDelegateForFunctionPointer<glBindTexture_f>(glfwGetProcAddress("glBindTexture")); | |
glTexStorage2D = Marshal.GetDelegateForFunctionPointer<glTexStorage2D_f>(glfwGetProcAddress("glTexStorage2D")); | |
glFramebufferTexture2D = Marshal.GetDelegateForFunctionPointer<glFramebufferTexture2D_f>(glfwGetProcAddress("glFramebufferTexture2D")); | |
glGenRenderbuffers = Marshal.GetDelegateForFunctionPointer<glGenRenderbuffers_f>(glfwGetProcAddress("glGenRenderbuffers")); | |
glBindRenderbuffer = Marshal.GetDelegateForFunctionPointer<glBindRenderbuffer_f>(glfwGetProcAddress("glBindRenderbuffer")); | |
glRenderbufferStorage = Marshal.GetDelegateForFunctionPointer<glRenderbufferStorage_f>(glfwGetProcAddress("glRenderbufferStorage")); | |
glFramebufferRenderbuffer = Marshal.GetDelegateForFunctionPointer<glFramebufferRenderbuffer_f>(glfwGetProcAddress("glFramebufferRenderbuffer")); | |
glCheckFramebufferStatus = Marshal.GetDelegateForFunctionPointer<glCheckFramebufferStatus_f>(glfwGetProcAddress("glCheckFramebufferStatus")); | |
glClear = Marshal.GetDelegateForFunctionPointer<glClear_f>(glfwGetProcAddress("glClear")); | |
glVertex2f = Marshal.GetDelegateForFunctionPointer<glVertex2f_f>(glfwGetProcAddress("glVertex2f")); | |
glBegin = Marshal.GetDelegateForFunctionPointer<glBegin_f>(glfwGetProcAddress("glBegin")); | |
glEnd = Marshal.GetDelegateForFunctionPointer<glEnd_f>(glfwGetProcAddress("glEnd")); | |
glOrtho = Marshal.GetDelegateForFunctionPointer<glOrtho_f>(glfwGetProcAddress("glOrtho")); | |
glRotatef = Marshal.GetDelegateForFunctionPointer<glRotatef_f>(glfwGetProcAddress("glRotatef")); | |
glColor3f = Marshal.GetDelegateForFunctionPointer<glColor3f_f>(glfwGetProcAddress("glColor3f")); | |
glVertex3f = Marshal.GetDelegateForFunctionPointer<glVertex3f_f>(glfwGetProcAddress("glVertex3f")); | |
uint status = glCheckFramebufferStatus(GL_FRAMEBUFFER); | |
if (status != GL_FRAMEBUFFER_COMPLETE) | |
{ | |
_windowHandle = IntPtr.Zero; | |
glfwDestroyWindow(_windowHandle); | |
glfwTerminate(); | |
return; | |
} | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glBindTexture(GL_TEXTURE_2D, 0); | |
glBindFramebuffer(GL_FRAMEBUFFER, 0); | |
float ratio; | |
int width, height; | |
/* Loop until the user closes the window */ | |
while (!glfwWindowShouldClose(_windowHandle)) | |
unsafe { | |
// /* Render here */ | |
glfwGetFramebufferSize(_windowHandle, &width, &height); | |
ratio = width / (float) height; | |
glViewport(0, 0, width, height); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glOrtho(-ratio, ratio, -1f, 1f, 1f, -1f); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
glRotatef((float) glfwGetTime() * 50f, 0f, 0f, 1f); | |
glBegin(GL_TRIANGLES); | |
glColor3f(1f, 0f, 0f); | |
glVertex3f(-0.6f, -0.4f, 0f); | |
glColor3f(0f, 1f, 0f); | |
glVertex3f(0.6f, -0.4f, 0f); | |
glColor3f(0f, 0f, 1f); | |
glVertex3f(0f, 0.6f, 0f); | |
glEnd(); | |
// Swap front and back buffers | |
glfwSwapBuffers(_windowHandle); | |
// Poll for and process events | |
glfwPollEvents(); | |
} | |
glfwDestroyWindow(_windowHandle); | |
glfwTerminate(); | |
} // Main | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment