From 82decde1b2805201804163fa0204a712e7699cc5 Mon Sep 17 00:00:00 2001 From: rexim Date: Wed, 21 Aug 2019 23:59:34 +0700 Subject: [PATCH 1/4] Add GLX example --- .gitignore | 3 ++- boomer.nimble | 4 ++-- default.nix | 3 ++- src/glxex.nim | 15 +++++++++++++++ 4 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 src/glxex.nim diff --git a/.gitignore b/.gitignore index d953a8d..d96a0f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ nimcache/ boomer -x11ex \ No newline at end of file +x11ex +glxex \ No newline at end of file diff --git a/boomer.nimble b/boomer.nimble index 9bff22b..f430f6c 100644 --- a/boomer.nimble +++ b/boomer.nimble @@ -3,6 +3,6 @@ author = "me" description = "Zoomer application for boomers" license = "MIT" srcDir = "src" -bin = @["boomer", "x11ex"] +bin = @["boomer", "x11ex", "glxex"] -requires "nim >= 0.18.0", "x11 >= 1.1" +requires "nim >= 0.18.0", "x11 >= 1.1", "opengl >= 1.2.3" diff --git a/default.nix b/default.nix index d5c8895..9ae2073 100644 --- a/default.nix +++ b/default.nix @@ -6,7 +6,8 @@ with import {}; { pkgconfig nim xorg.libX11 + libGL ]; - LD_LIBRARY_PATH="${xorg.libX11}/lib/"; + LD_LIBRARY_PATH="${xorg.libX11}/lib/;${libGL}/lib/"; }; } diff --git a/src/glxex.nim b/src/glxex.nim new file mode 100644 index 0000000..eb808f6 --- /dev/null +++ b/src/glxex.nim @@ -0,0 +1,15 @@ +import x11/xlib +import opengl/glx + +proc main() = + var display = XOpenDisplay(nil) + if display == nil: + quit "Failed to open display" + + var glxMajor : int + var glxMinor : int + + discard glXQueryVersion(display, glxMajor, glxMinor) + echo("GLX version ", glxMajor, ".", glxMinor) + +main() From 791cd4a10195f78864c74d05f9acc01943aa5cbc Mon Sep 17 00:00:00 2001 From: rexim Date: Thu, 22 Aug 2019 03:02:02 +0700 Subject: [PATCH 2/4] Implement actually working GLX example --- default.nix | 2 +- src/glxex.nim | 66 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/default.nix b/default.nix index 9ae2073..3f84a0d 100644 --- a/default.nix +++ b/default.nix @@ -8,6 +8,6 @@ with import {}; { xorg.libX11 libGL ]; - LD_LIBRARY_PATH="${xorg.libX11}/lib/;${libGL}/lib/"; + LD_LIBRARY_PATH="/run/opengl-driver/lib;${xorg.libX11}/lib/;${libGL}/lib/"; }; } diff --git a/src/glxex.nim b/src/glxex.nim index eb808f6..0ddcc23 100644 --- a/src/glxex.nim +++ b/src/glxex.nim @@ -1,15 +1,75 @@ -import x11/xlib -import opengl/glx +import x11/xlib, x11/x +import opengl, opengl/glx proc main() = var display = XOpenDisplay(nil) if display == nil: quit "Failed to open display" + let screen = XDefaultScreen(display) var glxMajor : int var glxMinor : int - discard glXQueryVersion(display, glxMajor, glxMinor) + if (not glXQueryVersion(display, glxMajor, glxMinor) or + (glxMajor == 1 and glxMinor < 3) or + (glxMajor < 1)): + quit "Invalid GLX version. Expected >=1.3" + echo("GLX version ", glxMajor, ".", glxMinor) + echo("GLX extension: ", $glXQueryExtensionsString(display, screen)) + + var att = [GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None] + + var root = DefaultRootWindow(display) + var vi = glXChooseVisual(display, 0, addr att[0]) + + if (vi == nil): + quit "No appropriate visual found" + + echo("Visual ", vi.visualid, " selected") + + var cmap = XCreateColormap(display, root, vi.visual, AllocNone) + var swa: TXSetWindowAttributes + swa.colormap = cmap + swa.event_mask = ExposureMask or KeyPressMask + + var win = XCreateWindow( + display, root, 0, 0, 600, 600, 0, + vi.depth, InputOutput, vi.visual, + CWColormap or CWEventMask, addr swa) + + discard XMapWindow(display, win) + + discard XStoreName(display, win, "Wordpress Application") + + var glc = glXCreateContext(display, vi, nil, GL_TRUE) + + discard glXMakeCurrent(display, win, glc) + + loadExtensions() + + glEnable(GL_DEPTH_TEST) + + var xev: TXEvent + + while true: + discard XNextEvent(display, addr xev) + + case xev.theType + of Expose: + var gwa: TXWindowAttributes + discard XGetWindowAttributes(display, win, addr gwa) + glViewport(0, 0, gwa.width, gwa.height) + # Draw Something + glXSwapBuffers(display, win) + of KeyPress: + discard glXMakeCurrent(display, None, nil) + glXDestroyContext(display, glc) + discard XDestroyWindow(display, win) + discard XCloseDisplay(display) + quit "done" + else: + discard + main() From 6a006dde62c3ac59174ed4ab79d5ed2faac493b8 Mon Sep 17 00:00:00 2001 From: rexim Date: Thu, 22 Aug 2019 03:18:20 +0700 Subject: [PATCH 3/4] Make glxex draw something --- default.nix | 3 ++- src/glxex.nim | 48 ++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/default.nix b/default.nix index 3f84a0d..fd4b500 100644 --- a/default.nix +++ b/default.nix @@ -7,7 +7,8 @@ with import {}; { nim xorg.libX11 libGL + libGLU ]; - LD_LIBRARY_PATH="/run/opengl-driver/lib;${xorg.libX11}/lib/;${libGL}/lib/"; + LD_LIBRARY_PATH="/run/opengl-driver/lib;${xorg.libX11}/lib/;${libGL}/lib/;${libGLU}/lib"; }; } diff --git a/src/glxex.nim b/src/glxex.nim index 0ddcc23..d182c35 100644 --- a/src/glxex.nim +++ b/src/glxex.nim @@ -1,5 +1,28 @@ import x11/xlib, x11/x -import opengl, opengl/glx +import opengl, opengl/glx, opengl/glu + +proc DrawAQuad() = + glClearColor(1.0, 1.0, 1.0, 1.0) + glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT) + + glMatrixMode(GL_PROJECTION) + glLoadIdentity() + glOrtho(-1.0, 1.0, -1.0, 1.0, 1.0, 20.0) + + glMatrixMode(GL_MODELVIEW) + glLoadIdentity() + gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) + + glBegin(GL_QUADS) + glColor3f(1.0, 0.0, 0.0) + glVertex3f(-0.75, -0.75, 0.0) + glColor3f(0.0, 1.0, 0.0) + glVertex3f( 0.75, -0.75, 0.0) + glColor3f(0.0, 0.0, 1.0) + glVertex3f( 0.75, 0.75, 0.0) + glColor3f(1.0, 1.0, 0.0) + glVertex3f(-0.75, 0.75, 0.0) + glEnd() proc main() = var display = XOpenDisplay(nil) @@ -53,16 +76,29 @@ proc main() = var xev: TXEvent + glClearColor(0.0, 1.0, 0.0, 1.0) + while true: + var gwa: TXWindowAttributes + discard XGetWindowAttributes(display, win, addr gwa) + glViewport(0, 0, gwa.width, gwa.height) + + glClear(GL_COLOR_BUFFER_BIT) + + glMatrixMode(GL_PROJECTION) + glLoadIdentity() + glOrtho(-1.0, 1.0, -1.0, 1.0, 1.0, 20.0) + + DrawAQuad() + + glXSwapBuffers(display, win) + discard XNextEvent(display, addr xev) case xev.theType of Expose: - var gwa: TXWindowAttributes - discard XGetWindowAttributes(display, win, addr gwa) - glViewport(0, 0, gwa.width, gwa.height) - # Draw Something - glXSwapBuffers(display, win) + echo "hello" + of KeyPress: discard glXMakeCurrent(display, None, nil) glXDestroyContext(display, glc) From b82aefb321ab1c381c1e566c97ba34b82adbc8d9 Mon Sep 17 00:00:00 2001 From: rexim Date: Thu, 22 Aug 2019 06:21:04 +0700 Subject: [PATCH 4/4] Add GLX example to References --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b8fcbfb..cc7145a 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ $ ./boomer - https://github.com/nim-lang/x11/blob/bf9dc74dd196a98b7c2a2beea4d92640734f7c60/examples/x11ex.nim - http://archive.xfce.org/src/xfce/xfwm4/4.13/ +- https://www.khronos.org/opengl/wiki/Programming_OpenGL_in_Linux:_GLX_and_Xlib ## Support