Guest User

OpenGL and X

a guest
Jul 17th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 1.85 KB | None | 0 0
  1. import x
  2. import xlib
  3. import glx
  4. import opengl
  5. import glu
  6.  
  7. var attr: array[0..4, int32] = [ GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None ]
  8. var swa: TXSetWindowAttributes
  9. var xev: TXEvent
  10. var gwa: TXWindowAttributes
  11.  
  12. proc drawQuad() =
  13.   glClearColor(1.0, 1.0, 1.0, 1.0)
  14.   glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
  15.  
  16.   glMatrixMode(GL_PROJECTION)
  17.   glLoadIdentity()
  18.   glOrtho(-1.0, 1.0, -1.0, 1.0, 1.0, 20.0)
  19.  
  20.   glMatrixMode(GL_MODELVIEW)
  21.   glLoadIdentity()
  22.   gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
  23.  
  24.   glBegin(GL_QUADS)
  25.   block draw:
  26.     glColor3f(1.0, 0.0, 0.0)
  27.     glVertex3f(-0.75, -0.75, 0.0)
  28.     glColor3f(0.0, 1.0, 0.0)
  29.     glVertex3f(0.75, -0.75, 0.0)
  30.     glColor3f(0.0, 0.0, 1.0)
  31.     glVertex3f(0.75, 0.75, 0.0)
  32.     glColor3f(1.0, 1.0, 0.0)
  33.     glVertex3f(-0.75, 0.75, 0.0)
  34.   glEnd()
  35.  
  36. let dpy = XOpenDisplay(nil)
  37.  
  38. if dpy == nil:
  39.   quit(1)
  40.  
  41. let root = DefaultRootWindow(dpy)
  42. let vi = glXChooseVisual(dpy, 0, addr attr[0])
  43.  
  44. if vi == nil:
  45.   quit(2)
  46.  
  47. let cmap = XCreateColormap(dpy, root, vi.visual, AllocNone)
  48.  
  49. swa.colormap = cmap
  50. swa.event_mask = ExposureMask or KeyPressMask
  51.  
  52. let win = XCreateWindow(dpy, root, 0, 0, 800, 600, 0, vi.depth, InputOutput, vi.visual, CWColorMap or CWEventMask, addr swa)
  53.  
  54. discard XMapWindow(dpy, win)
  55. discard XStoreName(dpy, win, "Test 1")
  56.  
  57. let glc = glXCreateContext(dpy, vi, nil, true)
  58. discard glXMakeCurrent(dpy, win, glc)
  59.  
  60. glEnable(GL_DEPTH_TEST)
  61.  
  62. while true:
  63.   discard XNextEvent(dpy, addr xev)
  64.   if xev.theType == Expose:
  65.     discard XGetWindowAttributes(dpy, win, addr gwa)
  66.     glViewport(0, 0, gwa.width, gwa.height)
  67.     drawQuad()
  68.     glXSwapBuffers(dpy, win)
  69.   elif xev.theType == KeyPress:
  70.     discard glXMakeCurrent(dpy, None, nil)
  71.     glXDestroyContext(dpy, glc)
  72.     discard XDestroyWindow(dpy, win)
  73.     discard XCloseDisplay(dpy)
  74.     quit(0)
Advertisement
Add Comment
Please, Sign In to add comment